1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _DBA_COREDATAACCESS_DATABASECONTEXT_HXX_
25 #define _DBA_COREDATAACCESS_DATABASECONTEXT_HXX_
26 
27 #include "ModelImpl.hxx"
28 
29 /** === begin UNO includes === **/
30 #include <com/sun/star/container/ElementExistException.hpp>
31 #include <com/sun/star/container/XContainer.hpp>
32 #include <com/sun/star/container/XEnumerationAccess.hpp>
33 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
34 #include <com/sun/star/container/XNameAccess.hpp>
35 #include <com/sun/star/lang/XEventListener.hpp>
36 #include <com/sun/star/lang/XServiceInfo.hpp>
37 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
38 #include <com/sun/star/lang/XUnoTunnel.hpp>
39 #include <com/sun/star/sdb/XDatabaseEnvironment.hpp>
40 #include <com/sun/star/sdb/XDatabaseRegistrations.hpp>
41 #include <com/sun/star/uno/XNamingService.hpp>
42 #include <com/sun/star/uno/XAggregation.hpp>
43 /** === end UNO includes === **/
44 
45 #include <basic/basicmanagerrepository.hxx>
46 #include <comphelper/componentcontext.hxx>
47 #include <comphelper/stl_types.hxx>
48 #include <cppuhelper/compbase8.hxx>
49 #include <cppuhelper/interfacecontainer.hxx>
50 
51 #include <boost/shared_ptr.hpp>
52 
53 // needed for registration
54 namespace com { namespace sun { namespace star {
55 	namespace lang
56 	{
57 		class XMultiServiceFactory;
58 		class IllegalArgumentException;
59 	}
60 } } }
61 
62 //........................................................................
63 namespace dbaccess
64 {
65 //........................................................................
66 class DatabaseDocumentLoader;
67 //============================================================
68 //= ODatabaseContext
69 //============================================================
70 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
71 	ODatabaseContext_CreateInstance(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&);
72 
73 typedef ::cppu::WeakComponentImplHelper8	<	::com::sun::star::lang::XServiceInfo
74 											,	::com::sun::star::container::XEnumerationAccess
75 											,	::com::sun::star::container::XNameAccess
76 											,	::com::sun::star::uno::XNamingService
77 											,	::com::sun::star::container::XContainer
78 											,	::com::sun::star::lang::XSingleServiceFactory
79 											,	::com::sun::star::lang::XUnoTunnel
80 											,	::com::sun::star::sdb::XDatabaseRegistrations
81 											>	DatabaseAccessContext_Base;
82 
83 class ODatabaseContext  :public DatabaseAccessContext_Base
84                         ,public ::basic::BasicManagerCreationListener
85 {
86 private:
87     /** loads the given object from the given URL
88     @throws WrappedTargetException
89         if an error occurs accessing the URL via the UCB
90 
91     */
92 	::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > loadObjectFromURL(const ::rtl::OUString& _rName,const ::rtl::OUString& _sURL);
93 	::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > getObject( const ::rtl::OUString& _rURL );
94 
95     /** sets all properties which were transient at the data source. e.g. password
96         @param  _sURL       The file URL of the data source
97         @param  _xObject    The data source itself.
98     */
99     void setTransientProperties(const ::rtl::OUString& _sURL, ODatabaseModelImpl& _rDataSourceModel );
100 
101     /** creates a new data source
102     */
103     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
104             impl_createNewDataSource();
105 
106 protected:
107 	::osl::Mutex	                m_aMutex;
108     ::comphelper::ComponentContext  m_aContext;
109 
110     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XAggregation >
111                                     m_xDBRegistrationAggregate;
112     ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XDatabaseRegistrations >
113                                     m_xDatabaseRegistrations;
114 
115 	DECLARE_STL_USTRINGACCESS_MAP( ODatabaseModelImpl*, ObjectCache );
116 	ObjectCache		m_aDatabaseObjects;
117 
118 	DECLARE_STL_USTRINGACCESS_MAP( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >, PropertyCache );
119 	PropertyCache	m_aDatasourceProperties;
120 		// as we hold our data sources weak, we have to cache all properties on the data sources which are
121 		// transient but stored as long as the session lasts. The database context is the session (as it lives
122 		// as long as the session does), but the data sources may die before the session does, and then be
123 		// recreated afterwards. So it's our (the context's) responsibility to store the session-persistent
124 		// properties.
125 
126 	::cppu::OInterfaceContainerHelper		m_aContainerListeners;
127     DatabaseDocumentLoader*                 m_pDatabaseDocumentLoader;
128 
129 public:
130 	ODatabaseContext( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& );
131 	virtual ~ODatabaseContext();
132 
133 
134     // OComponentHelper
135 	virtual void SAL_CALL disposing(void);
136 
137     // XSingleServiceFactory
138     virtual ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL createInstance(  ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
139     virtual ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL createInstanceWithArguments( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& _rArguments ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
140 
141     // XServiceInfo
142     virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw(::com::sun::star::uno::RuntimeException);
143     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException);
144     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException);
145 
146     // XServiceInfo - static methods
147 	static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_static(void) throw( ::com::sun::star::uno::RuntimeException );
148 	static ::rtl::OUString getImplementationName_static(void) throw( ::com::sun::star::uno::RuntimeException );
149 	static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
150 		SAL_CALL Create(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >&);
151 
152     // XElementAccess
153     virtual ::com::sun::star::uno::Type SAL_CALL getElementType(  ) throw(::com::sun::star::uno::RuntimeException);
154     virtual sal_Bool SAL_CALL hasElements(  ) throw(::com::sun::star::uno::RuntimeException);
155 
156     // XEnumerationAccess
157 	virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XEnumeration > SAL_CALL createEnumeration(  ) throw(::com::sun::star::uno::RuntimeException);
158 
159     // XNameAccess
160 	virtual ::com::sun::star::uno::Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
161     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames(  ) throw(::com::sun::star::uno::RuntimeException);
162     virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw(::com::sun::star::uno::RuntimeException);
163 
164     // XNamingService
165 	virtual ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL getRegisteredObject( const ::rtl::OUString& Name ) throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
166     virtual void SAL_CALL registerObject( const ::rtl::OUString& Name, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& Object ) throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
167     virtual void SAL_CALL revokeObject( const ::rtl::OUString& Name ) throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
168 
169     // XDatabaseRegistrations
170     virtual ::sal_Bool SAL_CALL hasRegisteredDatabase( const ::rtl::OUString& Name ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
171     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getRegistrationNames() throw (::com::sun::star::uno::RuntimeException);
172     virtual ::rtl::OUString SAL_CALL getDatabaseLocation( const ::rtl::OUString& Name ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException, ::com::sun::star::uno::RuntimeException);
173     virtual void SAL_CALL registerDatabaseLocation( const ::rtl::OUString& Name, const ::rtl::OUString& Location ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::ElementExistException, ::com::sun::star::uno::RuntimeException);
174     virtual void SAL_CALL revokeDatabaseLocation( const ::rtl::OUString& Name ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::IllegalAccessException, ::com::sun::star::uno::RuntimeException);
175     virtual void SAL_CALL changeDatabaseLocation( const ::rtl::OUString& Name, const ::rtl::OUString& NewLocation ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::IllegalAccessException, ::com::sun::star::uno::RuntimeException);
176     virtual ::sal_Bool SAL_CALL isDatabaseRegistrationReadOnly( const ::rtl::OUString& Name ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException, ::com::sun::star::uno::RuntimeException);
177     virtual void SAL_CALL addDatabaseRegistrationsListener( const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XDatabaseRegistrationsListener >& Listener ) throw (::com::sun::star::uno::RuntimeException);
178     virtual void SAL_CALL removeDatabaseRegistrationsListener( const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XDatabaseRegistrationsListener >& Listener ) throw (::com::sun::star::uno::RuntimeException);
179 
180     // XContainer
181     virtual void SAL_CALL addContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& xListener ) throw(::com::sun::star::uno::RuntimeException);
182     virtual void SAL_CALL removeContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& xListener ) throw(::com::sun::star::uno::RuntimeException);
183 
184 	// com::sun::star::lang::XUnoTunnel
185     virtual sal_Int64 SAL_CALL getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier ) throw(::com::sun::star::uno::RuntimeException);
186 	static ::com::sun::star::uno::Sequence< sal_Int8 > getUnoTunnelImplementationId();
187 
188 	void registerDatabaseDocument( ODatabaseModelImpl& _rModelImpl);
189 	void revokeDatabaseDocument( const ODatabaseModelImpl& _rModelImpl);
190 	void databaseDocumentURLChange(const ::rtl::OUString& _sOldName, const ::rtl::OUString& _sNewName);
191     void storeTransientProperties( ODatabaseModelImpl& _rModelImpl);
192     void appendAtTerminateListener(const ODatabaseModelImpl& _rDataSourceModel);
193     void removeFromTerminateListener(const ODatabaseModelImpl& _rDataSourceModel);
194 
195 private:
196     // BasicManagerCreationListener
197     virtual void onBasicManagerCreated(
198         const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >& _rxForDocument,
199         BasicManager& _rBasicManager
200     );
201 };
202 
203 //........................................................................
204 }	// namespace dbaccess
205 //........................................................................
206 
207 #endif // _DBA_COREDATAACCESS_DATABASECONTEXT_HXX_
208 
209 
210