LCOV - code coverage report
Current view: top level - libreoffice/extensions/source/propctrlr - formlinkdialog.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 293 0.0 %
Date: 2012-12-27 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, String& /* [out] */ _rName ) const;
      87             :         void    SetFieldName( LinkParticipant _eWhich, const String& _rName );
      88             : 
      89             :         void    fillList( LinkParticipant _eWhich, const Sequence< ::rtl::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< ::rtl::OUString >& _rFieldNames )
     113             :     {
     114           0 :         ComboBox* pBox = ( _eWhich == eDetailField ) ? &m_aDetailColumn : &m_aMasterColumn;
     115             : 
     116           0 :         const ::rtl::OUString* pFieldName    = _rFieldNames.getConstArray();
     117           0 :         const ::rtl::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, String& /* [out] */ _rName ) const
     124             :     {
     125           0 :         const ComboBox* pBox = ( _eWhich == eDetailField ) ? &m_aDetailColumn : &m_aMasterColumn;
     126           0 :         _rName = pBox->GetText();
     127           0 :         return _rName.Len() != 0;
     128             :     }
     129             : 
     130             :     //------------------------------------------------------------------------
     131           0 :     void FieldLinkRow::SetFieldName( LinkParticipant _eWhich, const String& _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 ::rtl::OUString& _sExplanation,
     153             :             const ::rtl::OUString& _sDetailLabel,
     154             :             const ::rtl::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< ::rtl::OUString > aDetailFields; aDetailFields.reserve( 4 );
     198           0 :         ::std::vector< ::rtl::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 :             String sDetailField, sMasterField;
     207           0 :             aRows[ i ]->GetFieldName( FieldLinkRow::eDetailField, sDetailField );
     208           0 :             aRows[ i ]->GetFieldName( FieldLinkRow::eMasterField, sMasterField );
     209           0 :             if ( !sDetailField.Len() && !sMasterField.Len() )
     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 :                 ::rtl::OUString *pFields = aDetailFields.empty() ? 0 : &aDetailFields[0];
     223           0 :                 xDetailFormProps->setPropertyValue( PROPERTY_DETAILFIELDS, makeAny( Sequence< ::rtl::OUString >( pFields, aDetailFields.size() ) ) );
     224           0 :                 pFields = aMasterFields.empty() ? 0 : &aMasterFields[0];
     225           0 :                 xDetailFormProps->setPropertyValue( PROPERTY_MASTERFIELDS, makeAny( Sequence< ::rtl::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< ::rtl::OUString > sDetailFields;
     249           0 :         getFormFields( m_xDetailForm, sDetailFields );
     250             : 
     251           0 :         Sequence< ::rtl::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 :         String sDetailType = getFormDataSourceType( m_xDetailForm );
     270           0 :         if ( !sDetailType.Len() )
     271             :         {
     272           0 :             if ( m_sDetailLabel.isEmpty() )
     273             :             {
     274           0 :                 ::svt::OLocalResourceAccess aStringAccess( PcrRes( RID_DLG_FORMLINKS ), RSC_MODALDIALOG );
     275           0 :                 m_sDetailLabel = String( PcrRes( STR_DETAIL_FORM ) );
     276             :             }
     277           0 :             sDetailType = m_sDetailLabel;
     278             :         }
     279           0 :         m_aDetailLabel.SetText( sDetailType );
     280             : 
     281             :         // label for the master form
     282           0 :         String sMasterType = getFormDataSourceType( m_xMasterForm );
     283           0 :         if ( !sMasterType.Len() )
     284             :         {
     285           0 :             if ( m_sMasterLabel.isEmpty() )
     286             :             {
     287           0 :                 ::svt::OLocalResourceAccess aStringAccess( PcrRes( RID_DLG_FORMLINKS ), RSC_MODALDIALOG );
     288           0 :                 m_sMasterLabel = String( PcrRes( STR_MASTER_FORM ) );
     289             :             }
     290           0 :             sMasterType = m_sMasterLabel;
     291             :         }
     292           0 :         m_aMasterLabel.SetText( sMasterType );
     293           0 :     }
     294             : 
     295             :     //------------------------------------------------------------------------
     296           0 :     void FormLinkDialog::initializeFieldRowsFrom( Sequence< ::rtl::OUString >& _rDetailFields, Sequence< ::rtl::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 ::rtl::OUString* pDetailFields = _rDetailFields.getConstArray();
     303           0 :         const ::rtl::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< ::rtl::OUString > aDetailFields;
     321           0 :             Sequence< ::rtl::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 :             String 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 :     String FormLinkDialog::getFormDataSourceType( const Reference< XPropertySet >& _rxForm ) const SAL_THROW(())
     364             :     {
     365           0 :         String sReturn;
     366           0 :         Reference< XPropertySet > xFormProps( _rxForm, UNO_QUERY );
     367           0 :         if ( !xFormProps.is() )
     368             :             return sReturn;
     369             : 
     370             :         try
     371             :         {
     372           0 :             sal_Int32       nCommandType = CommandType::COMMAND;
     373           0 :             ::rtl::OUString sCommand;
     374             : 
     375           0 :             xFormProps->getPropertyValue( PROPERTY_COMMANDTYPE ) >>= nCommandType;
     376           0 :             xFormProps->getPropertyValue( PROPERTY_COMMAND     ) >>= sCommand;
     377             : 
     378           0 :             if  (  ( nCommandType == CommandType::TABLE )
     379             :                 || ( nCommandType == CommandType::QUERY )
     380             :                 )
     381           0 :                 sReturn = sCommand;
     382             :         }
     383           0 :         catch( const Exception& )
     384             :         {
     385             :             OSL_FAIL( "FormLinkDialog::getFormDataSourceType: caught an exception!" );
     386             :         }
     387           0 :         return sReturn;
     388             :     }
     389             : 
     390             :     //------------------------------------------------------------------------
     391           0 :     void FormLinkDialog::getFormFields( const Reference< XPropertySet >& _rxForm, Sequence< ::rtl::OUString >& /* [out] */ _rNames ) const SAL_THROW(( ))
     392             :     {
     393           0 :         _rNames.realloc( 0 );
     394             : 
     395           0 :         ::dbtools::SQLExceptionInfo aErrorInfo;
     396           0 :         ::rtl::OUString sCommand;
     397             :         try
     398             :         {
     399           0 :             WaitObject aWaitCursor( const_cast< FormLinkDialog* >( this ) );
     400             : 
     401           0 :             Reference< XPropertySet > xFormProps( _rxForm, UNO_QUERY );
     402             :             OSL_ENSURE( xFormProps.is(), "FormLinkDialog::getFormFields: invalid form!" );
     403             : 
     404           0 :             sal_Int32       nCommandType = CommandType::COMMAND;
     405             : 
     406           0 :             xFormProps->getPropertyValue( PROPERTY_COMMANDTYPE ) >>= nCommandType;
     407           0 :             xFormProps->getPropertyValue( PROPERTY_COMMAND     ) >>= sCommand;
     408             : 
     409           0 :             Reference< XConnection > xConnection;
     410           0 :             ensureFormConnection( xFormProps, xConnection );
     411             : 
     412             :             _rNames = ::dbtools::getFieldNamesByCommandDescriptor(
     413             :                 xConnection,
     414             :                 nCommandType,
     415             :                 sCommand,
     416             :                 &aErrorInfo
     417           0 :             );
     418             :         }
     419           0 :         catch (const SQLContext& e)    { aErrorInfo = e; }
     420           0 :         catch (const SQLWarning& e)    { aErrorInfo = e; }
     421           0 :         catch (const SQLException& e ) { aErrorInfo = e; }
     422           0 :         catch( const Exception& )
     423             :         {
     424             :             OSL_FAIL( "FormLinkDialog::getFormFields: caught a non-SQL exception!" );
     425             :         }
     426             : 
     427           0 :         if ( aErrorInfo.isValid() )
     428             :         {
     429           0 :             String sErrorMessage;
     430             :             {
     431           0 :                 ::svt::OLocalResourceAccess aStringAccess( PcrRes( RID_DLG_FORMLINKS ), RSC_MODALDIALOG );
     432           0 :                 sErrorMessage = PcrRes(STR_ERROR_RETRIEVING_COLUMNS).toString();
     433           0 :                 sErrorMessage.SearchAndReplace(rtl::OUString('#'), sCommand);
     434             :             }
     435             : 
     436           0 :             SQLContext aContext;
     437           0 :             aContext.Message = sErrorMessage;
     438           0 :             aContext.NextException = aErrorInfo.get();
     439           0 :             ::dbtools::showError( aContext, VCLUnoHelper::GetInterface( const_cast< FormLinkDialog* >( this ) ), m_xContext );
     440           0 :         }
     441           0 :     }
     442             : 
     443             :     //------------------------------------------------------------------------
     444           0 :     void FormLinkDialog::ensureFormConnection( const Reference< XPropertySet >& _rxFormProps, Reference< XConnection >& /* [out] */ _rxConnection ) const SAL_THROW(( Exception ))
     445             :     {
     446             :         OSL_PRECOND( _rxFormProps.is(), "FormLinkDialog::ensureFormConnection: invalid form!" );
     447           0 :         if ( !_rxFormProps.is() )
     448           0 :             return;
     449           0 :         if ( _rxFormProps->getPropertySetInfo()->hasPropertyByName(PROPERTY_ACTIVE_CONNECTION) )
     450           0 :             _rxConnection.set(_rxFormProps->getPropertyValue(PROPERTY_ACTIVE_CONNECTION),UNO_QUERY);
     451             : 
     452           0 :         if ( !_rxConnection.is() )
     453           0 :             _rxConnection = ::dbtools::connectRowset( Reference< XRowSet >( _rxFormProps, UNO_QUERY ), m_xContext, sal_True );
     454             :     }
     455             : 
     456             :     //------------------------------------------------------------------------
     457           0 :     void FormLinkDialog::getConnectionMetaData( const Reference< XPropertySet >& _rxFormProps, Reference< XDatabaseMetaData >& /* [out] */ _rxMeta ) const SAL_THROW(( Exception ))
     458             :     {
     459           0 :         if ( _rxFormProps.is() )
     460             :         {
     461           0 :             Reference< XConnection > xConnection;
     462           0 :             if ( !::dbtools::isEmbeddedInDatabase( _rxFormProps, xConnection ) )
     463           0 :                 _rxFormProps->getPropertyValue( PROPERTY_ACTIVE_CONNECTION ) >>= xConnection;
     464           0 :             if ( xConnection.is() )
     465           0 :                 _rxMeta = xConnection->getMetaData();
     466             :         }
     467           0 :     }
     468             : 
     469             :     //------------------------------------------------------------------------
     470           0 :     Reference< XPropertySet > FormLinkDialog::getCanonicUnderlyingTable( const Reference< XPropertySet >& _rxFormProps ) const
     471             :     {
     472           0 :         Reference< XPropertySet > xTable;
     473             :         try
     474             :         {
     475           0 :             Reference< XTablesSupplier > xTablesInForm( ::dbtools::getCurrentSettingsComposer( _rxFormProps, m_xContext ), UNO_QUERY );
     476           0 :             Reference< XNameAccess > xTables;
     477           0 :             if ( xTablesInForm.is() )
     478           0 :                 xTables = xTablesInForm->getTables();
     479           0 :             Sequence< ::rtl::OUString > aTableNames;
     480           0 :             if ( xTables.is() )
     481           0 :                 aTableNames = xTables->getElementNames();
     482             : 
     483           0 :             if ( aTableNames.getLength() == 1 )
     484             :             {
     485           0 :                 xTables->getByName( aTableNames[ 0 ] ) >>= xTable;
     486             :                 OSL_ENSURE( xTable.is(), "FormLinkDialog::getCanonicUnderlyingTable: invalid table!" );
     487           0 :             }
     488             :         }
     489           0 :         catch( const Exception& )
     490             :         {
     491             :             OSL_FAIL( "FormLinkDialog::getCanonicUnderlyingTable: caught an exception!" );
     492             :         }
     493           0 :         return xTable;
     494             :     }
     495             : 
     496             :     //------------------------------------------------------------------------
     497           0 :     sal_Bool FormLinkDialog::getExistingRelation( const Reference< XPropertySet >& _rxLHS, const Reference< XPropertySet >& /*_rxRHS*/,
     498             :             // TODO: fix the usage of _rxRHS. This is issue #i81956#.
     499             :         Sequence< ::rtl::OUString >& _rLeftFields, Sequence< ::rtl::OUString >& _rRightFields ) const
     500             :     {
     501             :         try
     502             :         {
     503           0 :             Reference< XKeysSupplier > xSuppKeys( _rxLHS, UNO_QUERY );
     504           0 :             Reference< XIndexAccess >  xKeys;
     505           0 :             if ( xSuppKeys.is() )
     506           0 :                 xKeys = xSuppKeys->getKeys();
     507             : 
     508           0 :             if ( xKeys.is() )
     509             :             {
     510           0 :                 Reference< XPropertySet >     xKey;
     511           0 :                 Reference< XColumnsSupplier > xKeyColSupp( xKey, UNO_QUERY );
     512           0 :                 Reference< XIndexAccess >     xKeyColumns;
     513           0 :                 Reference< XPropertySet >     xKeyColumn;
     514           0 :                 ::rtl::OUString sColumnName, sRelatedColumnName;
     515             : 
     516           0 :                 const sal_Int32 keyCount = xKeys->getCount();
     517           0 :                 for ( sal_Int32 key = 0; key < keyCount; ++key )
     518             :                 {
     519           0 :                     xKeys->getByIndex( key ) >>= xKey;
     520           0 :                     sal_Int32 nKeyType = 0;
     521           0 :                     xKey->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Type" ) ) ) >>= nKeyType;
     522           0 :                     if ( nKeyType != KeyType::FOREIGN )
     523           0 :                         continue;
     524             : 
     525           0 :                     xKeyColumns.clear();
     526           0 :                     xKeyColSupp = xKeyColSupp.query( xKey );
     527           0 :                     if ( xKeyColSupp.is() )
     528           0 :                         xKeyColumns = xKeyColumns.query( xKeyColSupp->getColumns() );
     529             :                     OSL_ENSURE( xKeyColumns.is(), "FormLinkDialog::getExistingRelation: could not obtain the columns for the key!" );
     530             : 
     531           0 :                     if ( !xKeyColumns.is() )
     532           0 :                         continue;
     533             : 
     534           0 :                     const sal_Int32 columnCount = xKeyColumns->getCount();
     535           0 :                     _rLeftFields.realloc( columnCount );
     536           0 :                     _rRightFields.realloc( columnCount );
     537           0 :                     for ( sal_Int32 column = 0; column < columnCount; ++column )
     538             :                     {
     539           0 :                         xKeyColumn.clear();
     540           0 :                         xKeyColumns->getByIndex( column ) >>= xKeyColumn;
     541             :                         OSL_ENSURE( xKeyColumn.is(), "FormLinkDialog::getExistingRelation: invalid key column!" );
     542           0 :                         if ( xKeyColumn.is() )
     543             :                         {
     544           0 :                             xKeyColumn->getPropertyValue( PROPERTY_NAME ) >>= sColumnName;
     545           0 :                             xKeyColumn->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "RelatedColumn" ) ) ) >>= sRelatedColumnName;
     546             : 
     547           0 :                             _rLeftFields[ column ]  = sColumnName;
     548           0 :                             _rRightFields[ column ] = sRelatedColumnName;
     549             :                         }
     550             :                     }
     551           0 :                 }
     552           0 :             }
     553             :         }
     554           0 :         catch( const Exception& )
     555             :         {
     556             :             OSL_FAIL( "FormLinkDialog::getExistingRelation: caught an exception!" );
     557             :         }
     558             : 
     559           0 :         return ( _rLeftFields.getLength() > 0 ) && ( !_rLeftFields[ 0 ].isEmpty() );
     560             :     }
     561             : 
     562             :     //------------------------------------------------------------------------
     563           0 :     void FormLinkDialog::initializeSuggest()
     564             :     {
     565           0 :         Reference< XPropertySet > xDetailFormProps( m_xDetailForm, UNO_QUERY );
     566           0 :         Reference< XPropertySet > xMasterFormProps( m_xMasterForm, UNO_QUERY );
     567           0 :         if ( !xDetailFormProps.is() || !xMasterFormProps.is() )
     568           0 :             return;
     569             : 
     570             :         try
     571             :         {
     572           0 :             sal_Bool bEnable = sal_True;
     573             : 
     574             :             // only show the button when both forms are based on the same data source
     575           0 :             if ( bEnable )
     576             :             {
     577           0 :                 ::rtl::OUString sMasterDS, sDetailDS;
     578           0 :                 xMasterFormProps->getPropertyValue( PROPERTY_DATASOURCE ) >>= sMasterDS;
     579           0 :                 xDetailFormProps->getPropertyValue( PROPERTY_DATASOURCE ) >>= sDetailDS;
     580           0 :                 bEnable = ( sMasterDS == sDetailDS );
     581             :             }
     582             : 
     583             :             // only show the button when the connection supports relations
     584           0 :             if ( bEnable )
     585             :             {
     586           0 :                 Reference< XDatabaseMetaData > xMeta;
     587           0 :                 getConnectionMetaData( xDetailFormProps, xMeta );
     588             :                 OSL_ENSURE( xMeta.is(), "FormLinkDialog::initializeSuggest: unable to retrieve the meta data for the connection!" );
     589             :                 try
     590             :                 {
     591           0 :                     bEnable = xMeta.is() && xMeta->supportsIntegrityEnhancementFacility();
     592             :                 }
     593           0 :                 catch(const Exception&)
     594             :                 {
     595           0 :                     bEnable = sal_False;
     596           0 :                 }
     597             :             }
     598             : 
     599             :             // only enable the button if there is a "canonic" table underlying both forms
     600           0 :             Reference< XPropertySet > xDetailTable, xMasterTable;
     601           0 :             if ( bEnable )
     602             :             {
     603           0 :                 xDetailTable = getCanonicUnderlyingTable( xDetailFormProps );
     604           0 :                 xMasterTable = getCanonicUnderlyingTable( xMasterFormProps );
     605           0 :                 bEnable = xDetailTable.is() && xMasterTable.is();
     606             :             }
     607             : 
     608             :             // only enable the button if there is a relation between both tables
     609           0 :             m_aRelationDetailColumns.realloc( 0 );
     610           0 :             m_aRelationMasterColumns.realloc( 0 );
     611           0 :             if ( bEnable )
     612             :             {
     613           0 :                 bEnable = getExistingRelation( xDetailTable, xMasterTable, m_aRelationDetailColumns, m_aRelationMasterColumns );
     614             :                 OSL_POSTCOND( m_aRelationMasterColumns.getLength() == m_aRelationDetailColumns.getLength(), "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