1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 
21 #include "formlinkdialog.hxx"
22 
23 #include "modulepcr.hxx"
24 #include <strings.hrc>
25 #include "formstrings.hxx"
26 #include <sal/log.hxx>
27 #include <vcl/svapp.hxx>
28 #include <connectivity/dbtools.hxx>
29 #include <connectivity/dbexception.hxx>
30 #include <comphelper/sequence.hxx>
31 
32 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
33 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
34 #include <com/sun/star/sdbcx/KeyType.hpp>
35 #include <com/sun/star/container/XNameAccess.hpp>
36 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
37 #include <com/sun/star/sdbc/XRowSet.hpp>
38 #include <com/sun/star/sdb/CommandType.hpp>
39 #include <com/sun/star/sdb/SQLContext.hpp>
40 
41 
42 namespace pcr
43 {
44 
45 
46     using namespace ::com::sun::star::uno;
47     using namespace ::com::sun::star::lang;
48     using namespace ::com::sun::star::form;
49     using namespace ::com::sun::star::sdb;
50     using namespace ::com::sun::star::sdbc;
51     using namespace ::com::sun::star::sdbcx;
52     using namespace ::com::sun::star::beans;
53     using namespace ::com::sun::star::container;
54 
55 
56     //= FieldLinkRow
57 
58     class FieldLinkRow
59     {
60     private:
61         std::unique_ptr<weld::ComboBox> m_xDetailColumn;
62         std::unique_ptr<weld::ComboBox> m_xMasterColumn;
63 
64         Link<FieldLinkRow&,void> m_aLinkChangeHandler;
65 
66     public:
67         FieldLinkRow(std::unique_ptr<weld::ComboBox> xDetailColumn,
68                      std::unique_ptr<weld::ComboBox> xMasterColumn);
69 
70 
SetLinkChangeHandler(const Link<FieldLinkRow &,void> & _rHdl)71         void         SetLinkChangeHandler( const Link<FieldLinkRow&,void>& _rHdl ) { m_aLinkChangeHandler = _rHdl; }
72 
73         enum LinkParticipant
74         {
75             eDetailField,
76             eMasterField
77         };
78         /** retrieves the selected field name for either the master or the detail field
79             @return <TRUE/> if and only a valid field is selected
80         */
81         bool    GetFieldName( LinkParticipant _eWhich, OUString& /* [out] */ _rName ) const;
82         void    SetFieldName( LinkParticipant _eWhich, const OUString& _rName );
83 
84         void    fillList( LinkParticipant _eWhich, const Sequence< OUString >& _rFieldNames );
85 
Show()86         void    Show()
87         {
88             m_xDetailColumn->show();
89             m_xMasterColumn->show();
90         }
91 
92     private:
93         DECL_LINK( OnFieldNameChanged, weld::ComboBox&, void );
94     };
95 
96 
FieldLinkRow(std::unique_ptr<weld::ComboBox> xDetailColumn,std::unique_ptr<weld::ComboBox> xMasterColumn)97     FieldLinkRow::FieldLinkRow(std::unique_ptr<weld::ComboBox> xDetailColumn,
98                                std::unique_ptr<weld::ComboBox> xMasterColumn)
99         : m_xDetailColumn(std::move(xDetailColumn))
100         , m_xMasterColumn(std::move(xMasterColumn))
101     {
102         m_xDetailColumn->connect_changed( LINK( this, FieldLinkRow, OnFieldNameChanged ) );
103         m_xMasterColumn->connect_changed( LINK( this, FieldLinkRow, OnFieldNameChanged ) );
104     }
105 
fillList(LinkParticipant _eWhich,const Sequence<OUString> & _rFieldNames)106     void FieldLinkRow::fillList( LinkParticipant _eWhich, const Sequence< OUString >& _rFieldNames )
107     {
108         weld::ComboBox* pBox = ( _eWhich == eDetailField ) ? m_xDetailColumn.get() : m_xMasterColumn.get();
109 
110         const OUString* pFieldName    = _rFieldNames.getConstArray();
111         const OUString* pFieldNameEnd = pFieldName + _rFieldNames.getLength();
112         for ( ; pFieldName != pFieldNameEnd; ++pFieldName )
113             pBox->append_text( *pFieldName );
114     }
115 
GetFieldName(LinkParticipant _eWhich,OUString & _rName) const116     bool FieldLinkRow::GetFieldName( LinkParticipant _eWhich, OUString& /* [out] */ _rName ) const
117     {
118         const weld::ComboBox* pBox = ( _eWhich == eDetailField ) ? m_xDetailColumn.get() : m_xMasterColumn.get();
119         _rName = pBox->get_active_text();
120         return !_rName.isEmpty();
121     }
122 
SetFieldName(LinkParticipant _eWhich,const OUString & _rName)123     void FieldLinkRow::SetFieldName( LinkParticipant _eWhich, const OUString& _rName )
124     {
125         weld::ComboBox* pBox = ( _eWhich == eDetailField ) ? m_xDetailColumn.get() : m_xMasterColumn.get();
126         pBox->set_entry_text( _rName );
127     }
128 
IMPL_LINK_NOARG(FieldLinkRow,OnFieldNameChanged,weld::ComboBox &,void)129     IMPL_LINK_NOARG( FieldLinkRow, OnFieldNameChanged, weld::ComboBox&, void )
130     {
131         m_aLinkChangeHandler.Call( *this );
132     }
133 
134     //= FormLinkDialog
135 
FormLinkDialog(weld::Window * _pParent,const Reference<XPropertySet> & _rxDetailForm,const Reference<XPropertySet> & _rxMasterForm,const Reference<XComponentContext> & _rxContext,const OUString & _sExplanation,const OUString & _sDetailLabel,const OUString & _sMasterLabel)136     FormLinkDialog::FormLinkDialog(weld::Window* _pParent, const Reference< XPropertySet >& _rxDetailForm,
137             const Reference< XPropertySet >& _rxMasterForm, const Reference< XComponentContext >& _rxContext,
138             const OUString& _sExplanation,
139             const OUString& _sDetailLabel,
140             const OUString& _sMasterLabel)
141         : GenericDialogController(_pParent, "modules/spropctrlr/ui/formlinksdialog.ui", "FormLinks")
142         , m_xContext    ( _rxContext )
143         , m_xDetailForm( _rxDetailForm )
144         , m_xMasterForm( _rxMasterForm )
145         , m_sDetailLabel(_sDetailLabel)
146         , m_sMasterLabel(_sMasterLabel)
147         , m_xExplanation(m_xBuilder->weld_label("explanationLabel"))
148         , m_xDetailLabel(m_xBuilder->weld_label("detailLabel"))
149         , m_xMasterLabel(m_xBuilder->weld_label("masterLabel"))
150         , m_xRow1(std::make_unique<FieldLinkRow>(m_xBuilder->weld_combo_box("detailCombobox1"),
151                                                  m_xBuilder->weld_combo_box("masterCombobox1")))
152         , m_xRow2(std::make_unique<FieldLinkRow>(m_xBuilder->weld_combo_box("detailCombobox2"),
153                                                  m_xBuilder->weld_combo_box("masterCombobox2")))
154         , m_xRow3(std::make_unique<FieldLinkRow>(m_xBuilder->weld_combo_box("detailCombobox3"),
155                                                  m_xBuilder->weld_combo_box("masterCombobox3")))
156         , m_xRow4(std::make_unique<FieldLinkRow>(m_xBuilder->weld_combo_box("detailCombobox4"),
157                                                  m_xBuilder->weld_combo_box("masterCombobox4")))
158         , m_xOK(m_xBuilder->weld_button("ok"))
159         , m_xSuggest(m_xBuilder->weld_button("suggestButton"))
160     {
161         m_xRow1->Show();
162         m_xRow2->Show();
163         m_xRow3->Show();
164         m_xRow4->Show();
165         m_xDialog->set_size_request(600, -1);
166 
167         if ( !_sExplanation.isEmpty() )
168             m_xExplanation->set_label(_sExplanation);
169 
170         m_xSuggest->connect_clicked(LINK(this, FormLinkDialog, OnSuggest));
171         m_xRow1->SetLinkChangeHandler( LINK( this, FormLinkDialog, OnFieldChanged ) );
172         m_xRow2->SetLinkChangeHandler( LINK( this, FormLinkDialog, OnFieldChanged ) );
173         m_xRow3->SetLinkChangeHandler( LINK( this, FormLinkDialog, OnFieldChanged ) );
174         m_xRow4->SetLinkChangeHandler( LINK( this, FormLinkDialog, OnFieldChanged ) );
175 
176         Application::PostUserEvent(LINK(this, FormLinkDialog, OnInitialize));
177 
178         updateOkButton();
179     }
180 
~FormLinkDialog()181     FormLinkDialog::~FormLinkDialog()
182     {
183     }
184 
commitLinkPairs()185     void FormLinkDialog::commitLinkPairs()
186     {
187         // collect the field lists from the rows
188         std::vector< OUString > aDetailFields; aDetailFields.reserve( 4 );
189         std::vector< OUString > aMasterFields; aMasterFields.reserve( 4 );
190 
191         const FieldLinkRow* aRows[] = {
192             m_xRow1.get(), m_xRow2.get(), m_xRow3.get(), m_xRow4.get()
193         };
194 
195         for (const FieldLinkRow* aRow : aRows)
196         {
197             OUString sDetailField, sMasterField;
198             aRow->GetFieldName( FieldLinkRow::eDetailField, sDetailField );
199             aRow->GetFieldName( FieldLinkRow::eMasterField, sMasterField );
200             if ( sDetailField.isEmpty() && sMasterField.isEmpty() )
201                 continue;
202 
203             aDetailFields.push_back( sDetailField );
204             aMasterFields.push_back( sMasterField );
205         }
206 
207         // and set as property values
208         try
209         {
210             if ( m_xDetailForm.is() )
211             {
212                 m_xDetailForm->setPropertyValue( PROPERTY_DETAILFIELDS, makeAny( Sequence< OUString >( aDetailFields.data(), aDetailFields.size() ) ) );
213                 m_xDetailForm->setPropertyValue( PROPERTY_MASTERFIELDS, makeAny( Sequence< OUString >( aMasterFields.data(), aMasterFields.size() ) ) );
214             }
215         }
216         catch( const Exception& )
217         {
218             OSL_FAIL( "FormLinkDialog::commitLinkPairs: caught an exception while setting the properties!" );
219         }
220     }
221 
run()222     short FormLinkDialog::run()
223     {
224         short nResult = GenericDialogController::run();
225 
226         if ( RET_OK == nResult )
227             commitLinkPairs();
228 
229         return nResult;
230     }
231 
initializeFieldLists()232     void FormLinkDialog::initializeFieldLists()
233     {
234         Sequence< OUString > sDetailFields;
235         getFormFields( m_xDetailForm, sDetailFields );
236 
237         Sequence< OUString > sMasterFields;
238         getFormFields( m_xMasterForm, sMasterFields );
239 
240         FieldLinkRow* aRows[] = {
241             m_xRow1.get(), m_xRow2.get(), m_xRow3.get(), m_xRow4.get()
242         };
243         for (FieldLinkRow* aRow : aRows)
244         {
245             aRow->fillList( FieldLinkRow::eDetailField, sDetailFields );
246             aRow->fillList( FieldLinkRow::eMasterField, sMasterFields );
247         }
248 
249     }
250 
251 
initializeColumnLabels()252     void FormLinkDialog::initializeColumnLabels()
253     {
254         // label for the detail form
255         OUString sDetailType = getFormDataSourceType( m_xDetailForm );
256         if ( sDetailType.isEmpty() )
257         {
258             if ( m_sDetailLabel.isEmpty() )
259             {
260                 m_sDetailLabel = PcrRes(STR_DETAIL_FORM);
261             }
262             sDetailType = m_sDetailLabel;
263         }
264         m_xDetailLabel->set_label( sDetailType );
265 
266         // label for the master form
267         OUString sMasterType = getFormDataSourceType( m_xMasterForm );
268         if ( sMasterType.isEmpty() )
269         {
270             if ( m_sMasterLabel.isEmpty() )
271             {
272                 m_sMasterLabel = PcrRes(STR_MASTER_FORM);
273             }
274             sMasterType = m_sMasterLabel;
275         }
276         m_xMasterLabel->set_label( sMasterType );
277     }
278 
initializeFieldRowsFrom(std::vector<OUString> & _rDetailFields,std::vector<OUString> & _rMasterFields)279     void FormLinkDialog::initializeFieldRowsFrom( std::vector< OUString >& _rDetailFields, std::vector< OUString >& _rMasterFields )
280     {
281         // our UI does allow 4 fields max
282         _rDetailFields.resize( 4 );
283         _rMasterFields.resize( 4 );
284 
285         FieldLinkRow* aRows[] = {
286             m_xRow1.get(), m_xRow2.get(), m_xRow3.get(), m_xRow4.get()
287         };
288         for ( sal_Int32 i = 0; i < 4; ++i )
289         {
290             aRows[ i ]->SetFieldName( FieldLinkRow::eDetailField, _rDetailFields[i] );
291             aRows[ i ]->SetFieldName( FieldLinkRow::eMasterField, _rMasterFields[i] );
292         }
293     }
294 
295 
initializeLinks()296     void FormLinkDialog::initializeLinks()
297     {
298         try
299         {
300             Sequence< OUString > aDetailFields;
301             Sequence< OUString > aMasterFields;
302 
303             if ( m_xDetailForm.is() )
304             {
305                 m_xDetailForm->getPropertyValue( PROPERTY_DETAILFIELDS ) >>= aDetailFields;
306                 m_xDetailForm->getPropertyValue( PROPERTY_MASTERFIELDS ) >>= aMasterFields;
307             }
308 
309             std::vector< OUString > aDetailFields1;
310             comphelper::sequenceToContainer(aDetailFields1, aDetailFields);
311             std::vector< OUString > aMasterFields1;
312             comphelper::sequenceToContainer(aMasterFields1, aMasterFields);
313             initializeFieldRowsFrom( aDetailFields1, aMasterFields1 );
314         }
315         catch( const Exception& )
316         {
317             OSL_FAIL( "FormLinkDialog::initializeLinks: caught an exception!" );
318         }
319     }
320 
321 
updateOkButton()322     void FormLinkDialog::updateOkButton()
323     {
324         // in all rows, there must be either two valid selections, or none at all
325         // If there is at least one row with exactly one valid selection, then the
326         // OKButton needs to be disabled
327         bool bEnable = true;
328 
329         const FieldLinkRow* aRows[] = {
330             m_xRow1.get(), m_xRow2.get(), m_xRow3.get(), m_xRow4.get()
331         };
332 
333         for ( sal_Int32 i = 0; ( i < 4 ) && bEnable; ++i )
334         {
335             OUString sNotInterestedInRightNow;
336             if  (  aRows[ i ]->GetFieldName( FieldLinkRow::eDetailField, sNotInterestedInRightNow )
337                 != aRows[ i ]->GetFieldName( FieldLinkRow::eMasterField, sNotInterestedInRightNow )
338                 )
339                 bEnable = false;
340         }
341 
342         m_xOK->set_sensitive(bEnable);
343     }
344 
getFormDataSourceType(const Reference<XPropertySet> & _rxForm)345     OUString FormLinkDialog::getFormDataSourceType( const Reference< XPropertySet >& _rxForm )
346     {
347         OUString sReturn;
348         if ( !_rxForm.is() )
349             return sReturn;
350 
351         try
352         {
353             sal_Int32       nCommandType = CommandType::COMMAND;
354             OUString sCommand;
355 
356             _rxForm->getPropertyValue( PROPERTY_COMMANDTYPE ) >>= nCommandType;
357             _rxForm->getPropertyValue( PROPERTY_COMMAND     ) >>= sCommand;
358 
359             if  (  ( nCommandType == CommandType::TABLE )
360                 || ( nCommandType == CommandType::QUERY )
361                 )
362                 sReturn = sCommand;
363         }
364         catch( const Exception& )
365         {
366             OSL_FAIL( "FormLinkDialog::getFormDataSourceType: caught an exception!" );
367         }
368         return sReturn;
369     }
370 
getFormFields(const Reference<XPropertySet> & _rxForm,Sequence<OUString> & _rNames) const371     void FormLinkDialog::getFormFields( const Reference< XPropertySet >& _rxForm, Sequence< OUString >& /* [out] */ _rNames ) const
372     {
373         _rNames.realloc( 0 );
374 
375         ::dbtools::SQLExceptionInfo aErrorInfo;
376         OUString sCommand;
377         try
378         {
379             weld::WaitObject aWaitCursor(m_xDialog.get());
380 
381             OSL_ENSURE( _rxForm.is(), "FormLinkDialog::getFormFields: invalid form!" );
382 
383             sal_Int32       nCommandType = CommandType::COMMAND;
384 
385             _rxForm->getPropertyValue( PROPERTY_COMMANDTYPE ) >>= nCommandType;
386             _rxForm->getPropertyValue( PROPERTY_COMMAND     ) >>= sCommand;
387 
388             Reference< XConnection > xConnection;
389             ensureFormConnection( _rxForm, xConnection );
390 
391             _rNames = ::dbtools::getFieldNamesByCommandDescriptor(
392                 xConnection,
393                 nCommandType,
394                 sCommand,
395                 &aErrorInfo
396             );
397         }
398         catch (const SQLContext& e)    { aErrorInfo = e; }
399         catch (const SQLWarning& e)    { aErrorInfo = e; }
400         catch (const SQLException& e ) { aErrorInfo = e; }
401         catch( const Exception& )
402         {
403             OSL_FAIL( "FormLinkDialog::getFormFields: caught a non-SQL exception!" );
404         }
405 
406         if ( aErrorInfo.isValid() )
407         {
408             OUString sErrorMessage;
409             {
410                 sErrorMessage = PcrRes(STR_ERROR_RETRIEVING_COLUMNS);
411                 sErrorMessage = sErrorMessage.replaceFirst("#", sCommand);
412             }
413 
414             SQLContext aContext;
415             aContext.Message = sErrorMessage;
416             aContext.NextException = aErrorInfo.get();
417             ::dbtools::showError(aContext, m_xDialog->GetXWindow(), m_xContext);
418         }
419     }
420 
ensureFormConnection(const Reference<XPropertySet> & _rxFormProps,Reference<XConnection> & _rxConnection) const421     void FormLinkDialog::ensureFormConnection( const Reference< XPropertySet >& _rxFormProps, Reference< XConnection >& /* [out] */ _rxConnection ) const
422     {
423         OSL_PRECOND( _rxFormProps.is(), "FormLinkDialog::ensureFormConnection: invalid form!" );
424         if ( !_rxFormProps.is() )
425             return;
426         if ( _rxFormProps->getPropertySetInfo()->hasPropertyByName(PROPERTY_ACTIVE_CONNECTION) )
427             _rxConnection.set(_rxFormProps->getPropertyValue(PROPERTY_ACTIVE_CONNECTION),UNO_QUERY);
428 
429         if ( !_rxConnection.is() )
430             _rxConnection = ::dbtools::connectRowset( Reference< XRowSet >( _rxFormProps, UNO_QUERY ), m_xContext, nullptr );
431     }
432 
433 
getConnectionMetaData(const Reference<XPropertySet> & _rxFormProps,Reference<XDatabaseMetaData> & _rxMeta)434     void FormLinkDialog::getConnectionMetaData( const Reference< XPropertySet >& _rxFormProps, Reference< XDatabaseMetaData >& /* [out] */ _rxMeta )
435     {
436         if ( _rxFormProps.is() )
437         {
438             Reference< XConnection > xConnection;
439             if ( !::dbtools::isEmbeddedInDatabase( _rxFormProps, xConnection ) )
440                 _rxFormProps->getPropertyValue( PROPERTY_ACTIVE_CONNECTION ) >>= xConnection;
441             if ( xConnection.is() )
442                 _rxMeta = xConnection->getMetaData();
443         }
444     }
445 
446 
getCanonicUnderlyingTable(const Reference<XPropertySet> & _rxFormProps) const447     Reference< XPropertySet > FormLinkDialog::getCanonicUnderlyingTable( const Reference< XPropertySet >& _rxFormProps ) const
448     {
449         Reference< XPropertySet > xTable;
450         try
451         {
452             Reference< XTablesSupplier > xTablesInForm( ::dbtools::getCurrentSettingsComposer( _rxFormProps, m_xContext, nullptr ), UNO_QUERY );
453             Reference< XNameAccess > xTables;
454             if ( xTablesInForm.is() )
455                 xTables = xTablesInForm->getTables();
456             Sequence< OUString > aTableNames;
457             if ( xTables.is() )
458                 aTableNames = xTables->getElementNames();
459 
460             if ( aTableNames.getLength() == 1 )
461             {
462                 xTables->getByName( aTableNames[ 0 ] ) >>= xTable;
463                 OSL_ENSURE( xTable.is(), "FormLinkDialog::getCanonicUnderlyingTable: invalid table!" );
464             }
465         }
466         catch( const Exception& )
467         {
468             OSL_FAIL( "FormLinkDialog::getCanonicUnderlyingTable: caught an exception!" );
469         }
470         return xTable;
471     }
472 
473 
getExistingRelation(const Reference<XPropertySet> & _rxLHS,const Reference<XPropertySet> &,std::vector<OUString> & _rLeftFields,std::vector<OUString> & _rRightFields)474     bool FormLinkDialog::getExistingRelation( const Reference< XPropertySet >& _rxLHS, const Reference< XPropertySet >& /*_rxRHS*/,
475             // TODO: fix the usage of _rxRHS. This is issue #i81956#.
476         std::vector< OUString >& _rLeftFields, std::vector< OUString >& _rRightFields )
477     {
478         try
479         {
480             Reference< XKeysSupplier > xSuppKeys( _rxLHS, UNO_QUERY );
481             Reference< XIndexAccess >  xKeys;
482             if ( xSuppKeys.is() )
483                 xKeys = xSuppKeys->getKeys();
484 
485             if ( xKeys.is() )
486             {
487                 Reference< XPropertySet >     xKey;
488                 Reference< XColumnsSupplier > xKeyColSupp( xKey, UNO_QUERY );
489                 Reference< XIndexAccess >     xKeyColumns;
490                 Reference< XPropertySet >     xKeyColumn;
491                 OUString sColumnName, sRelatedColumnName;
492 
493                 const sal_Int32 keyCount = xKeys->getCount();
494                 for ( sal_Int32 key = 0; key < keyCount; ++key )
495                 {
496                     xKeys->getByIndex( key ) >>= xKey;
497                     sal_Int32 nKeyType = 0;
498                     xKey->getPropertyValue("Type") >>= nKeyType;
499                     if ( nKeyType != KeyType::FOREIGN )
500                         continue;
501 
502                     xKeyColumns.clear();
503                     xKeyColSupp.set(xKey, css::uno::UNO_QUERY);
504                     if ( xKeyColSupp.is() )
505                         xKeyColumns.set(xKeyColSupp->getColumns(), css::uno::UNO_QUERY);
506                     OSL_ENSURE( xKeyColumns.is(), "FormLinkDialog::getExistingRelation: could not obtain the columns for the key!" );
507 
508                     if ( !xKeyColumns.is() )
509                         continue;
510 
511                     const sal_Int32 columnCount = xKeyColumns->getCount();
512                     _rLeftFields.resize( columnCount );
513                     _rRightFields.resize( columnCount );
514                     for ( sal_Int32 column = 0; column < columnCount; ++column )
515                     {
516                         xKeyColumn.clear();
517                         xKeyColumns->getByIndex( column ) >>= xKeyColumn;
518                         OSL_ENSURE( xKeyColumn.is(), "FormLinkDialog::getExistingRelation: invalid key column!" );
519                         if ( xKeyColumn.is() )
520                         {
521                             xKeyColumn->getPropertyValue( PROPERTY_NAME ) >>= sColumnName;
522                             xKeyColumn->getPropertyValue("RelatedColumn") >>= sRelatedColumnName;
523 
524                             _rLeftFields[ column ]  = sColumnName;
525                             _rRightFields[ column ] = sRelatedColumnName;
526                         }
527                     }
528                 }
529             }
530         }
531         catch( const Exception& )
532         {
533             OSL_FAIL( "FormLinkDialog::getExistingRelation: caught an exception!" );
534         }
535 
536         return ( !_rLeftFields.empty() ) && ( !_rLeftFields[ 0 ].isEmpty() );
537     }
538 
539 
initializeSuggest()540     void FormLinkDialog::initializeSuggest()
541     {
542         if ( !m_xDetailForm.is() || !m_xMasterForm.is() )
543             return;
544 
545         try
546         {
547             // only show the button when both forms are based on the same data source
548             OUString sMasterDS, sDetailDS;
549             m_xMasterForm->getPropertyValue( PROPERTY_DATASOURCE ) >>= sMasterDS;
550             m_xDetailForm->getPropertyValue( PROPERTY_DATASOURCE ) >>= sDetailDS;
551             bool bEnable = ( sMasterDS == sDetailDS );
552 
553             // only show the button when the connection supports relations
554             if ( bEnable )
555             {
556                 Reference< XDatabaseMetaData > xMeta;
557                 getConnectionMetaData( m_xDetailForm, xMeta );
558                 OSL_ENSURE( xMeta.is(), "FormLinkDialog::initializeSuggest: unable to retrieve the meta data for the connection!" );
559                 try
560                 {
561                     bEnable = xMeta.is() && xMeta->supportsIntegrityEnhancementFacility();
562                 }
563                 catch(const Exception&)
564                 {
565                     bEnable = false;
566                 }
567             }
568 
569             // only enable the button if there is a "canonic" table underlying both forms
570             Reference< XPropertySet > xDetailTable, xMasterTable;
571             if ( bEnable )
572             {
573                 xDetailTable = getCanonicUnderlyingTable( m_xDetailForm );
574                 xMasterTable = getCanonicUnderlyingTable( m_xMasterForm );
575                 bEnable = xDetailTable.is() && xMasterTable.is();
576             }
577 
578             // only enable the button if there is a relation between both tables
579             m_aRelationDetailColumns.clear();
580             m_aRelationMasterColumns.clear();
581             if ( bEnable )
582             {
583                 bEnable = getExistingRelation( xDetailTable, xMasterTable, m_aRelationDetailColumns, m_aRelationMasterColumns );
584                 SAL_WARN_IF( m_aRelationMasterColumns.size() != m_aRelationDetailColumns.size(),
585                     "extensions.propctrlr",
586                     "FormLinkDialog::initializeSuggest: nonsense!" );
587                 if ( m_aRelationMasterColumns.empty() )
588                 {   // okay, there is no relation "pointing" (via a foreign key) from the detail table to the master table
589                     // but perhaps the other way round (would make less sense, but who knows ...)
590                     bEnable = getExistingRelation( xMasterTable, xDetailTable, m_aRelationMasterColumns, m_aRelationDetailColumns );
591                 }
592             }
593 
594             // only enable the button if the relation contains at most 4 field pairs
595             if ( bEnable )
596             {
597                 bEnable = ( m_aRelationMasterColumns.size() <= 4 );
598             }
599 
600             m_xSuggest->set_sensitive(bEnable);
601         }
602         catch( const Exception& )
603         {
604             OSL_FAIL( "FormLinkDialog::initializeSuggest: caught an exception!" );
605         }
606     }
607 
IMPL_LINK_NOARG(FormLinkDialog,OnSuggest,weld::Button &,void)608     IMPL_LINK_NOARG( FormLinkDialog, OnSuggest, weld::Button&, void )
609     {
610         initializeFieldRowsFrom( m_aRelationDetailColumns, m_aRelationMasterColumns );
611     }
612 
IMPL_LINK_NOARG(FormLinkDialog,OnFieldChanged,FieldLinkRow &,void)613     IMPL_LINK_NOARG( FormLinkDialog, OnFieldChanged, FieldLinkRow&, void )
614     {
615         updateOkButton();
616     }
617 
IMPL_LINK_NOARG(FormLinkDialog,OnInitialize,void *,void)618     IMPL_LINK_NOARG( FormLinkDialog, OnInitialize, void*, void )
619     {
620         initializeColumnLabels();
621         initializeFieldLists();
622         initializeLinks();
623         initializeSuggest();
624     }
625 
626 }   // namespace pcr
627 
628 
629 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
630