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