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