LCOV - code coverage report
Current view: top level - extensions/source/abpilot - datasourcehandling.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 208 0.0 %
Date: 2012-08-25 Functions: 0 39 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : 
      30                 :            : #include "abpresid.hrc"
      31                 :            : #include "abptypes.hxx"
      32                 :            : #include "componentmodule.hxx"
      33                 :            : #include "datasourcehandling.hxx"
      34                 :            : 
      35                 :            : #include <com/sun/star/beans/XPropertySet.hpp>
      36                 :            : #include <com/sun/star/container/XNameAccess.hpp>
      37                 :            : #include <com/sun/star/frame/XStorable.hpp>
      38                 :            : #include <com/sun/star/lang/XComponent.hpp>
      39                 :            : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      40                 :            : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
      41                 :            : #include <com/sun/star/sdb/SQLContext.hpp>
      42                 :            : #include <com/sun/star/sdb/XCompletedConnection.hpp>
      43                 :            : #include <com/sun/star/sdb/XDatabaseRegistrations.hpp>
      44                 :            : #include <com/sun/star/sdb/XDocumentDataSource.hpp>
      45                 :            : #include <com/sun/star/sdbc/XConnection.hpp>
      46                 :            : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
      47                 :            : #include <com/sun/star/task/XInteractionHandler.hpp>
      48                 :            : #include <com/sun/star/uno/XNamingService.hpp>
      49                 :            : 
      50                 :            : #include <comphelper/interaction.hxx>
      51                 :            : #include <comphelper/componentcontext.hxx>
      52                 :            : #include <tools/debug.hxx>
      53                 :            : #include <tools/diagnose_ex.h>
      54                 :            : #include <unotools/confignode.hxx>
      55                 :            : #include <unotools/sharedunocomponent.hxx>
      56                 :            : #include <vcl/stdtext.hxx>
      57                 :            : 
      58                 :            : //.........................................................................
      59                 :            : namespace abp
      60                 :            : {
      61                 :            : //.........................................................................
      62                 :            : 
      63                 :            :     using namespace ::utl;
      64                 :            :     using namespace ::comphelper;
      65                 :            :     using namespace ::com::sun::star::uno;
      66                 :            :     using namespace ::com::sun::star::lang;
      67                 :            :     using namespace ::com::sun::star::sdb;
      68                 :            :     using namespace ::com::sun::star::sdbc;
      69                 :            :     using namespace ::com::sun::star::task;
      70                 :            :     using namespace ::com::sun::star::beans;
      71                 :            :     using namespace ::com::sun::star::sdbcx;
      72                 :            :     using namespace ::com::sun::star::container;
      73                 :            :     using namespace ::com::sun::star::frame;
      74                 :            : 
      75                 :            :     //=====================================================================
      76                 :            :     struct PackageAccessControl { };
      77                 :            : 
      78                 :            :     //=====================================================================
      79                 :            :     //---------------------------------------------------------------------
      80                 :          0 :     static Reference< XNameAccess > lcl_getDataSourceContext( const Reference< XMultiServiceFactory >& _rxORB ) SAL_THROW (( Exception ))
      81                 :            :     {
      82                 :          0 :         Reference< XNameAccess > xContext( _rxORB->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.DatabaseContext" )) ), UNO_QUERY );
      83                 :            :         DBG_ASSERT(xContext.is(), "lcl_getDataSourceContext: could not access the data source context!");
      84                 :          0 :         return xContext;
      85                 :            :     }
      86                 :            : 
      87                 :            :     //---------------------------------------------------------------------
      88                 :            :     /// creates a new data source and inserts it into the context
      89                 :          0 :     static void lcl_implCreateAndInsert(
      90                 :            :         const Reference< XMultiServiceFactory >& _rxORB, const ::rtl::OUString& _rName,
      91                 :            :         Reference< XPropertySet >& /* [out] */ _rxNewDataSource ) SAL_THROW (( ::com::sun::star::uno::Exception ))
      92                 :            :     {
      93                 :            :         //.............................................................
      94                 :            :         // get the data source context
      95                 :          0 :         Reference< XNameAccess > xContext = lcl_getDataSourceContext( _rxORB );
      96                 :            : 
      97                 :            :         DBG_ASSERT( !xContext->hasByName( _rName ), "lcl_implCreateAndInsert: name already used!" );
      98                 :            :         (void)_rName;
      99                 :            : 
     100                 :            :         //.............................................................
     101                 :            :         // create a new data source
     102                 :          0 :         Reference< XSingleServiceFactory > xFactory( xContext, UNO_QUERY );
     103                 :          0 :         Reference< XPropertySet > xNewDataSource;
     104                 :          0 :         if (xFactory.is())
     105                 :          0 :             xNewDataSource = Reference< XPropertySet >( xFactory->createInstance(), UNO_QUERY );
     106                 :            :         DBG_ASSERT( xNewDataSource.is(), "lcl_implCreateAndInsert: could not create a new data source!" );
     107                 :            : 
     108                 :            :         //.............................................................
     109                 :            :         // insert the data source into the context
     110                 :          0 :         Reference< XNamingService > xDynamicContext( xContext, UNO_QUERY );
     111                 :            :         DBG_ASSERT( xDynamicContext.is(), "lcl_implCreateAndInsert: missing an interface on the context (XNamingService)!" );
     112                 :          0 :         if (xDynamicContext.is())
     113                 :            :         {
     114                 :            :             //  xDynamicContext->registerObject( _rName, xNewDataSource );
     115                 :          0 :             _rxNewDataSource = xNewDataSource;
     116                 :          0 :         }
     117                 :          0 :     }
     118                 :            : 
     119                 :            :     //---------------------------------------------------------------------
     120                 :            :     /// creates and inserts a data source, and sets it's URL property to the string given
     121                 :          0 :     static ODataSource lcl_implCreateAndSetURL(
     122                 :            :         const Reference< XMultiServiceFactory >& _rxORB, const ::rtl::OUString& _rName,
     123                 :            :         const sal_Char* _pInitialAsciiURL ) SAL_THROW (( ))
     124                 :            :     {
     125                 :          0 :         ODataSource aReturn( _rxORB );
     126                 :            :         try
     127                 :            :         {
     128                 :            :             // create the new data source
     129                 :          0 :             Reference< XPropertySet > xNewDataSource;
     130                 :          0 :             lcl_implCreateAndInsert( _rxORB, _rName, xNewDataSource );
     131                 :            : 
     132                 :            :             //.............................................................
     133                 :            :             // set the URL property
     134                 :          0 :             if (xNewDataSource.is())
     135                 :            :             {
     136                 :          0 :                 xNewDataSource->setPropertyValue(
     137                 :            :                     ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "URL" )),
     138                 :            :                     makeAny( ::rtl::OUString::createFromAscii( _pInitialAsciiURL ) )
     139                 :          0 :                 );
     140                 :            :             }
     141                 :            : 
     142                 :          0 :             aReturn.setDataSource( xNewDataSource, _rName,PackageAccessControl() );
     143                 :            :         }
     144                 :          0 :         catch(const Exception&)
     145                 :            :         {
     146                 :            :             OSL_FAIL( "lcl_implCreateAndSetURL: caught an exception while creating the data source!" );
     147                 :            :         }
     148                 :            : 
     149                 :          0 :         return aReturn;
     150                 :            :     }
     151                 :            :     //---------------------------------------------------------------------
     152                 :          0 :     void lcl_registerDataSource(
     153                 :            :         const Reference< XMultiServiceFactory >& _rxORB, const ::rtl::OUString& _sName,
     154                 :            :         const ::rtl::OUString& _sURL ) SAL_THROW (( ::com::sun::star::uno::Exception ))
     155                 :            :     {
     156                 :            :         OSL_ENSURE( !_sName.isEmpty(), "lcl_registerDataSource: invalid name!" );
     157                 :            :         OSL_ENSURE( !_sURL.isEmpty(), "lcl_registerDataSource: invalid URL!" );
     158                 :            :         try
     159                 :            :         {
     160                 :            : 
     161                 :          0 :             ::comphelper::ComponentContext aContext( _rxORB );
     162                 :            :             Reference< XDatabaseRegistrations > xRegistrations(
     163                 :          0 :                 aContext.createComponent( "com.sun.star.sdb.DatabaseContext" ), UNO_QUERY_THROW );
     164                 :            : 
     165                 :          0 :             if ( xRegistrations->hasRegisteredDatabase( _sName ) )
     166                 :          0 :                 xRegistrations->changeDatabaseLocation( _sName, _sURL );
     167                 :            :             else
     168                 :          0 :                 xRegistrations->registerDatabaseLocation( _sName, _sURL );
     169                 :            :         }
     170                 :          0 :         catch( const Exception& )
     171                 :            :         {
     172                 :            :             DBG_UNHANDLED_EXCEPTION();
     173                 :            :         }
     174                 :          0 :     }
     175                 :            : 
     176                 :            :     //=====================================================================
     177                 :            :     //= ODataSourceContextImpl
     178                 :            :     //=====================================================================
     179                 :            :     struct ODataSourceContextImpl
     180                 :            :     {
     181                 :            :         Reference< XMultiServiceFactory >   xORB;
     182                 :            :         Reference< XNameAccess >            xContext;           /// the UNO data source context
     183                 :            :         StringBag                           aDataSourceNames;   /// for quicker name checks (without the UNO overhead)
     184                 :            : 
     185                 :          0 :         ODataSourceContextImpl( const Reference< XMultiServiceFactory >& _rxORB ) : xORB( _rxORB ) { }
     186                 :            :         ODataSourceContextImpl( const ODataSourceContextImpl& _rSource )
     187                 :            :             :xORB       ( _rSource.xORB )
     188                 :            :             ,xContext   ( _rSource.xContext )
     189                 :            :         {
     190                 :            :         }
     191                 :            :     };
     192                 :            : 
     193                 :            :     //=====================================================================
     194                 :            :     //= ODataSourceContext
     195                 :            :     //=====================================================================
     196                 :            :     //---------------------------------------------------------------------
     197                 :          0 :     ODataSourceContext::ODataSourceContext(const Reference< XMultiServiceFactory >& _rxORB)
     198                 :          0 :         :m_pImpl( new ODataSourceContextImpl( _rxORB ) )
     199                 :            :     {
     200                 :            :         try
     201                 :            :         {
     202                 :            :             // create the UNO context
     203                 :          0 :             m_pImpl->xContext = lcl_getDataSourceContext( _rxORB );
     204                 :            : 
     205                 :          0 :             if (m_pImpl->xContext.is())
     206                 :            :             {
     207                 :            :                 // collect the data source names
     208                 :          0 :                 Sequence< ::rtl::OUString > aDSNames = m_pImpl->xContext->getElementNames();
     209                 :          0 :                 const ::rtl::OUString* pDSNames = aDSNames.getConstArray();
     210                 :          0 :                 const ::rtl::OUString* pDSNamesEnd = pDSNames + aDSNames.getLength();
     211                 :            : 
     212                 :          0 :                 for ( ;pDSNames != pDSNamesEnd; ++pDSNames )
     213                 :          0 :                     m_pImpl->aDataSourceNames.insert( *pDSNames );
     214                 :            :             }
     215                 :            :         }
     216                 :          0 :         catch( const Exception& )
     217                 :            :         {
     218                 :            :             OSL_FAIL( "ODataSourceContext::ODataSourceContext: caught an exception!" );
     219                 :            :         }
     220                 :          0 :     }
     221                 :            : 
     222                 :            :     //---------------------------------------------------------------------
     223                 :          0 :     ::rtl::OUString& ODataSourceContext::disambiguate(::rtl::OUString& _rDataSourceName)
     224                 :            :     {
     225                 :          0 :         ::rtl::OUString sCheck( _rDataSourceName );
     226                 :          0 :         ConstStringBagIterator aPos = m_pImpl->aDataSourceNames.find( sCheck );
     227                 :            : 
     228                 :          0 :         sal_Int32 nPostFix = 1;
     229                 :          0 :         while ( ( m_pImpl->aDataSourceNames.end() != aPos ) && ( nPostFix < 65535 ) )
     230                 :            :         {   // there already is a data source with this name
     231                 :          0 :             sCheck = _rDataSourceName;
     232                 :          0 :             sCheck += ::rtl::OUString::valueOf( nPostFix++ );
     233                 :            : 
     234                 :          0 :             aPos = m_pImpl->aDataSourceNames.find( sCheck );
     235                 :            :         }
     236                 :            : 
     237                 :          0 :         _rDataSourceName = sCheck;
     238                 :          0 :         return _rDataSourceName;
     239                 :            :     }
     240                 :            : 
     241                 :            :     //---------------------------------------------------------------------
     242                 :          0 :     void ODataSourceContext::getDataSourceNames( StringBag& _rNames ) const SAL_THROW (( ))
     243                 :            :     {
     244                 :          0 :         _rNames = m_pImpl->aDataSourceNames;
     245                 :          0 :     }
     246                 :            : 
     247                 :            :     //---------------------------------------------------------------------
     248                 :          0 :     ODataSource ODataSourceContext::createNewLDAP( const ::rtl::OUString& _rName) SAL_THROW (( ))
     249                 :            :     {
     250                 :          0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:ldap:" );
     251                 :            :     }
     252                 :            : 
     253                 :            :     //---------------------------------------------------------------------
     254                 :          0 :     ODataSource ODataSourceContext::createNewMORK( const ::rtl::OUString& _rName) SAL_THROW (( ))
     255                 :            :     {
     256                 :          0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:mozilla" );
     257                 :            :     }
     258                 :            : 
     259                 :            :     //---------------------------------------------------------------------
     260                 :          0 :     ODataSource ODataSourceContext::createNewThunderbird( const ::rtl::OUString& _rName ) SAL_THROW (( ))
     261                 :            :     {
     262                 :          0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:thunderbird" );
     263                 :            :     }
     264                 :            : 
     265                 :            :     //---------------------------------------------------------------------
     266                 :          0 :     ODataSource ODataSourceContext::createNewEvolutionLdap( const ::rtl::OUString& _rName) SAL_THROW (( ))
     267                 :            :     {
     268                 :          0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:evolution:ldap" );
     269                 :            :     }
     270                 :            :     //---------------------------------------------------------------------
     271                 :          0 :     ODataSource ODataSourceContext::createNewEvolutionGroupwise( const ::rtl::OUString& _rName) SAL_THROW (( ))
     272                 :            :     {
     273                 :          0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:evolution:groupwise" );
     274                 :            :     }
     275                 :            :     //---------------------------------------------------------------------
     276                 :          0 :     ODataSource ODataSourceContext::createNewEvolution( const ::rtl::OUString& _rName) SAL_THROW (( ))
     277                 :            :     {
     278                 :          0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:evolution:local" );
     279                 :            :     }
     280                 :            : 
     281                 :            :     //---------------------------------------------------------------------
     282                 :          0 :     ODataSource ODataSourceContext::createNewKab( const ::rtl::OUString& _rName) SAL_THROW (( ))
     283                 :            :     {
     284                 :          0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:kab" );
     285                 :            :     }
     286                 :            : 
     287                 :            :     //---------------------------------------------------------------------
     288                 :          0 :     ODataSource ODataSourceContext::createNewMacab( const ::rtl::OUString& _rName) SAL_THROW (( ))
     289                 :            :     {
     290                 :          0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:macab" );
     291                 :            :     }
     292                 :            : 
     293                 :            :     //---------------------------------------------------------------------
     294                 :          0 :     ODataSource ODataSourceContext::createNewOutlook( const ::rtl::OUString& _rName) SAL_THROW (( ))
     295                 :            :     {
     296                 :          0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:outlook" );
     297                 :            :     }
     298                 :            : 
     299                 :            :     //---------------------------------------------------------------------
     300                 :          0 :     ODataSource ODataSourceContext::createNewOE( const ::rtl::OUString& _rName) SAL_THROW (( ))
     301                 :            :     {
     302                 :          0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:address:outlookexp" );
     303                 :            :     }
     304                 :            : 
     305                 :            :     //---------------------------------------------------------------------
     306                 :          0 :     ODataSource ODataSourceContext::createNewDBase( const ::rtl::OUString& _rName) SAL_THROW (( ))
     307                 :            :     {
     308                 :          0 :         return lcl_implCreateAndSetURL( m_pImpl->xORB, _rName, "sdbc:dbase:" );
     309                 :            :     }
     310                 :            : 
     311                 :            :     //=====================================================================
     312                 :            :     //= ODataSourceImpl
     313                 :            :     //=====================================================================
     314                 :          0 :     struct ODataSourceImpl
     315                 :            :     {
     316                 :            :     public:
     317                 :            :         Reference< XMultiServiceFactory >       xORB;               /// the service factory
     318                 :            :         Reference< XPropertySet >               xDataSource;        /// the UNO data source
     319                 :            :         ::utl::SharedUNOComponent< XConnection >
     320                 :            :                                                 xConnection;
     321                 :            :         StringBag                               aTables;            // the cached table names
     322                 :            :         ::rtl::OUString                         sName;
     323                 :            :         sal_Bool                                bTablesUpToDate;    // table name cache up-to-date?
     324                 :            : 
     325                 :          0 :         ODataSourceImpl( const Reference< XMultiServiceFactory >& _rxORB )
     326                 :            :             :xORB( _rxORB )
     327                 :          0 :             ,bTablesUpToDate( sal_False )
     328                 :            :         {
     329                 :          0 :         }
     330                 :            : 
     331                 :            :         ODataSourceImpl( const ODataSourceImpl& _rSource );
     332                 :            :     };
     333                 :            : 
     334                 :            :     //---------------------------------------------------------------------
     335                 :          0 :     ODataSourceImpl::ODataSourceImpl( const ODataSourceImpl& _rSource )
     336                 :            :         :xORB( _rSource.xORB )
     337                 :            :         ,xDataSource( _rSource.xDataSource )
     338                 :            :         ,xConnection( _rSource.xConnection )
     339                 :            :         ,aTables( _rSource.aTables )
     340                 :            :         ,sName( _rSource.sName )
     341                 :          0 :         ,bTablesUpToDate( _rSource.bTablesUpToDate )
     342                 :            :     {
     343                 :          0 :     }
     344                 :            : 
     345                 :            :     //=====================================================================
     346                 :            :     //= ODataSource
     347                 :            :     //=====================================================================
     348                 :            :     //---------------------------------------------------------------------
     349                 :          0 :     ODataSource::ODataSource( const ODataSource& _rSource )
     350                 :          0 :         :m_pImpl( NULL )
     351                 :            :     {
     352                 :          0 :         *this = _rSource;
     353                 :          0 :     }
     354                 :            : 
     355                 :            :     //---------------------------------------------------------------------
     356                 :          0 :     ODataSource& ODataSource::operator=( const ODataSource& _rSource )
     357                 :            :     {
     358                 :          0 :         if( this != &_rSource )
     359                 :            :         {
     360                 :          0 :             delete m_pImpl;
     361                 :          0 :             m_pImpl = new ODataSourceImpl( *_rSource.m_pImpl );
     362                 :            :         }
     363                 :          0 :         return *this;
     364                 :            :     }
     365                 :            : 
     366                 :            :     //---------------------------------------------------------------------
     367                 :          0 :     ODataSource::ODataSource( const Reference< XMultiServiceFactory >& _rxORB )
     368                 :          0 :         :m_pImpl(new ODataSourceImpl(_rxORB))
     369                 :            :     {
     370                 :          0 :     }
     371                 :            : 
     372                 :            :     //---------------------------------------------------------------------
     373                 :          0 :     ODataSource::~ODataSource( )
     374                 :            :     {
     375                 :          0 :         delete m_pImpl;
     376                 :          0 :     }
     377                 :            : 
     378                 :            :     //---------------------------------------------------------------------
     379                 :          0 :     void ODataSource::store() SAL_THROW (( ))
     380                 :            :     {
     381                 :          0 :         if (!isValid())
     382                 :            :             // nothing to do
     383                 :          0 :             return;
     384                 :            :         try
     385                 :            :         {
     386                 :          0 :             Reference< XDocumentDataSource > xDocAccess( m_pImpl->xDataSource, UNO_QUERY );
     387                 :          0 :             Reference< XStorable > xStorable;
     388                 :          0 :             if ( xDocAccess.is() )
     389                 :          0 :                 xStorable = xStorable.query( xDocAccess->getDatabaseDocument() );
     390                 :            :             OSL_ENSURE( xStorable.is(),"DataSource is no XStorable!" );
     391                 :          0 :             if ( xStorable.is() )
     392                 :          0 :                 xStorable->storeAsURL(m_pImpl->sName,Sequence<PropertyValue>());
     393                 :            :         }
     394                 :          0 :         catch(const Exception&)
     395                 :            :         {
     396                 :            :             OSL_FAIL( "ODataSource::registerDataSource: caught an exception while creating the data source!" );
     397                 :            :         }
     398                 :            :     }
     399                 :            :     //---------------------------------------------------------------------
     400                 :          0 :     void ODataSource::registerDataSource( const ::rtl::OUString& _sRegisteredDataSourceName) SAL_THROW (( ))
     401                 :            :     {
     402                 :          0 :         if (!isValid())
     403                 :            :             // nothing to do
     404                 :          0 :             return;
     405                 :            : 
     406                 :            :         try
     407                 :            :         {
     408                 :            :             // invalidate ourself
     409                 :          0 :             lcl_registerDataSource(m_pImpl->xORB,_sRegisteredDataSourceName,m_pImpl->sName);
     410                 :            :         }
     411                 :          0 :         catch(const Exception&)
     412                 :            :         {
     413                 :            :             OSL_FAIL( "ODataSource::registerDataSource: caught an exception while creating the data source!" );
     414                 :            :         }
     415                 :            :     }
     416                 :            : 
     417                 :            :     //---------------------------------------------------------------------
     418                 :          0 :     void ODataSource::setDataSource( const Reference< XPropertySet >& _rxDS,const ::rtl::OUString& _sName, PackageAccessControl )
     419                 :            :     {
     420                 :          0 :         if (m_pImpl->xDataSource.get() == _rxDS.get())
     421                 :            :             // nothing to do
     422                 :          0 :             return;
     423                 :            : 
     424                 :          0 :         if ( isConnected() )
     425                 :          0 :             disconnect();
     426                 :            : 
     427                 :          0 :         m_pImpl->sName = _sName;
     428                 :          0 :         m_pImpl->xDataSource = _rxDS;
     429                 :            :     }
     430                 :            : 
     431                 :            :     //---------------------------------------------------------------------
     432                 :          0 :     void ODataSource::remove() SAL_THROW (( ))
     433                 :            :     {
     434                 :          0 :         if (!isValid())
     435                 :            :             // nothing to do
     436                 :          0 :             return;
     437                 :            : 
     438                 :            :         try
     439                 :            :         {
     440                 :            :             // invalidate ourself
     441                 :          0 :             m_pImpl->xDataSource.clear();
     442                 :            :         }
     443                 :            :         catch(const Exception&)
     444                 :            :         {
     445                 :            :             OSL_FAIL( "ODataSource::remove: caught an exception while creating the data source!" );
     446                 :            :         }
     447                 :            :     }
     448                 :            : 
     449                 :            :     //---------------------------------------------------------------------
     450                 :          0 :     sal_Bool ODataSource::rename( const ::rtl::OUString& _rName ) SAL_THROW (( ))
     451                 :            :     {
     452                 :          0 :         if (!isValid())
     453                 :            :             // nothing to do
     454                 :          0 :             return sal_False;
     455                 :            : 
     456                 :          0 :         m_pImpl->sName = _rName;
     457                 :          0 :         return sal_True;
     458                 :            :     }
     459                 :            : 
     460                 :            :     //---------------------------------------------------------------------
     461                 :          0 :     ::rtl::OUString ODataSource::getName() const SAL_THROW (( ))
     462                 :            :     {
     463                 :          0 :         if ( !isValid() )
     464                 :          0 :             return ::rtl::OUString();
     465                 :          0 :         return m_pImpl->sName;
     466                 :            :     }
     467                 :            : 
     468                 :            :     //---------------------------------------------------------------------
     469                 :          0 :     bool ODataSource::hasTable( const ::rtl::OUString& _rTableName ) const
     470                 :            :     {
     471                 :          0 :         if ( !isConnected() )
     472                 :          0 :             return false;
     473                 :            : 
     474                 :          0 :         const StringBag& aTables( getTableNames() );
     475                 :          0 :         return aTables.find( _rTableName ) != aTables.end();
     476                 :            :     }
     477                 :            : 
     478                 :            :     //---------------------------------------------------------------------
     479                 :          0 :     const StringBag& ODataSource::getTableNames() const SAL_THROW (( ))
     480                 :            :     {
     481                 :          0 :         m_pImpl->aTables.clear();
     482                 :          0 :         if ( !isConnected() )
     483                 :            :         {
     484                 :            :             OSL_FAIL( "ODataSource::getTableNames: not connected!" );
     485                 :            :         }
     486                 :            :         else
     487                 :            :         {
     488                 :            :             try
     489                 :            :             {
     490                 :            :                 // get the tables container from the connection
     491                 :          0 :                 Reference< XTablesSupplier > xSuppTables( m_pImpl->xConnection.getTyped(), UNO_QUERY );
     492                 :          0 :                 Reference< XNameAccess > xTables;
     493                 :          0 :                 if ( xSuppTables.is( ) )
     494                 :          0 :                     xTables = xSuppTables->getTables();
     495                 :            :                 DBG_ASSERT( xTables.is(), "ODataSource::getTableNames: could not retrieve the tables container!" );
     496                 :            : 
     497                 :            :                 // get the names
     498                 :          0 :                 Sequence< ::rtl::OUString > aTableNames;
     499                 :          0 :                 if ( xTables.is( ) )
     500                 :          0 :                     aTableNames = xTables->getElementNames( );
     501                 :            : 
     502                 :            :                 // copy the names
     503                 :          0 :                 const ::rtl::OUString* pTableNames = aTableNames.getConstArray();
     504                 :          0 :                 const ::rtl::OUString* pTableNamesEnd = pTableNames + aTableNames.getLength();
     505                 :          0 :                 for (;pTableNames < pTableNamesEnd; ++pTableNames)
     506                 :          0 :                     m_pImpl->aTables.insert( *pTableNames );
     507                 :            :             }
     508                 :          0 :             catch(const Exception&)
     509                 :            :             {
     510                 :            :             }
     511                 :            :         }
     512                 :            : 
     513                 :            :         // now the table cache is up-to-date
     514                 :          0 :         m_pImpl->bTablesUpToDate = sal_True;
     515                 :          0 :         return m_pImpl->aTables;
     516                 :            :     }
     517                 :            : 
     518                 :            :     //---------------------------------------------------------------------
     519                 :          0 :     sal_Bool ODataSource::connect( Window* _pMessageParent ) SAL_THROW (( ))
     520                 :            :     {
     521                 :          0 :         if ( isConnected( ) )
     522                 :            :             // nothing to do
     523                 :          0 :             return sal_True;
     524                 :            : 
     525                 :            :         // ................................................................
     526                 :            :         // create the interaction handler (needed for authentication and error handling)
     527                 :          0 :         static ::rtl::OUString s_sInteractionHandlerServiceName(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.task.InteractionHandler"));
     528                 :          0 :         Reference< XInteractionHandler > xInteractions;
     529                 :            :         try
     530                 :            :         {
     531                 :            :             xInteractions = Reference< XInteractionHandler >(
     532                 :          0 :                 m_pImpl->xORB->createInstance( s_sInteractionHandlerServiceName ),
     533                 :            :                 UNO_QUERY
     534                 :          0 :             );
     535                 :            :         }
     536                 :          0 :         catch(const Exception&)
     537                 :            :         {
     538                 :            :         }
     539                 :            : 
     540                 :            :         // ................................................................
     541                 :            :         // failure to create the interaction handler is a serious issue ...
     542                 :          0 :         if (!xInteractions.is())
     543                 :            :         {
     544                 :          0 :             if ( _pMessageParent )
     545                 :          0 :                 ShowServiceNotAvailableError( _pMessageParent, s_sInteractionHandlerServiceName, sal_True );
     546                 :          0 :             return sal_False;
     547                 :            :         }
     548                 :            : 
     549                 :            :         // ................................................................
     550                 :            :         // open the connection
     551                 :          0 :         Any aError;
     552                 :          0 :         Reference< XConnection > xConnection;
     553                 :            :         try
     554                 :            :         {
     555                 :          0 :             Reference< XCompletedConnection > xComplConn( m_pImpl->xDataSource, UNO_QUERY );
     556                 :            :             DBG_ASSERT( xComplConn.is(), "ODataSource::connect: missing the XCompletedConnection interface on the data source!" );
     557                 :          0 :             if ( xComplConn.is() )
     558                 :          0 :                 xConnection = xComplConn->connectWithCompletion( xInteractions );
     559                 :            :         }
     560                 :          0 :         catch( const SQLContext& e ) { aError <<= e; }
     561                 :          0 :         catch( const SQLWarning& e ) { aError <<= e; }
     562                 :          0 :         catch( const SQLException& e ) { aError <<= e; }
     563                 :          0 :         catch( const Exception& )
     564                 :            :         {
     565                 :            :             OSL_FAIL( "ODataSource::connect: caught a generic exception!" );
     566                 :            :         }
     567                 :            : 
     568                 :            :         // ................................................................
     569                 :            :         // handle errors
     570                 :          0 :         if ( aError.hasValue() && _pMessageParent )
     571                 :            :         {
     572                 :            :             try
     573                 :            :             {
     574                 :          0 :                 SQLException aException;
     575                 :          0 :                   aError >>= aException;
     576                 :          0 :                   if ( aException.Message.isEmpty() )
     577                 :            :                   {
     578                 :            :                     // prepend some context info
     579                 :          0 :                     SQLContext aDetailedError;
     580                 :          0 :                     aDetailedError.Message = String( ModuleRes( RID_STR_NOCONNECTION ) );
     581                 :          0 :                     aDetailedError.Details = String( ModuleRes( RID_STR_PLEASECHECKSETTINGS ) );
     582                 :          0 :                     aDetailedError.NextException = aError;
     583                 :            :                     // handle (aka display) the new context info
     584                 :          0 :                     xInteractions->handle( new OInteractionRequest( makeAny( aDetailedError ) ) );
     585                 :            :                   }
     586                 :            :                   else
     587                 :            :                   {
     588                 :            :                       // handle (aka display) the original error
     589                 :          0 :                     xInteractions->handle( new OInteractionRequest( makeAny( aException ) ) );
     590                 :          0 :                 }
     591                 :            :             }
     592                 :          0 :             catch( const Exception& )
     593                 :            :             {
     594                 :            :                 OSL_FAIL( "ODataSource::connect: caught an exception while trying to display the error!" );
     595                 :            :             }
     596                 :            :         }
     597                 :            : 
     598                 :          0 :         if ( !xConnection.is() )
     599                 :          0 :             return sal_False;
     600                 :            : 
     601                 :            :         // ................................................................
     602                 :            :         // success
     603                 :          0 :         m_pImpl->xConnection.reset( xConnection );
     604                 :          0 :         m_pImpl->aTables.clear();
     605                 :          0 :         m_pImpl->bTablesUpToDate = sal_False;
     606                 :            : 
     607                 :          0 :         return sal_True;
     608                 :            :     }
     609                 :            : 
     610                 :            :     //---------------------------------------------------------------------
     611                 :          0 :     void ODataSource::disconnect( ) SAL_THROW (( ))
     612                 :            :     {
     613                 :          0 :         m_pImpl->xConnection.clear();
     614                 :          0 :         m_pImpl->aTables.clear();
     615                 :          0 :         m_pImpl->bTablesUpToDate = sal_False;
     616                 :          0 :     }
     617                 :            : 
     618                 :            :     //---------------------------------------------------------------------
     619                 :          0 :     sal_Bool ODataSource::isConnected( ) const SAL_THROW (( ))
     620                 :            :     {
     621                 :          0 :         return m_pImpl->xConnection.is();
     622                 :            :     }
     623                 :            : 
     624                 :            :     //---------------------------------------------------------------------
     625                 :          0 :     sal_Bool ODataSource::isValid() const SAL_THROW (( ))
     626                 :            :     {
     627                 :          0 :         return m_pImpl && m_pImpl->xDataSource.is();
     628                 :            :     }
     629                 :            :     //---------------------------------------------------------------------
     630                 :          0 :     Reference< XPropertySet > ODataSource::getDataSource() const SAL_THROW (( ))
     631                 :            :     {
     632                 :          0 :         return m_pImpl ? m_pImpl->xDataSource : Reference< XPropertySet >();
     633                 :            :     }
     634                 :            : 
     635                 :            : //.........................................................................
     636                 :            : }   // namespace abp
     637                 :            : //.........................................................................
     638                 :            : 
     639                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10