1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef _CONNECTIVITY_ADO_COLLECTION_HXX_
29*cdf0e10cSrcweir #define _CONNECTIVITY_ADO_COLLECTION_HXX_
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <cppuhelper/implbase3.hxx>
32*cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/container/XIndexAccess.hpp>
34*cdf0e10cSrcweir #include "ado/Awrapadox.hxx"
35*cdf0e10cSrcweir #include "ado/Aolevariant.hxx"
36*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir namespace connectivity
39*cdf0e10cSrcweir {
40*cdf0e10cSrcweir 	namespace ado
41*cdf0e10cSrcweir 	{
42*cdf0e10cSrcweir 		namespace starcontainer	= ::com::sun::star::container;
43*cdf0e10cSrcweir 		namespace starlang		= ::com::sun::star::lang;
44*cdf0e10cSrcweir 		namespace staruno		= ::com::sun::star::uno;
45*cdf0e10cSrcweir 		namespace starbeans		= ::com::sun::star::beans;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir 		typedef ::cppu::WeakImplHelper3< starcontainer::XNameAccess,
48*cdf0e10cSrcweir 										 starcontainer::XIndexAccess,
49*cdf0e10cSrcweir 										 starlang::XServiceInfo> OCollectionBase;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 		//************************************************************
52*cdf0e10cSrcweir 		//  OCollection
53*cdf0e10cSrcweir 		//************************************************************
54*cdf0e10cSrcweir 		template <class T,class SimT,class OCl> class OCollection : public OCollectionBase
55*cdf0e10cSrcweir 		{
56*cdf0e10cSrcweir 		private:
57*cdf0e10cSrcweir 			OCollection( const OCollection& );				// never implemented
58*cdf0e10cSrcweir 			OCollection& operator=( const OCollection& );	// never implemented
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir 		protected:
61*cdf0e10cSrcweir 			vector<OCl*>							m_aElements;
62*cdf0e10cSrcweir 			::cppu::OWeakObject&					m_rParent;
63*cdf0e10cSrcweir 			::osl::Mutex&							m_rMutex;		// mutex of the parent
64*cdf0e10cSrcweir 			T*										m_pCollection;
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir 		public:
68*cdf0e10cSrcweir 			OCollection(::cppu::OWeakObject& _rParent, ::osl::Mutex& _rMutex,T* _pCollection)
69*cdf0e10cSrcweir 					 : m_rParent(_rParent)
70*cdf0e10cSrcweir 					 ,m_rMutex(_rMutex)
71*cdf0e10cSrcweir 					 ,m_pCollection(_pCollection)
72*cdf0e10cSrcweir 			{
73*cdf0e10cSrcweir 				m_pCollection->AddRef();
74*cdf0e10cSrcweir 			}
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir 			~OCollection()
77*cdf0e10cSrcweir 			{
78*cdf0e10cSrcweir 				m_pCollection->Release();
79*cdf0e10cSrcweir 			}
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 			virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw (staruno::RuntimeException)
82*cdf0e10cSrcweir 			{
83*cdf0e10cSrcweir 				return ::rtl::OUString::createFromAscii("com.sun.star.sdbcx.ACollection");
84*cdf0e10cSrcweir 			}
85*cdf0e10cSrcweir 			virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& _rServiceName ) throw(staruno::RuntimeException)
86*cdf0e10cSrcweir 			{
87*cdf0e10cSrcweir 				staruno::Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());
88*cdf0e10cSrcweir 				const ::rtl::OUString* pSupported = aSupported.getConstArray();
89*cdf0e10cSrcweir 				for (sal_Int32 i=0; i<aSupported.getLength(); ++i, ++pSupported)
90*cdf0e10cSrcweir 					if (pSupported->equals(_rServiceName))
91*cdf0e10cSrcweir 						return sal_True;
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 				return sal_False;
94*cdf0e10cSrcweir 			}
95*cdf0e10cSrcweir 			virtual staruno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw(staruno::RuntimeException)
96*cdf0e10cSrcweir 			{
97*cdf0e10cSrcweir 				staruno::Sequence< ::rtl::OUString > aSupported(1);
98*cdf0e10cSrcweir 				aSupported[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdbcx.Container");
99*cdf0e10cSrcweir 				return aSupported;
100*cdf0e10cSrcweir 			}
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir 			// dispatch the refcounting to the parent
103*cdf0e10cSrcweir 			virtual void SAL_CALL acquire() throw()
104*cdf0e10cSrcweir 			{
105*cdf0e10cSrcweir 				m_rParent.acquire();
106*cdf0e10cSrcweir 			}
107*cdf0e10cSrcweir 			virtual void SAL_CALL release() throw()
108*cdf0e10cSrcweir 			{
109*cdf0e10cSrcweir 				m_rParent.release();
110*cdf0e10cSrcweir 			}
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 		// ::com::sun::star::container::XElementAccess
113*cdf0e10cSrcweir 			virtual staruno::Type SAL_CALL getElementType(  ) throw(staruno::RuntimeException)
114*cdf0e10cSrcweir 			{
115*cdf0e10cSrcweir 				return::getCppuType(static_cast< staruno::Reference< starbeans::XPropertySet>*>(NULL));
116*cdf0e10cSrcweir 			}
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 			virtual sal_Bool SAL_CALL hasElements(  ) throw(staruno::RuntimeException)
119*cdf0e10cSrcweir 			{
120*cdf0e10cSrcweir 				::osl::MutexGuard aGuard(m_rMutex);
121*cdf0e10cSrcweir 				return getCount() > 0;
122*cdf0e10cSrcweir 			}
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 		// starcontainer::XIndexAccess
125*cdf0e10cSrcweir 			virtual sal_Int32 SAL_CALL getCount(  ) throw(staruno::RuntimeException)
126*cdf0e10cSrcweir 			{
127*cdf0e10cSrcweir 				::osl::MutexGuard aGuard(m_rMutex);
128*cdf0e10cSrcweir 				sal_Int32 nCnt = 0;
129*cdf0e10cSrcweir 				m_pCollection->get_Count(&nCnt);
130*cdf0e10cSrcweir 				return nCnt;
131*cdf0e10cSrcweir 			}
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 			virtual staruno::Any SAL_CALL getByIndex( sal_Int32 Index ) throw(starlang::IndexOutOfBoundsException, starlang::WrappedTargetException, staruno::RuntimeException)
134*cdf0e10cSrcweir 			{
135*cdf0e10cSrcweir 				::osl::MutexGuard aGuard(m_rMutex);
136*cdf0e10cSrcweir 				if (Index < 0 || Index >= getCount())
137*cdf0e10cSrcweir 					throw starlang::IndexOutOfBoundsException();
138*cdf0e10cSrcweir 				SimT* pCol = NULL;
139*cdf0e10cSrcweir 				m_pCollection->get_Item(OLEVariant(Index),&pCol);
140*cdf0e10cSrcweir 				if(!pCol)
141*cdf0e10cSrcweir 					throw starlang::IndexOutOfBoundsException();
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 				OCl* pIndex = new OCl(pCol);
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 				m_aElements.push_back(pIndex);
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 				return staruno::makeAny( staruno::Reference< starbeans::XPropertySet >(pIndex));
148*cdf0e10cSrcweir 			}
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 		// starcontainer::XNameAccess
152*cdf0e10cSrcweir 			virtual staruno::Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException)
153*cdf0e10cSrcweir 			{
154*cdf0e10cSrcweir 				::osl::MutexGuard aGuard(m_rMutex);
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 				SimT* pCol = NULL;
157*cdf0e10cSrcweir 				m_pCollection->get_Item(OLEVariant(aName),&pCol);
158*cdf0e10cSrcweir 				if(!pCol)
159*cdf0e10cSrcweir 					throw starlang::IndexOutOfBoundsException();
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 				OCl* pIndex = new OCl(pCol);
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 				m_aElements.push_back(pIndex);
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir 				return staruno::makeAny( staruno::Reference< starbeans::XPropertySet >(pIndex));
166*cdf0e10cSrcweir 			}
167*cdf0e10cSrcweir 			virtual staruno::Sequence< ::rtl::OUString > SAL_CALL getElementNames(  ) throw(staruno::RuntimeException)
168*cdf0e10cSrcweir 			{
169*cdf0e10cSrcweir 				::osl::MutexGuard aGuard(m_rMutex);
170*cdf0e10cSrcweir 				sal_Int32 nLen = getCount();
171*cdf0e10cSrcweir 				staruno::Sequence< ::rtl::OUString > aNameList(nLen);
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 				::rtl::OUString* pStringArray = aNameList.getArray();
174*cdf0e10cSrcweir 				OLEVariant aVar;
175*cdf0e10cSrcweir 				for (sal_Int32 i=0;i<nLen;++i)
176*cdf0e10cSrcweir 				{
177*cdf0e10cSrcweir 					aVar.setInt32(i);
178*cdf0e10cSrcweir 					SimT* pIdx = NULL;
179*cdf0e10cSrcweir 					m_pCollection->get_Item(aVar,&pIdx);
180*cdf0e10cSrcweir 					pIdx->AddRef();
181*cdf0e10cSrcweir 					_bstr_t sBSTR;
182*cdf0e10cSrcweir 					pIdx->get_Name(&sBSTR);
183*cdf0e10cSrcweir 					(*pStringArray) = (sal_Unicode*)sBSTR;
184*cdf0e10cSrcweir 					pIdx->Release();
185*cdf0e10cSrcweir 					++pStringArray;
186*cdf0e10cSrcweir 				}
187*cdf0e10cSrcweir 				return aNameList;
188*cdf0e10cSrcweir 			}
189*cdf0e10cSrcweir 			virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw(staruno::RuntimeException)
190*cdf0e10cSrcweir 			{
191*cdf0e10cSrcweir 				::osl::MutexGuard aGuard(m_rMutex);
192*cdf0e10cSrcweir 				SimT* pCol = NULL;
193*cdf0e10cSrcweir 				m_pCollection->get_Item(OLEVariant(aName),&pCol);
194*cdf0e10cSrcweir 				return pCol != NULL;
195*cdf0e10cSrcweir 			}
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 			void SAL_CALL disposing()
198*cdf0e10cSrcweir 			{
199*cdf0e10cSrcweir 				::osl::MutexGuard aGuard(m_rMutex);
200*cdf0e10cSrcweir 				for (::std::vector<OCl*>::const_iterator i = m_aElements.begin(); i != m_aElements.end(); ++i)
201*cdf0e10cSrcweir 				{
202*cdf0e10cSrcweir 					(*i)->disposing();
203*cdf0e10cSrcweir 					(*i)->release();
204*cdf0e10cSrcweir 				}
205*cdf0e10cSrcweir 				m_aElements.clear();
206*cdf0e10cSrcweir 			}
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir 		};
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 		class OIndex;
211*cdf0e10cSrcweir 		class OKey;
212*cdf0e10cSrcweir 		class OColumn;
213*cdf0e10cSrcweir 		class OTable;
214*cdf0e10cSrcweir 		class OView;
215*cdf0e10cSrcweir 		class OGroup;
216*cdf0e10cSrcweir 		class OUser;
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 		typedef OCollection< ADOIndexes,ADOIndex,OIndex>	OIndexes;
219*cdf0e10cSrcweir 		typedef OCollection< ADOKeys,ADOKey,OKey>			OKeys;
220*cdf0e10cSrcweir 		typedef OCollection< ADOColumns,ADOColumn,OColumn>	OColumns;
221*cdf0e10cSrcweir 		typedef OCollection< ADOTables,ADOTable,OTable>		OTables;
222*cdf0e10cSrcweir 		typedef OCollection< ADOViews,ADOView,OView>		OViews;
223*cdf0e10cSrcweir 		typedef OCollection< ADOGroups,ADOGroup,OGroup>		OGroups;
224*cdf0e10cSrcweir 		typedef OCollection< ADOUsers,ADOUser,OUser>		OUsers;
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 	}
227*cdf0e10cSrcweir }
228*cdf0e10cSrcweir #endif // _CONNECTIVITY_ADO_COLLECTION_HXX_
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir 
232