1*2e2212a7SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2e2212a7SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2e2212a7SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2e2212a7SAndrew Rist  * distributed with this work for additional information
6*2e2212a7SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2e2212a7SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2e2212a7SAndrew Rist  * "License"); you may not use this file except in compliance
9*2e2212a7SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2e2212a7SAndrew Rist  *
11*2e2212a7SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2e2212a7SAndrew Rist  *
13*2e2212a7SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2e2212a7SAndrew Rist  * software distributed under the License is distributed on an
15*2e2212a7SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2e2212a7SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2e2212a7SAndrew Rist  * specific language governing permissions and limitations
18*2e2212a7SAndrew Rist  * under the License.
19*2e2212a7SAndrew Rist  *
20*2e2212a7SAndrew Rist  *************************************************************/
21*2e2212a7SAndrew Rist 
22*2e2212a7SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef DBAUI_DATASOURCEMAP_HXX
25cdf0e10cSrcweir #define DBAUI_DATASOURCEMAP_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
28cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
29cdf0e10cSrcweir #endif
30cdf0e10cSrcweir #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_
31cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
32cdf0e10cSrcweir #endif
33cdf0e10cSrcweir #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_
34cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir #ifndef _COMPHELPER_STLTYPES_HXX_
37cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
38cdf0e10cSrcweir #endif
39cdf0e10cSrcweir #ifndef _STRING_HXX
40cdf0e10cSrcweir #include <tools/string.hxx>
41cdf0e10cSrcweir #endif
42cdf0e10cSrcweir 
43cdf0e10cSrcweir class SfxItemPool;
44cdf0e10cSrcweir class SfxItemSet;
45cdf0e10cSrcweir //.........................................................................
46cdf0e10cSrcweir namespace dbaui
47cdf0e10cSrcweir {
48cdf0e10cSrcweir //.........................................................................
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 	//=====================================================================
51cdf0e10cSrcweir 	//= ODatasourceMap
52cdf0e10cSrcweir 	//=====================================================================
53cdf0e10cSrcweir 	class ODatasourceMap
54cdf0e10cSrcweir 	{
55cdf0e10cSrcweir 		struct DatasourceInfo
56cdf0e10cSrcweir 		{
57cdf0e10cSrcweir 			::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >
58cdf0e10cSrcweir 								xDatasource;
59cdf0e10cSrcweir 			SfxItemSet*			pModifications;
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 			DatasourceInfo() :pModifications (NULL) {  }
62cdf0e10cSrcweir 			DatasourceInfo(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxDS,
63cdf0e10cSrcweir 					SfxItemSet* _pMods = NULL)
64cdf0e10cSrcweir 				:xDatasource(_rxDS), pModifications(_pMods) { }
65cdf0e10cSrcweir 		};
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 		::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >
68cdf0e10cSrcweir 								m_xORB;					/// service factory
69cdf0e10cSrcweir 		::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >
70cdf0e10cSrcweir 								m_xDatabaseContext;		/// database context we're working in
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 		DECLARE_STL_USTRINGACCESS_MAP(DatasourceInfo, DatasourceInfos);
73cdf0e10cSrcweir 		DatasourceInfos			m_aDatasources;			/// known infos about data sources
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 		// deleted data sources, not necessarily with distinct names, that's why accessed via unique ids
76cdf0e10cSrcweir 		DECLARE_STL_MAP(sal_Int32, DatasourceInfo, ::std::less< sal_Int32 >, MapInt2Info);
77cdf0e10cSrcweir 		MapInt2Info				m_aDeletedDatasources;
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 	public:
80cdf0e10cSrcweir 		/// iterating through all data sources
81cdf0e10cSrcweir 		class Iterator;
82cdf0e10cSrcweir 		friend class ODatasourceMap::Iterator;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 		/// encapsulates the infos about a data source for access from outside the class
85cdf0e10cSrcweir 		class ODatasourceInfo;
86cdf0e10cSrcweir 		friend class ODatasourceMap::ODatasourceInfo;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 		ODatasourceInfo operator[](const ::rtl::OUString _rName);
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 		ODatasourceMap(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > _rxORB);
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 		// get the database context
93cdf0e10cSrcweir 		::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >
94cdf0e10cSrcweir 					getContext() { return m_xDatabaseContext; }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 		/// first element for iterating through the datasources
97cdf0e10cSrcweir 		Iterator	begin();
98cdf0e10cSrcweir 		/// last element for iterating through the datasources
99cdf0e10cSrcweir 		Iterator	end();
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 		/// first element for iterating through the deleted datasources
102cdf0e10cSrcweir 		Iterator	beginDeleted();
103cdf0e10cSrcweir 		/// last element for iterating through the deleted datasources
104cdf0e10cSrcweir 		Iterator	endDeleted();
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 		/// check if the object contains a valid datasource enumeration
107cdf0e10cSrcweir 		sal_Bool	isValid() const { return m_xDatabaseContext.is(); }
108cdf0e10cSrcweir 		/// check if a datasource with the given name exists
109cdf0e10cSrcweir 		sal_Bool	exists(const ::rtl::OUString& _rName) const;
110cdf0e10cSrcweir 		/// return the number of datasources available
111cdf0e10cSrcweir 		sal_Int32	size() const { return m_aDatasources.size(); }
112cdf0e10cSrcweir 		/// clear the map (non-deleted <em>and</em> deleted items)
113cdf0e10cSrcweir 		void		clear();
114cdf0e10cSrcweir 		/// clear the map (deleted items only)
115cdf0e10cSrcweir 		void		clearDeleted();
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 		/// clear the modification items for a datasource
118cdf0e10cSrcweir 		void		clearModifiedFlag(const ::rtl::OUString& _rName);
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 		/** tell the map that a data source is scheduled to be deleted.
121cdf0e10cSrcweir 			@return		id for accessing the deleted data source later. -1 if no free id existed or an error occured
122cdf0e10cSrcweir 		*/
123cdf0e10cSrcweir 		sal_Int32	markDeleted(const ::rtl::OUString& _rName);
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 		/** restores a datasource which has previously been marked as deleted.<p/>
126cdf0e10cSrcweir 			@param		_nAccessId		the access id as got from <method>markDeleted</method>
127cdf0e10cSrcweir 			@param		_rName			contains, upon return, the name of the datasource the access key refers to
128cdf0e10cSrcweir 			@return		sal_True if the datasource was successfully restored, sal_False if it could not be restored
129cdf0e10cSrcweir 						because of a naming conflict (e.g. because another data source now has the name of the
130cdf0e10cSrcweir 						to-be-restored one).
131cdf0e10cSrcweir 			@see	renamed
132cdf0e10cSrcweir 			@see	markDeleted
133cdf0e10cSrcweir 		*/
134cdf0e10cSrcweir 		sal_Bool	restoreDeleted(sal_Int32 _nAccessId, ::rtl::OUString& _rName);
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 		/// remove an element from the map
137cdf0e10cSrcweir 		void		deleted(const ::rtl::OUString& _rName);
138cdf0e10cSrcweir 			// (should be an erase(const Iterator&), but this is way to general ...
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 		/// update the infos for a data source with a given item set
141cdf0e10cSrcweir 		void		update(const ::rtl::OUString& _rName, SfxItemSet& _rSet);
142cdf0e10cSrcweir 		/** Tells the map that an entry has been renamed in a sense that it should be accessible under
143cdf0e10cSrcweir 			a new name. This does not necesssarily mean that the data source has been renamed within
144cdf0e10cSrcweir 			it's database context
145cdf0e10cSrcweir 		*/
146cdf0e10cSrcweir 		void		renamed(const ::rtl::OUString& _rOldName, const ::rtl::OUString& _rNewName);
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 		/** adjust the registration name if necessary<p/>
149cdf0e10cSrcweir 			The real name of the data source (as indicated in the SfxItemSet for this ds) may be another
150cdf0e10cSrcweir 			one than the name the ds is registered for. This method corrects this, the ds will become registered
151cdf0e10cSrcweir 			under it's real name.
152cdf0e10cSrcweir 			@param		_rName		the name the ds is registered for
153cdf0e10cSrcweir 			@return					the real name of the data source
154cdf0e10cSrcweir 		*/
155cdf0e10cSrcweir 		::rtl::OUString adjustRealName(const ::rtl::OUString& _rName);
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 	protected:
158cdf0e10cSrcweir 		/** ensure that the DatabaseInfo for the named object is filled<p/>
159cdf0e10cSrcweir 			This method allows us lazy access to the data sources: They're retrieved from the context
160cdf0e10cSrcweir 			only if they're accessed by somebody.
161cdf0e10cSrcweir 		*/
162cdf0e10cSrcweir 		void	ensureObject(const ::rtl::OUString& _rName);
163cdf0e10cSrcweir 	};
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 	//-------------------------------------------------------------------------
166cdf0e10cSrcweir 	//- ODatasourceMap::ODatasourceInfo
167cdf0e10cSrcweir 	//-------------------------------------------------------------------------
168cdf0e10cSrcweir 	class ODatasourceMap::ODatasourceInfo
169cdf0e10cSrcweir 	{
170cdf0e10cSrcweir 		friend class ODatasourceMap;
171cdf0e10cSrcweir 		friend class ODatasourceMap::Iterator;
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 	private:
174cdf0e10cSrcweir 		ODatasourceMap*								m_pOwner;
175cdf0e10cSrcweir 		const ODatasourceMap::DatasourceInfo&		m_rInfoImpl;
176cdf0e10cSrcweir 		::rtl::OUString								m_sName;
177cdf0e10cSrcweir 		sal_Int32									m_nAccessKey;
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 	public:
180cdf0e10cSrcweir 		ODatasourceInfo(const ODatasourceInfo& _rSource)
181cdf0e10cSrcweir 			:m_pOwner(_rSource.m_pOwner), m_sName(_rSource.m_sName), m_rInfoImpl(_rSource.m_rInfoImpl), m_nAccessKey(_rSource.m_nAccessKey) { }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 		/// check if the datasource settings are modified
184cdf0e10cSrcweir 		sal_Bool		isModified() const;
185cdf0e10cSrcweir 		/// get the name the datasource is registered under
186cdf0e10cSrcweir 		::rtl::OUString	getName() const { return m_sName; }
187cdf0e10cSrcweir 		/// get the original name of a datasource (may habe been renamed)
188cdf0e10cSrcweir 		::rtl::OUString	getOriginalName() const;
189cdf0e10cSrcweir 		/// get the real name of the datasource, which is the name which is in the item set
190cdf0e10cSrcweir 		::rtl::OUString	getRealName() const;
191cdf0e10cSrcweir 		/// check if the datasource should is about to be renamed (which means the original name does not equal the real name
192cdf0e10cSrcweir 		sal_Bool		isRenamed() const { return !getRealName().equals(getOriginalName()); }
193cdf0e10cSrcweir 		/// get the key used to acces the object in the data source map
194cdf0e10cSrcweir 		sal_Int32		getAccessKey() const { return m_nAccessKey; }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 		/// return the datasource the object represents
197cdf0e10cSrcweir 		::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >
198cdf0e10cSrcweir 						getDatasource() const;
199cdf0e10cSrcweir 		/** return the modifications for the data source<p/>
200cdf0e10cSrcweir 			The return value is non-NULL if and only if <method>isModified</method> returned sal_True
201cdf0e10cSrcweir 		*/
202cdf0e10cSrcweir 		const SfxItemSet*
203cdf0e10cSrcweir 						getModifications() const { return m_rInfoImpl.pModifications; }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 		operator ::rtl::OUString() const { return getName(); }
206cdf0e10cSrcweir 		operator String() const { return getName().getStr(); }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 		const ODatasourceInfo* const operator->() const { return this; }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 	protected:
211cdf0e10cSrcweir 		ODatasourceInfo(
212cdf0e10cSrcweir 				ODatasourceMap* _pOwner, const ::rtl::OUString& _rName,
213cdf0e10cSrcweir 				const ODatasourceMap::DatasourceInfo& _rSource, sal_Int32 _nAccessKey)
214cdf0e10cSrcweir 			:m_pOwner(_pOwner), m_sName(_rName), m_rInfoImpl(_rSource), m_nAccessKey(_nAccessKey) { }
215cdf0e10cSrcweir 	};
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 	//-------------------------------------------------------------------------
218cdf0e10cSrcweir 	//- ODatasourceMap::Iterator
219cdf0e10cSrcweir 	//-------------------------------------------------------------------------
220cdf0e10cSrcweir 	class ODatasourceMap::Iterator
221cdf0e10cSrcweir 	{
222cdf0e10cSrcweir 		friend class ODatasourceMap;
223cdf0e10cSrcweir 	protected:
224cdf0e10cSrcweir 		ODatasourceMap*									m_pOwner;
225cdf0e10cSrcweir 		ODatasourceMap::ConstDatasourceInfosIterator	m_aPos;
226cdf0e10cSrcweir 		ODatasourceMap::ConstMapInt2InfoIterator		m_aPosDeleted;
227cdf0e10cSrcweir 		sal_Bool										m_bLoopingDeleted;
228cdf0e10cSrcweir 
229cdf0e10cSrcweir 	public:
230cdf0e10cSrcweir 		Iterator(const Iterator& _rSource);
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 		ODatasourceInfo operator->() const;
233cdf0e10cSrcweir 		ODatasourceInfo operator*() const;
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 		/// prefix increment
236cdf0e10cSrcweir 		const Iterator&	operator++();
237cdf0e10cSrcweir 		/// postfix increment
238cdf0e10cSrcweir 		const Iterator	operator++(int) { Iterator hold(*this); ++*this; return hold; }
239cdf0e10cSrcweir 
240cdf0e10cSrcweir 		/// prefix decrement
241cdf0e10cSrcweir 		const Iterator&	operator--();
242cdf0e10cSrcweir 		/// postfix decrement
243cdf0e10cSrcweir 		const Iterator	operator--(int) { Iterator hold(*this); --*this; return hold; }
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 	// compare two iterators
247cdf0e10cSrcweir 		friend bool operator==(const Iterator& lhs, const Iterator& rhs)
248cdf0e10cSrcweir 		{
249cdf0e10cSrcweir 			if (lhs.m_bLoopingDeleted)
250cdf0e10cSrcweir 				return lhs.m_aPosDeleted == rhs.m_aPosDeleted;
251cdf0e10cSrcweir 			else
252cdf0e10cSrcweir 				return lhs.m_aPos == rhs.m_aPos;
253cdf0e10cSrcweir 		}
254cdf0e10cSrcweir 
255cdf0e10cSrcweir 		friend bool operator!=(const Iterator& lhs, const Iterator& rhs) { return !(lhs == rhs); }
256cdf0e10cSrcweir 
257cdf0e10cSrcweir 	protected:
258cdf0e10cSrcweir 		Iterator(ODatasourceMap* _pOwner, ODatasourceMap::ConstDatasourceInfosIterator _rPos);
259cdf0e10cSrcweir 		Iterator(ODatasourceMap* _pOwner, ODatasourceMap::ConstMapInt2InfoIterator _rPos);
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 	protected:
262cdf0e10cSrcweir 		::rtl::OUString implGetName(const ODatasourceMap::DatasourceInfo& _rInfo) const;
263cdf0e10cSrcweir 	};
264cdf0e10cSrcweir 
265cdf0e10cSrcweir //.........................................................................
266cdf0e10cSrcweir }	// namespace dbaui
267cdf0e10cSrcweir //.........................................................................
268cdf0e10cSrcweir 
269cdf0e10cSrcweir #endif // DBAUI_DATASOURCEMAP_HXX
270cdf0e10cSrcweir 
271