1*96de5490SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*96de5490SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*96de5490SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*96de5490SAndrew Rist  * distributed with this work for additional information
6*96de5490SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*96de5490SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*96de5490SAndrew Rist  * "License"); you may not use this file except in compliance
9*96de5490SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*96de5490SAndrew Rist  *
11*96de5490SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*96de5490SAndrew Rist  *
13*96de5490SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*96de5490SAndrew Rist  * software distributed under the License is distributed on an
15*96de5490SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*96de5490SAndrew Rist  * KIND, either express or implied.  See the License for the
17*96de5490SAndrew Rist  * specific language governing permissions and limitations
18*96de5490SAndrew Rist  * under the License.
19*96de5490SAndrew Rist  *
20*96de5490SAndrew Rist  *************************************************************/
21*96de5490SAndrew Rist 
22*96de5490SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "apitools.hxx"
28cdf0e10cSrcweir #include "core_resource.hrc"
29cdf0e10cSrcweir #include "core_resource.hxx"
30cdf0e10cSrcweir #include "databasecontext.hxx"
31cdf0e10cSrcweir #include "databasedocument.hxx"
32cdf0e10cSrcweir #include "databaseregistrations.hxx"
33cdf0e10cSrcweir #include "datasource.hxx"
34cdf0e10cSrcweir #include "dbastrings.hrc"
35cdf0e10cSrcweir #include "module_dba.hxx"
36cdf0e10cSrcweir 
37cdf0e10cSrcweir /** === being UNO includes === **/
38cdf0e10cSrcweir #include <com/sun/star/beans/NamedValue.hpp>
39cdf0e10cSrcweir #include <com/sun/star/beans/PropertyAttribute.hpp>
40cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
41cdf0e10cSrcweir #include <com/sun/star/document/MacroExecMode.hpp>
42cdf0e10cSrcweir #include <com/sun/star/document/XFilter.hpp>
43cdf0e10cSrcweir #include <com/sun/star/document/XImporter.hpp>
44cdf0e10cSrcweir #include <com/sun/star/frame/XDesktop.hpp>
45cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
46cdf0e10cSrcweir #include <com/sun/star/frame/XModel2.hpp>
47cdf0e10cSrcweir #include <com/sun/star/frame/XTerminateListener.hpp>
48cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
49cdf0e10cSrcweir #include <com/sun/star/registry/InvalidRegistryException.hpp>
50cdf0e10cSrcweir #include <com/sun/star/sdbc/XDataSource.hpp>
51cdf0e10cSrcweir #include <com/sun/star/task/InteractionClassification.hpp>
52cdf0e10cSrcweir #include <com/sun/star/ucb/InteractiveIOException.hpp>
53cdf0e10cSrcweir #include <com/sun/star/ucb/IOErrorCode.hpp>
54cdf0e10cSrcweir #include <com/sun/star/util/XCloseable.hpp>
55cdf0e10cSrcweir /** === end UNO includes === **/
56cdf0e10cSrcweir 
57cdf0e10cSrcweir #include <basic/basmgr.hxx>
58cdf0e10cSrcweir #include <comphelper/enumhelper.hxx>
59cdf0e10cSrcweir #include <comphelper/evtlistenerhlp.hxx>
60cdf0e10cSrcweir #include <comphelper/namedvaluecollection.hxx>
61cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
62cdf0e10cSrcweir #include <comphelper/sequence.hxx>
63cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
64cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
65cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
66cdf0e10cSrcweir #include <svl/filenotation.hxx>
67cdf0e10cSrcweir #include <tools/debug.hxx>
68cdf0e10cSrcweir #include <tools/diagnose_ex.h>
69cdf0e10cSrcweir #include <tools/fsys.hxx>
70cdf0e10cSrcweir #include <tools/urlobj.hxx>
71cdf0e10cSrcweir #include <ucbhelper/content.hxx>
72cdf0e10cSrcweir #include <unotools/confignode.hxx>
73cdf0e10cSrcweir #include <unotools/pathoptions.hxx>
74cdf0e10cSrcweir #include <unotools/sharedunocomponent.hxx>
75cdf0e10cSrcweir #include <list>
76cdf0e10cSrcweir #include <boost/bind.hpp>
77cdf0e10cSrcweir 
78cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
79cdf0e10cSrcweir using namespace ::com::sun::star::sdb;
80cdf0e10cSrcweir using namespace ::com::sun::star::beans;
81cdf0e10cSrcweir using namespace ::com::sun::star::uno;
82cdf0e10cSrcweir using namespace ::com::sun::star::document;
83cdf0e10cSrcweir using namespace ::com::sun::star::frame;
84cdf0e10cSrcweir using namespace ::com::sun::star::lang;
85cdf0e10cSrcweir using namespace ::com::sun::star::container;
86cdf0e10cSrcweir using namespace ::com::sun::star::util;
87cdf0e10cSrcweir using namespace ::com::sun::star::registry;
88cdf0e10cSrcweir using namespace ::com::sun::star;
89cdf0e10cSrcweir using namespace ::cppu;
90cdf0e10cSrcweir using namespace ::osl;
91cdf0e10cSrcweir using namespace ::utl;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir using ::com::sun::star::task::InteractionClassification_ERROR;
94cdf0e10cSrcweir using ::com::sun::star::ucb::IOErrorCode_NO_FILE;
95cdf0e10cSrcweir using ::com::sun::star::ucb::InteractiveIOException;
96cdf0e10cSrcweir using ::com::sun::star::ucb::IOErrorCode_NOT_EXISTING;
97cdf0e10cSrcweir using ::com::sun::star::ucb::IOErrorCode_NOT_EXISTING_PATH;
98cdf0e10cSrcweir 
99cdf0e10cSrcweir //==========================================================================
100cdf0e10cSrcweir 
createRegistryInfo_ODatabaseContext()101cdf0e10cSrcweir extern "C" void SAL_CALL createRegistryInfo_ODatabaseContext()
102cdf0e10cSrcweir {
103cdf0e10cSrcweir 	static ::dba::OLegacySingletonRegistration< ::dbaccess::ODatabaseContext > aODatabaseContext_AutoRegistration;
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir //........................................................................
107cdf0e10cSrcweir namespace dbaccess
108cdf0e10cSrcweir {
109cdf0e10cSrcweir //........................................................................
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     // .............................................................................
112cdf0e10cSrcweir         typedef ::cppu::WeakImplHelper1 <   XTerminateListener
113cdf0e10cSrcweir                                         >   DatabaseDocumentLoader_Base;
114cdf0e10cSrcweir         class DatabaseDocumentLoader : public DatabaseDocumentLoader_Base
115cdf0e10cSrcweir         {
116cdf0e10cSrcweir         private:
117cdf0e10cSrcweir             Reference< XDesktop >               m_xDesktop;
118cdf0e10cSrcweir             ::std::list< const ODatabaseModelImpl* >  m_aDatabaseDocuments;
119cdf0e10cSrcweir 
120cdf0e10cSrcweir         public:
121cdf0e10cSrcweir             DatabaseDocumentLoader( const comphelper::ComponentContext& _aContext);
122cdf0e10cSrcweir 
append(const ODatabaseModelImpl & _rModelImpl)123cdf0e10cSrcweir             inline void append(const ODatabaseModelImpl& _rModelImpl )
124cdf0e10cSrcweir             {
125cdf0e10cSrcweir                 m_aDatabaseDocuments.push_back(&_rModelImpl);
126cdf0e10cSrcweir             }
remove(const ODatabaseModelImpl & _rModelImpl)127cdf0e10cSrcweir             inline void remove(const ODatabaseModelImpl& _rModelImpl) { m_aDatabaseDocuments.remove(&_rModelImpl); }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         private:
130cdf0e10cSrcweir             // XTerminateListener
131cdf0e10cSrcweir             virtual void SAL_CALL queryTermination( const lang::EventObject& Event ) throw (TerminationVetoException, RuntimeException);
132cdf0e10cSrcweir             virtual void SAL_CALL notifyTermination( const lang::EventObject& Event ) throw (RuntimeException);
133cdf0e10cSrcweir             // XEventListener
134cdf0e10cSrcweir             virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
135cdf0e10cSrcweir         };
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         // .............................................................................
DatabaseDocumentLoader(const comphelper::ComponentContext & _aContext)138cdf0e10cSrcweir         DatabaseDocumentLoader::DatabaseDocumentLoader( const comphelper::ComponentContext& _aContext )
139cdf0e10cSrcweir         {
140cdf0e10cSrcweir             acquire();
141cdf0e10cSrcweir             try
142cdf0e10cSrcweir             {
143cdf0e10cSrcweir                 m_xDesktop.set( _aContext.createComponent( (rtl::OUString)SERVICE_FRAME_DESKTOP ), UNO_QUERY_THROW );
144cdf0e10cSrcweir                 m_xDesktop->addTerminateListener( this );
145cdf0e10cSrcweir             }
146cdf0e10cSrcweir             catch( const Exception& )
147cdf0e10cSrcweir             {
148cdf0e10cSrcweir             	DBG_UNHANDLED_EXCEPTION();
149cdf0e10cSrcweir             }
150cdf0e10cSrcweir         }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir         struct TerminateFunctor : ::std::unary_function<ODatabaseModelImpl* , void>
153cdf0e10cSrcweir         {
operator ()dbaccess::TerminateFunctor154cdf0e10cSrcweir             void operator()( const ODatabaseModelImpl* _pModelImpl ) const
155cdf0e10cSrcweir             {
156cdf0e10cSrcweir                 try
157cdf0e10cSrcweir                 {
158cdf0e10cSrcweir                     const Reference< XModel2> xModel( _pModelImpl ->getModel_noCreate(),UNO_QUERY_THROW );
159cdf0e10cSrcweir                     if ( !xModel->getControllers()->hasMoreElements() )
160cdf0e10cSrcweir                     {
161cdf0e10cSrcweir                         Reference<util::XCloseable> xCloseable(xModel,UNO_QUERY_THROW);
162cdf0e10cSrcweir                         xCloseable->close(sal_False);
163cdf0e10cSrcweir                     } // if ( !xModel->getControllers()->hasMoreElements() )
164cdf0e10cSrcweir                 }
165cdf0e10cSrcweir                 catch(const CloseVetoException&)
166cdf0e10cSrcweir                 {
167cdf0e10cSrcweir                     throw TerminationVetoException();
168cdf0e10cSrcweir                 }
169cdf0e10cSrcweir             }
170cdf0e10cSrcweir         };
171cdf0e10cSrcweir         // .............................................................................
queryTermination(const lang::EventObject &)172cdf0e10cSrcweir         void SAL_CALL DatabaseDocumentLoader::queryTermination( const lang::EventObject& /*Event*/ ) throw (TerminationVetoException, RuntimeException)
173cdf0e10cSrcweir         {
174cdf0e10cSrcweir             ::std::list< const ODatabaseModelImpl* > aCopy(m_aDatabaseDocuments);
175cdf0e10cSrcweir             ::std::for_each(aCopy.begin(),aCopy.end(),TerminateFunctor());
176cdf0e10cSrcweir         }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir         // .............................................................................
notifyTermination(const lang::EventObject &)179cdf0e10cSrcweir         void SAL_CALL DatabaseDocumentLoader::notifyTermination( const lang::EventObject& /*Event*/ ) throw (RuntimeException)
180cdf0e10cSrcweir         {
181cdf0e10cSrcweir         }
182cdf0e10cSrcweir         // .............................................................................
disposing(const lang::EventObject &)183cdf0e10cSrcweir         void SAL_CALL DatabaseDocumentLoader::disposing( const lang::EventObject& /*Source*/ ) throw (RuntimeException)
184cdf0e10cSrcweir         {
185cdf0e10cSrcweir         }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir //= ODatabaseContext
188cdf0e10cSrcweir //==========================================================================
189cdf0e10cSrcweir //--------------------------------------------------------------------------
ODatabaseContext(const Reference<XComponentContext> & _rxContext)190cdf0e10cSrcweir ODatabaseContext::ODatabaseContext( const Reference< XComponentContext >& _rxContext )
191cdf0e10cSrcweir     :DatabaseAccessContext_Base(m_aMutex)
192cdf0e10cSrcweir     ,m_aContext( _rxContext )
193cdf0e10cSrcweir     ,m_aContainerListeners(m_aMutex)
194cdf0e10cSrcweir {
195cdf0e10cSrcweir     m_pDatabaseDocumentLoader = new DatabaseDocumentLoader( m_aContext );
196cdf0e10cSrcweir     ::basic::BasicManagerRepository::registerCreationListener( *this );
197cdf0e10cSrcweir 
198cdf0e10cSrcweir     osl_incrementInterlockedCount( &m_refCount );
199cdf0e10cSrcweir     {
200cdf0e10cSrcweir         m_xDBRegistrationAggregate.set( createDataSourceRegistrations( m_aContext ), UNO_SET_THROW );
201cdf0e10cSrcweir         m_xDatabaseRegistrations.set( m_xDBRegistrationAggregate, UNO_QUERY_THROW );
202cdf0e10cSrcweir 
203cdf0e10cSrcweir         m_xDBRegistrationAggregate->setDelegator( *this );
204cdf0e10cSrcweir     }
205cdf0e10cSrcweir     osl_decrementInterlockedCount( &m_refCount );
206cdf0e10cSrcweir }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir //--------------------------------------------------------------------------
~ODatabaseContext()209cdf0e10cSrcweir ODatabaseContext::~ODatabaseContext()
210cdf0e10cSrcweir {
211cdf0e10cSrcweir     ::basic::BasicManagerRepository::revokeCreationListener( *this );
212cdf0e10cSrcweir     if ( m_pDatabaseDocumentLoader )
213cdf0e10cSrcweir         m_pDatabaseDocumentLoader->release();
214cdf0e10cSrcweir 
215cdf0e10cSrcweir     m_xDBRegistrationAggregate->setDelegator( NULL );
216cdf0e10cSrcweir     m_xDBRegistrationAggregate.clear();
217cdf0e10cSrcweir     m_xDatabaseRegistrations.clear();
218cdf0e10cSrcweir }
219cdf0e10cSrcweir 
220cdf0e10cSrcweir // Helper
221cdf0e10cSrcweir //------------------------------------------------------------------------------
getImplementationName_static()222cdf0e10cSrcweir rtl::OUString ODatabaseContext::getImplementationName_static() throw( RuntimeException )
223cdf0e10cSrcweir 
224cdf0e10cSrcweir {
225cdf0e10cSrcweir 	return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.dba.ODatabaseContext"));
226cdf0e10cSrcweir }
227cdf0e10cSrcweir 
228cdf0e10cSrcweir //------------------------------------------------------------------------------
Create(const Reference<XComponentContext> & _rxContext)229cdf0e10cSrcweir Reference< XInterface > ODatabaseContext::Create(const Reference< XComponentContext >& _rxContext)
230cdf0e10cSrcweir {
231cdf0e10cSrcweir 	return *( new ODatabaseContext( _rxContext ) );
232cdf0e10cSrcweir }
233cdf0e10cSrcweir 
234cdf0e10cSrcweir //------------------------------------------------------------------------------
getSupportedServiceNames_static(void)235cdf0e10cSrcweir Sequence< rtl::OUString > ODatabaseContext::getSupportedServiceNames_static(void) throw( RuntimeException )
236cdf0e10cSrcweir {
237cdf0e10cSrcweir 	Sequence< ::rtl::OUString > aSNS( 1 );
238cdf0e10cSrcweir 	aSNS[0] = SERVICE_SDB_DATABASECONTEXT;
239cdf0e10cSrcweir 	return aSNS;
240cdf0e10cSrcweir }
241cdf0e10cSrcweir 
242cdf0e10cSrcweir // XServiceInfo
243cdf0e10cSrcweir //------------------------------------------------------------------------------
getImplementationName()244cdf0e10cSrcweir rtl::OUString ODatabaseContext::getImplementationName(  ) throw(RuntimeException)
245cdf0e10cSrcweir {
246cdf0e10cSrcweir 	return getImplementationName_static();
247cdf0e10cSrcweir }
248cdf0e10cSrcweir 
249cdf0e10cSrcweir //------------------------------------------------------------------------------
supportsService(const::rtl::OUString & _rServiceName)250cdf0e10cSrcweir sal_Bool ODatabaseContext::supportsService( const ::rtl::OUString& _rServiceName ) throw (RuntimeException)
251cdf0e10cSrcweir {
252cdf0e10cSrcweir 	return ::comphelper::findValue(getSupportedServiceNames(), _rServiceName, sal_True).getLength() != 0;
253cdf0e10cSrcweir }
254cdf0e10cSrcweir 
255cdf0e10cSrcweir //------------------------------------------------------------------------------
getSupportedServiceNames()256cdf0e10cSrcweir Sequence< ::rtl::OUString > ODatabaseContext::getSupportedServiceNames(  ) throw (RuntimeException)
257cdf0e10cSrcweir {
258cdf0e10cSrcweir 	return getSupportedServiceNames_static();
259cdf0e10cSrcweir }
260cdf0e10cSrcweir 
261cdf0e10cSrcweir //--------------------------------------------------------------------------
impl_createNewDataSource()262cdf0e10cSrcweir Reference< XInterface > ODatabaseContext::impl_createNewDataSource()
263cdf0e10cSrcweir {
264cdf0e10cSrcweir 	::rtl::Reference<ODatabaseModelImpl> pImpl( new ODatabaseModelImpl( m_aContext.getLegacyServiceFactory(), *this ) );
265cdf0e10cSrcweir     Reference< XDataSource > xDataSource( pImpl->getOrCreateDataSource() );
266cdf0e10cSrcweir 
267cdf0e10cSrcweir     return xDataSource.get();
268cdf0e10cSrcweir }
269cdf0e10cSrcweir 
270cdf0e10cSrcweir //--------------------------------------------------------------------------
createInstance()271cdf0e10cSrcweir Reference< XInterface > SAL_CALL ODatabaseContext::createInstance(  ) throw (Exception, RuntimeException)
272cdf0e10cSrcweir {
273cdf0e10cSrcweir     // for convenience of the API user, we ensure the document is fully initialized (effectively: XLoadable::initNew
274cdf0e10cSrcweir     // has been called at the DatabaseDocument).
275cdf0e10cSrcweir     return impl_createNewDataSource();
276cdf0e10cSrcweir }
277cdf0e10cSrcweir 
278cdf0e10cSrcweir //--------------------------------------------------------------------------
createInstanceWithArguments(const Sequence<Any> & _rArguments)279cdf0e10cSrcweir Reference< XInterface > SAL_CALL ODatabaseContext::createInstanceWithArguments( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException)
280cdf0e10cSrcweir {
281cdf0e10cSrcweir     ::comphelper::NamedValueCollection aArgs( _rArguments );
282cdf0e10cSrcweir     ::rtl::OUString sURL = aArgs.getOrDefault( (::rtl::OUString)INFO_POOLURL, ::rtl::OUString() );
283cdf0e10cSrcweir 
284cdf0e10cSrcweir 	Reference< XInterface > xDataSource;
285cdf0e10cSrcweir     if ( sURL.getLength() )
286cdf0e10cSrcweir         xDataSource = getObject( sURL );
287cdf0e10cSrcweir 
288cdf0e10cSrcweir     if ( !xDataSource.is() )
289cdf0e10cSrcweir 		xDataSource = impl_createNewDataSource();
290cdf0e10cSrcweir 
291cdf0e10cSrcweir 	return xDataSource;
292cdf0e10cSrcweir }
293cdf0e10cSrcweir // DatabaseAccessContext_Base
294cdf0e10cSrcweir //------------------------------------------------------------------------------
disposing()295cdf0e10cSrcweir void ODatabaseContext::disposing()
296cdf0e10cSrcweir {
297cdf0e10cSrcweir 	// notify our listener
298cdf0e10cSrcweir 	com::sun::star::lang::EventObject aDisposeEvent(static_cast< XContainer* >(this));
299cdf0e10cSrcweir 	m_aContainerListeners.disposeAndClear(aDisposeEvent);
300cdf0e10cSrcweir 
301cdf0e10cSrcweir 	// dispose the data sources
302cdf0e10cSrcweir 	ObjectCache::iterator aEnd = m_aDatabaseObjects.end();
303cdf0e10cSrcweir 	for	(	ObjectCache::iterator	aIter = m_aDatabaseObjects.begin();
304cdf0e10cSrcweir 			aIter != aEnd;
305cdf0e10cSrcweir 			++aIter
306cdf0e10cSrcweir 		)
307cdf0e10cSrcweir 	{
308cdf0e10cSrcweir 		aIter->second->dispose();
309cdf0e10cSrcweir 	}
310cdf0e10cSrcweir 	m_aDatabaseObjects.clear();
311cdf0e10cSrcweir }
312cdf0e10cSrcweir 
313cdf0e10cSrcweir // XNamingService
314cdf0e10cSrcweir //------------------------------------------------------------------------------
getRegisteredObject(const rtl::OUString & _rName)315cdf0e10cSrcweir Reference< XInterface >  ODatabaseContext::getRegisteredObject(const rtl::OUString& _rName) throw( Exception, RuntimeException )
316cdf0e10cSrcweir {
317cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
318cdf0e10cSrcweir 	::connectivity::checkDisposed(DatabaseAccessContext_Base::rBHelper.bDisposed);
319cdf0e10cSrcweir 
320cdf0e10cSrcweir     ::rtl::OUString sURL( getDatabaseLocation( _rName ) );
321cdf0e10cSrcweir 
322cdf0e10cSrcweir 	if ( !sURL.getLength() )
323cdf0e10cSrcweir         // there is a registration for this name, but no URL
324cdf0e10cSrcweir 		throw IllegalArgumentException();
325cdf0e10cSrcweir 
326cdf0e10cSrcweir 	// check if URL is already loaded
327cdf0e10cSrcweir 	Reference< XInterface > xExistent = getObject( sURL );
328cdf0e10cSrcweir 	if ( xExistent.is() )
329cdf0e10cSrcweir 		return xExistent;
330cdf0e10cSrcweir 
331cdf0e10cSrcweir 	return loadObjectFromURL( _rName, sURL );
332cdf0e10cSrcweir }
333cdf0e10cSrcweir // -----------------------------------------------------------------------------
loadObjectFromURL(const::rtl::OUString & _rName,const::rtl::OUString & _sURL)334cdf0e10cSrcweir Reference< XInterface > ODatabaseContext::loadObjectFromURL(const ::rtl::OUString& _rName,const ::rtl::OUString& _sURL)
335cdf0e10cSrcweir {
336cdf0e10cSrcweir     INetURLObject aURL( _sURL );
337cdf0e10cSrcweir     if ( aURL.GetProtocol() == INET_PROT_NOT_VALID )
338cdf0e10cSrcweir         throw NoSuchElementException( _rName, *this );
339cdf0e10cSrcweir 
340cdf0e10cSrcweir 	try
341cdf0e10cSrcweir 	{
342cdf0e10cSrcweir 		::ucbhelper::Content aContent( _sURL, NULL );
343cdf0e10cSrcweir 		if ( !aContent.isDocument() )
344cdf0e10cSrcweir 			throw InteractiveIOException(
345cdf0e10cSrcweir                 _sURL, *this, InteractionClassification_ERROR, IOErrorCode_NO_FILE
346cdf0e10cSrcweir             );
347cdf0e10cSrcweir 	}
348cdf0e10cSrcweir 	catch ( const InteractiveIOException& e )
349cdf0e10cSrcweir 	{
350cdf0e10cSrcweir         if  (   ( e.Code == IOErrorCode_NO_FILE )
351cdf0e10cSrcweir             ||  ( e.Code == IOErrorCode_NOT_EXISTING )
352cdf0e10cSrcweir             ||  ( e.Code == IOErrorCode_NOT_EXISTING_PATH )
353cdf0e10cSrcweir             )
354cdf0e10cSrcweir         {
355cdf0e10cSrcweir             // #i40463# #i39187#
356cdf0e10cSrcweir             String sErrorMessage( DBACORE_RESSTRING( RID_STR_FILE_DOES_NOT_EXIST ) );
357cdf0e10cSrcweir             ::svt::OFileNotation aTransformer( _sURL );
358cdf0e10cSrcweir 		    sErrorMessage.SearchAndReplaceAscii( "$file$", aTransformer.get( ::svt::OFileNotation::N_SYSTEM ) );
359cdf0e10cSrcweir 
360cdf0e10cSrcweir             SQLException aError;
361cdf0e10cSrcweir             aError.Message = sErrorMessage;
362cdf0e10cSrcweir 
363cdf0e10cSrcweir             throw WrappedTargetException( _sURL, *this, makeAny( aError ) );
364cdf0e10cSrcweir         }
365cdf0e10cSrcweir 		throw WrappedTargetException( _sURL, *this, ::cppu::getCaughtException() );
366cdf0e10cSrcweir 	}
367cdf0e10cSrcweir 	catch( const Exception& )
368cdf0e10cSrcweir 	{
369cdf0e10cSrcweir         throw WrappedTargetException( _sURL, *this, ::cppu::getCaughtException() );
370cdf0e10cSrcweir 	}
371cdf0e10cSrcweir 
372cdf0e10cSrcweir     OSL_ENSURE( m_aDatabaseObjects.find( _sURL ) == m_aDatabaseObjects.end(),
373cdf0e10cSrcweir         "ODatabaseContext::loadObjectFromURL: not intended for already-cached objects!" );
374cdf0e10cSrcweir 
375cdf0e10cSrcweir     ::rtl::Reference< ODatabaseModelImpl > pModelImpl;
376cdf0e10cSrcweir     {
377cdf0e10cSrcweir 		pModelImpl.set( new ODatabaseModelImpl( _rName, m_aContext.getLegacyServiceFactory(), *this ) );
378cdf0e10cSrcweir 
379cdf0e10cSrcweir 	    Reference< XModel > xModel( pModelImpl->createNewModel_deliverOwnership( false ), UNO_SET_THROW );
380cdf0e10cSrcweir         Reference< XLoadable > xLoad( xModel, UNO_QUERY_THROW );
381cdf0e10cSrcweir 
382cdf0e10cSrcweir         ::comphelper::NamedValueCollection aArgs;
383cdf0e10cSrcweir         aArgs.put( "URL", _sURL );
384cdf0e10cSrcweir         aArgs.put( "MacroExecutionMode", MacroExecMode::USE_CONFIG );
385cdf0e10cSrcweir         aArgs.put( "InteractionHandler", m_aContext.createComponent( "com.sun.star.task.InteractionHandler" ) );
386cdf0e10cSrcweir 
387cdf0e10cSrcweir         Sequence< PropertyValue > aResource( aArgs.getPropertyValues() );
388cdf0e10cSrcweir         xLoad->load( aResource );
389cdf0e10cSrcweir         xModel->attachResource( _sURL, aResource );
390cdf0e10cSrcweir 
391cdf0e10cSrcweir         ::utl::CloseableComponent aEnsureClose( xModel );
392cdf0e10cSrcweir     }
393cdf0e10cSrcweir 
394cdf0e10cSrcweir     setTransientProperties( _sURL, *pModelImpl );
395cdf0e10cSrcweir 
396cdf0e10cSrcweir     return pModelImpl->getOrCreateDataSource().get();
397cdf0e10cSrcweir }
398cdf0e10cSrcweir // -----------------------------------------------------------------------------
appendAtTerminateListener(const ODatabaseModelImpl & _rDataSourceModel)399cdf0e10cSrcweir void ODatabaseContext::appendAtTerminateListener(const ODatabaseModelImpl& _rDataSourceModel)
400cdf0e10cSrcweir {
401cdf0e10cSrcweir     m_pDatabaseDocumentLoader->append(_rDataSourceModel);
402cdf0e10cSrcweir }
403cdf0e10cSrcweir // -----------------------------------------------------------------------------
removeFromTerminateListener(const ODatabaseModelImpl & _rDataSourceModel)404cdf0e10cSrcweir void ODatabaseContext::removeFromTerminateListener(const ODatabaseModelImpl& _rDataSourceModel)
405cdf0e10cSrcweir {
406cdf0e10cSrcweir     m_pDatabaseDocumentLoader->remove(_rDataSourceModel);
407cdf0e10cSrcweir }
408cdf0e10cSrcweir // -----------------------------------------------------------------------------
setTransientProperties(const::rtl::OUString & _sURL,ODatabaseModelImpl & _rDataSourceModel)409cdf0e10cSrcweir void ODatabaseContext::setTransientProperties(const ::rtl::OUString& _sURL, ODatabaseModelImpl& _rDataSourceModel )
410cdf0e10cSrcweir {
411cdf0e10cSrcweir 	if ( m_aDatasourceProperties.end() == m_aDatasourceProperties.find(_sURL) )
412cdf0e10cSrcweir         return;
413cdf0e10cSrcweir     try
414cdf0e10cSrcweir     {
415cdf0e10cSrcweir         ::rtl::OUString sAuthFailedPassword;
416cdf0e10cSrcweir 	    Reference< XPropertySet > xDSProps( _rDataSourceModel.getOrCreateDataSource(), UNO_QUERY_THROW );
417cdf0e10cSrcweir 		const Sequence< PropertyValue >& rSessionPersistentProps = m_aDatasourceProperties[_sURL];
418cdf0e10cSrcweir 		const PropertyValue* pProp = rSessionPersistentProps.getConstArray();
419cdf0e10cSrcweir 		const PropertyValue* pPropsEnd = rSessionPersistentProps.getConstArray() + rSessionPersistentProps.getLength();
420cdf0e10cSrcweir         for ( ; pProp != pPropsEnd; ++pProp )
421cdf0e10cSrcweir         {
422cdf0e10cSrcweir             if ( pProp->Name.equalsAscii( "AuthFailedPassword" ) )
423cdf0e10cSrcweir             {
424cdf0e10cSrcweir                 OSL_VERIFY( pProp->Value >>= sAuthFailedPassword );
425cdf0e10cSrcweir             }
426cdf0e10cSrcweir             else
427cdf0e10cSrcweir             {
428cdf0e10cSrcweir 				xDSProps->setPropertyValue( pProp->Name, pProp->Value );
429cdf0e10cSrcweir             }
430cdf0e10cSrcweir 		}
431cdf0e10cSrcweir 
432cdf0e10cSrcweir         _rDataSourceModel.m_sFailedPassword = sAuthFailedPassword;
433cdf0e10cSrcweir     }
434cdf0e10cSrcweir     catch( const Exception& )
435cdf0e10cSrcweir     {
436cdf0e10cSrcweir     	DBG_UNHANDLED_EXCEPTION();
437cdf0e10cSrcweir     }
438cdf0e10cSrcweir }
439cdf0e10cSrcweir 
440cdf0e10cSrcweir //------------------------------------------------------------------------------
registerObject(const rtl::OUString & _rName,const Reference<XInterface> & _rxObject)441cdf0e10cSrcweir void ODatabaseContext::registerObject(const rtl::OUString& _rName, const Reference< XInterface > & _rxObject) throw( Exception, RuntimeException )
442cdf0e10cSrcweir {
443cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
444cdf0e10cSrcweir 	::connectivity::checkDisposed(DatabaseAccessContext_Base::rBHelper.bDisposed);
445cdf0e10cSrcweir 
446cdf0e10cSrcweir 	if ( !_rName.getLength() )
447cdf0e10cSrcweir 		throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
448cdf0e10cSrcweir 
449cdf0e10cSrcweir     Reference< XDocumentDataSource > xDocDataSource( _rxObject, UNO_QUERY );
450cdf0e10cSrcweir 	Reference< XModel > xModel( xDocDataSource.is() ? xDocDataSource->getDatabaseDocument() : Reference< XOfficeDatabaseDocument >(), UNO_QUERY );
451cdf0e10cSrcweir     if ( !xModel.is() )
452cdf0e10cSrcweir 		throw IllegalArgumentException( ::rtl::OUString(), *this, 2 );
453cdf0e10cSrcweir 
454cdf0e10cSrcweir 	::rtl::OUString sURL = xModel->getURL();
455cdf0e10cSrcweir 	if ( !sURL.getLength() )
456cdf0e10cSrcweir 		throw IllegalArgumentException( DBACORE_RESSTRING( RID_STR_DATASOURCE_NOT_STORED ), *this, 2 );
457cdf0e10cSrcweir 
458cdf0e10cSrcweir     registerDatabaseLocation( _rName, sURL );
459cdf0e10cSrcweir 
460cdf0e10cSrcweir     ODatabaseSource::setName( xDocDataSource, _rName, ODatabaseSource::DBContextAccess() );
461cdf0e10cSrcweir 
462cdf0e10cSrcweir 	// notify our container listeners
463cdf0e10cSrcweir 	ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(_rName), makeAny(_rxObject), Any());
464cdf0e10cSrcweir     m_aContainerListeners.notifyEach( &XContainerListener::elementInserted, aEvent );
465cdf0e10cSrcweir }
466cdf0e10cSrcweir 
467cdf0e10cSrcweir //------------------------------------------------------------------------------
storeTransientProperties(ODatabaseModelImpl & _rModelImpl)468cdf0e10cSrcweir void ODatabaseContext::storeTransientProperties( ODatabaseModelImpl& _rModelImpl)
469cdf0e10cSrcweir {
470cdf0e10cSrcweir     Reference< XPropertySet > xSource( _rModelImpl.getOrCreateDataSource(), UNO_QUERY );
471cdf0e10cSrcweir     ::comphelper::NamedValueCollection aRememberProps;
472cdf0e10cSrcweir 
473cdf0e10cSrcweir 	try
474cdf0e10cSrcweir 	{
475cdf0e10cSrcweir 		// get the info about the properties, check which ones are transient and not readonly
476cdf0e10cSrcweir 		Reference< XPropertySetInfo > xSetInfo;
477cdf0e10cSrcweir 		if (xSource.is())
478cdf0e10cSrcweir 			xSetInfo = xSource->getPropertySetInfo();
479cdf0e10cSrcweir 		Sequence< Property > aProperties;
480cdf0e10cSrcweir 		if (xSetInfo.is())
481cdf0e10cSrcweir 			aProperties = xSetInfo->getProperties();
482cdf0e10cSrcweir 
483cdf0e10cSrcweir 		if (aProperties.getLength())
484cdf0e10cSrcweir 		{
485cdf0e10cSrcweir 			const Property* pProperties = aProperties.getConstArray();
486cdf0e10cSrcweir 			for ( sal_Int32 i=0; i<aProperties.getLength(); ++i, ++pProperties )
487cdf0e10cSrcweir 			{
488cdf0e10cSrcweir 				if	(	( ( pProperties->Attributes & PropertyAttribute::TRANSIENT) != 0 )
489cdf0e10cSrcweir 					&&	( ( pProperties->Attributes & PropertyAttribute::READONLY) == 0 )
490cdf0e10cSrcweir 					)
491cdf0e10cSrcweir 				{
492cdf0e10cSrcweir 					// found such a property
493cdf0e10cSrcweir                     aRememberProps.put( pProperties->Name, xSource->getPropertyValue( pProperties->Name ) );
494cdf0e10cSrcweir 				}
495cdf0e10cSrcweir 			}
496cdf0e10cSrcweir 		}
497cdf0e10cSrcweir 	}
498cdf0e10cSrcweir 	catch ( const Exception& )
499cdf0e10cSrcweir 	{
500cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
501cdf0e10cSrcweir 	}
502cdf0e10cSrcweir 
503cdf0e10cSrcweir     // additionally, remember the "failed password", which is not available as property
504cdf0e10cSrcweir     // #i86178# / 2008-02-19 / frank.schoenheit@sun.com
505cdf0e10cSrcweir     aRememberProps.put( "AuthFailedPassword", _rModelImpl.m_sFailedPassword );
506cdf0e10cSrcweir 
507cdf0e10cSrcweir     ::rtl::OUString sDocumentURL( _rModelImpl.getURL() );
508cdf0e10cSrcweir     if ( m_aDatabaseObjects.find( sDocumentURL ) != m_aDatabaseObjects.end() )
509cdf0e10cSrcweir     {
510cdf0e10cSrcweir 	    m_aDatasourceProperties[ sDocumentURL ] = aRememberProps.getPropertyValues();
511cdf0e10cSrcweir     }
512cdf0e10cSrcweir     else if ( m_aDatabaseObjects.find( _rModelImpl.m_sName ) != m_aDatabaseObjects.end() )
513cdf0e10cSrcweir     {
514cdf0e10cSrcweir         OSL_ENSURE( false, "ODatabaseContext::storeTransientProperties: a database document register by name? This shouldn't happen anymore!" );
515cdf0e10cSrcweir             // all the code should have been changed so that registration is by URL only
516cdf0e10cSrcweir 	    m_aDatasourceProperties[ _rModelImpl.m_sName ] = aRememberProps.getPropertyValues();
517cdf0e10cSrcweir     }
518cdf0e10cSrcweir     else
519cdf0e10cSrcweir     {
520cdf0e10cSrcweir         OSL_ENSURE( ( sDocumentURL.getLength() == 0 ) && ( _rModelImpl.m_sName.getLength() == 0 ),
521cdf0e10cSrcweir             "ODatabaseContext::storeTransientProperties: a non-empty data source which I do not know?!" );
522cdf0e10cSrcweir     }
523cdf0e10cSrcweir }
524cdf0e10cSrcweir 
525cdf0e10cSrcweir //------------------------------------------------------------------------------
addContainerListener(const Reference<XContainerListener> & _rxListener)526cdf0e10cSrcweir void SAL_CALL ODatabaseContext::addContainerListener( const Reference< XContainerListener >& _rxListener ) throw(RuntimeException)
527cdf0e10cSrcweir {
528cdf0e10cSrcweir 	m_aContainerListeners.addInterface(_rxListener);
529cdf0e10cSrcweir }
530cdf0e10cSrcweir 
531cdf0e10cSrcweir //------------------------------------------------------------------------------
removeContainerListener(const Reference<XContainerListener> & _rxListener)532cdf0e10cSrcweir void SAL_CALL ODatabaseContext::removeContainerListener( const Reference< XContainerListener >& _rxListener ) throw(RuntimeException)
533cdf0e10cSrcweir {
534cdf0e10cSrcweir 	m_aContainerListeners.removeInterface(_rxListener);
535cdf0e10cSrcweir }
536cdf0e10cSrcweir 
537cdf0e10cSrcweir //------------------------------------------------------------------------------
revokeObject(const rtl::OUString & _rName)538cdf0e10cSrcweir void ODatabaseContext::revokeObject(const rtl::OUString& _rName) throw( Exception, RuntimeException )
539cdf0e10cSrcweir {
540cdf0e10cSrcweir 	ClearableMutexGuard aGuard(m_aMutex);
541cdf0e10cSrcweir     ::connectivity::checkDisposed(DatabaseAccessContext_Base::rBHelper.bDisposed);
542cdf0e10cSrcweir 
543cdf0e10cSrcweir     ::rtl::OUString sURL = getDatabaseLocation( _rName );
544cdf0e10cSrcweir 
545cdf0e10cSrcweir     revokeDatabaseLocation( _rName );
546cdf0e10cSrcweir         // will throw if something goes wrong
547cdf0e10cSrcweir 
548cdf0e10cSrcweir     if ( m_aDatabaseObjects.find( _rName ) != m_aDatabaseObjects.end() )
549cdf0e10cSrcweir     {
550cdf0e10cSrcweir         m_aDatasourceProperties[ sURL ] = m_aDatasourceProperties[ _rName ];
551cdf0e10cSrcweir     }
552cdf0e10cSrcweir 
553cdf0e10cSrcweir 	// check if URL is already loaded
554cdf0e10cSrcweir 	ObjectCacheIterator aExistent = m_aDatabaseObjects.find( sURL );
555cdf0e10cSrcweir 	if ( aExistent != m_aDatabaseObjects.end() )
556cdf0e10cSrcweir 		m_aDatabaseObjects.erase( aExistent );
557cdf0e10cSrcweir 
558cdf0e10cSrcweir 	// notify our container listeners
559cdf0e10cSrcweir 	ContainerEvent aEvent( *this, makeAny( _rName ), Any(), Any() );
560cdf0e10cSrcweir     aGuard.clear();
561cdf0e10cSrcweir     m_aContainerListeners.notifyEach( &XContainerListener::elementRemoved, aEvent );
562cdf0e10cSrcweir }
563cdf0e10cSrcweir 
564cdf0e10cSrcweir //------------------------------------------------------------------------------
hasRegisteredDatabase(const::rtl::OUString & _Name)565cdf0e10cSrcweir ::sal_Bool SAL_CALL ODatabaseContext::hasRegisteredDatabase( const ::rtl::OUString& _Name ) throw (IllegalArgumentException, RuntimeException)
566cdf0e10cSrcweir {
567cdf0e10cSrcweir     return m_xDatabaseRegistrations->hasRegisteredDatabase( _Name );
568cdf0e10cSrcweir }
569cdf0e10cSrcweir 
570cdf0e10cSrcweir //------------------------------------------------------------------------------
getRegistrationNames()571cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL ODatabaseContext::getRegistrationNames() throw (RuntimeException)
572cdf0e10cSrcweir {
573cdf0e10cSrcweir     return m_xDatabaseRegistrations->getRegistrationNames();
574cdf0e10cSrcweir }
575cdf0e10cSrcweir 
576cdf0e10cSrcweir //------------------------------------------------------------------------------
getDatabaseLocation(const::rtl::OUString & _Name)577cdf0e10cSrcweir ::rtl::OUString SAL_CALL ODatabaseContext::getDatabaseLocation( const ::rtl::OUString& _Name ) throw (IllegalArgumentException, NoSuchElementException, RuntimeException)
578cdf0e10cSrcweir {
579cdf0e10cSrcweir     return m_xDatabaseRegistrations->getDatabaseLocation( _Name );
580cdf0e10cSrcweir }
581cdf0e10cSrcweir 
582cdf0e10cSrcweir //------------------------------------------------------------------------------
registerDatabaseLocation(const::rtl::OUString & _Name,const::rtl::OUString & _Location)583cdf0e10cSrcweir void SAL_CALL ODatabaseContext::registerDatabaseLocation( const ::rtl::OUString& _Name, const ::rtl::OUString& _Location ) throw (IllegalArgumentException, ElementExistException, RuntimeException)
584cdf0e10cSrcweir {
585cdf0e10cSrcweir     m_xDatabaseRegistrations->registerDatabaseLocation( _Name, _Location );
586cdf0e10cSrcweir }
587cdf0e10cSrcweir 
588cdf0e10cSrcweir //------------------------------------------------------------------------------
revokeDatabaseLocation(const::rtl::OUString & _Name)589cdf0e10cSrcweir void SAL_CALL ODatabaseContext::revokeDatabaseLocation( const ::rtl::OUString& _Name ) throw (IllegalArgumentException, NoSuchElementException, IllegalAccessException, RuntimeException)
590cdf0e10cSrcweir {
591cdf0e10cSrcweir     m_xDatabaseRegistrations->revokeDatabaseLocation( _Name );
592cdf0e10cSrcweir }
593cdf0e10cSrcweir 
594cdf0e10cSrcweir //------------------------------------------------------------------------------
changeDatabaseLocation(const::rtl::OUString & _Name,const::rtl::OUString & _NewLocation)595cdf0e10cSrcweir void SAL_CALL ODatabaseContext::changeDatabaseLocation( const ::rtl::OUString& _Name, const ::rtl::OUString& _NewLocation ) throw (IllegalArgumentException, NoSuchElementException, IllegalAccessException, RuntimeException)
596cdf0e10cSrcweir {
597cdf0e10cSrcweir     m_xDatabaseRegistrations->changeDatabaseLocation( _Name, _NewLocation );
598cdf0e10cSrcweir }
599cdf0e10cSrcweir 
600cdf0e10cSrcweir //------------------------------------------------------------------------------
isDatabaseRegistrationReadOnly(const::rtl::OUString & _Name)601cdf0e10cSrcweir ::sal_Bool SAL_CALL ODatabaseContext::isDatabaseRegistrationReadOnly( const ::rtl::OUString& _Name ) throw (IllegalArgumentException, NoSuchElementException, RuntimeException)
602cdf0e10cSrcweir {
603cdf0e10cSrcweir     return m_xDatabaseRegistrations->isDatabaseRegistrationReadOnly( _Name );
604cdf0e10cSrcweir }
605cdf0e10cSrcweir 
606cdf0e10cSrcweir //------------------------------------------------------------------------------
addDatabaseRegistrationsListener(const Reference<XDatabaseRegistrationsListener> & _Listener)607cdf0e10cSrcweir void SAL_CALL ODatabaseContext::addDatabaseRegistrationsListener( const Reference< XDatabaseRegistrationsListener >& _Listener ) throw (RuntimeException)
608cdf0e10cSrcweir {
609cdf0e10cSrcweir     m_xDatabaseRegistrations->addDatabaseRegistrationsListener( _Listener );
610cdf0e10cSrcweir }
611cdf0e10cSrcweir 
612cdf0e10cSrcweir //------------------------------------------------------------------------------
removeDatabaseRegistrationsListener(const Reference<XDatabaseRegistrationsListener> & _Listener)613cdf0e10cSrcweir void SAL_CALL ODatabaseContext::removeDatabaseRegistrationsListener( const Reference< XDatabaseRegistrationsListener >& _Listener ) throw (RuntimeException)
614cdf0e10cSrcweir {
615cdf0e10cSrcweir     m_xDatabaseRegistrations->removeDatabaseRegistrationsListener( _Listener );
616cdf0e10cSrcweir }
617cdf0e10cSrcweir 
618cdf0e10cSrcweir // ::com::sun::star::container::XElementAccess
619cdf0e10cSrcweir //------------------------------------------------------------------------------
getElementType()620cdf0e10cSrcweir Type ODatabaseContext::getElementType(  ) throw(RuntimeException)
621cdf0e10cSrcweir {
622cdf0e10cSrcweir 	return::getCppuType(static_cast<Reference<XDataSource>*>(NULL));
623cdf0e10cSrcweir }
624cdf0e10cSrcweir 
625cdf0e10cSrcweir //------------------------------------------------------------------------------
hasElements(void)626cdf0e10cSrcweir sal_Bool ODatabaseContext::hasElements(void) throw( RuntimeException )
627cdf0e10cSrcweir {
628cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
629cdf0e10cSrcweir 	::connectivity::checkDisposed(DatabaseAccessContext_Base::rBHelper.bDisposed);
630cdf0e10cSrcweir 
631cdf0e10cSrcweir 	return 0 != getElementNames().getLength();
632cdf0e10cSrcweir }
633cdf0e10cSrcweir 
634cdf0e10cSrcweir // ::com::sun::star::container::XEnumerationAccess
635cdf0e10cSrcweir //------------------------------------------------------------------------------
createEnumeration(void)636cdf0e10cSrcweir Reference< ::com::sun::star::container::XEnumeration >  ODatabaseContext::createEnumeration(void) throw( RuntimeException )
637cdf0e10cSrcweir {
638cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
639cdf0e10cSrcweir 	return new ::comphelper::OEnumerationByName(static_cast<XNameAccess*>(this));
640cdf0e10cSrcweir }
641cdf0e10cSrcweir 
642cdf0e10cSrcweir // ::com::sun::star::container::XNameAccess
643cdf0e10cSrcweir //------------------------------------------------------------------------------
getByName(const rtl::OUString & _rName)644cdf0e10cSrcweir Any ODatabaseContext::getByName(const rtl::OUString& _rName) throw( NoSuchElementException,
645cdf0e10cSrcweir 														  WrappedTargetException, RuntimeException )
646cdf0e10cSrcweir {
647cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
648cdf0e10cSrcweir 	::connectivity::checkDisposed(DatabaseAccessContext_Base::rBHelper.bDisposed);
649cdf0e10cSrcweir 	if ( !_rName.getLength() )
650cdf0e10cSrcweir 		throw NoSuchElementException(_rName, *this);
651cdf0e10cSrcweir 
652cdf0e10cSrcweir 	try
653cdf0e10cSrcweir 	{
654cdf0e10cSrcweir 		Reference< XInterface > xExistent = getObject( _rName );
655cdf0e10cSrcweir 		if ( xExistent.is() )
656cdf0e10cSrcweir 			return makeAny( xExistent );
657cdf0e10cSrcweir 
658cdf0e10cSrcweir         // see whether this is an registered name
659cdf0e10cSrcweir         ::rtl::OUString sURL;
660cdf0e10cSrcweir         if ( hasRegisteredDatabase( _rName ) )
661cdf0e10cSrcweir         {
662cdf0e10cSrcweir             sURL = getDatabaseLocation( _rName );
663cdf0e10cSrcweir             // is the object cached under its URL?
664cdf0e10cSrcweir 	        xExistent = getObject( sURL );
665cdf0e10cSrcweir         }
666cdf0e10cSrcweir         else
667cdf0e10cSrcweir             // interpret the name as URL
668cdf0e10cSrcweir             sURL = _rName;
669cdf0e10cSrcweir 
670cdf0e10cSrcweir         if ( !xExistent.is() )
671cdf0e10cSrcweir 		    // try to load this as URL
672cdf0e10cSrcweir             xExistent = loadObjectFromURL( _rName, sURL );
673cdf0e10cSrcweir 		return makeAny( xExistent );
674cdf0e10cSrcweir 	}
675cdf0e10cSrcweir 	catch (NoSuchElementException&)
676cdf0e10cSrcweir 	{	// let these exceptions through
677cdf0e10cSrcweir 		throw;
678cdf0e10cSrcweir 	}
679cdf0e10cSrcweir 	catch (WrappedTargetException&)
680cdf0e10cSrcweir 	{	// let these exceptions through
681cdf0e10cSrcweir 		throw;
682cdf0e10cSrcweir 	}
683cdf0e10cSrcweir 	catch (RuntimeException&)
684cdf0e10cSrcweir 	{	// let these exceptions through
685cdf0e10cSrcweir 		throw;
686cdf0e10cSrcweir 	}
687cdf0e10cSrcweir 	catch (Exception& e)
688cdf0e10cSrcweir 	{	// exceptions other than the speciafied ones -> wrap
689cdf0e10cSrcweir         Any aError = ::cppu::getCaughtException();
690cdf0e10cSrcweir 		throw WrappedTargetException(_rName, *this, aError );
691cdf0e10cSrcweir 	}
692cdf0e10cSrcweir }
693cdf0e10cSrcweir 
694cdf0e10cSrcweir //------------------------------------------------------------------------------
getElementNames(void)695cdf0e10cSrcweir Sequence< rtl::OUString > ODatabaseContext::getElementNames(void) throw( RuntimeException )
696cdf0e10cSrcweir {
697cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
698cdf0e10cSrcweir     ::connectivity::checkDisposed(DatabaseAccessContext_Base::rBHelper.bDisposed);
699cdf0e10cSrcweir 
700cdf0e10cSrcweir     return getRegistrationNames();
701cdf0e10cSrcweir }
702cdf0e10cSrcweir 
703cdf0e10cSrcweir //------------------------------------------------------------------------------
hasByName(const rtl::OUString & _rName)704cdf0e10cSrcweir sal_Bool ODatabaseContext::hasByName(const rtl::OUString& _rName) throw( RuntimeException )
705cdf0e10cSrcweir {
706cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
707cdf0e10cSrcweir 	::connectivity::checkDisposed(DatabaseAccessContext_Base::rBHelper.bDisposed);
708cdf0e10cSrcweir 
709cdf0e10cSrcweir     return hasRegisteredDatabase( _rName );
710cdf0e10cSrcweir }
711cdf0e10cSrcweir 
712cdf0e10cSrcweir // -----------------------------------------------------------------------------
getObject(const::rtl::OUString & _rURL)713cdf0e10cSrcweir Reference< XInterface > ODatabaseContext::getObject( const ::rtl::OUString& _rURL )
714cdf0e10cSrcweir {
715cdf0e10cSrcweir 	ObjectCacheIterator aFind = m_aDatabaseObjects.find( _rURL );
716cdf0e10cSrcweir 	Reference< XInterface > xExistent;
717cdf0e10cSrcweir 	if ( aFind != m_aDatabaseObjects.end() )
718cdf0e10cSrcweir 		xExistent = aFind->second->getOrCreateDataSource();
719cdf0e10cSrcweir 	return xExistent;
720cdf0e10cSrcweir }
721cdf0e10cSrcweir // -----------------------------------------------------------------------------
registerDatabaseDocument(ODatabaseModelImpl & _rModelImpl)722cdf0e10cSrcweir void ODatabaseContext::registerDatabaseDocument( ODatabaseModelImpl& _rModelImpl )
723cdf0e10cSrcweir {
724cdf0e10cSrcweir     ::rtl::OUString sURL( _rModelImpl.getURL() );
725cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
726cdf0e10cSrcweir     OSL_TRACE( "DatabaseContext: registering %s", ::rtl::OUStringToOString( sURL, RTL_TEXTENCODING_UTF8 ).getStr() );
727cdf0e10cSrcweir #endif
728cdf0e10cSrcweir 	if ( m_aDatabaseObjects.find( sURL ) == m_aDatabaseObjects.end() )
729cdf0e10cSrcweir 	{
730cdf0e10cSrcweir 		m_aDatabaseObjects[ sURL ] = &_rModelImpl;
731cdf0e10cSrcweir         setTransientProperties( sURL, _rModelImpl );
732cdf0e10cSrcweir 	}
733cdf0e10cSrcweir     else
734cdf0e10cSrcweir         OSL_ENSURE( false, "ODatabaseContext::registerDatabaseDocument: already have an object registered for this URL!" );
735cdf0e10cSrcweir }
736cdf0e10cSrcweir // -----------------------------------------------------------------------------
revokeDatabaseDocument(const ODatabaseModelImpl & _rModelImpl)737cdf0e10cSrcweir void ODatabaseContext::revokeDatabaseDocument( const ODatabaseModelImpl& _rModelImpl )
738cdf0e10cSrcweir {
739cdf0e10cSrcweir     ::rtl::OUString sURL( _rModelImpl.getURL() );
740cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
741cdf0e10cSrcweir     OSL_TRACE( "DatabaseContext: deregistering %s", ::rtl::OUStringToOString( sURL, RTL_TEXTENCODING_UTF8 ).getStr() );
742cdf0e10cSrcweir #endif
743cdf0e10cSrcweir 	m_aDatabaseObjects.erase( sURL );
744cdf0e10cSrcweir }
745cdf0e10cSrcweir // -----------------------------------------------------------------------------
databaseDocumentURLChange(const::rtl::OUString & _rOldURL,const::rtl::OUString & _rNewURL)746cdf0e10cSrcweir void ODatabaseContext::databaseDocumentURLChange( const ::rtl::OUString& _rOldURL, const ::rtl::OUString& _rNewURL )
747cdf0e10cSrcweir {
748cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
749cdf0e10cSrcweir     OSL_TRACE( "DatabaseContext: changing registration from %s to %s",
750cdf0e10cSrcweir         ::rtl::OUStringToOString( _rOldURL, RTL_TEXTENCODING_UTF8 ).getStr(),
751cdf0e10cSrcweir         ::rtl::OUStringToOString( _rNewURL, RTL_TEXTENCODING_UTF8 ).getStr() );
752cdf0e10cSrcweir #endif
753cdf0e10cSrcweir     ObjectCache::iterator oldPos = m_aDatabaseObjects.find( _rOldURL );
754cdf0e10cSrcweir     ENSURE_OR_THROW( oldPos != m_aDatabaseObjects.end(), "illegal old database document URL" );
755cdf0e10cSrcweir     ObjectCache::iterator newPos = m_aDatabaseObjects.find( _rNewURL );
756cdf0e10cSrcweir     ENSURE_OR_THROW( newPos == m_aDatabaseObjects.end(), "illegal new database document URL" );
757cdf0e10cSrcweir 
758cdf0e10cSrcweir     m_aDatabaseObjects[ _rNewURL ] = oldPos->second;
759cdf0e10cSrcweir     m_aDatabaseObjects.erase( oldPos );
760cdf0e10cSrcweir }
761cdf0e10cSrcweir // -----------------------------------------------------------------------------
getSomething(const Sequence<sal_Int8> & rId)762cdf0e10cSrcweir sal_Int64 SAL_CALL ODatabaseContext::getSomething( const Sequence< sal_Int8 >& rId ) throw(RuntimeException)
763cdf0e10cSrcweir {
764cdf0e10cSrcweir 	if (rId.getLength() == 16 && 0 == rtl_compareMemory(getUnoTunnelImplementationId().getConstArray(),  rId.getConstArray(), 16 ) )
765cdf0e10cSrcweir 		return reinterpret_cast<sal_Int64>(this);
766cdf0e10cSrcweir 
767cdf0e10cSrcweir 	return 0;
768cdf0e10cSrcweir }
769cdf0e10cSrcweir // -----------------------------------------------------------------------------
getUnoTunnelImplementationId()770cdf0e10cSrcweir Sequence< sal_Int8 > ODatabaseContext::getUnoTunnelImplementationId()
771cdf0e10cSrcweir {
772cdf0e10cSrcweir 	static ::cppu::OImplementationId * pId = 0;
773cdf0e10cSrcweir 	if (! pId)
774cdf0e10cSrcweir 	{
775cdf0e10cSrcweir 		::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
776cdf0e10cSrcweir 		if (! pId)
777cdf0e10cSrcweir 		{
778cdf0e10cSrcweir 			static ::cppu::OImplementationId aId;
779cdf0e10cSrcweir 			pId = &aId;
780cdf0e10cSrcweir 		}
781cdf0e10cSrcweir 	}
782cdf0e10cSrcweir 	return pId->getImplementationId();
783cdf0e10cSrcweir }
784cdf0e10cSrcweir 
785cdf0e10cSrcweir // -----------------------------------------------------------------------------
onBasicManagerCreated(const Reference<XModel> & _rxForDocument,BasicManager & _rBasicManager)786cdf0e10cSrcweir void ODatabaseContext::onBasicManagerCreated( const Reference< XModel >& _rxForDocument, BasicManager& _rBasicManager )
787cdf0e10cSrcweir {
788cdf0e10cSrcweir     // if it's a database document ...
789cdf0e10cSrcweir     Reference< XOfficeDatabaseDocument > xDatabaseDocument( _rxForDocument, UNO_QUERY );
790cdf0e10cSrcweir     // ... or a sub document of a database document ...
791cdf0e10cSrcweir     if ( !xDatabaseDocument.is() )
792cdf0e10cSrcweir     {
793cdf0e10cSrcweir         Reference< XChild > xDocAsChild( _rxForDocument, UNO_QUERY );
794cdf0e10cSrcweir         if ( xDocAsChild.is() )
795cdf0e10cSrcweir             xDatabaseDocument.set( xDocAsChild->getParent(), UNO_QUERY );
796cdf0e10cSrcweir     }
797cdf0e10cSrcweir 
798cdf0e10cSrcweir     // ... whose BasicManager has just been created, then add the global DatabaseDocument variable to its scope.
799cdf0e10cSrcweir     if ( xDatabaseDocument.is() )
800cdf0e10cSrcweir         _rBasicManager.SetGlobalUNOConstant( "ThisDatabaseDocument", makeAny( xDatabaseDocument ) );
801cdf0e10cSrcweir }
802cdf0e10cSrcweir 
803cdf0e10cSrcweir //........................................................................
804cdf0e10cSrcweir }	// namespace dbaccess
805cdf0e10cSrcweir //........................................................................
806