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