LCOV - code coverage report
Current view: top level - extensions/source/propctrlr - formlinkdialog.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 294 0.0 %
Date: 2014-04-14 Functions: 0 32 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : #include "formlinkdialog.hrc"
      23             : 
      24             : #include "modulepcr.hxx"
      25             : #include "formresid.hrc"
      26             : #include "formstrings.hxx"
      27             : #include <vcl/combobox.hxx>
      28             : #include <vcl/msgbox.hxx>
      29             : #include <vcl/waitobj.hxx>
      30             : #include <svtools/localresaccess.hxx>
      31             : #include <connectivity/dbtools.hxx>
      32             : #include <connectivity/dbexception.hxx>
      33             : #include <toolkit/helper/vclunohelper.hxx>
      34             : #include <comphelper/processfactory.hxx>
      35             : 
      36             : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
      37             : #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
      38             : #include <com/sun/star/sdbcx/KeyType.hpp>
      39             : #include <com/sun/star/container/XNameAccess.hpp>
      40             : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
      41             : #include <com/sun/star/sdbc/XRowSet.hpp>
      42             : #include <com/sun/star/sdb/CommandType.hpp>
      43             : #include <com/sun/star/sdb/SQLContext.hpp>
      44             : 
      45             : 
      46             : 
      47             : namespace pcr
      48             : {
      49             : 
      50             : 
      51             :     using namespace ::com::sun::star::uno;
      52             :     using namespace ::com::sun::star::lang;
      53             :     using namespace ::com::sun::star::form;
      54             :     using namespace ::com::sun::star::sdb;
      55             :     using namespace ::com::sun::star::sdbc;
      56             :     using namespace ::com::sun::star::sdbcx;
      57             :     using namespace ::com::sun::star::beans;
      58             :     using namespace ::com::sun::star::container;
      59             : 
      60             : 
      61             :     //= FieldLinkRow
      62             : 
      63           0 :     class FieldLinkRow : public Window
      64             :     {
      65             :     private:
      66             :         ComboBox    m_aDetailColumn;
      67             :         FixedText   m_aEqualSign;
      68             :         ComboBox    m_aMasterColumn;
      69             : 
      70             :         Link        m_aLinkChangeHandler;
      71             : 
      72             :     public:
      73             :         FieldLinkRow( Window* _pParent, const ResId& _rId );
      74             : 
      75           0 :         inline void         SetLinkChangeHandler( const Link& _rHdl ) { m_aLinkChangeHandler = _rHdl; }
      76             :         inline const Link&  GetLinkChangeHandler( ) const             { return m_aLinkChangeHandler;  }
      77             : 
      78             :         enum LinkParticipant
      79             :         {
      80             :             eDetailField,
      81             :             eMasterField
      82             :         };
      83             :         /** retrieves the selected field name for either the master or the detail field
      84             :             @return <TRUE/> if and only a valid field is selected
      85             :         */
      86             :         bool    GetFieldName( LinkParticipant _eWhich, OUString& /* [out] */ _rName ) const;
      87             :         void    SetFieldName( LinkParticipant _eWhich, const OUString& _rName );
      88             : 
      89             :         void    fillList( LinkParticipant _eWhich, const Sequence< OUString >& _rFieldNames );
      90             : 
      91             :     private:
      92             :         DECL_LINK( OnFieldNameChanged, ComboBox* );
      93             :     };
      94             : 
      95             : 
      96           0 :     FieldLinkRow::FieldLinkRow( Window* _pParent, const ResId& _rId )
      97             :         :Window( _pParent, _rId )
      98           0 :         ,m_aDetailColumn( this, ResId( 1, *_rId.GetResMgr() ) )
      99           0 :         ,m_aEqualSign   ( this, ResId( 1, *_rId.GetResMgr() ) )
     100           0 :         ,m_aMasterColumn( this, ResId( 2, *_rId.GetResMgr() ) )
     101             :     {
     102           0 :         FreeResource();
     103             : 
     104           0 :         m_aDetailColumn.SetDropDownLineCount( 10 );
     105           0 :         m_aMasterColumn.SetDropDownLineCount( 10 );
     106             : 
     107           0 :         m_aDetailColumn.SetModifyHdl( LINK( this, FieldLinkRow, OnFieldNameChanged ) );
     108           0 :         m_aMasterColumn.SetModifyHdl( LINK( this, FieldLinkRow, OnFieldNameChanged ) );
     109           0 :     }
     110             : 
     111             : 
     112           0 :     void FieldLinkRow::fillList( LinkParticipant _eWhich, const Sequence< OUString >& _rFieldNames )
     113             :     {
     114           0 :         ComboBox* pBox = ( _eWhich == eDetailField ) ? &m_aDetailColumn : &m_aMasterColumn;
     115             : 
     116           0 :         const OUString* pFieldName    = _rFieldNames.getConstArray();
     117           0 :         const OUString* pFieldNameEnd = pFieldName + _rFieldNames.getLength();
     118           0 :         for ( ; pFieldName != pFieldNameEnd; ++pFieldName )
     119           0 :             pBox->InsertEntry( *pFieldName );
     120           0 :     }
     121             : 
     122             : 
     123           0 :     bool FieldLinkRow::GetFieldName( LinkParticipant _eWhich, OUString& /* [out] */ _rName ) const
     124             :     {
     125           0 :         const ComboBox* pBox = ( _eWhich == eDetailField ) ? &m_aDetailColumn : &m_aMasterColumn;
     126           0 :         _rName = pBox->GetText();
     127           0 :         return !_rName.isEmpty();
     128             :     }
     129             : 
     130             : 
     131           0 :     void FieldLinkRow::SetFieldName( LinkParticipant _eWhich, const OUString& _rName )
     132             :     {
     133           0 :         ComboBox* pBox = ( _eWhich == eDetailField ) ? &m_aDetailColumn : &m_aMasterColumn;
     134           0 :         pBox->SetText( _rName );
     135           0 :     }
     136             : 
     137             : 
     138           0 :     IMPL_LINK( FieldLinkRow, OnFieldNameChanged, ComboBox*, /*_pBox*/ )
     139             :     {
     140           0 :         if ( m_aLinkChangeHandler.IsSet() )
     141           0 :             return m_aLinkChangeHandler.Call( this );
     142             : 
     143           0 :         return 0L;
     144             :     }
     145             : 
     146             : 
     147             :     //= FormLinkDialog
     148             : 
     149             : 
     150           0 :     FormLinkDialog::FormLinkDialog( Window* _pParent, const Reference< XPropertySet >& _rxDetailForm,
     151             :             const Reference< XPropertySet >& _rxMasterForm, const Reference< XComponentContext >& _rxContext,
     152             :             const OUString& _sExplanation,
     153             :             const OUString& _sDetailLabel,
     154             :             const OUString& _sMasterLabel)
     155             :         :ModalDialog( _pParent, PcrRes( RID_DLG_FORMLINKS ) )
     156             :         ,m_aExplanation( this, PcrRes( FT_EXPLANATION  ) )
     157             :         ,m_aDetailLabel( this, PcrRes( FT_DETAIL_LABEL ) )
     158             :         ,m_aMasterLabel( this, PcrRes( FT_MASTER_LABEL ) )
     159           0 :         ,m_aRow1       ( new FieldLinkRow( this, PcrRes( 1 ) ) )
     160           0 :         ,m_aRow2       ( new FieldLinkRow( this, PcrRes( 2 ) ) )
     161           0 :         ,m_aRow3       ( new FieldLinkRow( this, PcrRes( 3 ) ) )
     162           0 :         ,m_aRow4       ( new FieldLinkRow( this, PcrRes( 4 ) ) )
     163             :         ,m_aOK         ( this, PcrRes( PB_OK           ) )
     164             :         ,m_aCancel     ( this, PcrRes( PB_CANCEL       ) )
     165             :         ,m_aHelp       ( this, PcrRes( PB_HELP         ) )
     166             :         ,m_aSuggest    ( this, PcrRes( PB_SUGGEST      ) )
     167             :         ,m_xContext    ( _rxContext        )
     168             :         ,m_xDetailForm( _rxDetailForm )
     169             :         ,m_xMasterForm( _rxMasterForm )
     170             :         ,m_sDetailLabel(_sDetailLabel)
     171           0 :         ,m_sMasterLabel(_sMasterLabel)
     172             :     {
     173           0 :         FreeResource();
     174           0 :         if ( !_sExplanation.isEmpty() )
     175           0 :             m_aExplanation.SetText(_sExplanation);
     176             : 
     177           0 :         m_aSuggest.SetClickHdl       ( LINK( this, FormLinkDialog, OnSuggest      ) );
     178           0 :         m_aRow1->SetLinkChangeHandler( LINK( this, FormLinkDialog, OnFieldChanged ) );
     179           0 :         m_aRow2->SetLinkChangeHandler( LINK( this, FormLinkDialog, OnFieldChanged ) );
     180           0 :         m_aRow3->SetLinkChangeHandler( LINK( this, FormLinkDialog, OnFieldChanged ) );
     181           0 :         m_aRow4->SetLinkChangeHandler( LINK( this, FormLinkDialog, OnFieldChanged ) );
     182             : 
     183           0 :         PostUserEvent( LINK( this, FormLinkDialog, OnInitialize ) );
     184             : 
     185           0 :         updateOkButton();
     186           0 :     }
     187             : 
     188             : 
     189           0 :     FormLinkDialog::~FormLinkDialog( )
     190             :     {
     191           0 :     }
     192             : 
     193             : 
     194           0 :     void FormLinkDialog::commitLinkPairs()
     195             :     {
     196             :         // collect the field lists from the rows
     197           0 :         ::std::vector< OUString > aDetailFields; aDetailFields.reserve( 4 );
     198           0 :         ::std::vector< OUString > aMasterFields; aMasterFields.reserve( 4 );
     199             : 
     200             :         const FieldLinkRow* aRows[] = {
     201           0 :             m_aRow1.get(), m_aRow2.get(), m_aRow3.get(), m_aRow4.get()
     202           0 :         };
     203             : 
     204           0 :         for ( sal_Int32 i = 0; i < 4; ++i )
     205             :         {
     206           0 :             OUString sDetailField, sMasterField;
     207           0 :             aRows[ i ]->GetFieldName( FieldLinkRow::eDetailField, sDetailField );
     208           0 :             aRows[ i ]->GetFieldName( FieldLinkRow::eMasterField, sMasterField );
     209           0 :             if ( sDetailField.isEmpty() && sMasterField.isEmpty() )
     210           0 :                 continue;
     211             : 
     212           0 :             aDetailFields.push_back( sDetailField );
     213           0 :             aMasterFields.push_back( sMasterField );
     214           0 :         }
     215             : 
     216             :         // and set as property values
     217             :         try
     218             :         {
     219           0 :             Reference< XPropertySet > xDetailFormProps( m_xDetailForm, UNO_QUERY );
     220           0 :             if ( xDetailFormProps.is() )
     221             :             {
     222           0 :                 OUString *pFields = aDetailFields.empty() ? 0 : &aDetailFields[0];
     223           0 :                 xDetailFormProps->setPropertyValue( PROPERTY_DETAILFIELDS, makeAny( Sequence< OUString >( pFields, aDetailFields.size() ) ) );
     224           0 :                 pFields = aMasterFields.empty() ? 0 : &aMasterFields[0];
     225           0 :                 xDetailFormProps->setPropertyValue( PROPERTY_MASTERFIELDS, makeAny( Sequence< OUString >( pFields, aMasterFields.size() ) ) );
     226           0 :             }
     227             :         }
     228           0 :         catch( const Exception& )
     229             :         {
     230             :             OSL_FAIL( "FormLinkDialog::commitLinkPairs: caught an exception while setting the properties!" );
     231           0 :         }
     232           0 :     }
     233             : 
     234             : 
     235           0 :     short FormLinkDialog::Execute()
     236             :     {
     237           0 :         short nResult = ModalDialog::Execute();
     238             : 
     239           0 :         if ( RET_OK == nResult )
     240           0 :             commitLinkPairs();
     241             : 
     242           0 :         return nResult;
     243             :     }
     244             : 
     245             : 
     246           0 :     void FormLinkDialog::initializeFieldLists()
     247             :     {
     248           0 :         Sequence< OUString > sDetailFields;
     249           0 :         getFormFields( m_xDetailForm, sDetailFields );
     250             : 
     251           0 :         Sequence< OUString > sMasterFields;
     252           0 :         getFormFields( m_xMasterForm, sMasterFields );
     253             : 
     254             :         FieldLinkRow* aRows[] = {
     255           0 :             m_aRow1.get(), m_aRow2.get(), m_aRow3.get(), m_aRow4.get()
     256           0 :         };
     257           0 :         for ( sal_Int32 i = 0; i < 4 ; ++i )
     258             :         {
     259           0 :             aRows[i]->fillList( FieldLinkRow::eDetailField, sDetailFields );
     260           0 :             aRows[i]->fillList( FieldLinkRow::eMasterField, sMasterFields );
     261           0 :         }
     262             : 
     263           0 :     }
     264             : 
     265             : 
     266           0 :     void FormLinkDialog::initializeColumnLabels()
     267             :     {
     268             :         // label for the detail form
     269           0 :         OUString sDetailType = getFormDataSourceType( m_xDetailForm );
     270           0 :         if ( sDetailType.isEmpty() )
     271             :         {
     272           0 :             if ( m_sDetailLabel.isEmpty() )
     273             :             {
     274           0 :                 ::svt::OLocalResourceAccess aStringAccess( PcrRes( RID_DLG_FORMLINKS ), RSC_MODALDIALOG );
     275           0 :                 m_sDetailLabel = PcrRes(STR_DETAIL_FORM).toString();
     276             :             }
     277           0 :             sDetailType = m_sDetailLabel;
     278             :         }
     279           0 :         m_aDetailLabel.SetText( sDetailType );
     280             : 
     281             :         // label for the master form
     282           0 :         OUString sMasterType = getFormDataSourceType( m_xMasterForm );
     283           0 :         if ( sMasterType.isEmpty() )
     284             :         {
     285           0 :             if ( m_sMasterLabel.isEmpty() )
     286             :             {
     287           0 :                 ::svt::OLocalResourceAccess aStringAccess( PcrRes( RID_DLG_FORMLINKS ), RSC_MODALDIALOG );
     288           0 :                 m_sMasterLabel = PcrRes(STR_MASTER_FORM).toString();
     289             :             }
     290           0 :             sMasterType = m_sMasterLabel;
     291             :         }
     292           0 :         m_aMasterLabel.SetText( sMasterType );
     293           0 :     }
     294             : 
     295             : 
     296           0 :     void FormLinkDialog::initializeFieldRowsFrom( Sequence< OUString >& _rDetailFields, Sequence< OUString >& _rMasterFields )
     297             :     {
     298             :         // our UI does allow 4 fields max
     299           0 :         _rDetailFields.realloc( 4 );
     300           0 :         _rMasterFields.realloc( 4 );
     301             : 
     302           0 :         const OUString* pDetailFields = _rDetailFields.getConstArray();
     303           0 :         const OUString* pMasterFields = _rMasterFields.getConstArray();
     304             : 
     305             :         FieldLinkRow* aRows[] = {
     306           0 :             m_aRow1.get(), m_aRow2.get(), m_aRow3.get(), m_aRow4.get()
     307           0 :         };
     308           0 :         for ( sal_Int32 i = 0; i < 4; ++i, ++pDetailFields, ++pMasterFields )
     309             :         {
     310           0 :             aRows[ i ]->SetFieldName( FieldLinkRow::eDetailField, *pDetailFields );
     311           0 :             aRows[ i ]->SetFieldName( FieldLinkRow::eMasterField, *pMasterFields );
     312             :         }
     313           0 :     }
     314             : 
     315             : 
     316           0 :     void FormLinkDialog::initializeLinks()
     317             :     {
     318             :         try
     319             :         {
     320           0 :             Sequence< OUString > aDetailFields;
     321           0 :             Sequence< OUString > aMasterFields;
     322             : 
     323           0 :             Reference< XPropertySet > xDetailFormProps( m_xDetailForm, UNO_QUERY );
     324           0 :             if ( xDetailFormProps.is() )
     325             :             {
     326           0 :                 xDetailFormProps->getPropertyValue( PROPERTY_DETAILFIELDS ) >>= aDetailFields;
     327           0 :                 xDetailFormProps->getPropertyValue( PROPERTY_MASTERFIELDS ) >>= aMasterFields;
     328             :             }
     329             : 
     330           0 :             initializeFieldRowsFrom( aDetailFields, aMasterFields );
     331             :         }
     332           0 :         catch( const Exception& )
     333             :         {
     334             :             OSL_FAIL( "FormLinkDialog::initializeLinks: caught an exception!" );
     335             :         }
     336           0 :     }
     337             : 
     338             : 
     339           0 :     void FormLinkDialog::updateOkButton()
     340             :     {
     341             :         // in all rows, there must be either two valid selections, or none at all
     342             :         // If there is at least one row with exactly one valid selection, then the
     343             :         // OKButton needs to be disabled
     344           0 :         sal_Bool bEnable = sal_True;
     345             : 
     346             :         const FieldLinkRow* aRows[] = {
     347           0 :             m_aRow1.get(), m_aRow2.get(), m_aRow3.get(), m_aRow4.get()
     348           0 :         };
     349             : 
     350           0 :         for ( sal_Int32 i = 0; ( i < 4 ) && bEnable; ++i )
     351             :         {
     352           0 :             OUString sNotInterestedInRightNow;
     353           0 :             if  (  aRows[ i ]->GetFieldName( FieldLinkRow::eDetailField, sNotInterestedInRightNow )
     354           0 :                 != aRows[ i ]->GetFieldName( FieldLinkRow::eMasterField, sNotInterestedInRightNow )
     355             :                 )
     356           0 :                 bEnable = sal_False;
     357           0 :         }
     358             : 
     359           0 :         m_aOK.Enable( bEnable );
     360           0 :     }
     361             : 
     362             : 
     363           0 :     OUString FormLinkDialog::getFormDataSourceType( const Reference< XPropertySet >& _rxForm ) const SAL_THROW(())
     364             :     {
     365           0 :         OUString sReturn;
     366           0 :         if ( !_rxForm.is() )
     367           0 :             return sReturn;
     368             : 
     369             :         try
     370             :         {
     371           0 :             sal_Int32       nCommandType = CommandType::COMMAND;
     372           0 :             OUString sCommand;
     373             : 
     374           0 :             _rxForm->getPropertyValue( PROPERTY_COMMANDTYPE ) >>= nCommandType;
     375           0 :             _rxForm->getPropertyValue( PROPERTY_COMMAND     ) >>= sCommand;
     376             : 
     377           0 :             if  (  ( nCommandType == CommandType::TABLE )
     378           0 :                 || ( nCommandType == CommandType::QUERY )
     379             :                 )
     380           0 :                 sReturn = sCommand;
     381             :         }
     382           0 :         catch( const Exception& )
     383             :         {
     384             :             OSL_FAIL( "FormLinkDialog::getFormDataSourceType: caught an exception!" );
     385             :         }
     386           0 :         return sReturn;
     387             :     }
     388             : 
     389             : 
     390           0 :     void FormLinkDialog::getFormFields( const Reference< XPropertySet >& _rxForm, Sequence< OUString >& /* [out] */ _rNames ) const SAL_THROW(( ))
     391             :     {
     392           0 :         _rNames.realloc( 0 );
     393             : 
     394           0 :         ::dbtools::SQLExceptionInfo aErrorInfo;
     395           0 :         OUString sCommand;
     396             :         try
     397             :         {
     398           0 :             WaitObject aWaitCursor( const_cast< FormLinkDialog* >( this ) );
     399             : 
     400             :             OSL_ENSURE( _rxForm.is(), "FormLinkDialog::getFormFields: invalid form!" );
     401             : 
     402           0 :             sal_Int32       nCommandType = CommandType::COMMAND;
     403             : 
     404           0 :             _rxForm->getPropertyValue( PROPERTY_COMMANDTYPE ) >>= nCommandType;
     405           0 :             _rxForm->getPropertyValue( PROPERTY_COMMAND     ) >>= sCommand;
     406             : 
     407           0 :             Reference< XConnection > xConnection;
     408           0 :             ensureFormConnection( _rxForm, xConnection );
     409             : 
     410           0 :             _rNames = ::dbtools::getFieldNamesByCommandDescriptor(
     411             :                 xConnection,
     412             :                 nCommandType,
     413             :                 sCommand,
     414             :                 &aErrorInfo
     415           0 :             );
     416             :         }
     417           0 :         catch (const SQLContext& e)    { aErrorInfo = e; }
     418           0 :         catch (const SQLWarning& e)    { aErrorInfo = e; }
     419           0 :         catch (const SQLException& e ) { aErrorInfo = e; }
     420           0 :         catch( const Exception& )
     421             :         {
     422             :             OSL_FAIL( "FormLinkDialog::getFormFields: caught a non-SQL exception!" );
     423             :         }
     424             : 
     425           0 :         if ( aErrorInfo.isValid() )
     426             :         {
     427           0 :             OUString sErrorMessage;
     428             :             {
     429           0 :                 ::svt::OLocalResourceAccess aStringAccess( PcrRes( RID_DLG_FORMLINKS ), RSC_MODALDIALOG );
     430           0 :                 sErrorMessage = PcrRes(STR_ERROR_RETRIEVING_COLUMNS).toString();
     431           0 :                 sErrorMessage = sErrorMessage.replaceFirst("#", sCommand);
     432             :             }
     433             : 
     434           0 :             SQLContext aContext;
     435           0 :             aContext.Message = sErrorMessage;
     436           0 :             aContext.NextException = aErrorInfo.get();
     437           0 :             ::dbtools::showError( aContext, VCLUnoHelper::GetInterface( const_cast< FormLinkDialog* >( this ) ), m_xContext );
     438           0 :         }
     439           0 :     }
     440             : 
     441             : 
     442           0 :     void FormLinkDialog::ensureFormConnection( const Reference< XPropertySet >& _rxFormProps, Reference< XConnection >& /* [out] */ _rxConnection ) const SAL_THROW(( Exception ))
     443             :     {
     444             :         OSL_PRECOND( _rxFormProps.is(), "FormLinkDialog::ensureFormConnection: invalid form!" );
     445           0 :         if ( !_rxFormProps.is() )
     446           0 :             return;
     447           0 :         if ( _rxFormProps->getPropertySetInfo()->hasPropertyByName(PROPERTY_ACTIVE_CONNECTION) )
     448           0 :             _rxConnection.set(_rxFormProps->getPropertyValue(PROPERTY_ACTIVE_CONNECTION),UNO_QUERY);
     449             : 
     450           0 :         if ( !_rxConnection.is() )
     451           0 :             _rxConnection = ::dbtools::connectRowset( Reference< XRowSet >( _rxFormProps, UNO_QUERY ), m_xContext, true );
     452             :     }
     453             : 
     454             : 
     455           0 :     void FormLinkDialog::getConnectionMetaData( const Reference< XPropertySet >& _rxFormProps, Reference< XDatabaseMetaData >& /* [out] */ _rxMeta ) const SAL_THROW(( Exception ))
     456             :     {
     457           0 :         if ( _rxFormProps.is() )
     458             :         {
     459           0 :             Reference< XConnection > xConnection;
     460           0 :             if ( !::dbtools::isEmbeddedInDatabase( _rxFormProps, xConnection ) )
     461           0 :                 _rxFormProps->getPropertyValue( PROPERTY_ACTIVE_CONNECTION ) >>= xConnection;
     462           0 :             if ( xConnection.is() )
     463           0 :                 _rxMeta = xConnection->getMetaData();
     464             :         }
     465           0 :     }
     466             : 
     467             : 
     468           0 :     Reference< XPropertySet > FormLinkDialog::getCanonicUnderlyingTable( const Reference< XPropertySet >& _rxFormProps ) const
     469             :     {
     470           0 :         Reference< XPropertySet > xTable;
     471             :         try
     472             :         {
     473           0 :             Reference< XTablesSupplier > xTablesInForm( ::dbtools::getCurrentSettingsComposer( _rxFormProps, m_xContext ), UNO_QUERY );
     474           0 :             Reference< XNameAccess > xTables;
     475           0 :             if ( xTablesInForm.is() )
     476           0 :                 xTables = xTablesInForm->getTables();
     477           0 :             Sequence< OUString > aTableNames;
     478           0 :             if ( xTables.is() )
     479           0 :                 aTableNames = xTables->getElementNames();
     480             : 
     481           0 :             if ( aTableNames.getLength() == 1 )
     482             :             {
     483           0 :                 xTables->getByName( aTableNames[ 0 ] ) >>= xTable;
     484             :                 OSL_ENSURE( xTable.is(), "FormLinkDialog::getCanonicUnderlyingTable: invalid table!" );
     485           0 :             }
     486             :         }
     487           0 :         catch( const Exception& )
     488             :         {
     489             :             OSL_FAIL( "FormLinkDialog::getCanonicUnderlyingTable: caught an exception!" );
     490             :         }
     491           0 :         return xTable;
     492             :     }
     493             : 
     494             : 
     495           0 :     sal_Bool FormLinkDialog::getExistingRelation( const Reference< XPropertySet >& _rxLHS, const Reference< XPropertySet >& /*_rxRHS*/,
     496             :             // TODO: fix the usage of _rxRHS. This is issue #i81956#.
     497             :         Sequence< OUString >& _rLeftFields, Sequence< OUString >& _rRightFields ) const
     498             :     {
     499             :         try
     500             :         {
     501           0 :             Reference< XKeysSupplier > xSuppKeys( _rxLHS, UNO_QUERY );
     502           0 :             Reference< XIndexAccess >  xKeys;
     503           0 :             if ( xSuppKeys.is() )
     504           0 :                 xKeys = xSuppKeys->getKeys();
     505             : 
     506           0 :             if ( xKeys.is() )
     507             :             {
     508           0 :                 Reference< XPropertySet >     xKey;
     509           0 :                 Reference< XColumnsSupplier > xKeyColSupp( xKey, UNO_QUERY );
     510           0 :                 Reference< XIndexAccess >     xKeyColumns;
     511           0 :                 Reference< XPropertySet >     xKeyColumn;
     512           0 :                 OUString sColumnName, sRelatedColumnName;
     513             : 
     514           0 :                 const sal_Int32 keyCount = xKeys->getCount();
     515           0 :                 for ( sal_Int32 key = 0; key < keyCount; ++key )
     516             :                 {
     517           0 :                     xKeys->getByIndex( key ) >>= xKey;
     518           0 :                     sal_Int32 nKeyType = 0;
     519           0 :                     xKey->getPropertyValue("Type") >>= nKeyType;
     520           0 :                     if ( nKeyType != KeyType::FOREIGN )
     521           0 :                         continue;
     522             : 
     523           0 :                     xKeyColumns.clear();
     524           0 :                     xKeyColSupp = xKeyColSupp.query( xKey );
     525           0 :                     if ( xKeyColSupp.is() )
     526           0 :                         xKeyColumns = xKeyColumns.query( xKeyColSupp->getColumns() );
     527             :                     OSL_ENSURE( xKeyColumns.is(), "FormLinkDialog::getExistingRelation: could not obtain the columns for the key!" );
     528             : 
     529           0 :                     if ( !xKeyColumns.is() )
     530           0 :                         continue;
     531             : 
     532           0 :                     const sal_Int32 columnCount = xKeyColumns->getCount();
     533           0 :                     _rLeftFields.realloc( columnCount );
     534           0 :                     _rRightFields.realloc( columnCount );
     535           0 :                     for ( sal_Int32 column = 0; column < columnCount; ++column )
     536             :                     {
     537           0 :                         xKeyColumn.clear();
     538           0 :                         xKeyColumns->getByIndex( column ) >>= xKeyColumn;
     539             :                         OSL_ENSURE( xKeyColumn.is(), "FormLinkDialog::getExistingRelation: invalid key column!" );
     540           0 :                         if ( xKeyColumn.is() )
     541             :                         {
     542           0 :                             xKeyColumn->getPropertyValue( PROPERTY_NAME ) >>= sColumnName;
     543           0 :                             xKeyColumn->getPropertyValue("RelatedColumn") >>= sRelatedColumnName;
     544             : 
     545           0 :                             _rLeftFields[ column ]  = sColumnName;
     546           0 :                             _rRightFields[ column ] = sRelatedColumnName;
     547             :                         }
     548             :                     }
     549           0 :                 }
     550           0 :             }
     551             :         }
     552           0 :         catch( const Exception& )
     553             :         {
     554             :             OSL_FAIL( "FormLinkDialog::getExistingRelation: caught an exception!" );
     555             :         }
     556             : 
     557           0 :         return ( _rLeftFields.getLength() > 0 ) && ( !_rLeftFields[ 0 ].isEmpty() );
     558             :     }
     559             : 
     560             : 
     561           0 :     void FormLinkDialog::initializeSuggest()
     562             :     {
     563           0 :         Reference< XPropertySet > xDetailFormProps( m_xDetailForm, UNO_QUERY );
     564           0 :         Reference< XPropertySet > xMasterFormProps( m_xMasterForm, UNO_QUERY );
     565           0 :         if ( !xDetailFormProps.is() || !xMasterFormProps.is() )
     566           0 :             return;
     567             : 
     568             :         try
     569             :         {
     570           0 :             sal_Bool bEnable = sal_True;
     571             : 
     572             :             // only show the button when both forms are based on the same data source
     573           0 :             if ( bEnable )
     574             :             {
     575           0 :                 OUString sMasterDS, sDetailDS;
     576           0 :                 xMasterFormProps->getPropertyValue( PROPERTY_DATASOURCE ) >>= sMasterDS;
     577           0 :                 xDetailFormProps->getPropertyValue( PROPERTY_DATASOURCE ) >>= sDetailDS;
     578           0 :                 bEnable = ( sMasterDS == sDetailDS );
     579             :             }
     580             : 
     581             :             // only show the button when the connection supports relations
     582           0 :             if ( bEnable )
     583             :             {
     584           0 :                 Reference< XDatabaseMetaData > xMeta;
     585           0 :                 getConnectionMetaData( xDetailFormProps, xMeta );
     586             :                 OSL_ENSURE( xMeta.is(), "FormLinkDialog::initializeSuggest: unable to retrieve the meta data for the connection!" );
     587             :                 try
     588             :                 {
     589           0 :                     bEnable = xMeta.is() && xMeta->supportsIntegrityEnhancementFacility();
     590             :                 }
     591           0 :                 catch(const Exception&)
     592             :                 {
     593           0 :                     bEnable = sal_False;
     594           0 :                 }
     595             :             }
     596             : 
     597             :             // only enable the button if there is a "canonic" table underlying both forms
     598           0 :             Reference< XPropertySet > xDetailTable, xMasterTable;
     599           0 :             if ( bEnable )
     600             :             {
     601           0 :                 xDetailTable = getCanonicUnderlyingTable( xDetailFormProps );
     602           0 :                 xMasterTable = getCanonicUnderlyingTable( xMasterFormProps );
     603           0 :                 bEnable = xDetailTable.is() && xMasterTable.is();
     604             :             }
     605             : 
     606             :             // only enable the button if there is a relation between both tables
     607           0 :             m_aRelationDetailColumns.realloc( 0 );
     608           0 :             m_aRelationMasterColumns.realloc( 0 );
     609           0 :             if ( bEnable )
     610             :             {
     611           0 :                 bEnable = getExistingRelation( xDetailTable, xMasterTable, m_aRelationDetailColumns, m_aRelationMasterColumns );
     612             :                 SAL_WARN_IF( m_aRelationMasterColumns.getLength() != m_aRelationDetailColumns.getLength(),
     613             :                     "extensions.propctrlr",
     614             :                     "FormLinkDialog::initializeSuggest: nonsense!" );
     615           0 :                 if ( m_aRelationMasterColumns.getLength() == 0 )
     616             :                 {   // okay, there is no relation "pointing" (via a foreign key) from the detail table to the master table
     617             :                     // but perhaps the other way round (would make less sense, but who knows ...)
     618           0 :                     bEnable = getExistingRelation( xMasterTable, xDetailTable, m_aRelationMasterColumns, m_aRelationDetailColumns );
     619             :                 }
     620             :             }
     621             : 
     622             :             // only enable the button if the relation contains at most 4 field pairs
     623           0 :             if ( bEnable )
     624             :             {
     625           0 :                 bEnable = ( m_aRelationMasterColumns.getLength() <= 4 );
     626             :             }
     627             : 
     628           0 :             m_aSuggest.Enable( bEnable );
     629             :         }
     630           0 :         catch( const Exception& )
     631             :         {
     632             :             OSL_FAIL( "FormLinkDialog::initializeSuggest: caught an exception!" );
     633           0 :         }
     634             :     }
     635             : 
     636             : 
     637           0 :     IMPL_LINK( FormLinkDialog, OnSuggest, void*, /*_pNotInterestedIn*/ )
     638             :     {
     639           0 :         initializeFieldRowsFrom( m_aRelationDetailColumns, m_aRelationMasterColumns );
     640           0 :         return 0L;
     641             :     }
     642             : 
     643             : 
     644           0 :     IMPL_LINK( FormLinkDialog, OnFieldChanged, FieldLinkRow*, /*_pRow*/ )
     645             :     {
     646           0 :         updateOkButton();
     647           0 :         return 0L;
     648             :     }
     649             : 
     650             : 
     651           0 :     IMPL_LINK( FormLinkDialog, OnInitialize, void*, /*_pNotInterestedIn*/ )
     652             :     {
     653           0 :         initializeColumnLabels();
     654           0 :         initializeFieldLists();
     655           0 :         initializeLinks();
     656           0 :         initializeSuggest();
     657           0 :         return 0L;
     658             :     }
     659             : 
     660             : }   // namespace pcr
     661             : 
     662             : 
     663             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10