1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmlhelp.hxx"
26 #include <ucbhelper/contentidentifier.hxx>
27 #include <com/sun/star/ucb/OpenMode.hpp>
28 #include <com/sun/star/uno/Reference.h>
29 #ifndef _COM_SUN_STAR_BEANS_PROPERTYATTRIBBUTE_HPP_
30 #include <com/sun/star/beans/PropertyAttribute.hpp>
31 #endif
32 #include <com/sun/star/ucb/ListActionType.hpp>
33 #include <com/sun/star/ucb/XSourceInitialization.hpp>
34 #include <ucbhelper/resultsetmetadata.hxx>
35
36 #include "resultsetbase.hxx"
37
38 using namespace chelp;
39 using namespace com::sun::star;
40
ResultSetBase(const uno::Reference<lang::XMultiServiceFactory> & xMSF,const uno::Reference<ucb::XContentProvider> & xProvider,sal_Int32 nOpenMode,const uno::Sequence<beans::Property> & seq,const uno::Sequence<ucb::NumberedSortingInfo> & seqSort)41 ResultSetBase::ResultSetBase( const uno::Reference< lang::XMultiServiceFactory >& xMSF,
42 const uno::Reference< ucb::XContentProvider >& xProvider,
43 sal_Int32 nOpenMode,
44 const uno::Sequence< beans::Property >& seq,
45 const uno::Sequence< ucb::NumberedSortingInfo >& seqSort )
46 : m_xMSF( xMSF ),
47 m_xProvider( xProvider ),
48 m_nRow( -1 ),
49 m_nWasNull( true ),
50 m_nOpenMode( nOpenMode ),
51 m_bRowCountFinal( true ),
52 m_sProperty( seq ),
53 m_sSortingInfo( seqSort ),
54 m_pDisposeEventListeners( 0 ),
55 m_pRowCountListeners( 0 ),
56 m_pIsFinalListeners( 0 )
57 {
58 }
59
~ResultSetBase()60 ResultSetBase::~ResultSetBase()
61 {
62 delete m_pIsFinalListeners;
63 delete m_pRowCountListeners;
64 delete m_pDisposeEventListeners;
65 }
66
67
68 // XInterface
69
70 void SAL_CALL
acquire(void)71 ResultSetBase::acquire(
72 void )
73 throw()
74 {
75 OWeakObject::acquire();
76 }
77
78
79 void SAL_CALL
release(void)80 ResultSetBase::release(
81 void )
82 throw()
83 {
84 OWeakObject::release();
85 }
86
87
88
89 uno::Any SAL_CALL
queryInterface(const uno::Type & rType)90 ResultSetBase::queryInterface(
91 const uno::Type& rType )
92 {
93 uno::Any aRet = cppu::queryInterface( rType,
94 SAL_STATIC_CAST( lang::XComponent*, this),
95 SAL_STATIC_CAST( sdbc::XRow*, this),
96 SAL_STATIC_CAST( sdbc::XResultSet*, this),
97 SAL_STATIC_CAST( sdbc::XResultSetMetaDataSupplier*, this),
98 SAL_STATIC_CAST( beans::XPropertySet*, this ),
99 SAL_STATIC_CAST( ucb::XContentAccess*, this) );
100 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
101 }
102
103
104
105 // XComponent
106
107
108 void SAL_CALL
addEventListener(const uno::Reference<lang::XEventListener> & Listener)109 ResultSetBase::addEventListener(
110 const uno::Reference< lang::XEventListener >& Listener )
111 {
112 osl::MutexGuard aGuard( m_aMutex );
113
114 if ( ! m_pDisposeEventListeners )
115 m_pDisposeEventListeners =
116 new cppu::OInterfaceContainerHelper( m_aMutex );
117
118 m_pDisposeEventListeners->addInterface( Listener );
119 }
120
121
122 void SAL_CALL
removeEventListener(const uno::Reference<lang::XEventListener> & Listener)123 ResultSetBase::removeEventListener(
124 const uno::Reference< lang::XEventListener >& Listener )
125 {
126 osl::MutexGuard aGuard( m_aMutex );
127
128 if ( m_pDisposeEventListeners )
129 m_pDisposeEventListeners->removeInterface( Listener );
130 }
131
132
133
134 void SAL_CALL
dispose()135 ResultSetBase::dispose()
136 {
137 osl::MutexGuard aGuard( m_aMutex );
138
139 lang::EventObject aEvt;
140 aEvt.Source = static_cast< lang::XComponent * >( this );
141
142 if ( m_pDisposeEventListeners && m_pDisposeEventListeners->getLength() )
143 {
144 m_pDisposeEventListeners->disposeAndClear( aEvt );
145 }
146 if( m_pRowCountListeners && m_pRowCountListeners->getLength() )
147 {
148 m_pRowCountListeners->disposeAndClear( aEvt );
149 }
150 if( m_pIsFinalListeners && m_pIsFinalListeners->getLength() )
151 {
152 m_pIsFinalListeners->disposeAndClear( aEvt );
153 }
154 }
155
156
157
158 // XResultSet
159
160 sal_Bool SAL_CALL
next(void)161 ResultSetBase::next(
162 void )
163 {
164 sal_Bool test;
165 m_nRow++;
166 if( sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size() )
167 test = true;
168 else
169 test = false;
170 return test;
171 }
172
173
174 sal_Bool SAL_CALL
isBeforeFirst(void)175 ResultSetBase::isBeforeFirst(
176 void )
177 {
178 return m_nRow == -1;
179 }
180
181
182 sal_Bool SAL_CALL
isAfterLast(void)183 ResultSetBase::isAfterLast(
184 void )
185 {
186 return sal::static_int_cast<sal_uInt32>( m_nRow ) >= m_aItems.size(); // Cannot happen, if m_aFolder.isOpen()
187 }
188
189
190 sal_Bool SAL_CALL
isFirst(void)191 ResultSetBase::isFirst(
192 void )
193 {
194 return m_nRow == 0;
195 }
196
197
198 sal_Bool SAL_CALL
isLast(void)199 ResultSetBase::isLast(
200 void )
201 {
202 if( sal::static_int_cast<sal_uInt32>( m_nRow ) == m_aItems.size() - 1 )
203 return true;
204 else
205 return false;
206 }
207
208
209 void SAL_CALL
beforeFirst(void)210 ResultSetBase::beforeFirst(
211 void )
212 {
213 m_nRow = -1;
214 }
215
216
217 void SAL_CALL
afterLast(void)218 ResultSetBase::afterLast(
219 void )
220 {
221 m_nRow = m_aItems.size();
222 }
223
224
225 sal_Bool SAL_CALL
first(void)226 ResultSetBase::first(
227 void )
228 {
229 m_nRow = -1;
230 return next();
231 }
232
233
234 sal_Bool SAL_CALL
last(void)235 ResultSetBase::last(
236 void )
237 {
238 m_nRow = m_aItems.size() - 1;
239 return true;
240 }
241
242
243 sal_Int32 SAL_CALL
getRow(void)244 ResultSetBase::getRow(
245 void )
246 {
247 // Test, whether behind last row
248 if( -1 == m_nRow || sal::static_int_cast<sal_uInt32>( m_nRow ) >= m_aItems.size() )
249 return 0;
250 else
251 return m_nRow+1;
252 }
253
254
absolute(sal_Int32 row)255 sal_Bool SAL_CALL ResultSetBase::absolute( sal_Int32 row )
256 {
257 if( row >= 0 )
258 m_nRow = row - 1;
259 else
260 {
261 last();
262 m_nRow += ( row + 1 );
263 if( m_nRow < -1 )
264 m_nRow = -1;
265 }
266
267 return 0<= m_nRow && sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size();
268 }
269
270
271
272
273 sal_Bool SAL_CALL
relative(sal_Int32 row)274 ResultSetBase::relative(
275 sal_Int32 row )
276 {
277 if( isAfterLast() || isBeforeFirst() )
278 throw sdbc::SQLException();
279
280 if( row > 0 )
281 while( row-- )
282 next();
283 else if( row < 0 )
284 while( row++ && m_nRow > -1 )
285 previous();
286
287 return 0 <= m_nRow && sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size();
288 }
289
290
291
292 sal_Bool SAL_CALL
previous(void)293 ResultSetBase::previous(
294 void )
295 {
296 if( sal::static_int_cast<sal_uInt32>( m_nRow ) > m_aItems.size() )
297 m_nRow = m_aItems.size(); // Correct Handling of afterLast
298 if( 0 <= m_nRow ) -- m_nRow;
299
300 return 0 <= m_nRow && sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size();
301 }
302
303
304 void SAL_CALL
refreshRow(void)305 ResultSetBase::refreshRow(
306 void )
307 {
308 }
309
310
311 sal_Bool SAL_CALL
rowUpdated(void)312 ResultSetBase::rowUpdated(
313 void )
314 {
315 return false;
316 }
317
318 sal_Bool SAL_CALL
rowInserted(void)319 ResultSetBase::rowInserted(
320 void )
321 {
322 return false;
323 }
324
325 sal_Bool SAL_CALL
rowDeleted(void)326 ResultSetBase::rowDeleted(
327 void )
328 {
329 return false;
330 }
331
332
333 uno::Reference< uno::XInterface > SAL_CALL
getStatement(void)334 ResultSetBase::getStatement(
335 void )
336 {
337 uno::Reference< uno::XInterface > test( 0 );
338 return test;
339 }
340
341
342 // XCloseable
343
344 void SAL_CALL
close(void)345 ResultSetBase::close(
346 void )
347 {
348 }
349
350
351 rtl::OUString SAL_CALL
queryContentIdentifierString(void)352 ResultSetBase::queryContentIdentifierString(
353 void )
354 {
355 if( 0 <= m_nRow && sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size() )
356 return m_aPath[m_nRow];
357 else
358 return rtl::OUString();
359 }
360
361
362 uno::Reference< ucb::XContentIdentifier > SAL_CALL
queryContentIdentifier(void)363 ResultSetBase::queryContentIdentifier(
364 void )
365 {
366 if( 0 <= m_nRow && sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size() )
367 {
368 rtl::OUString url = queryContentIdentifierString();
369 if( ! m_aIdents[m_nRow].is() && url.getLength() )
370 m_aIdents[m_nRow] = uno::Reference< ucb::XContentIdentifier >(
371 new ::ucbhelper::ContentIdentifier( m_xMSF,url ) );
372 return m_aIdents[m_nRow];
373 }
374
375 return uno::Reference< ucb::XContentIdentifier >();
376 }
377
378
379 uno::Reference< ucb::XContent > SAL_CALL
queryContent(void)380 ResultSetBase::queryContent(
381 void )
382 {
383 if( 0 <= m_nRow && sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size() )
384 return m_xProvider->queryContent( queryContentIdentifier() );
385 else
386 return uno::Reference< ucb::XContent >();
387 }
388
389
390
391 class XPropertySetInfoImpl
392 : public cppu::OWeakObject,
393 public beans::XPropertySetInfo
394 {
395 public:
396
XPropertySetInfoImpl(const uno::Sequence<beans::Property> & aSeq)397 XPropertySetInfoImpl( const uno::Sequence< beans::Property >& aSeq )
398 : m_aSeq( aSeq )
399 {
400 }
401
acquire(void)402 void SAL_CALL acquire( void )
403 throw()
404 {
405 OWeakObject::acquire();
406 }
407
408
release(void)409 void SAL_CALL release( void )
410 throw()
411 {
412 OWeakObject::release();
413 }
414
queryInterface(const uno::Type & rType)415 uno::Any SAL_CALL queryInterface( const uno::Type& rType )
416 {
417 uno::Any aRet = cppu::queryInterface( rType,
418 SAL_STATIC_CAST( beans::XPropertySetInfo*, this ) );
419 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
420 }
421
getProperties()422 uno::Sequence< beans::Property > SAL_CALL getProperties()
423 {
424 return m_aSeq;
425 }
426
getPropertyByName(const::rtl::OUString & aName)427 beans::Property SAL_CALL getPropertyByName( const ::rtl::OUString& aName )
428 {
429 for( int i = 0; i < m_aSeq.getLength(); ++i )
430 if( aName == m_aSeq[i].Name )
431 return m_aSeq[i];
432 throw beans::UnknownPropertyException();
433 }
434
hasPropertyByName(const::rtl::OUString & Name)435 sal_Bool SAL_CALL hasPropertyByName( const ::rtl::OUString& Name )
436 {
437 for( int i = 0; i < m_aSeq.getLength(); ++i )
438 if( Name == m_aSeq[i].Name )
439 return true;
440 return false;
441 }
442
443 private:
444
445 uno::Sequence< beans::Property > m_aSeq;
446 };
447
448
449
450 // XPropertySet
451 uno::Reference< beans::XPropertySetInfo > SAL_CALL
getPropertySetInfo()452 ResultSetBase::getPropertySetInfo()
453 {
454 uno::Sequence< beans::Property > seq(2);
455 seq[0].Name = rtl::OUString::createFromAscii( "RowCount" );
456 seq[0].Handle = -1;
457 seq[0].Type = getCppuType( static_cast< sal_Int32* >(0) );
458 seq[0].Attributes = beans::PropertyAttribute::READONLY;
459
460 seq[1].Name = rtl::OUString::createFromAscii( "IsRowCountFinal" );
461 seq[1].Handle = -1;
462 seq[1].Type = getCppuType( static_cast< sal_Bool* >(0) );
463 seq[1].Attributes = beans::PropertyAttribute::READONLY;
464
465 //t
466 return uno::Reference< beans::XPropertySetInfo > ( new XPropertySetInfoImpl( seq ) );
467 }
468
469
470
setPropertyValue(const rtl::OUString & aPropertyName,const uno::Any & aValue)471 void SAL_CALL ResultSetBase::setPropertyValue(
472 const rtl::OUString& aPropertyName, const uno::Any& aValue )
473 {
474 (void)aValue;
475
476 if( aPropertyName == rtl::OUString::createFromAscii( "IsRowCountFinal" ) ||
477 aPropertyName == rtl::OUString::createFromAscii( "RowCount" ) )
478 return;
479
480 throw beans::UnknownPropertyException();
481 }
482
483
getPropertyValue(const rtl::OUString & PropertyName)484 uno::Any SAL_CALL ResultSetBase::getPropertyValue(
485 const rtl::OUString& PropertyName )
486 {
487 if( PropertyName == rtl::OUString::createFromAscii( "IsRowCountFinal" ) )
488 {
489 uno::Any aAny;
490 aAny <<= m_bRowCountFinal;
491 return aAny;
492 }
493 else if ( PropertyName == rtl::OUString::createFromAscii( "RowCount" ) )
494 {
495 uno::Any aAny;
496 sal_Int32 count = m_aItems.size();
497 aAny <<= count;
498 return aAny;
499 }
500 else
501 throw beans::UnknownPropertyException();
502 }
503
504
addPropertyChangeListener(const rtl::OUString & aPropertyName,const uno::Reference<beans::XPropertyChangeListener> & xListener)505 void SAL_CALL ResultSetBase::addPropertyChangeListener(
506 const rtl::OUString& aPropertyName,
507 const uno::Reference< beans::XPropertyChangeListener >& xListener )
508 {
509 if( aPropertyName == rtl::OUString::createFromAscii( "IsRowCountFinal" ) )
510 {
511 osl::MutexGuard aGuard( m_aMutex );
512 if ( ! m_pIsFinalListeners )
513 m_pIsFinalListeners =
514 new cppu::OInterfaceContainerHelper( m_aMutex );
515
516 m_pIsFinalListeners->addInterface( xListener );
517 }
518 else if ( aPropertyName == rtl::OUString::createFromAscii( "RowCount" ) )
519 {
520 osl::MutexGuard aGuard( m_aMutex );
521 if ( ! m_pRowCountListeners )
522 m_pRowCountListeners =
523 new cppu::OInterfaceContainerHelper( m_aMutex );
524 m_pRowCountListeners->addInterface( xListener );
525 }
526 else
527 throw beans::UnknownPropertyException();
528 }
529
530
removePropertyChangeListener(const rtl::OUString & aPropertyName,const uno::Reference<beans::XPropertyChangeListener> & aListener)531 void SAL_CALL ResultSetBase::removePropertyChangeListener(
532 const rtl::OUString& aPropertyName,
533 const uno::Reference< beans::XPropertyChangeListener >& aListener )
534 {
535 if( aPropertyName == rtl::OUString::createFromAscii( "IsRowCountFinal" ) &&
536 m_pIsFinalListeners )
537 {
538 osl::MutexGuard aGuard( m_aMutex );
539 m_pIsFinalListeners->removeInterface( aListener );
540 }
541 else if ( aPropertyName == rtl::OUString::createFromAscii( "RowCount" ) &&
542 m_pRowCountListeners )
543 {
544 osl::MutexGuard aGuard( m_aMutex );
545 m_pRowCountListeners->removeInterface( aListener );
546 }
547 else
548 throw beans::UnknownPropertyException();
549 }
550
551
addVetoableChangeListener(const rtl::OUString & PropertyName,const uno::Reference<beans::XVetoableChangeListener> & aListener)552 void SAL_CALL ResultSetBase::addVetoableChangeListener(
553 const rtl::OUString& PropertyName,
554 const uno::Reference< beans::XVetoableChangeListener >& aListener )
555 {
556 (void)PropertyName;
557 (void)aListener;
558 }
559
560
removeVetoableChangeListener(const rtl::OUString & PropertyName,const uno::Reference<beans::XVetoableChangeListener> & aListener)561 void SAL_CALL ResultSetBase::removeVetoableChangeListener(
562 const rtl::OUString& PropertyName,
563 const uno::Reference< beans::XVetoableChangeListener >& aListener )
564 {
565 (void)PropertyName;
566 (void)aListener;
567 }
568
569
570
571 // XResultSetMetaDataSupplier
572 uno::Reference< sdbc::XResultSetMetaData > SAL_CALL
getMetaData(void)573 ResultSetBase::getMetaData(
574 void )
575 {
576 ::ucbhelper::ResultSetMetaData* p =
577 new ::ucbhelper::ResultSetMetaData(
578 m_xMSF, m_sProperty );
579 return uno::Reference< sdbc::XResultSetMetaData >( p );
580 }
581