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