1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "formlinkdialog.hxx"
31 #include "formlinkdialog.hrc"
33 #include "modulepcr.hxx"
34 #include "formresid.hrc"
35 #include "formstrings.hxx"
36 #include <vcl/combobox.hxx>
37 #include <vcl/msgbox.hxx>
38 #include <vcl/waitobj.hxx>
39 #include <svtools/localresaccess.hxx>
40 #include <connectivity/dbtools.hxx>
41 #include <connectivity/dbexception.hxx>
42 #include <toolkit/helper/vclunohelper.hxx>
44 /** === begin UNO includes === **/
45 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
46 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
47 #include <com/sun/star/sdbcx/KeyType.hpp>
48 #include <com/sun/star/container/XNameAccess.hpp>
49 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
50 #include <com/sun/star/sdbc/XRowSet.hpp>
51 #include <com/sun/star/sdb/CommandType.hpp>
52 #include <com/sun/star/sdb/SQLContext.hpp>
53 /** === end UNO includes === **/
56 //............................................................................
59 //............................................................................
61 using namespace ::com::sun::star::uno
;
62 using namespace ::com::sun::star::lang
;
63 using namespace ::com::sun::star::form
;
64 using namespace ::com::sun::star::sdb
;
65 using namespace ::com::sun::star::sdbc
;
66 using namespace ::com::sun::star::sdbcx
;
67 using namespace ::com::sun::star::beans
;
68 using namespace ::com::sun::star::container
;
70 //========================================================================
72 //========================================================================
73 class FieldLinkRow
: public Window
76 ComboBox m_aDetailColumn
;
77 FixedText m_aEqualSign
;
78 ComboBox m_aMasterColumn
;
80 Link m_aLinkChangeHandler
;
83 FieldLinkRow( Window
* _pParent
, const ResId
& _rId
);
85 inline void SetLinkChangeHandler( const Link
& _rHdl
) { m_aLinkChangeHandler
= _rHdl
; }
86 inline const Link
& GetLinkChangeHandler( ) const { return m_aLinkChangeHandler
; }
93 /** retrieves the selected field name for either the master or the detail field
94 @return <TRUE/> if and only a valid field is selected
96 bool GetFieldName( LinkParticipant _eWhich
, String
& /* [out] */ _rName
) const;
97 void SetFieldName( LinkParticipant _eWhich
, const String
& _rName
);
99 void fillList( LinkParticipant _eWhich
, const Sequence
< ::rtl::OUString
>& _rFieldNames
);
102 DECL_LINK( OnFieldNameChanged
, ComboBox
* );
105 //------------------------------------------------------------------------
106 FieldLinkRow::FieldLinkRow( Window
* _pParent
, const ResId
& _rId
)
107 :Window( _pParent
, _rId
)
108 ,m_aDetailColumn( this, ResId( 1, *_rId
.GetResMgr() ) )
109 ,m_aEqualSign ( this, ResId( 1, *_rId
.GetResMgr() ) )
110 ,m_aMasterColumn( this, ResId( 2, *_rId
.GetResMgr() ) )
114 m_aDetailColumn
.SetDropDownLineCount( 10 );
115 m_aMasterColumn
.SetDropDownLineCount( 10 );
117 m_aDetailColumn
.SetModifyHdl( LINK( this, FieldLinkRow
, OnFieldNameChanged
) );
118 m_aMasterColumn
.SetModifyHdl( LINK( this, FieldLinkRow
, OnFieldNameChanged
) );
121 //------------------------------------------------------------------------
122 void FieldLinkRow::fillList( LinkParticipant _eWhich
, const Sequence
< ::rtl::OUString
>& _rFieldNames
)
124 ComboBox
* pBox
= ( _eWhich
== eDetailField
) ? &m_aDetailColumn
: &m_aMasterColumn
;
126 const ::rtl::OUString
* pFieldName
= _rFieldNames
.getConstArray();
127 const ::rtl::OUString
* pFieldNameEnd
= pFieldName
+ _rFieldNames
.getLength();
128 for ( ; pFieldName
!= pFieldNameEnd
; ++pFieldName
)
129 pBox
->InsertEntry( *pFieldName
);
132 //------------------------------------------------------------------------
133 bool FieldLinkRow::GetFieldName( LinkParticipant _eWhich
, String
& /* [out] */ _rName
) const
135 const ComboBox
* pBox
= ( _eWhich
== eDetailField
) ? &m_aDetailColumn
: &m_aMasterColumn
;
136 _rName
= pBox
->GetText();
137 return _rName
.Len() != 0;
140 //------------------------------------------------------------------------
141 void FieldLinkRow::SetFieldName( LinkParticipant _eWhich
, const String
& _rName
)
143 ComboBox
* pBox
= ( _eWhich
== eDetailField
) ? &m_aDetailColumn
: &m_aMasterColumn
;
144 pBox
->SetText( _rName
);
147 //------------------------------------------------------------------------
148 IMPL_LINK( FieldLinkRow
, OnFieldNameChanged
, ComboBox
*, /*_pBox*/ )
150 if ( m_aLinkChangeHandler
.IsSet() )
151 return m_aLinkChangeHandler
.Call( this );
156 //========================================================================
158 //========================================================================
159 //------------------------------------------------------------------------
160 FormLinkDialog::FormLinkDialog( Window
* _pParent
, const Reference
< XPropertySet
>& _rxDetailForm
,
161 const Reference
< XPropertySet
>& _rxMasterForm
, const Reference
< XMultiServiceFactory
>& _rxORB
,
162 const ::rtl::OUString
& _sExplanation
,
163 const ::rtl::OUString
& _sDetailLabel
,
164 const ::rtl::OUString
& _sMasterLabel
)
165 :ModalDialog( _pParent
, PcrRes( RID_DLG_FORMLINKS
) )
166 ,m_aExplanation( this, PcrRes( FT_EXPLANATION
) )
167 ,m_aDetailLabel( this, PcrRes( FT_DETAIL_LABEL
) )
168 ,m_aMasterLabel( this, PcrRes( FT_MASTER_LABEL
) )
169 ,m_aRow1 ( new FieldLinkRow( this, PcrRes( 1 ) ) )
170 ,m_aRow2 ( new FieldLinkRow( this, PcrRes( 2 ) ) )
171 ,m_aRow3 ( new FieldLinkRow( this, PcrRes( 3 ) ) )
172 ,m_aRow4 ( new FieldLinkRow( this, PcrRes( 4 ) ) )
173 ,m_aOK ( this, PcrRes( PB_OK
) )
174 ,m_aCancel ( this, PcrRes( PB_CANCEL
) )
175 ,m_aHelp ( this, PcrRes( PB_HELP
) )
176 ,m_aSuggest ( this, PcrRes( PB_SUGGEST
) )
178 ,m_xDetailForm( _rxDetailForm
)
179 ,m_xMasterForm( _rxMasterForm
)
180 ,m_sDetailLabel(_sDetailLabel
)
181 ,m_sMasterLabel(_sMasterLabel
)
184 if ( !_sExplanation
.isEmpty() )
185 m_aExplanation
.SetText(_sExplanation
);
187 m_aSuggest
.SetClickHdl ( LINK( this, FormLinkDialog
, OnSuggest
) );
188 m_aRow1
->SetLinkChangeHandler( LINK( this, FormLinkDialog
, OnFieldChanged
) );
189 m_aRow2
->SetLinkChangeHandler( LINK( this, FormLinkDialog
, OnFieldChanged
) );
190 m_aRow3
->SetLinkChangeHandler( LINK( this, FormLinkDialog
, OnFieldChanged
) );
191 m_aRow4
->SetLinkChangeHandler( LINK( this, FormLinkDialog
, OnFieldChanged
) );
193 PostUserEvent( LINK( this, FormLinkDialog
, OnInitialize
) );
198 //------------------------------------------------------------------------
199 FormLinkDialog::~FormLinkDialog( )
203 //------------------------------------------------------------------------
204 void FormLinkDialog::commitLinkPairs()
206 // collect the field lists from the rows
207 ::std::vector
< ::rtl::OUString
> aDetailFields
; aDetailFields
.reserve( 4 );
208 ::std::vector
< ::rtl::OUString
> aMasterFields
; aMasterFields
.reserve( 4 );
210 const FieldLinkRow
* aRows
[] = {
211 m_aRow1
.get(), m_aRow2
.get(), m_aRow3
.get(), m_aRow4
.get()
214 for ( sal_Int32 i
= 0; i
< 4; ++i
)
216 String sDetailField
, sMasterField
;
217 aRows
[ i
]->GetFieldName( FieldLinkRow::eDetailField
, sDetailField
);
218 aRows
[ i
]->GetFieldName( FieldLinkRow::eMasterField
, sMasterField
);
219 if ( !sDetailField
.Len() && !sMasterField
.Len() )
222 aDetailFields
.push_back( sDetailField
);
223 aMasterFields
.push_back( sMasterField
);
226 // and set as property values
229 Reference
< XPropertySet
> xDetailFormProps( m_xDetailForm
, UNO_QUERY
);
230 if ( xDetailFormProps
.is() )
232 ::rtl::OUString
*pFields
= aDetailFields
.empty() ? 0 : &aDetailFields
[0];
233 xDetailFormProps
->setPropertyValue( PROPERTY_DETAILFIELDS
, makeAny( Sequence
< ::rtl::OUString
>( pFields
, aDetailFields
.size() ) ) );
234 pFields
= aMasterFields
.empty() ? 0 : &aMasterFields
[0];
235 xDetailFormProps
->setPropertyValue( PROPERTY_MASTERFIELDS
, makeAny( Sequence
< ::rtl::OUString
>( pFields
, aMasterFields
.size() ) ) );
238 catch( const Exception
& )
240 OSL_FAIL( "FormLinkDialog::commitLinkPairs: caught an exception while setting the properties!" );
244 //------------------------------------------------------------------------
245 short FormLinkDialog::Execute()
247 short nResult
= ModalDialog::Execute();
249 if ( RET_OK
== nResult
)
255 //------------------------------------------------------------------------
256 void FormLinkDialog::initializeFieldLists()
258 Sequence
< ::rtl::OUString
> sDetailFields
;
259 getFormFields( m_xDetailForm
, sDetailFields
);
261 Sequence
< ::rtl::OUString
> sMasterFields
;
262 getFormFields( m_xMasterForm
, sMasterFields
);
264 FieldLinkRow
* aRows
[] = {
265 m_aRow1
.get(), m_aRow2
.get(), m_aRow3
.get(), m_aRow4
.get()
267 for ( sal_Int32 i
= 0; i
< 4 ; ++i
)
269 aRows
[i
]->fillList( FieldLinkRow::eDetailField
, sDetailFields
);
270 aRows
[i
]->fillList( FieldLinkRow::eMasterField
, sMasterFields
);
275 //------------------------------------------------------------------------
276 void FormLinkDialog::initializeColumnLabels()
278 // label for the detail form
279 String sDetailType
= getFormDataSourceType( m_xDetailForm
);
280 if ( !sDetailType
.Len() )
282 if ( m_sDetailLabel
.isEmpty() )
284 ::svt::OLocalResourceAccess
aStringAccess( PcrRes( RID_DLG_FORMLINKS
), RSC_MODALDIALOG
);
285 m_sDetailLabel
= String( PcrRes( STR_DETAIL_FORM
) );
287 sDetailType
= m_sDetailLabel
;
289 m_aDetailLabel
.SetText( sDetailType
);
291 // label for the master form
292 String sMasterType
= getFormDataSourceType( m_xMasterForm
);
293 if ( !sMasterType
.Len() )
295 if ( m_sMasterLabel
.isEmpty() )
297 ::svt::OLocalResourceAccess
aStringAccess( PcrRes( RID_DLG_FORMLINKS
), RSC_MODALDIALOG
);
298 m_sMasterLabel
= String( PcrRes( STR_MASTER_FORM
) );
300 sMasterType
= m_sMasterLabel
;
302 m_aMasterLabel
.SetText( sMasterType
);
305 //------------------------------------------------------------------------
306 void FormLinkDialog::initializeFieldRowsFrom( Sequence
< ::rtl::OUString
>& _rDetailFields
, Sequence
< ::rtl::OUString
>& _rMasterFields
)
308 // our UI does allow 4 fields max
309 _rDetailFields
.realloc( 4 );
310 _rMasterFields
.realloc( 4 );
312 const ::rtl::OUString
* pDetailFields
= _rDetailFields
.getConstArray();
313 const ::rtl::OUString
* pMasterFields
= _rMasterFields
.getConstArray();
315 FieldLinkRow
* aRows
[] = {
316 m_aRow1
.get(), m_aRow2
.get(), m_aRow3
.get(), m_aRow4
.get()
318 for ( sal_Int32 i
= 0; i
< 4; ++i
, ++pDetailFields
, ++pMasterFields
)
320 aRows
[ i
]->SetFieldName( FieldLinkRow::eDetailField
, *pDetailFields
);
321 aRows
[ i
]->SetFieldName( FieldLinkRow::eMasterField
, *pMasterFields
);
325 //------------------------------------------------------------------------
326 void FormLinkDialog::initializeLinks()
330 Sequence
< ::rtl::OUString
> aDetailFields
;
331 Sequence
< ::rtl::OUString
> aMasterFields
;
333 Reference
< XPropertySet
> xDetailFormProps( m_xDetailForm
, UNO_QUERY
);
334 if ( xDetailFormProps
.is() )
336 xDetailFormProps
->getPropertyValue( PROPERTY_DETAILFIELDS
) >>= aDetailFields
;
337 xDetailFormProps
->getPropertyValue( PROPERTY_MASTERFIELDS
) >>= aMasterFields
;
340 initializeFieldRowsFrom( aDetailFields
, aMasterFields
);
342 catch( const Exception
& )
344 OSL_FAIL( "FormLinkDialog::initializeLinks: caught an exception!" );
348 //------------------------------------------------------------------------
349 void FormLinkDialog::updateOkButton()
351 // in all rows, there must be either two valid selections, or none at all
352 // If there is at least one row with exactly one valid selection, then the
353 // OKButton needs to be disabled
354 sal_Bool bEnable
= sal_True
;
356 const FieldLinkRow
* aRows
[] = {
357 m_aRow1
.get(), m_aRow2
.get(), m_aRow3
.get(), m_aRow4
.get()
360 for ( sal_Int32 i
= 0; ( i
< 4 ) && bEnable
; ++i
)
362 String sNotInterestedInRightNow
;
363 if ( aRows
[ i
]->GetFieldName( FieldLinkRow::eDetailField
, sNotInterestedInRightNow
)
364 != aRows
[ i
]->GetFieldName( FieldLinkRow::eMasterField
, sNotInterestedInRightNow
)
369 m_aOK
.Enable( bEnable
);
372 //------------------------------------------------------------------------
373 String
FormLinkDialog::getFormDataSourceType( const Reference
< XPropertySet
>& _rxForm
) const SAL_THROW(())
376 Reference
< XPropertySet
> xFormProps( _rxForm
, UNO_QUERY
);
377 if ( !xFormProps
.is() )
382 sal_Int32 nCommandType
= CommandType::COMMAND
;
383 ::rtl::OUString sCommand
;
385 xFormProps
->getPropertyValue( PROPERTY_COMMANDTYPE
) >>= nCommandType
;
386 xFormProps
->getPropertyValue( PROPERTY_COMMAND
) >>= sCommand
;
388 if ( ( nCommandType
== CommandType::TABLE
)
389 || ( nCommandType
== CommandType::QUERY
)
393 catch( const Exception
& )
395 OSL_FAIL( "FormLinkDialog::getFormDataSourceType: caught an exception!" );
400 //------------------------------------------------------------------------
401 void FormLinkDialog::getFormFields( const Reference
< XPropertySet
>& _rxForm
, Sequence
< ::rtl::OUString
>& /* [out] */ _rNames
) const SAL_THROW(( ))
403 _rNames
.realloc( 0 );
405 ::dbtools::SQLExceptionInfo aErrorInfo
;
406 ::rtl::OUString sCommand
;
409 WaitObject
aWaitCursor( const_cast< FormLinkDialog
* >( this ) );
411 Reference
< XPropertySet
> xFormProps( _rxForm
, UNO_QUERY
);
412 OSL_ENSURE( xFormProps
.is(), "FormLinkDialog::getFormFields: invalid form!" );
414 sal_Int32 nCommandType
= CommandType::COMMAND
;
416 xFormProps
->getPropertyValue( PROPERTY_COMMANDTYPE
) >>= nCommandType
;
417 xFormProps
->getPropertyValue( PROPERTY_COMMAND
) >>= sCommand
;
419 Reference
< XConnection
> xConnection
;
420 ensureFormConnection( xFormProps
, xConnection
);
422 _rNames
= ::dbtools::getFieldNamesByCommandDescriptor(
429 catch (const SQLContext
& e
) { aErrorInfo
= e
; }
430 catch (const SQLWarning
& e
) { aErrorInfo
= e
; }
431 catch (const SQLException
& e
) { aErrorInfo
= e
; }
432 catch( const Exception
& )
434 OSL_FAIL( "FormLinkDialog::getFormFields: caught a non-SQL exception!" );
437 if ( aErrorInfo
.isValid() )
439 String sErrorMessage
;
441 ::svt::OLocalResourceAccess
aStringAccess( PcrRes( RID_DLG_FORMLINKS
), RSC_MODALDIALOG
);
442 sErrorMessage
= String( PcrRes( STR_ERROR_RETRIEVING_COLUMNS
) );
443 sErrorMessage
.SearchAndReplace('#',sCommand
);
447 aContext
.Message
= sErrorMessage
;
448 aContext
.NextException
= aErrorInfo
.get();
449 ::dbtools::showError( aContext
, VCLUnoHelper::GetInterface( const_cast< FormLinkDialog
* >( this ) ), m_xORB
);
453 //------------------------------------------------------------------------
454 void FormLinkDialog::ensureFormConnection( const Reference
< XPropertySet
>& _rxFormProps
, Reference
< XConnection
>& /* [out] */ _rxConnection
) const SAL_THROW(( Exception
))
456 OSL_PRECOND( _rxFormProps
.is(), "FormLinkDialog::ensureFormConnection: invalid form!" );
457 if ( !_rxFormProps
.is() )
459 if ( _rxFormProps
->getPropertySetInfo()->hasPropertyByName(PROPERTY_ACTIVE_CONNECTION
) )
460 _rxConnection
.set(_rxFormProps
->getPropertyValue(PROPERTY_ACTIVE_CONNECTION
),UNO_QUERY
);
462 if ( !_rxConnection
.is() )
463 _rxConnection
= ::dbtools::connectRowset( Reference
< XRowSet
>( _rxFormProps
, UNO_QUERY
), m_xORB
, sal_True
);
466 //------------------------------------------------------------------------
467 void FormLinkDialog::getConnectionMetaData( const Reference
< XPropertySet
>& _rxFormProps
, Reference
< XDatabaseMetaData
>& /* [out] */ _rxMeta
) const SAL_THROW(( Exception
))
469 if ( _rxFormProps
.is() )
471 Reference
< XConnection
> xConnection
;
472 if ( !::dbtools::isEmbeddedInDatabase( _rxFormProps
, xConnection
) )
473 _rxFormProps
->getPropertyValue( PROPERTY_ACTIVE_CONNECTION
) >>= xConnection
;
474 if ( xConnection
.is() )
475 _rxMeta
= xConnection
->getMetaData();
479 //------------------------------------------------------------------------
480 Reference
< XPropertySet
> FormLinkDialog::getCanonicUnderlyingTable( const Reference
< XPropertySet
>& _rxFormProps
) const
482 Reference
< XPropertySet
> xTable
;
485 Reference
< XTablesSupplier
> xTablesInForm( ::dbtools::getCurrentSettingsComposer( _rxFormProps
, m_xORB
), UNO_QUERY
);
486 Reference
< XNameAccess
> xTables
;
487 if ( xTablesInForm
.is() )
488 xTables
= xTablesInForm
->getTables();
489 Sequence
< ::rtl::OUString
> aTableNames
;
491 aTableNames
= xTables
->getElementNames();
493 if ( aTableNames
.getLength() == 1 )
495 xTables
->getByName( aTableNames
[ 0 ] ) >>= xTable
;
496 OSL_ENSURE( xTable
.is(), "FormLinkDialog::getCanonicUnderlyingTable: invalid table!" );
499 catch( const Exception
& )
501 OSL_FAIL( "FormLinkDialog::getCanonicUnderlyingTable: caught an exception!" );
506 //------------------------------------------------------------------------
507 sal_Bool
FormLinkDialog::getExistingRelation( const Reference
< XPropertySet
>& _rxLHS
, const Reference
< XPropertySet
>& /*_rxRHS*/,
508 // TODO: fix the usage of _rxRHS. This is issue #i81956#.
509 Sequence
< ::rtl::OUString
>& _rLeftFields
, Sequence
< ::rtl::OUString
>& _rRightFields
) const
513 Reference
< XKeysSupplier
> xSuppKeys( _rxLHS
, UNO_QUERY
);
514 Reference
< XIndexAccess
> xKeys
;
515 if ( xSuppKeys
.is() )
516 xKeys
= xSuppKeys
->getKeys();
520 Reference
< XPropertySet
> xKey
;
521 Reference
< XColumnsSupplier
> xKeyColSupp( xKey
, UNO_QUERY
);
522 Reference
< XIndexAccess
> xKeyColumns
;
523 Reference
< XPropertySet
> xKeyColumn
;
524 ::rtl::OUString sColumnName
, sRelatedColumnName
;
526 const sal_Int32 keyCount
= xKeys
->getCount();
527 for ( sal_Int32 key
= 0; key
< keyCount
; ++key
)
529 xKeys
->getByIndex( key
) >>= xKey
;
530 sal_Int32 nKeyType
= 0;
531 xKey
->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Type" ) ) ) >>= nKeyType
;
532 if ( nKeyType
!= KeyType::FOREIGN
)
536 xKeyColSupp
= xKeyColSupp
.query( xKey
);
537 if ( xKeyColSupp
.is() )
538 xKeyColumns
= xKeyColumns
.query( xKeyColSupp
->getColumns() );
539 OSL_ENSURE( xKeyColumns
.is(), "FormLinkDialog::getExistingRelation: could not obtain the columns for the key!" );
541 if ( !xKeyColumns
.is() )
544 const sal_Int32 columnCount
= xKeyColumns
->getCount();
545 _rLeftFields
.realloc( columnCount
);
546 _rRightFields
.realloc( columnCount
);
547 for ( sal_Int32 column
= 0; column
< columnCount
; ++column
)
550 xKeyColumns
->getByIndex( column
) >>= xKeyColumn
;
551 OSL_ENSURE( xKeyColumn
.is(), "FormLinkDialog::getExistingRelation: invalid key column!" );
552 if ( xKeyColumn
.is() )
554 xKeyColumn
->getPropertyValue( PROPERTY_NAME
) >>= sColumnName
;
555 xKeyColumn
->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "RelatedColumn" ) ) ) >>= sRelatedColumnName
;
557 _rLeftFields
[ column
] = sColumnName
;
558 _rRightFields
[ column
] = sRelatedColumnName
;
564 catch( const Exception
& )
566 OSL_FAIL( "FormLinkDialog::getExistingRelation: caught an exception!" );
569 return ( _rLeftFields
.getLength() > 0 ) && ( !_rLeftFields
[ 0 ].isEmpty() );
572 //------------------------------------------------------------------------
573 void FormLinkDialog::initializeSuggest()
575 Reference
< XPropertySet
> xDetailFormProps( m_xDetailForm
, UNO_QUERY
);
576 Reference
< XPropertySet
> xMasterFormProps( m_xMasterForm
, UNO_QUERY
);
577 if ( !xDetailFormProps
.is() || !xMasterFormProps
.is() )
582 sal_Bool bEnable
= sal_True
;
584 // only show the button when both forms are based on the same data source
587 ::rtl::OUString sMasterDS
, sDetailDS
;
588 xMasterFormProps
->getPropertyValue( PROPERTY_DATASOURCE
) >>= sMasterDS
;
589 xDetailFormProps
->getPropertyValue( PROPERTY_DATASOURCE
) >>= sDetailDS
;
590 bEnable
= ( sMasterDS
== sDetailDS
);
593 // only show the button when the connection supports relations
596 Reference
< XDatabaseMetaData
> xMeta
;
597 getConnectionMetaData( xDetailFormProps
, xMeta
);
598 OSL_ENSURE( xMeta
.is(), "FormLinkDialog::initializeSuggest: unable to retrieve the meta data for the connection!" );
601 bEnable
= xMeta
.is() && xMeta
->supportsIntegrityEnhancementFacility();
603 catch(const Exception
&)
609 // only enable the button if there is a "canonic" table underlying both forms
610 Reference
< XPropertySet
> xDetailTable
, xMasterTable
;
613 xDetailTable
= getCanonicUnderlyingTable( xDetailFormProps
);
614 xMasterTable
= getCanonicUnderlyingTable( xMasterFormProps
);
615 bEnable
= xDetailTable
.is() && xMasterTable
.is();
618 // only enable the button if there is a relation between both tables
619 m_aRelationDetailColumns
.realloc( 0 );
620 m_aRelationMasterColumns
.realloc( 0 );
623 bEnable
= getExistingRelation( xDetailTable
, xMasterTable
, m_aRelationDetailColumns
, m_aRelationMasterColumns
);
624 OSL_POSTCOND( m_aRelationMasterColumns
.getLength() == m_aRelationDetailColumns
.getLength(), "FormLinkDialog::initializeSuggest: nonsense!" );
625 if ( m_aRelationMasterColumns
.getLength() == 0 )
626 { // okay, there is no relation "pointing" (via a foreign key) from the detail table to the master table
627 // but perhaps the other way round (would make less sense, but who knows ...)
628 bEnable
= getExistingRelation( xMasterTable
, xDetailTable
, m_aRelationMasterColumns
, m_aRelationDetailColumns
);
632 // only enable the button if the relation contains at most 4 field pairs
635 bEnable
= ( m_aRelationMasterColumns
.getLength() <= 4 );
638 m_aSuggest
.Enable( bEnable
);
640 catch( const Exception
& )
642 OSL_FAIL( "FormLinkDialog::initializeSuggest: caught an exception!" );
646 //------------------------------------------------------------------------
647 IMPL_LINK( FormLinkDialog
, OnSuggest
, void*, /*_pNotInterestedIn*/ )
649 initializeFieldRowsFrom( m_aRelationDetailColumns
, m_aRelationMasterColumns
);
653 //------------------------------------------------------------------------
654 IMPL_LINK( FormLinkDialog
, OnFieldChanged
, FieldLinkRow
*, /*_pRow*/ )
660 //------------------------------------------------------------------------
661 IMPL_LINK( FormLinkDialog
, OnInitialize
, void*, /*_pNotInterestedIn*/ )
663 initializeColumnLabels();
664 initializeFieldLists();
669 //............................................................................
671 //............................................................................
673 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */