1*caf5cd79SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*caf5cd79SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*caf5cd79SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*caf5cd79SAndrew Rist  * distributed with this work for additional information
6*caf5cd79SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*caf5cd79SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*caf5cd79SAndrew Rist  * "License"); you may not use this file except in compliance
9*caf5cd79SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*caf5cd79SAndrew Rist  *
11*caf5cd79SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*caf5cd79SAndrew Rist  *
13*caf5cd79SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*caf5cd79SAndrew Rist  * software distributed under the License is distributed on an
15*caf5cd79SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*caf5cd79SAndrew Rist  * KIND, either express or implied.  See the License for the
17*caf5cd79SAndrew Rist  * specific language governing permissions and limitations
18*caf5cd79SAndrew Rist  * under the License.
19*caf5cd79SAndrew Rist  *
20*caf5cd79SAndrew Rist  *************************************************************/
21*caf5cd79SAndrew Rist 
22*caf5cd79SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _CONNECTIVITY_SDBCX_COLLECTION_HXX_
25cdf0e10cSrcweir #define _CONNECTIVITY_SDBCX_COLLECTION_HXX_
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <cppuhelper/implbase10.hxx>
28cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
29cdf0e10cSrcweir #include <com/sun/star/container/XIndexAccess.hpp>
30cdf0e10cSrcweir #include <com/sun/star/container/XEnumerationAccess.hpp>
31cdf0e10cSrcweir #include <com/sun/star/container/XEnumeration.hpp>
32cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
33cdf0e10cSrcweir #include <com/sun/star/util/XRefreshable.hpp>
34cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
35cdf0e10cSrcweir #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
36cdf0e10cSrcweir #include <com/sun/star/sdbcx/XAppend.hpp>
37cdf0e10cSrcweir #include <com/sun/star/sdbcx/XDrop.hpp>
38cdf0e10cSrcweir #include <com/sun/star/sdbc/XColumnLocate.hpp>
39cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
40cdf0e10cSrcweir #include <cppuhelper/interfacecontainer.h>
41cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
42cdf0e10cSrcweir #include "connectivity/CommonTools.hxx"
43cdf0e10cSrcweir #include <com/sun/star/container/XContainer.hpp>
44cdf0e10cSrcweir #include "connectivity/StdTypeDefs.hxx"
45cdf0e10cSrcweir #include "connectivity/dbtoolsdllapi.hxx"
46cdf0e10cSrcweir #include <memory>
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 
49cdf0e10cSrcweir namespace connectivity
50cdf0e10cSrcweir {
51cdf0e10cSrcweir 	namespace sdbcx
52cdf0e10cSrcweir 	{
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 		// the class OCollection is base class for collections :-)
55cdf0e10cSrcweir         typedef ::cppu::ImplHelper10< ::com::sun::star::container::XNameAccess,
56cdf0e10cSrcweir                                          ::com::sun::star::container::XIndexAccess,
57cdf0e10cSrcweir                                          ::com::sun::star::container::XEnumerationAccess,
58cdf0e10cSrcweir 										 ::com::sun::star::container::XContainer,
59cdf0e10cSrcweir                                          ::com::sun::star::sdbc::XColumnLocate,
60cdf0e10cSrcweir                                          ::com::sun::star::util::XRefreshable,
61cdf0e10cSrcweir                                          ::com::sun::star::sdbcx::XDataDescriptorFactory,
62cdf0e10cSrcweir                                          ::com::sun::star::sdbcx::XAppend,
63cdf0e10cSrcweir                                          ::com::sun::star::sdbcx::XDrop,
64cdf0e10cSrcweir                                          ::com::sun::star::lang::XServiceInfo> OCollectionBase;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir         typedef ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > ObjectType;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 		class OOO_DLLPUBLIC_DBTOOLS SAL_NO_VTABLE IObjectCollection
69cdf0e10cSrcweir 		{
70cdf0e10cSrcweir 		public:
71cdf0e10cSrcweir 			virtual void reserve(size_t nLength) = 0;
72cdf0e10cSrcweir 			virtual bool exists(const ::rtl::OUString& _sName ) = 0;
73cdf0e10cSrcweir 			virtual bool empty() = 0;
74cdf0e10cSrcweir 			virtual void swapAll() = 0;
75cdf0e10cSrcweir 			virtual void swap() = 0;
76cdf0e10cSrcweir 			virtual void clear() = 0;
77cdf0e10cSrcweir 			virtual void reFill(const TStringVector &_rVector) = 0;
78cdf0e10cSrcweir 			virtual void insert(const ::rtl::OUString& _sName,const ObjectType& _xObject) = 0;
79cdf0e10cSrcweir 			virtual bool rename(const ::rtl::OUString _sOldName,const ::rtl::OUString _sNewName) = 0;
80cdf0e10cSrcweir 			virtual sal_Int32 size() = 0;
81cdf0e10cSrcweir 			virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > getElementNames() = 0;
82cdf0e10cSrcweir 			virtual ::rtl::OUString getName(sal_Int32 _nIndex) = 0;
83cdf0e10cSrcweir 			virtual void disposeAndErase(sal_Int32 _nIndex) = 0;
84cdf0e10cSrcweir 			virtual void disposeElements() = 0;
85cdf0e10cSrcweir 			virtual sal_Int32 findColumn( const ::rtl::OUString& columnName ) = 0;
86cdf0e10cSrcweir 			virtual ::rtl::OUString findColumnAtIndex(  sal_Int32 _nIndex) = 0;
87cdf0e10cSrcweir 			virtual ObjectType getObject(sal_Int32 _nIndex) = 0;
88cdf0e10cSrcweir 			virtual ObjectType getObject(const ::rtl::OUString& columnName) = 0;
89cdf0e10cSrcweir 			virtual void setObject(sal_Int32 _nIndex,const ObjectType& _xObject) = 0;
90cdf0e10cSrcweir 			virtual sal_Bool isCaseSensitive() const = 0;
91cdf0e10cSrcweir 		};
92cdf0e10cSrcweir 		//************************************************************
93cdf0e10cSrcweir 		//  OCollection
94cdf0e10cSrcweir 		//************************************************************
95cdf0e10cSrcweir 		class OOO_DLLPUBLIC_DBTOOLS SAL_NO_VTABLE OCollection :
96cdf0e10cSrcweir             public OCollectionBase
97cdf0e10cSrcweir 		{
98cdf0e10cSrcweir 		protected:
99cdf0e10cSrcweir 			::std::auto_ptr<IObjectCollection>		m_pElements;
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 			::cppu::OInterfaceContainerHelper		m_aContainerListeners;
102cdf0e10cSrcweir 			::cppu::OInterfaceContainerHelper		m_aRefreshListeners;
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 		protected:
105cdf0e10cSrcweir 			::cppu::OWeakObject&					m_rParent;			// parent of the collection
106cdf0e10cSrcweir 			::osl::Mutex&							m_rMutex;			// mutex of the parent
107cdf0e10cSrcweir 			sal_Bool								m_bUseIndexOnly;	// is only TRUE when only an indexaccess is needed
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 			// the implementing class should refresh their elements
110cdf0e10cSrcweir             virtual void impl_refresh() throw(::com::sun::star::uno::RuntimeException) = 0;
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 			// will be called when a object was requested by one of the accessing methods like getByIndex
113cdf0e10cSrcweir             virtual ObjectType createObject(const ::rtl::OUString& _rName) = 0;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 			// will be called when a new object should be generated by a call of createDataDescriptor
116cdf0e10cSrcweir 			// the returned object is empty will be filled outside and added to the collection
117cdf0e10cSrcweir             virtual ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > createDescriptor();
118cdf0e10cSrcweir 
119cdf0e10cSrcweir             /** appends an object described by a descriptor, under a given name
120cdf0e10cSrcweir                 @param _rForName
121cdf0e10cSrcweir                     is the name under which the object should be appended. Guaranteed to not be empty.
122cdf0e10cSrcweir                     This is passed for convenience only, since it's the result of a call of
123cdf0e10cSrcweir                     getNameForObject for the given descriptor
124cdf0e10cSrcweir                 @param descriptor
125cdf0e10cSrcweir                     describes the object to append
126cdf0e10cSrcweir                 @return
127cdf0e10cSrcweir                     the new object which is to be inserted into the collection. This might be the result
128cdf0e10cSrcweir                     of a call of <code>createObject( _rForName )</code>, or a clone of the descriptor.
129cdf0e10cSrcweir             */
130cdf0e10cSrcweir             virtual ObjectType appendObject( const ::rtl::OUString& _rForName, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& descriptor );
131cdf0e10cSrcweir 
132cdf0e10cSrcweir             // called when XDrop was called
133cdf0e10cSrcweir 			virtual void dropObject(sal_Int32 _nPos,const ::rtl::OUString _sElementName);
134cdf0e10cSrcweir 
135cdf0e10cSrcweir             /** returns the name for the object. The default implementation ask for the property NAME. If this doesn't satisfy, it has to be overloaded.
136cdf0e10cSrcweir                 @param  _xObject    The object where the name should be extracted.
137cdf0e10cSrcweir                 @return The name of the object.
138cdf0e10cSrcweir             */
139cdf0e10cSrcweir             virtual ::rtl::OUString getNameForObject(const ObjectType& _xObject);
140cdf0e10cSrcweir 
141cdf0e10cSrcweir             /** clones the given descriptor
142cdf0e10cSrcweir 
143cdf0e10cSrcweir                 The method calls createDescriptor to create a new, empty descriptor, and then copies all properties from
144cdf0e10cSrcweir                 _descriptor to the new object, which is returned.
145cdf0e10cSrcweir 
146cdf0e10cSrcweir                 This method might come handy in derived classes for implementing appendObject, when the object
147cdf0e10cSrcweir                 is not actually appended to any backend (e.g. for the columns collection of a descriptor object itself,
148cdf0e10cSrcweir                 where there is not yet a database backend to append the column to).
149cdf0e10cSrcweir             */
150cdf0e10cSrcweir             ObjectType cloneDescriptor( const ObjectType& _descriptor );
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 			OCollection(::cppu::OWeakObject& _rParent,
153cdf0e10cSrcweir 						sal_Bool _bCase,
154cdf0e10cSrcweir 						::osl::Mutex& _rMutex,
155cdf0e10cSrcweir 						const TStringVector &_rVector,
156cdf0e10cSrcweir 						sal_Bool _bUseIndexOnly = sal_False,
157cdf0e10cSrcweir 						sal_Bool _bUseHardRef = sal_True);
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 			/** clear the name map
160cdf0e10cSrcweir 				<p>Does <em>not</em> dispose the objects hold by the collection.</p>
161cdf0e10cSrcweir 			*/
162cdf0e10cSrcweir 			void clear_NoDispose();
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 			/**  insert a new element into the collection
165cdf0e10cSrcweir 			*/
166cdf0e10cSrcweir 			void insertElement(const ::rtl::OUString& _sElementName,const ObjectType& _xElement);
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 			/** return the name of element at index _nIndex
169cdf0e10cSrcweir 			*/
getElementName(sal_Int32 _nIndex)170cdf0e10cSrcweir 			inline ::rtl::OUString getElementName(sal_Int32 _nIndex)
171cdf0e10cSrcweir 			{
172cdf0e10cSrcweir 				return m_pElements->findColumnAtIndex(_nIndex);
173cdf0e10cSrcweir 			}
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 
176cdf0e10cSrcweir 			/** return the object, if not existent it creates it.
177cdf0e10cSrcweir 				@param	_nIndex
178cdf0e10cSrcweir 					The index of the object to create.
179cdf0e10cSrcweir 				@return	ObjectType
180cdf0e10cSrcweir 			*/
181cdf0e10cSrcweir 			ObjectType getObject(sal_Int32 _nIndex);
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 		public:
184cdf0e10cSrcweir 			virtual ~OCollection();
185cdf0e10cSrcweir 			DECLARE_SERVICE_INFO();
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 			void reFill(const TStringVector &_rVector);
isCaseSensitive() const188cdf0e10cSrcweir 			inline sal_Bool isCaseSensitive() const { return m_pElements->isCaseSensitive(); }
189cdf0e10cSrcweir 			void renameObject(const ::rtl::OUString _sOldName,const ::rtl::OUString _sNewName);
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 			// only the name is identical to ::cppu::OComponentHelper
192cdf0e10cSrcweir 			virtual void SAL_CALL disposing(void);
193cdf0e10cSrcweir 			// dispatch the refcounting to the parent
194cdf0e10cSrcweir             virtual void SAL_CALL acquire() throw();
195cdf0e10cSrcweir             virtual void SAL_CALL release() throw();
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 			// XInterface
198cdf0e10cSrcweir 			virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType ) throw (::com::sun::star::uno::RuntimeException);
199cdf0e10cSrcweir 			virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getTypes() throw (::com::sun::star::uno::RuntimeException);
200cdf0e10cSrcweir 
201cdf0e10cSrcweir             // ::com::sun::star::container::XElementAccess
202cdf0e10cSrcweir             virtual ::com::sun::star::uno::Type SAL_CALL getElementType(  ) throw(::com::sun::star::uno::RuntimeException);
203cdf0e10cSrcweir 			virtual sal_Bool SAL_CALL hasElements(  ) throw(::com::sun::star::uno::RuntimeException);
204cdf0e10cSrcweir             // ::com::sun::star::container::XIndexAccess
205cdf0e10cSrcweir             virtual sal_Int32 SAL_CALL getCount(  ) throw(::com::sun::star::uno::RuntimeException);
206cdf0e10cSrcweir             virtual ::com::sun::star::uno::Any SAL_CALL getByIndex( sal_Int32 Index ) throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
207cdf0e10cSrcweir 
208cdf0e10cSrcweir                 // ::com::sun::star::container::XNameAccess
209cdf0e10cSrcweir             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);
210cdf0e10cSrcweir             virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames(  ) throw(::com::sun::star::uno::RuntimeException);
211cdf0e10cSrcweir             virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw(::com::sun::star::uno::RuntimeException);
212cdf0e10cSrcweir 			// XEnumerationAccess
213cdf0e10cSrcweir             virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XEnumeration > SAL_CALL createEnumeration(  ) throw(::com::sun::star::uno::RuntimeException);
214cdf0e10cSrcweir                         // ::com::sun::star::util::XRefreshable
215cdf0e10cSrcweir             virtual void SAL_CALL refresh(  ) throw(::com::sun::star::uno::RuntimeException);
216cdf0e10cSrcweir             virtual void SAL_CALL addRefreshListener( const ::com::sun::star::uno::Reference< ::com::sun::star::util::XRefreshListener >& l ) throw(::com::sun::star::uno::RuntimeException);
217cdf0e10cSrcweir             virtual void SAL_CALL removeRefreshListener( const ::com::sun::star::uno::Reference< ::com::sun::star::util::XRefreshListener >& l ) throw(::com::sun::star::uno::RuntimeException);
218cdf0e10cSrcweir 			// XDataDescriptorFactory
219cdf0e10cSrcweir             virtual ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > SAL_CALL createDataDescriptor(  ) throw(::com::sun::star::uno::RuntimeException);
220cdf0e10cSrcweir 			// XAppend
221cdf0e10cSrcweir             virtual void SAL_CALL appendByDescriptor( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& descriptor ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::container::ElementExistException, ::com::sun::star::uno::RuntimeException);
222cdf0e10cSrcweir 			// XDrop
223cdf0e10cSrcweir             virtual void SAL_CALL dropByName( const ::rtl::OUString& elementName ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::container::NoSuchElementException, ::com::sun::star::uno::RuntimeException);
224cdf0e10cSrcweir             virtual void SAL_CALL dropByIndex( sal_Int32 index ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
225cdf0e10cSrcweir 			// XColumnLocate
226cdf0e10cSrcweir 			virtual sal_Int32 SAL_CALL findColumn( const ::rtl::OUString& columnName ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
227cdf0e10cSrcweir 			// ::com::sun::star::container::XContainer
228cdf0e10cSrcweir 			virtual void SAL_CALL addContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& xListener ) throw(::com::sun::star::uno::RuntimeException);
229cdf0e10cSrcweir 			virtual void SAL_CALL removeContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& xListener ) throw(::com::sun::star::uno::RuntimeException);
230cdf0e10cSrcweir 		private:
231cdf0e10cSrcweir 			void notifyElementRemoved(const ::rtl::OUString& _sName);
232cdf0e10cSrcweir 			void disposeElements();
233cdf0e10cSrcweir 			void dropImpl(sal_Int32 _nIndex,sal_Bool _bReallyDrop = sal_True);
234cdf0e10cSrcweir 		};
235cdf0e10cSrcweir 	}
236cdf0e10cSrcweir }
237cdf0e10cSrcweir #endif // _CONNECTIVITY_SDBCX_COLLECTION_HXX_
238cdf0e10cSrcweir 
239cdf0e10cSrcweir 
240