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

Generated by: LCOV version 1.11