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