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