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_ADO_COLLECTION_HXX_
25 #define _CONNECTIVITY_ADO_COLLECTION_HXX_
26 
27 #include <cppuhelper/implbase3.hxx>
28 #include <com/sun/star/container/XNameAccess.hpp>
29 #include <com/sun/star/container/XIndexAccess.hpp>
30 #include "ado/Awrapadox.hxx"
31 #include "ado/Aolevariant.hxx"
32 #include <com/sun/star/lang/XServiceInfo.hpp>
33 
34 namespace connectivity
35 {
36 	namespace ado
37 	{
38 		namespace starcontainer	= ::com::sun::star::container;
39 		namespace starlang		= ::com::sun::star::lang;
40 		namespace staruno		= ::com::sun::star::uno;
41 		namespace starbeans		= ::com::sun::star::beans;
42 
43 		typedef ::cppu::WeakImplHelper3< starcontainer::XNameAccess,
44 										 starcontainer::XIndexAccess,
45 										 starlang::XServiceInfo> OCollectionBase;
46 
47 		//************************************************************
48 		//  OCollection
49 		//************************************************************
50 		template <class T,class SimT,class OCl> class OCollection : public OCollectionBase
51 		{
52 		private:
53 			OCollection( const OCollection& );				// never implemented
54 			OCollection& operator=( const OCollection& );	// never implemented
55 
56 		protected:
57 			vector<OCl*>							m_aElements;
58 			::cppu::OWeakObject&					m_rParent;
59 			::osl::Mutex&							m_rMutex;		// mutex of the parent
60 			T*										m_pCollection;
61 
62 
63 		public:
OCollection(::cppu::OWeakObject & _rParent,::osl::Mutex & _rMutex,T * _pCollection)64 			OCollection(::cppu::OWeakObject& _rParent, ::osl::Mutex& _rMutex,T* _pCollection)
65 					 : m_rParent(_rParent)
66 					 ,m_rMutex(_rMutex)
67 					 ,m_pCollection(_pCollection)
68 			{
69 				m_pCollection->AddRef();
70 			}
71 
~OCollection()72 			~OCollection()
73 			{
74 				m_pCollection->Release();
75 			}
76 
getImplementationName()77 			virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw (staruno::RuntimeException)
78 			{
79 				return ::rtl::OUString::createFromAscii("com.sun.star.sdbcx.ACollection");
80 			}
supportsService(const::rtl::OUString & _rServiceName)81 			virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& _rServiceName ) throw(staruno::RuntimeException)
82 			{
83 				staruno::Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());
84 				const ::rtl::OUString* pSupported = aSupported.getConstArray();
85 				for (sal_Int32 i=0; i<aSupported.getLength(); ++i, ++pSupported)
86 					if (pSupported->equals(_rServiceName))
87 						return sal_True;
88 
89 				return sal_False;
90 			}
getSupportedServiceNames()91 			virtual staruno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw(staruno::RuntimeException)
92 			{
93 				staruno::Sequence< ::rtl::OUString > aSupported(1);
94 				aSupported[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdbcx.Container");
95 				return aSupported;
96 			}
97 
98 			// dispatch the refcounting to the parent
acquire()99 			virtual void SAL_CALL acquire() throw()
100 			{
101 				m_rParent.acquire();
102 			}
release()103 			virtual void SAL_CALL release() throw()
104 			{
105 				m_rParent.release();
106 			}
107 
108 		// ::com::sun::star::container::XElementAccess
getElementType()109 			virtual staruno::Type SAL_CALL getElementType(  ) throw(staruno::RuntimeException)
110 			{
111 				return::getCppuType(static_cast< staruno::Reference< starbeans::XPropertySet>*>(NULL));
112 			}
113 
hasElements()114 			virtual sal_Bool SAL_CALL hasElements(  ) throw(staruno::RuntimeException)
115 			{
116 				::osl::MutexGuard aGuard(m_rMutex);
117 				return getCount() > 0;
118 			}
119 
120 		// starcontainer::XIndexAccess
getCount()121 			virtual sal_Int32 SAL_CALL getCount(  ) throw(staruno::RuntimeException)
122 			{
123 				::osl::MutexGuard aGuard(m_rMutex);
124 				sal_Int32 nCnt = 0;
125 				m_pCollection->get_Count(&nCnt);
126 				return nCnt;
127 			}
128 
getByIndex(sal_Int32 Index)129 			virtual staruno::Any SAL_CALL getByIndex( sal_Int32 Index ) throw(starlang::IndexOutOfBoundsException, starlang::WrappedTargetException, staruno::RuntimeException)
130 			{
131 				::osl::MutexGuard aGuard(m_rMutex);
132 				if (Index < 0 || Index >= getCount())
133 					throw starlang::IndexOutOfBoundsException();
134 				SimT* pCol = NULL;
135 				m_pCollection->get_Item(OLEVariant(Index),&pCol);
136 				if(!pCol)
137 					throw starlang::IndexOutOfBoundsException();
138 
139 				OCl* pIndex = new OCl(pCol);
140 
141 				m_aElements.push_back(pIndex);
142 
143 				return staruno::makeAny( staruno::Reference< starbeans::XPropertySet >(pIndex));
144 			}
145 
146 
147 		// starcontainer::XNameAccess
getByName(const::rtl::OUString & aName)148 			virtual staruno::Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException)
149 			{
150 				::osl::MutexGuard aGuard(m_rMutex);
151 
152 				SimT* pCol = NULL;
153 				m_pCollection->get_Item(OLEVariant(aName),&pCol);
154 				if(!pCol)
155 					throw starlang::IndexOutOfBoundsException();
156 
157 				OCl* pIndex = new OCl(pCol);
158 
159 				m_aElements.push_back(pIndex);
160 
161 				return staruno::makeAny( staruno::Reference< starbeans::XPropertySet >(pIndex));
162 			}
getElementNames()163 			virtual staruno::Sequence< ::rtl::OUString > SAL_CALL getElementNames(  ) throw(staruno::RuntimeException)
164 			{
165 				::osl::MutexGuard aGuard(m_rMutex);
166 				sal_Int32 nLen = getCount();
167 				staruno::Sequence< ::rtl::OUString > aNameList(nLen);
168 
169 				::rtl::OUString* pStringArray = aNameList.getArray();
170 				OLEVariant aVar;
171 				for (sal_Int32 i=0;i<nLen;++i)
172 				{
173 					aVar.setInt32(i);
174 					SimT* pIdx = NULL;
175 					m_pCollection->get_Item(aVar,&pIdx);
176 					pIdx->AddRef();
177 					_bstr_t sBSTR;
178 					pIdx->get_Name(&sBSTR);
179 					(*pStringArray) = (sal_Unicode*)sBSTR;
180 					pIdx->Release();
181 					++pStringArray;
182 				}
183 				return aNameList;
184 			}
hasByName(const::rtl::OUString & aName)185 			virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw(staruno::RuntimeException)
186 			{
187 				::osl::MutexGuard aGuard(m_rMutex);
188 				SimT* pCol = NULL;
189 				m_pCollection->get_Item(OLEVariant(aName),&pCol);
190 				return pCol != NULL;
191 			}
192 
disposing()193 			void SAL_CALL disposing()
194 			{
195 				::osl::MutexGuard aGuard(m_rMutex);
196 				for (::std::vector<OCl*>::const_iterator i = m_aElements.begin(); i != m_aElements.end(); ++i)
197 				{
198 					(*i)->disposing();
199 					(*i)->release();
200 				}
201 				m_aElements.clear();
202 			}
203 
204 		};
205 
206 		class OIndex;
207 		class OKey;
208 		class OColumn;
209 		class OTable;
210 		class OView;
211 		class OGroup;
212 		class OUser;
213 
214 		typedef OCollection< ADOIndexes,ADOIndex,OIndex>	OIndexes;
215 		typedef OCollection< ADOKeys,ADOKey,OKey>			OKeys;
216 		typedef OCollection< ADOColumns,ADOColumn,OColumn>	OColumns;
217 		typedef OCollection< ADOTables,ADOTable,OTable>		OTables;
218 		typedef OCollection< ADOViews,ADOView,OView>		OViews;
219 		typedef OCollection< ADOGroups,ADOGroup,OGroup>		OGroups;
220 		typedef OCollection< ADOUsers,ADOUser,OUser>		OUsers;
221 
222 	}
223 }
224 #endif // _CONNECTIVITY_ADO_COLLECTION_HXX_
225 
226 
227 
228