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