update dev300-m58
[ooovba.git] / ucb / source / ucp / odma / odma_datasupplier.cxx
blobbbe9ce9a47da81c67ea75a439653d12c65f6cfce
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: odma_datasupplier.cxx,v $
10 * $Revision: 1.7 $
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_ucb.hxx"
34 /**************************************************************************
35 TODO
36 **************************************************************************
38 *************************************************************************/
40 #include <vector>
41 #include <ucbhelper/contentidentifier.hxx>
42 #include <ucbhelper/providerhelper.hxx>
43 #include "odma_datasupplier.hxx"
44 #include "odma_content.hxx"
45 #include "odma_contentprops.hxx"
46 #include "odma_provider.hxx"
48 using namespace com::sun::star::beans;
49 using namespace com::sun::star::lang;
50 using namespace com::sun::star::ucb;
51 using namespace com::sun::star::uno;
52 using namespace com::sun::star::sdbc;
54 using namespace odma;
56 namespace odma
59 //=========================================================================
61 // struct ResultListEntry.
63 //=========================================================================
65 struct ResultListEntry
67 ::rtl::OUString aId;
68 Reference< XContentIdentifier > xId;
69 Reference< XContent > xContent;
70 Reference< XRow > xRow;
71 ::rtl::Reference<ContentProperties> rData;
73 ResultListEntry( const ::rtl::Reference<ContentProperties>& rEntry ) : rData( rEntry ) {}
76 //=========================================================================
78 // ResultList.
80 //=========================================================================
82 typedef std::vector< ResultListEntry* > ResultList;
84 //=========================================================================
86 // struct DataSupplier_Impl.
88 //=========================================================================
90 struct DataSupplier_Impl
92 osl::Mutex m_aMutex;
93 ResultList m_aResults;
94 rtl::Reference< Content > m_xContent;
95 Reference< XMultiServiceFactory > m_xSMgr;
96 // @@@ The data source and an iterator for it
97 // Entry m_aFolder;
98 // Entry::iterator m_aIterator;
99 sal_Int32 m_nOpenMode;
100 sal_Bool m_bCountFinal;
102 DataSupplier_Impl( const Reference< XMultiServiceFactory >& rxSMgr,
103 const rtl::Reference< Content >& rContent,
104 sal_Int32 nOpenMode )
105 : m_xContent( rContent ), m_xSMgr( rxSMgr ),
106 // m_aFolder( rxSMgr, rContent->getIdentifier()->getContentIdentifier() ),
107 m_nOpenMode( nOpenMode ), m_bCountFinal( sal_False ) {}
108 ~DataSupplier_Impl();
111 //=========================================================================
112 DataSupplier_Impl::~DataSupplier_Impl()
114 ResultList::const_iterator it = m_aResults.begin();
115 ResultList::const_iterator end = m_aResults.end();
117 while ( it != end )
119 delete (*it);
120 it++;
126 //=========================================================================
127 //=========================================================================
129 // DataSupplier Implementation.
131 //=========================================================================
132 //=========================================================================
134 DataSupplier::DataSupplier( const Reference<XMultiServiceFactory >& rxSMgr,
135 const rtl::Reference< ::odma::Content >& rContent,
136 sal_Int32 nOpenMode )
137 : m_pImpl( new DataSupplier_Impl( rxSMgr, rContent, nOpenMode ) )
141 //=========================================================================
142 // virtual
143 DataSupplier::~DataSupplier()
145 delete m_pImpl;
148 //=========================================================================
149 // virtual
150 ::rtl::OUString DataSupplier::queryContentIdentifierString( sal_uInt32 nIndex )
152 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
154 if ( nIndex < m_pImpl->m_aResults.size() )
156 ::rtl::OUString aId = m_pImpl->m_aResults[ nIndex ]->aId;
157 if ( aId.getLength() )
159 // Already cached.
160 return aId;
164 if ( getResult( nIndex ) )
166 ::rtl::OUString aId
167 = m_pImpl->m_xContent->getIdentifier()->getContentIdentifier();
169 aId += m_pImpl->m_aResults[ nIndex ]->rData->m_sTitle;
171 m_pImpl->m_aResults[ nIndex ]->aId = aId;
172 return aId;
174 return ::rtl::OUString();
177 //=========================================================================
178 // virtual
179 Reference< XContentIdentifier > DataSupplier::queryContentIdentifier(
180 sal_uInt32 nIndex )
182 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
184 if ( nIndex < m_pImpl->m_aResults.size() )
186 Reference< XContentIdentifier > xId
187 = m_pImpl->m_aResults[ nIndex ]->xId;
188 if ( xId.is() )
190 // Already cached.
191 return xId;
195 ::rtl::OUString aId = queryContentIdentifierString( nIndex );
196 if ( aId.getLength() )
198 Reference< XContentIdentifier > xId
199 = new ucbhelper::ContentIdentifier( aId );
200 m_pImpl->m_aResults[ nIndex ]->xId = xId;
201 return xId;
203 return Reference< XContentIdentifier >();
206 //=========================================================================
207 // virtual
208 Reference< XContent > DataSupplier::queryContent( sal_uInt32 nIndex )
210 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
212 if ( nIndex < m_pImpl->m_aResults.size() )
214 Reference< XContent > xContent
215 = m_pImpl->m_aResults[ nIndex ]->xContent;
216 if ( xContent.is() )
218 // Already cached.
219 return xContent;
223 Reference< XContentIdentifier > xId = queryContentIdentifier( nIndex );
224 if ( xId.is() )
228 Reference< XContent > xContent
229 = m_pImpl->m_xContent->getProvider()->queryContent( xId );
230 m_pImpl->m_aResults[ nIndex ]->xContent = xContent;
231 return xContent;
234 catch ( IllegalIdentifierException& )
238 return Reference< XContent >();
241 //=========================================================================
242 // virtual
243 sal_Bool DataSupplier::getResult( sal_uInt32 nIndex )
245 osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
247 if ( m_pImpl->m_aResults.size() > nIndex )
249 // Result already present.
250 return sal_True;
253 // Result not (yet) present.
255 if ( m_pImpl->m_bCountFinal )
256 return sal_False;
258 // Try to obtain result...
260 sal_uInt32 nOldCount = m_pImpl->m_aResults.size();
261 sal_Bool bFound = sal_False;
262 // sal_uInt32 nPos = nOldCount;
264 // @@@ Obtain data and put it into result list...
266 while ( m_pImpl->m_aFolder.next( m_pImpl->m_aIterator ) )
268 m_pImpl->m_aResults.push_back(
269 new ResultListEntry( *m_pImpl->m_aIterator ) );
271 if ( nPos == nIndex )
273 // Result obtained.
274 bFound = sal_True;
275 break;
278 nPos++;
281 // now query for all documents in the DMS
282 OSL_ENSURE(ContentProvider::getHandle(),"No Handle!");
283 sal_Char* pQueryId = new sal_Char[ODM_QUERYID_MAX];
284 sal_Char* lpszDMSList = new sal_Char[ODM_DMSID_MAX];
286 ODMSTATUS odm = NODMGetDMS(ODMA_ODMA_REGNAME, lpszDMSList);
287 lpszDMSList[strlen(lpszDMSList)+1] = '\0';
289 ::rtl::OString sQuery("SELECT ODM_DOCID_LATEST, ODM_NAME");
291 DWORD dwFlags = ODM_SPECIFIC;
292 odm = NODMQueryExecute(ContentProvider::getHandle(), sQuery,dwFlags, lpszDMSList, pQueryId );
293 if(odm != ODM_SUCCESS)
294 return sal_False;
296 sal_uInt16 nCount = 10;
297 sal_uInt16 nMaxCount = 10;
298 sal_Char* lpszDocId = new sal_Char[ODM_DOCID_MAX * nMaxCount];
299 sal_Char* lpszDocName = new sal_Char[ODM_NAME_MAX * nMaxCount];
302 ::rtl::OUString sContentType(RTL_CONSTASCII_USTRINGPARAM(ODMA_CONTENT_TYPE));
303 sal_uInt32 nCurrentCount = 0;
306 if(nCount >= nMaxCount)
308 nCount = nMaxCount;
309 odm = NODMQueryGetResults(ContentProvider::getHandle(), pQueryId,lpszDocId, lpszDocName, ODM_NAME_MAX, (WORD*)&nCount);
310 nCurrentCount += nCount;
312 if(odm == ODM_SUCCESS && nIndex < nCurrentCount)
314 bFound = sal_True;
315 for(sal_uInt16 i = 0; i < nCount; ++i)
317 ::rtl::Reference<ContentProperties> rProps = new ContentProperties();
318 rProps->m_sDocumentId = ::rtl::OString(&lpszDocId[ODM_DOCID_MAX*i]);
319 rProps->m_sContentType = sContentType;
320 m_pImpl->m_xContent->getContentProvider()->append(rProps);
321 m_pImpl->m_aResults.push_back( new ResultListEntry(rProps));
325 while(nCount > nMaxCount);
328 // now close the query
329 odm = NODMQueryClose(ContentProvider::getHandle(), pQueryId);
331 delete lpszDMSList;
332 delete pQueryId;
333 delete lpszDocId;
334 delete lpszDocName;
336 if ( !bFound )
337 m_pImpl->m_bCountFinal = sal_True;
339 rtl::Reference< ucbhelper::ResultSet > xResultSet = getResultSet();
340 if ( xResultSet.is() )
342 // Callbacks follow!
343 aGuard.clear();
345 if ( nOldCount < m_pImpl->m_aResults.size() )
346 xResultSet->rowCountChanged(
347 nOldCount, m_pImpl->m_aResults.size() );
349 if ( m_pImpl->m_bCountFinal )
350 xResultSet->rowCountFinal();
353 return bFound;
356 //=========================================================================
357 // virtual
358 sal_uInt32 DataSupplier::totalCount()
360 osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
362 if ( m_pImpl->m_bCountFinal )
363 return m_pImpl->m_aResults.size();
365 sal_uInt32 nOldCount = m_pImpl->m_aResults.size();
367 // @@@ Obtain data and put it into result list...
369 while ( m_pImpl->m_aFolder.next( m_pImpl->m_aIterator ) )
370 m_pImpl->m_aResults.push_back(
371 new ResultListEntry( *m_pImpl->m_aIterator ) );
373 m_pImpl->m_bCountFinal = sal_True;
375 rtl::Reference< ucbhelper::ResultSet > xResultSet = getResultSet();
376 if ( xResultSet.is() )
378 // Callbacks follow!
379 aGuard.clear();
381 if ( nOldCount < m_pImpl->m_aResults.size() )
382 xResultSet->rowCountChanged(
383 nOldCount, m_pImpl->m_aResults.size() );
385 xResultSet->rowCountFinal();
388 return m_pImpl->m_aResults.size();
391 //=========================================================================
392 // virtual
393 sal_uInt32 DataSupplier::currentCount()
395 return m_pImpl->m_aResults.size();
398 //=========================================================================
399 // virtual
400 sal_Bool DataSupplier::isCountFinal()
402 return m_pImpl->m_bCountFinal;
405 //=========================================================================
406 // virtual
407 Reference< XRow > DataSupplier::queryPropertyValues( sal_uInt32 nIndex )
409 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
411 if ( nIndex < m_pImpl->m_aResults.size() )
413 Reference< XRow > xRow = m_pImpl->m_aResults[ nIndex ]->xRow;
414 if ( xRow.is() )
416 // Already cached.
417 return xRow;
421 if ( getResult( nIndex ) )
423 Reference< XRow > xRow = Content::getPropertyValues(
424 m_pImpl->m_xSMgr,
425 getResultSet()->getProperties(),
426 m_pImpl->m_aResults[ nIndex ]->rData,
427 m_pImpl->m_xContent->getProvider(),
428 queryContentIdentifierString( nIndex ) );
429 m_pImpl->m_aResults[ nIndex ]->xRow = xRow;
430 return xRow;
433 return Reference< XRow >();
436 //=========================================================================
437 // virtual
438 void DataSupplier::releasePropertyValues( sal_uInt32 nIndex )
440 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
442 if ( nIndex < m_pImpl->m_aResults.size() )
443 m_pImpl->m_aResults[ nIndex ]->xRow = Reference< XRow >();
446 //=========================================================================
447 // virtual
448 void DataSupplier::close()
452 //=========================================================================
453 // virtual
454 void DataSupplier::validate()
455 throw( ResultSetException )