LCOV - code coverage report
Current view: top level - extensions/source/propctrlr - formlinkdialog.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 1 302 0.3 %
Date: 2014-11-03 Functions: 2 35 5.7 %
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 <vcl/combobox.hxx>
      27             : #include <vcl/msgbox.hxx>
      28             : #include <vcl/waitobj.hxx>
      29             : #include <vcl/tabpage.hxx>
      30             : #include <vcl/layout.hxx>
      31             : #include <svtools/localresaccess.hxx>
      32             : #include <connectivity/dbtools.hxx>
      33             : #include <connectivity/dbexception.hxx>
      34             : #include <toolkit/helper/vclunohelper.hxx>
      35             : #include <comphelper/processfactory.hxx>
      36             : 
      37             : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
      38             : #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
      39             : #include <com/sun/star/sdbcx/KeyType.hpp>
      40             : #include <com/sun/star/container/XNameAccess.hpp>
      41             : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
      42             : #include <com/sun/star/sdbc/XRowSet.hpp>
      43             : #include <com/sun/star/sdb/CommandType.hpp>
      44             : #include <com/sun/star/sdb/SQLContext.hpp>
      45             : 
      46             : 
      47             : 
      48             : namespace pcr
      49             : {
      50             : 
      51             : 
      52             :     using namespace ::com::sun::star::uno;
      53             :     using namespace ::com::sun::star::lang;
      54             :     using namespace ::com::sun::star::form;
      55             :     using namespace ::com::sun::star::sdb;
      56             :     using namespace ::com::sun::star::sdbc;
      57             :     using namespace ::com::sun::star::sdbcx;
      58             :     using namespace ::com::sun::star::beans;
      59             :     using namespace ::com::sun::star::container;
      60             : 
      61             : 
      62             :     //= FieldLinkRow
      63             : 
      64           0 :     class FieldLinkRow : public TabPage
      65             :     {
      66             :     private:
      67             :         ComboBox*   m_pDetailColumn;
      68             :         ComboBox*   m_pMasterColumn;
      69             : 
      70             :         Link        m_aLinkChangeHandler;
      71             : 
      72             :     public:
      73             :         FieldLinkRow( vcl::Window* _pParent );
      74             : 
      75           0 :         inline void         SetLinkChangeHandler( const Link& _rHdl ) { m_aLinkChangeHandler = _rHdl; }
      76             : 
      77             :         enum LinkParticipant
      78             :         {
      79             :             eDetailField,
      80             :             eMasterField
      81             :         };
      82             :         /** retrieves the selected field name for either the master or the detail field
      83             :             @return <TRUE/> if and only a valid field is selected
      84             :         */
      85             :         bool    GetFieldName( LinkParticipant _eWhich, OUString& /* [out] */ _rName ) const;
      86             :         void    SetFieldName( LinkParticipant _eWhich, const OUString& _rName );
      87             : 
      88             :         void    fillList( LinkParticipant _eWhich, const Sequence< OUString >& _rFieldNames );
      89             : 
      90             :     private:
      91             :         DECL_LINK( OnFieldNameChanged, ComboBox* );
      92             :     };
      93             : 
      94             : 
      95           0 :     FieldLinkRow::FieldLinkRow( vcl::Window* _pParent )
      96           0 :         :TabPage( _pParent, "FieldLinkRow", "modules/spropctrlr/ui/fieldlinkrow.ui" )
      97             :     {
      98           0 :         get(m_pDetailColumn, "detailCombobox");
      99           0 :         get(m_pMasterColumn, "masterCombobox");
     100             : 
     101           0 :         m_pDetailColumn->SetDropDownLineCount( 10 );
     102           0 :         m_pMasterColumn->SetDropDownLineCount( 10 );
     103             : 
     104           0 :         m_pDetailColumn->SetModifyHdl( LINK( this, FieldLinkRow, OnFieldNameChanged ) );
     105           0 :         m_pMasterColumn->SetModifyHdl( LINK( this, FieldLinkRow, OnFieldNameChanged ) );
     106           0 :     }
     107             : 
     108             : 
     109           0 :     void FieldLinkRow::fillList( LinkParticipant _eWhich, const Sequence< OUString >& _rFieldNames )
     110             :     {
     111           0 :         ComboBox* pBox = ( _eWhich == eDetailField ) ? m_pDetailColumn : m_pMasterColumn;
     112             : 
     113           0 :         const OUString* pFieldName    = _rFieldNames.getConstArray();
     114           0 :         const OUString* pFieldNameEnd = pFieldName + _rFieldNames.getLength();
     115           0 :         for ( ; pFieldName != pFieldNameEnd; ++pFieldName )
     116           0 :             pBox->InsertEntry( *pFieldName );
     117           0 :     }
     118             : 
     119             : 
     120           0 :     bool FieldLinkRow::GetFieldName( LinkParticipant _eWhich, OUString& /* [out] */ _rName ) const
     121             :     {
     122           0 :         const ComboBox* pBox = ( _eWhich == eDetailField ) ? m_pDetailColumn : m_pMasterColumn;
     123           0 :         _rName = pBox->GetText();
     124           0 :         return !_rName.isEmpty();
     125             :     }
     126             : 
     127             : 
     128           0 :     void FieldLinkRow::SetFieldName( LinkParticipant _eWhich, const OUString& _rName )
     129             :     {
     130           0 :         ComboBox* pBox = ( _eWhich == eDetailField ) ? m_pDetailColumn : m_pMasterColumn;
     131           0 :         pBox->SetText( _rName );
     132           0 :     }
     133             : 
     134             : 
     135           0 :     IMPL_LINK( FieldLinkRow, OnFieldNameChanged, ComboBox*, /*_pBox*/ )
     136             :     {
     137           0 :         if ( m_aLinkChangeHandler.IsSet() )
     138           0 :             return m_aLinkChangeHandler.Call( this );
     139             : 
     140           0 :         return 0L;
     141             :     }
     142             : 
     143           0 :     extern "C" SAL_DLLPUBLIC_EXPORT vcl::Window* SAL_CALL makeFieldLinkRow(vcl::Window *pParent, VclBuilder::stringmap &)
     144             :     {
     145           0 :         return new FieldLinkRow(pParent);
     146             :     }
     147             : 
     148             : 
     149             :     //= FormLinkDialog
     150             : 
     151             : 
     152           0 :     FormLinkDialog::FormLinkDialog( vcl::Window* _pParent, const Reference< XPropertySet >& _rxDetailForm,
     153             :             const Reference< XPropertySet >& _rxMasterForm, const Reference< XComponentContext >& _rxContext,
     154             :             const OUString& _sExplanation,
     155             :             const OUString& _sDetailLabel,
     156             :             const OUString& _sMasterLabel)
     157             :         :ModalDialog( _pParent, "FormLinks", "modules/spropctrlr/ui/formlinksdialog.ui" )
     158           0 :         ,m_aRow1       ( new FieldLinkRow( get<VclVBox>("box") ) )
     159           0 :         ,m_aRow2       ( new FieldLinkRow( get<VclVBox>("box") ) )
     160           0 :         ,m_aRow3       ( new FieldLinkRow( get<VclVBox>("box") ) )
     161           0 :         ,m_aRow4       ( new FieldLinkRow( get<VclVBox>("box") ) )
     162             :         ,m_xContext    ( _rxContext )
     163             :         ,m_xDetailForm( _rxDetailForm )
     164             :         ,m_xMasterForm( _rxMasterForm )
     165             :         ,m_sDetailLabel(_sDetailLabel)
     166           0 :         ,m_sMasterLabel(_sMasterLabel)
     167             :     {
     168           0 :         get(m_pExplanation, "explanationLabel");
     169           0 :         get(m_pDetailLabel, "detailLabel");
     170           0 :         get(m_pMasterLabel, "masterLabel");
     171           0 :         get(m_pOK, "ok");
     172           0 :         get(m_pSuggest, "suggestButton");
     173           0 :         m_aRow1->Show();
     174           0 :         m_aRow2->Show();
     175           0 :         m_aRow3->Show();
     176           0 :         m_aRow4->Show();
     177           0 :         set_width_request(600);
     178             : 
     179           0 :         if ( !_sExplanation.isEmpty() )
     180           0 :             m_pExplanation->SetText(_sExplanation);
     181             : 
     182           0 :         m_pSuggest->SetClickHdl       ( LINK( this, FormLinkDialog, OnSuggest      ) );
     183           0 :         m_aRow1->SetLinkChangeHandler( LINK( this, FormLinkDialog, OnFieldChanged ) );
     184           0 :         m_aRow2->SetLinkChangeHandler( LINK( this, FormLinkDialog, OnFieldChanged ) );
     185           0 :         m_aRow3->SetLinkChangeHandler( LINK( this, FormLinkDialog, OnFieldChanged ) );
     186           0 :         m_aRow4->SetLinkChangeHandler( LINK( this, FormLinkDialog, OnFieldChanged ) );
     187             : 
     188           0 :         PostUserEvent( LINK( this, FormLinkDialog, OnInitialize ) );
     189             : 
     190           0 :         updateOkButton();
     191           0 :     }
     192             : 
     193             : 
     194           0 :     FormLinkDialog::~FormLinkDialog( )
     195             :     {
     196           0 :     }
     197             : 
     198             : 
     199           0 :     void FormLinkDialog::commitLinkPairs()
     200             :     {
     201             :         // collect the field lists from the rows
     202           0 :         ::std::vector< OUString > aDetailFields; aDetailFields.reserve( 4 );
     203           0 :         ::std::vector< OUString > aMasterFields; aMasterFields.reserve( 4 );
     204             : 
     205             :         const FieldLinkRow* aRows[] = {
     206           0 :             m_aRow1.get(), m_aRow2.get(), m_aRow3.get(), m_aRow4.get()
     207           0 :         };
     208             : 
     209           0 :         for ( sal_Int32 i = 0; i < 4; ++i )
     210             :         {
     211           0 :             OUString sDetailField, sMasterField;
     212           0 :             aRows[ i ]->GetFieldName( FieldLinkRow::eDetailField, sDetailField );
     213           0 :             aRows[ i ]->GetFieldName( FieldLinkRow::eMasterField, sMasterField );
     214           0 :             if ( sDetailField.isEmpty() && sMasterField.isEmpty() )
     215           0 :                 continue;
     216             : 
     217           0 :             aDetailFields.push_back( sDetailField );
     218           0 :             aMasterFields.push_back( sMasterField );
     219           0 :         }
     220             : 
     221             :         // and set as property values
     222             :         try
     223             :         {
     224           0 :             Reference< XPropertySet > xDetailFormProps( m_xDetailForm, UNO_QUERY );
     225           0 :             if ( xDetailFormProps.is() )
     226             :             {
     227           0 :                 OUString *pFields = aDetailFields.empty() ? 0 : &aDetailFields[0];
     228           0 :                 xDetailFormProps->setPropertyValue( PROPERTY_DETAILFIELDS, makeAny( Sequence< OUString >( pFields, aDetailFields.size() ) ) );
     229           0 :                 pFields = aMasterFields.empty() ? 0 : &aMasterFields[0];
     230           0 :                 xDetailFormProps->setPropertyValue( PROPERTY_MASTERFIELDS, makeAny( Sequence< OUString >( pFields, aMasterFields.size() ) ) );
     231           0 :             }
     232             :         }
     233           0 :         catch( const Exception& )
     234             :         {
     235             :             OSL_FAIL( "FormLinkDialog::commitLinkPairs: caught an exception while setting the properties!" );
     236           0 :         }
     237           0 :     }
     238             : 
     239             : 
     240           0 :     short FormLinkDialog::Execute()
     241             :     {
     242           0 :         short nResult = ModalDialog::Execute();
     243             : 
     244           0 :         if ( RET_OK == nResult )
     245           0 :             commitLinkPairs();
     246             : 
     247           0 :         return nResult;
     248             :     }
     249             : 
     250             : 
     251           0 :     void FormLinkDialog::initializeFieldLists()
     252             :     {
     253           0 :         Sequence< OUString > sDetailFields;
     254           0 :         getFormFields( m_xDetailForm, sDetailFields );
     255             : 
     256           0 :         Sequence< OUString > sMasterFields;
     257           0 :         getFormFields( m_xMasterForm, sMasterFields );
     258             : 
     259             :         FieldLinkRow* aRows[] = {
     260           0 :             m_aRow1.get(), m_aRow2.get(), m_aRow3.get(), m_aRow4.get()
     261           0 :         };
     262           0 :         for ( sal_Int32 i = 0; i < 4 ; ++i )
     263             :         {
     264           0 :             aRows[i]->fillList( FieldLinkRow::eDetailField, sDetailFields );
     265           0 :             aRows[i]->fillList( FieldLinkRow::eMasterField, sMasterFields );
     266           0 :         }
     267             : 
     268           0 :     }
     269             : 
     270             : 
     271           0 :     void FormLinkDialog::initializeColumnLabels()
     272             :     {
     273             :         // label for the detail form
     274           0 :         OUString sDetailType = getFormDataSourceType( m_xDetailForm );
     275           0 :         if ( sDetailType.isEmpty() )
     276             :         {
     277           0 :             if ( m_sDetailLabel.isEmpty() )
     278             :             {
     279           0 :                 m_sDetailLabel = PcrRes(STR_DETAIL_FORM).toString();
     280             :             }
     281           0 :             sDetailType = m_sDetailLabel;
     282             :         }
     283           0 :         m_pDetailLabel->SetText( sDetailType );
     284             : 
     285             :         // label for the master form
     286           0 :         OUString sMasterType = getFormDataSourceType( m_xMasterForm );
     287           0 :         if ( sMasterType.isEmpty() )
     288             :         {
     289           0 :             if ( m_sMasterLabel.isEmpty() )
     290             :             {
     291           0 :                 m_sMasterLabel = PcrRes(STR_MASTER_FORM).toString();
     292             :             }
     293           0 :             sMasterType = m_sMasterLabel;
     294             :         }
     295           0 :         m_pMasterLabel->SetText( sMasterType );
     296           0 :     }
     297             : 
     298             : 
     299           0 :     void FormLinkDialog::initializeFieldRowsFrom( Sequence< OUString >& _rDetailFields, Sequence< OUString >& _rMasterFields )
     300             :     {
     301             :         // our UI does allow 4 fields max
     302           0 :         _rDetailFields.realloc( 4 );
     303           0 :         _rMasterFields.realloc( 4 );
     304             : 
     305           0 :         const OUString* pDetailFields = _rDetailFields.getConstArray();
     306           0 :         const OUString* pMasterFields = _rMasterFields.getConstArray();
     307             : 
     308             :         FieldLinkRow* aRows[] = {
     309           0 :             m_aRow1.get(), m_aRow2.get(), m_aRow3.get(), m_aRow4.get()
     310           0 :         };
     311           0 :         for ( sal_Int32 i = 0; i < 4; ++i, ++pDetailFields, ++pMasterFields )
     312             :         {
     313           0 :             aRows[ i ]->SetFieldName( FieldLinkRow::eDetailField, *pDetailFields );
     314           0 :             aRows[ i ]->SetFieldName( FieldLinkRow::eMasterField, *pMasterFields );
     315             :         }
     316           0 :     }
     317             : 
     318             : 
     319           0 :     void FormLinkDialog::initializeLinks()
     320             :     {
     321             :         try
     322             :         {
     323           0 :             Sequence< OUString > aDetailFields;
     324           0 :             Sequence< OUString > aMasterFields;
     325             : 
     326           0 :             Reference< XPropertySet > xDetailFormProps( m_xDetailForm, UNO_QUERY );
     327           0 :             if ( xDetailFormProps.is() )
     328             :             {
     329           0 :                 xDetailFormProps->getPropertyValue( PROPERTY_DETAILFIELDS ) >>= aDetailFields;
     330           0 :                 xDetailFormProps->getPropertyValue( PROPERTY_MASTERFIELDS ) >>= aMasterFields;
     331             :             }
     332             : 
     333           0 :             initializeFieldRowsFrom( aDetailFields, aMasterFields );
     334             :         }
     335           0 :         catch( const Exception& )
     336             :         {
     337             :             OSL_FAIL( "FormLinkDialog::initializeLinks: caught an exception!" );
     338             :         }
     339           0 :     }
     340             : 
     341             : 
     342           0 :     void FormLinkDialog::updateOkButton()
     343             :     {
     344             :         // in all rows, there must be either two valid selections, or none at all
     345             :         // If there is at least one row with exactly one valid selection, then the
     346             :         // OKButton needs to be disabled
     347           0 :         bool bEnable = true;
     348             : 
     349             :         const FieldLinkRow* aRows[] = {
     350           0 :             m_aRow1.get(), m_aRow2.get(), m_aRow3.get(), m_aRow4.get()
     351           0 :         };
     352             : 
     353           0 :         for ( sal_Int32 i = 0; ( i < 4 ) && bEnable; ++i )
     354             :         {
     355           0 :             OUString sNotInterestedInRightNow;
     356           0 :             if  (  aRows[ i ]->GetFieldName( FieldLinkRow::eDetailField, sNotInterestedInRightNow )
     357           0 :                 != aRows[ i ]->GetFieldName( FieldLinkRow::eMasterField, sNotInterestedInRightNow )
     358             :                 )
     359           0 :                 bEnable = false;
     360           0 :         }
     361             : 
     362           0 :         m_pOK->Enable( bEnable );
     363           0 :     }
     364             : 
     365             : 
     366           0 :     OUString FormLinkDialog::getFormDataSourceType( const Reference< XPropertySet >& _rxForm ) const
     367             :     {
     368           0 :         OUString sReturn;
     369           0 :         if ( !_rxForm.is() )
     370           0 :             return sReturn;
     371             : 
     372             :         try
     373             :         {
     374           0 :             sal_Int32       nCommandType = CommandType::COMMAND;
     375           0 :             OUString sCommand;
     376             : 
     377           0 :             _rxForm->getPropertyValue( PROPERTY_COMMANDTYPE ) >>= nCommandType;
     378           0 :             _rxForm->getPropertyValue( PROPERTY_COMMAND     ) >>= sCommand;
     379             : 
     380           0 :             if  (  ( nCommandType == CommandType::TABLE )
     381           0 :                 || ( nCommandType == CommandType::QUERY )
     382             :                 )
     383           0 :                 sReturn = sCommand;
     384             :         }
     385           0 :         catch( const Exception& )
     386             :         {
     387             :             OSL_FAIL( "FormLinkDialog::getFormDataSourceType: caught an exception!" );
     388             :         }
     389           0 :         return sReturn;
     390             :     }
     391             : 
     392             : 
     393           0 :     void FormLinkDialog::getFormFields( const Reference< XPropertySet >& _rxForm, Sequence< OUString >& /* [out] */ _rNames ) const
     394             :     {
     395           0 :         _rNames.realloc( 0 );
     396             : 
     397           0 :         ::dbtools::SQLExceptionInfo aErrorInfo;
     398           0 :         OUString sCommand;
     399             :         try
     400             :         {
     401           0 :             WaitObject aWaitCursor( const_cast< FormLinkDialog* >( this ) );
     402             : 
     403             :             OSL_ENSURE( _rxForm.is(), "FormLinkDialog::getFormFields: invalid form!" );
     404             : 
     405           0 :             sal_Int32       nCommandType = CommandType::COMMAND;
     406             : 
     407           0 :             _rxForm->getPropertyValue( PROPERTY_COMMANDTYPE ) >>= nCommandType;
     408           0 :             _rxForm->getPropertyValue( PROPERTY_COMMAND     ) >>= sCommand;
     409             : 
     410           0 :             Reference< XConnection > xConnection;
     411           0 :             ensureFormConnection( _rxForm, xConnection );
     412             : 
     413           0 :             _rNames = ::dbtools::getFieldNamesByCommandDescriptor(
     414             :                 xConnection,
     415             :                 nCommandType,
     416             :                 sCommand,
     417             :                 &aErrorInfo
     418           0 :             );
     419             :         }
     420           0 :         catch (const SQLContext& e)    { aErrorInfo = e; }
     421           0 :         catch (const SQLWarning& e)    { aErrorInfo = e; }
     422           0 :         catch (const SQLException& e ) { aErrorInfo = e; }
     423           0 :         catch( const Exception& )
     424             :         {
     425             :             OSL_FAIL( "FormLinkDialog::getFormFields: caught a non-SQL exception!" );
     426             :         }
     427             : 
     428           0 :         if ( aErrorInfo.isValid() )
     429             :         {
     430           0 :             OUString sErrorMessage;
     431             :             {
     432           0 :                 sErrorMessage = PcrRes(STR_ERROR_RETRIEVING_COLUMNS).toString();
     433           0 :                 sErrorMessage = sErrorMessage.replaceFirst("#", 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
     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, true );
     454             :     }
     455             : 
     456             : 
     457           0 :     void FormLinkDialog::getConnectionMetaData( const Reference< XPropertySet >& _rxFormProps, Reference< XDatabaseMetaData >& /* [out] */ _rxMeta ) const
     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< 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 :     bool FormLinkDialog::getExistingRelation( const Reference< XPropertySet >& _rxLHS, const Reference< XPropertySet >& /*_rxRHS*/,
     498             :             // TODO: fix the usage of _rxRHS. This is issue #i81956#.
     499             :         Sequence< OUString >& _rLeftFields, Sequence< 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 :                 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("Type") >>= nKeyType;
     522           0 :                     if ( nKeyType != KeyType::FOREIGN )
     523           0 :                         continue;
     524             : 
     525           0 :                     xKeyColumns.clear();
     526           0 :                     xKeyColSupp.set(xKey, css::uno::UNO_QUERY);
     527           0 :                     if ( xKeyColSupp.is() )
     528           0 :                         xKeyColumns.set(xKeyColSupp->getColumns(), css::uno::UNO_QUERY);
     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("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 :             bool bEnable = true;
     573             : 
     574             :             // only show the button when both forms are based on the same data source
     575           0 :             if ( bEnable )
     576             :             {
     577           0 :                 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 = 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             :                 SAL_WARN_IF( m_aRelationMasterColumns.getLength() != m_aRelationDetailColumns.getLength(),
     615             :                     "extensions.propctrlr",
     616             :                     "FormLinkDialog::initializeSuggest: nonsense!" );
     617           0 :                 if ( m_aRelationMasterColumns.getLength() == 0 )
     618             :                 {   // okay, there is no relation "pointing" (via a foreign key) from the detail table to the master table
     619             :                     // but perhaps the other way round (would make less sense, but who knows ...)
     620           0 :                     bEnable = getExistingRelation( xMasterTable, xDetailTable, m_aRelationMasterColumns, m_aRelationDetailColumns );
     621             :                 }
     622             :             }
     623             : 
     624             :             // only enable the button if the relation contains at most 4 field pairs
     625           0 :             if ( bEnable )
     626             :             {
     627           0 :                 bEnable = ( m_aRelationMasterColumns.getLength() <= 4 );
     628             :             }
     629             : 
     630           0 :             m_pSuggest->Enable( bEnable );
     631             :         }
     632           0 :         catch( const Exception& )
     633             :         {
     634             :             OSL_FAIL( "FormLinkDialog::initializeSuggest: caught an exception!" );
     635           0 :         }
     636             :     }
     637             : 
     638             : 
     639           0 :     IMPL_LINK( FormLinkDialog, OnSuggest, void*, /*_pNotInterestedIn*/ )
     640             :     {
     641           0 :         initializeFieldRowsFrom( m_aRelationDetailColumns, m_aRelationMasterColumns );
     642           0 :         return 0L;
     643             :     }
     644             : 
     645             : 
     646           0 :     IMPL_LINK( FormLinkDialog, OnFieldChanged, FieldLinkRow*, /*_pRow*/ )
     647             :     {
     648           0 :         updateOkButton();
     649           0 :         return 0L;
     650             :     }
     651             : 
     652             : 
     653           0 :     IMPL_LINK( FormLinkDialog, OnInitialize, void*, /*_pNotInterestedIn*/ )
     654             :     {
     655           0 :         initializeColumnLabels();
     656           0 :         initializeFieldLists();
     657           0 :         initializeLinks();
     658           0 :         initializeSuggest();
     659           0 :         return 0L;
     660             :     }
     661             : 
     662          12 : }   // namespace pcr
     663             : 
     664             : 
     665             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10