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