update dev300-m57
[ooovba.git] / ucbhelper / workben / myucp / myucp_datasupplier.cxx
blob655a12de5644d0ef157e61a82cb474c996b53e16
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.9 $
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_ucbhelper.hxx"
34 /**************************************************************************
35 TODO
36 **************************************************************************
38 *************************************************************************/
40 #include <vector>
42 #include "ucbhelper/contentidentifier.hxx"
43 #include "ucbhelper/providerhelper.hxx"
45 #include "myucp_datasupplier.hxx"
46 #include "myucp_content.hxx"
48 using namespace com::sun::star;
50 // @@@ Adjust namespace name.
51 namespace myucp
54 //=========================================================================
56 // struct ResultListEntry.
58 //=========================================================================
60 struct ResultListEntry
62 rtl::OUString aId;
63 uno::Reference< ucb::XContentIdentifier > xId;
64 uno::Reference< ucb::XContent > xContent;
65 uno::Reference< sdbc::XRow > xRow;
66 const ContentProperties& rData;
68 ResultListEntry( const ContentProperties& rEntry ) : rData( rEntry ) {}
71 //=========================================================================
73 // ResultList.
75 //=========================================================================
77 typedef std::vector< ResultListEntry* > ResultList;
79 //=========================================================================
81 // struct DataSupplier_Impl.
83 //=========================================================================
85 struct DataSupplier_Impl
87 osl::Mutex m_aMutex;
88 ResultList m_aResults;
89 rtl::Reference< Content > m_xContent;
90 uno::Reference< lang::XMultiServiceFactory > m_xSMgr;
91 // @@@ The data source and an iterator for it
92 // Entry m_aFolder;
93 // Entry::iterator m_aIterator;
94 sal_Int32 m_nOpenMode;
95 sal_Bool m_bCountFinal;
97 DataSupplier_Impl( const uno::Reference< lang::XMultiServiceFactory >& rxSMgr,
98 const rtl::Reference< Content >& rContent,
99 sal_Int32 nOpenMode )
100 : m_xContent( rContent ), m_xSMgr( rxSMgr ),
101 // m_aFolder( rxSMgr, rContent->getIdentifier()->getContentIdentifier() ),
102 m_nOpenMode( nOpenMode ), m_bCountFinal( sal_False ) {}
103 ~DataSupplier_Impl();
106 //=========================================================================
107 DataSupplier_Impl::~DataSupplier_Impl()
109 ResultList::const_iterator it = m_aResults.begin();
110 ResultList::const_iterator end = m_aResults.end();
112 while ( it != end )
114 delete (*it);
115 it++;
119 //=========================================================================
120 //=========================================================================
122 // DataSupplier Implementation.
124 //=========================================================================
125 //=========================================================================
127 DataSupplier::DataSupplier( const uno::Reference< lang::XMultiServiceFactory >& rxSMgr,
128 const rtl::Reference< Content >& rContent,
129 sal_Int32 nOpenMode )
130 : m_pImpl( new DataSupplier_Impl( rxSMgr, rContent, nOpenMode ) )
134 //=========================================================================
135 // virtual
136 DataSupplier::~DataSupplier()
138 delete m_pImpl;
141 //=========================================================================
142 // virtual
143 rtl::OUString DataSupplier::queryContentIdentifierString( sal_uInt32 nIndex )
145 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
147 if ( nIndex < m_pImpl->m_aResults.size() )
149 rtl::OUString aId = m_pImpl->m_aResults[ nIndex ]->aId;
150 if ( aId.getLength() )
152 // Already cached.
153 return aId;
157 if ( getResult( nIndex ) )
159 rtl::OUString aId
160 = m_pImpl->m_xContent->getIdentifier()->getContentIdentifier();
162 aId += m_pImpl->m_aResults[ nIndex ]->rData.aTitle;
164 m_pImpl->m_aResults[ nIndex ]->aId = aId;
165 return aId;
167 return rtl::OUString();
170 //=========================================================================
171 // virtual
172 uno::Reference< ucb::XContentIdentifier >
173 DataSupplier::queryContentIdentifier( sal_uInt32 nIndex )
175 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
177 if ( nIndex < m_pImpl->m_aResults.size() )
179 uno::Reference< ucb::XContentIdentifier > xId
180 = m_pImpl->m_aResults[ nIndex ]->xId;
181 if ( xId.is() )
183 // Already cached.
184 return xId;
188 rtl::OUString aId = queryContentIdentifierString( nIndex );
189 if ( aId.getLength() )
191 uno::Reference< ucb::XContentIdentifier > xId
192 = new ::ucbhelper::ContentIdentifier( aId );
193 m_pImpl->m_aResults[ nIndex ]->xId = xId;
194 return xId;
196 return uno::Reference< ucb::XContentIdentifier >();
199 //=========================================================================
200 // virtual
201 uno::Reference< ucb::XContent >
202 DataSupplier::queryContent( sal_uInt32 nIndex )
204 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
206 if ( nIndex < m_pImpl->m_aResults.size() )
208 uno::Reference< ucb::XContent > xContent
209 = m_pImpl->m_aResults[ nIndex ]->xContent;
210 if ( xContent.is() )
212 // Already cached.
213 return xContent;
217 uno::Reference< ucb::XContentIdentifier > xId
218 = queryContentIdentifier( nIndex );
219 if ( xId.is() )
223 uno::Reference< ucb::XContent > xContent
224 = m_pImpl->m_xContent->getProvider()->queryContent( xId );
225 m_pImpl->m_aResults[ nIndex ]->xContent = xContent;
226 return xContent;
229 catch ( ucb::IllegalIdentifierException& )
233 return uno::Reference< ucb::XContent >();
236 //=========================================================================
237 // virtual
238 sal_Bool DataSupplier::getResult( sal_uInt32 nIndex )
240 osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
242 if ( m_pImpl->m_aResults.size() > nIndex )
244 // Result already present.
245 return sal_True;
248 // Result not (yet) present.
250 if ( m_pImpl->m_bCountFinal )
251 return sal_False;
253 // Try to obtain result...
255 sal_uInt32 nOldCount = m_pImpl->m_aResults.size();
256 sal_Bool bFound = sal_False;
258 // @@@ Obtain data and put it into result list...
260 sal_uInt32 nPos = nOldCount;
261 while ( m_pImpl->m_aFolder.next( m_pImpl->m_aIterator ) )
263 m_pImpl->m_aResults.push_back(
264 new ResultListEntry( *m_pImpl->m_aIterator ) );
266 if ( nPos == nIndex )
268 // Result obtained.
269 bFound = sal_True;
270 break;
273 nPos++;
277 if ( !bFound )
278 m_pImpl->m_bCountFinal = sal_True;
280 rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet().get();
281 if ( xResultSet.is() )
283 // Callbacks follow!
284 aGuard.clear();
286 if ( nOldCount < m_pImpl->m_aResults.size() )
287 xResultSet->rowCountChanged(
288 nOldCount, m_pImpl->m_aResults.size() );
290 if ( m_pImpl->m_bCountFinal )
291 xResultSet->rowCountFinal();
294 return bFound;
297 //=========================================================================
298 // virtual
299 sal_uInt32 DataSupplier::totalCount()
301 osl::ClearableGuard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
303 if ( m_pImpl->m_bCountFinal )
304 return m_pImpl->m_aResults.size();
306 sal_uInt32 nOldCount = m_pImpl->m_aResults.size();
308 // @@@ Obtain data and put it into result list...
310 while ( m_pImpl->m_aFolder.next( m_pImpl->m_aIterator ) )
311 m_pImpl->m_aResults.push_back(
312 new ResultListEntry( *m_pImpl->m_aIterator ) );
314 m_pImpl->m_bCountFinal = sal_True;
316 rtl::Reference< ::ucbhelper::ResultSet > xResultSet = getResultSet().get();
317 if ( xResultSet.is() )
319 // Callbacks follow!
320 aGuard.clear();
322 if ( nOldCount < m_pImpl->m_aResults.size() )
323 xResultSet->rowCountChanged(
324 nOldCount, m_pImpl->m_aResults.size() );
326 xResultSet->rowCountFinal();
329 return m_pImpl->m_aResults.size();
332 //=========================================================================
333 // virtual
334 sal_uInt32 DataSupplier::currentCount()
336 return m_pImpl->m_aResults.size();
339 //=========================================================================
340 // virtual
341 sal_Bool DataSupplier::isCountFinal()
343 return m_pImpl->m_bCountFinal;
346 //=========================================================================
347 // virtual
348 uno::Reference< sdbc::XRow >
349 DataSupplier::queryPropertyValues( sal_uInt32 nIndex )
351 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
353 if ( nIndex < m_pImpl->m_aResults.size() )
355 uno::Reference< sdbc::XRow > xRow = m_pImpl->m_aResults[ nIndex ]->xRow;
356 if ( xRow.is() )
358 // Already cached.
359 return xRow;
363 if ( getResult( nIndex ) )
365 uno::Reference< sdbc::XRow > xRow = Content::getPropertyValues(
366 m_pImpl->m_xSMgr,
367 getResultSet()->getProperties(),
368 m_pImpl->m_aResults[ nIndex ]->rData,
369 m_pImpl->m_xContent->getProvider().get(),
370 queryContentIdentifierString( nIndex ) );
371 m_pImpl->m_aResults[ nIndex ]->xRow = xRow;
372 return xRow;
375 return uno::Reference< sdbc::XRow >();
378 //=========================================================================
379 // virtual
380 void DataSupplier::releasePropertyValues( sal_uInt32 nIndex )
382 osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );
384 if ( nIndex < m_pImpl->m_aResults.size() )
385 m_pImpl->m_aResults[ nIndex ]->xRow = uno::Reference< sdbc::XRow >();
388 //=========================================================================
389 // virtual
390 void DataSupplier::close()
394 //=========================================================================
395 // virtual
396 void DataSupplier::validate()
397 throw( ucb::ResultSetException )
401 } // namespace