Update ooo320-m1
[ooovba.git] / dbaccess / source / core / dataaccess / myucp_datasupplier.cxx
blobb2b82f85d996cad216f4946ed195d5cf6ff651f1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: myucp_datasupplier.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 /**************************************************************************
35 TODO
36 **************************************************************************
38 *************************************************************************/
40 #include <vector>
42 #ifndef _UCBHELPER_CONTENTIDENTIFIER_HXX
43 #include <ucbhelper/contentidentifier.hxx>
44 #endif
45 #ifndef _UCBHELPER_PROVIDERHELPER_HXX
46 #include <ucbhelper/providerhelper.hxx>
47 #endif
49 #ifndef DBA_DATASUPPLIER_HXX
50 #include "myucp_datasupplier.hxx"
51 #endif
52 #ifndef DBA_CONTENTHELPER_HXX
53 #include "ContentHelper.hxx"
54 #endif
55 #ifndef _COM_SUN_STAR_CONTAINER_XHIERARCHICALNAMEACCESS_HPP_
56 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
57 #endif
58 #ifndef _TOOLS_DEBUG_HXX
59 #include <tools/debug.hxx>
60 #endif
62 using namespace ::com::sun::star::uno;
63 using namespace ::com::sun::star::ucb;
64 using namespace ::com::sun::star::beans;
65 using namespace ::com::sun::star::lang;
66 using namespace ::com::sun::star::sdbc;
67 using namespace ::com::sun::star::io;
68 using namespace ::com::sun::star::container;
70 // @@@ Adjust namespace name.
71 using namespace dbaccess;
73 // @@@ Adjust namespace name.
74 namespace dbaccess
77 //=========================================================================
79 // struct ResultListEntry.
81 //=========================================================================
83 struct ResultListEntry
85 rtl::OUString aId;
86 Reference< XContentIdentifier > xId;
87 ::rtl::Reference< OContentHelper > xContent;
88 Reference< XRow > xRow;
89 const ContentProperties& rData;
91 ResultListEntry( const ContentProperties& rEntry ) : rData( rEntry ) {}
94 //=========================================================================
96 // ResultList.
98 //=========================================================================
100 typedef std::vector< ResultListEntry* > ResultList;
102 //=========================================================================
104 // struct DataSupplier_Impl.
106 //=========================================================================
108 struct DataSupplier_Impl
110 osl::Mutex m_aMutex;
111 ResultList m_aResults;
112 rtl::Reference< ODocumentContainer > m_xContent;
113 Reference< XMultiServiceFactory > m_xSMgr;
114 sal_Int32 m_nOpenMode;
115 sal_Bool m_bCountFinal;
117 DataSupplier_Impl( const Reference< XMultiServiceFactory >& rxSMgr,
118 const rtl::Reference< ODocumentContainer >& rContent,
119 sal_Int32 nOpenMode )
120 : m_xContent(rContent)
121 , m_xSMgr( rxSMgr )
122 , m_nOpenMode( nOpenMode )
123 , m_bCountFinal( sal_False ) {}
124 ~DataSupplier_Impl();
127 //=========================================================================
128 DataSupplier_Impl::~DataSupplier_Impl()
130 ResultList::const_iterator it = m_aResults.begin();
131 ResultList::const_iterator end = m_aResults.end();
133 while ( it != end )
135 delete (*it);
136 it++;
142 //=========================================================================
143 //=========================================================================
145 // DataSupplier Implementation.
147 //=========================================================================
148 //=========================================================================
149 DBG_NAME(DataSupplier)
151 DataSupplier::DataSupplier( const Reference< XMultiServiceFactory >& rxSMgr,
152 const rtl::Reference< ODocumentContainer >& rContent,
153 sal_Int32 nOpenMode )
154 : m_pImpl( new DataSupplier_Impl( rxSMgr, rContent,nOpenMode ) )
156 DBG_CTOR(DataSupplier,NULL);
160 //=========================================================================
161 // virtual
162 DataSupplier::~DataSupplier()
165 DBG_DTOR(DataSupplier,NULL);
168 //=========================================================================
169 // virtual
170 rtl::OUString DataSupplier::queryContentIdentifierString( sal_uInt32 nIndex )
172 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
174 if ( (size_t)nIndex < m_pImpl->m_aResults.size() )
176 rtl::OUString aId = m_pImpl->m_aResults[ nIndex ]->aId;
177 if ( aId.getLength() )
179 // Already cached.
180 return aId;
184 if ( getResult( nIndex ) )
186 rtl::OUString aId
187 = m_pImpl->m_xContent->getIdentifier()->getContentIdentifier();
189 if ( aId.getLength() )
190 aId += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/"));
192 aId += m_pImpl->m_aResults[ nIndex ]->rData.aTitle;
194 m_pImpl->m_aResults[ nIndex ]->aId = aId;
195 return aId;
197 return rtl::OUString();
200 //=========================================================================
201 // virtual
202 Reference< XContentIdentifier >
203 DataSupplier::queryContentIdentifier( sal_uInt32 nIndex )
205 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
207 if ( (size_t)nIndex < m_pImpl->m_aResults.size() )
209 Reference< XContentIdentifier > xId = m_pImpl->m_aResults[ nIndex ]->xId;
210 if ( xId.is() )
212 // Already cached.
213 return xId;
217 rtl::OUString aId = queryContentIdentifierString( nIndex );
218 if ( aId.getLength() )
220 Reference< XContentIdentifier > xId = new ::ucbhelper::ContentIdentifier( aId );
221 m_pImpl->m_aResults[ nIndex ]->xId = xId;
222 return xId;
224 return Reference< XContentIdentifier >();
227 //=========================================================================
228 // virtual
229 Reference< XContent >
230 DataSupplier::queryContent( sal_uInt32 _nIndex )
232 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
234 if ( (size_t)_nIndex < m_pImpl->m_aResults.size() )
236 Reference< XContent > xContent = m_pImpl->m_aResults[ _nIndex ]->xContent.get();
237 if ( xContent.is() )
239 // Already cached.
240 return xContent;
244 Reference< XContentIdentifier > xId = queryContentIdentifier( _nIndex );
245 if ( xId.is() )
249 Reference< XContent > xContent;
250 ::rtl::OUString sName = xId->getContentIdentifier();
251 sal_Int32 nIndex = sName.lastIndexOf('/') + 1;
252 sName = sName.getToken(0,'/',nIndex);
254 m_pImpl->m_aResults[ _nIndex ]->xContent = m_pImpl->m_xContent->getContent(sName);
256 xContent = m_pImpl->m_aResults[ _nIndex ]->xContent.get();
257 return xContent;
260 catch ( IllegalIdentifierException& )
264 return Reference< XContent >();
267 //=========================================================================
268 // virtual
269 sal_Bool DataSupplier::getResult( sal_uInt32 nIndex )
271 osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
273 if ( (size_t)nIndex < m_pImpl->m_aResults.size() )
275 // Result already present.
276 return sal_True;
279 // Result not (yet) present.
281 if ( m_pImpl->m_bCountFinal )
282 return sal_False;
284 // Try to obtain result...
286 sal_uInt32 nOldCount = m_pImpl->m_aResults.size();
287 sal_Bool bFound = sal_False;
288 sal_uInt32 nPos = nOldCount;
290 // @@@ Obtain data and put it into result list...
291 Sequence< ::rtl::OUString> aSeq = m_pImpl->m_xContent->getElementNames();
292 if ( nIndex < sal::static_int_cast< sal_uInt32 >( aSeq.getLength() ) )
294 const ::rtl::OUString* pIter = aSeq.getConstArray();
295 const ::rtl::OUString* pEnd = pIter + aSeq.getLength();
296 for(pIter = pIter + nPos;pIter != pEnd;++pIter,++nPos)
298 m_pImpl->m_aResults.push_back(
299 new ResultListEntry( m_pImpl->m_xContent->getContent(*pIter)->getContentProperties() ) );
301 if ( nPos == nIndex )
303 // Result obtained.
304 bFound = sal_True;
305 break;
310 if ( !bFound )
311 m_pImpl->m_bCountFinal = sal_True;
313 rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet().get();
314 if ( xResultSet.is() )
316 // Callbacks follow!
317 aGuard.clear();
319 if ( (size_t)nOldCount < m_pImpl->m_aResults.size() )
320 xResultSet->rowCountChanged(
321 nOldCount, m_pImpl->m_aResults.size() );
323 if ( m_pImpl->m_bCountFinal )
324 xResultSet->rowCountFinal();
327 return bFound;
330 //=========================================================================
331 // virtual
332 sal_uInt32 DataSupplier::totalCount()
334 osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
336 if ( m_pImpl->m_bCountFinal )
337 return m_pImpl->m_aResults.size();
339 sal_uInt32 nOldCount = m_pImpl->m_aResults.size();
341 // @@@ Obtain data and put it into result list...
342 Sequence< ::rtl::OUString> aSeq = m_pImpl->m_xContent->getElementNames();
343 const ::rtl::OUString* pIter = aSeq.getConstArray();
344 const ::rtl::OUString* pEnd = pIter + aSeq.getLength();
345 for(;pIter != pEnd;++pIter)
346 m_pImpl->m_aResults.push_back(
347 new ResultListEntry( m_pImpl->m_xContent->getContent(*pIter)->getContentProperties() ) );
349 m_pImpl->m_bCountFinal = sal_True;
351 rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet().get();
352 if ( xResultSet.is() )
354 // Callbacks follow!
355 aGuard.clear();
357 if ( (size_t)nOldCount < m_pImpl->m_aResults.size() )
358 xResultSet->rowCountChanged(
359 nOldCount, m_pImpl->m_aResults.size() );
361 xResultSet->rowCountFinal();
364 return m_pImpl->m_aResults.size();
367 //=========================================================================
368 // virtual
369 sal_uInt32 DataSupplier::currentCount()
371 return m_pImpl->m_aResults.size();
374 //=========================================================================
375 // virtual
376 sal_Bool DataSupplier::isCountFinal()
378 return m_pImpl->m_bCountFinal;
381 //=========================================================================
382 // virtual
383 Reference< XRow >
384 DataSupplier::queryPropertyValues( sal_uInt32 nIndex )
386 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
388 if ( (size_t)nIndex < m_pImpl->m_aResults.size() )
390 Reference< XRow > xRow = m_pImpl->m_aResults[ nIndex ]->xRow;
391 if ( xRow.is() )
393 // Already cached.
394 return xRow;
398 if ( getResult( nIndex ) )
400 if ( !m_pImpl->m_aResults[ nIndex ]->xContent.is() )
401 queryContent(nIndex);
403 Reference< XRow > xRow = m_pImpl->m_aResults[ nIndex ]->xContent->getPropertyValues(getResultSet()->getProperties());
404 m_pImpl->m_aResults[ nIndex ]->xRow = xRow;
405 return xRow;
408 return Reference< XRow >();
411 //=========================================================================
412 // virtual
413 void DataSupplier::releasePropertyValues( sal_uInt32 nIndex )
415 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
417 if ( (size_t)nIndex < m_pImpl->m_aResults.size() )
418 m_pImpl->m_aResults[ nIndex ]->xRow = Reference< XRow >();
421 //=========================================================================
422 // virtual
423 void DataSupplier::close()
427 //=========================================================================
428 // virtual
429 void DataSupplier::validate()
430 throw( ResultSetException )