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