xref: /aoo41x/main/ucb/source/sorter/sortresult.hxx (revision cdf0e10c)
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 _SORTRESULT_HXX
29 #define _SORTRESULT_HXX
30 
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 #include <com/sun/star/lang/XTypeProvider.hpp>
34 #include <com/sun/star/lang/XServiceInfo.hpp>
35 #include <com/sun/star/lang/XComponent.hpp>
36 #include <com/sun/star/sdbc/XCloseable.hpp>
37 #include <com/sun/star/sdbc/XResultSet.hpp>
38 #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
39 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
40 #include <com/sun/star/sdbc/XRow.hpp>
41 #include <com/sun/star/ucb/XContentAccess.hpp>
42 #include <com/sun/star/ucb/NumberedSortingInfo.hpp>
43 #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
44 #include <com/sun/star/ucb/ListAction.hpp>
45 #include <cppuhelper/weak.hxx>
46 #include <osl/mutex.hxx>
47 
48 #include <deque>
49 #include <ucbhelper/macros.hxx>
50 
51 namespace cppu {
52 	class OInterfaceContainerHelper;
53 }
54 
55 //-----------------------------------------------------------------------------
56 struct	SortInfo;
57 struct	SortListData;
58 class	SRSPropertySetInfo;
59 class	PropertyChangeListeners_Impl;
60 
61 //-----------------------------------------------------------------------------
62 class SortedEntryList
63 {
64 	std::deque < SortListData* > maData;
65 
66 public:
67 						 SortedEntryList(){}
68 						~SortedEntryList(){ Clear(); }
69 
70 	sal_uInt32			Count() const { return (sal_uInt32) maData.size(); }
71 
72 	void				Clear();
73 	void				Insert( SortListData *pEntry, long nPos );
74 	SortListData*		Remove( long nPos );
75 	SortListData*		GetData( long nPos );
76 
77 	long				operator [] ( long nPos ) const;
78 };
79 
80 //-----------------------------------------------------------------------------
81 #define LISTACTION	com::sun::star::ucb::ListAction
82 
83 class EventList
84 {
85 	std::deque < LISTACTION* > maData;
86 
87 public:
88 					 EventList(){}
89 					~EventList(){ Clear(); }
90 
91 	sal_uInt32		Count() { return (sal_uInt32) maData.size(); }
92 
93 	void			AddEvent( long nType, long nPos, long nCount );
94 	void			Insert( LISTACTION *pAction ) { maData.push_back( pAction ); }
95 	void			Clear();
96 	LISTACTION*		GetAction( long nIndex ) { return maData[ nIndex ]; }
97 };
98 
99 //-----------------------------------------------------------------------------
100 
101 class SimpleList
102 {
103 	std::deque < void* > maData;
104 
105 public:
106 					 SimpleList(){}
107 					~SimpleList(){ Clear(); }
108 
109 	sal_uInt32		Count() { return (sal_uInt32) maData.size(); }
110 	void			Clear() { maData.clear(); }
111 
112 	void			Remove( sal_uInt32 nPos );
113 	void			Remove( void* pData );
114 
115 	void			Append( void* pData )
116 						{ maData.push_back( pData ); }
117 	void			Insert( void* pData, sal_uInt32 nPos );
118 	void*			GetObject( sal_uInt32 nPos ) const;
119 	void			Replace( void* pData, sal_uInt32 nPos );
120 };
121 
122 //-----------------------------------------------------------------------------
123 
124 #define PROPERTYCHANGEEVENT		com::sun::star::beans::PropertyChangeEvent
125 #define RUNTIME_EXCEPTION		com::sun::star::uno::RuntimeException
126 #define REFERENCE				com::sun::star::uno::Reference
127 #define SEQUENCE				com::sun::star::uno::Sequence
128 #define XEVENTLISTENER			com::sun::star::lang::XEventListener
129 #define XRESULTSET				com::sun::star::sdbc::XResultSet
130 #define SQLEXCEPTION			com::sun::star::sdbc::SQLException
131 #define XRESULTSETMETADATA		com::sun::star::sdbc::XResultSetMetaData
132 #define NUMBERED_SORTINGINFO	com::sun::star::ucb::NumberedSortingInfo
133 #define XANYCOMPAREFACTORY		com::sun::star::ucb::XAnyCompareFactory
134 
135 #define RESULTSET_SERVICE_NAME 	"com.sun.star.ucb.SortedResultSet"
136 
137 //-----------------------------------------------------------------------------
138 
139 class SortedResultSet:
140 				public cppu::OWeakObject,
141 				public com::sun::star::lang::XTypeProvider,
142 				public com::sun::star::lang::XServiceInfo,
143 				public com::sun::star::lang::XComponent,
144 				public com::sun::star::ucb::XContentAccess,
145 				public XRESULTSET,
146 				public com::sun::star::sdbc::XRow,
147 				public com::sun::star::sdbc::XCloseable,
148 				public com::sun::star::sdbc::XResultSetMetaDataSupplier,
149 				public com::sun::star::beans::XPropertySet
150 {
151 	cppu::OInterfaceContainerHelper	*mpDisposeEventListeners;
152 	PropertyChangeListeners_Impl	*mpPropChangeListeners;
153 	PropertyChangeListeners_Impl	*mpVetoChangeListeners;
154 
155 	REFERENCE < XRESULTSET >			mxOriginal;
156 	REFERENCE < XRESULTSET >			mxOther;
157 
158 	SRSPropertySetInfo*	mpPropSetInfo;
159 	SortInfo*			mpSortInfo;
160 	osl::Mutex			maMutex;
161 	SortedEntryList		maS2O;			// maps the sorted entries to the original ones
162 	SimpleList			maO2S;			// maps the original Entries to the sorted ones
163 	SimpleList			maModList;		// keeps track of modified entries
164 	long				mnLastSort;		// index of the last sorted entry;
165 	long				mnCurEntry;		// index of the current entry
166 	long				mnCount;		// total count of the elements
167 	sal_Bool			mbIsCopy;
168 
169 
170 private:
171 
172 	long				FindPos( SortListData *pEntry, long nStart, long nEnd )
173 							throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
174 	long				Compare( SortListData *pOne,
175 								 SortListData *pTwo )
176 							throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
177 	void				BuildSortInfo( REFERENCE< XRESULTSET > aResult,
178 									   const SEQUENCE < NUMBERED_SORTINGINFO > &xSortInfo,
179 									   const REFERENCE< XANYCOMPAREFACTORY > &xCompFac );
180 	long				CompareImpl( REFERENCE < XRESULTSET > xResultOne,
181 									 REFERENCE < XRESULTSET > xResultTwo,
182 									 long nIndexOne, long nIndexTwo,
183 									 SortInfo* pSortInfo )
184 							throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
185 	long				CompareImpl( REFERENCE < XRESULTSET > xResultOne,
186 									 REFERENCE < XRESULTSET > xResultTwo,
187 									 long nIndexOne, long nIndexTwo )
188 							throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
189 	void				PropertyChanged( const PROPERTYCHANGEEVENT& rEvt );
190 
191 public:
192 						SortedResultSet( REFERENCE< XRESULTSET > aResult );
193 						~SortedResultSet();
194 
195 	const SortedEntryList*		GetS2OList() const { return &maS2O; }
196 	const SimpleList*			GetO2SList() const { return &maO2S; }
197 	REFERENCE < XRESULTSET >	GetResultSet() const { return mxOriginal; }
198 	SortInfo*					GetSortInfo() const { return mpSortInfo; }
199 	long						GetCount() const { return mnCount; }
200 
201 	void				CopyData( SortedResultSet* pSource );
202 	void				Initialize( const SEQUENCE < NUMBERED_SORTINGINFO > &xSortInfo,
203 									const REFERENCE< XANYCOMPAREFACTORY > &xCompFac );
204 	void				CheckProperties( long nOldCount, sal_Bool bWasFinal );
205 
206 	void				InsertNew( long nPos, long nCount );
207 	void				SetChanged( long nPos, long nCount );
208 	void				Remove( long nPos, long nCount, EventList *pList );
209 	void				Move( long nPos, long nCount, long nOffset );
210 
211 	void				ResortModified( EventList* pList );
212 	void				ResortNew( EventList* pList );
213 
214 	// XInterface
215 	XINTERFACE_DECL()
216 
217 	// XTypeProvider
218 	XTYPEPROVIDER_DECL()
219 
220     // XServiceInfo
221 	XSERVICEINFO_NOFACTORY_DECL()
222 
223 	// XComponent
224 	virtual void SAL_CALL
225 	dispose() throw( RUNTIME_EXCEPTION );
226 
227     virtual void SAL_CALL
228 	addEventListener( const REFERENCE< XEVENTLISTENER >& Listener )
229 		throw( RUNTIME_EXCEPTION );
230 
231     virtual void SAL_CALL
232 	removeEventListener( const REFERENCE< XEVENTLISTENER >& Listener )
233 		throw( RUNTIME_EXCEPTION );
234 
235 	// XContentAccess
236     virtual rtl::OUString SAL_CALL
237 	queryContentIdentifierString()
238 		throw( RUNTIME_EXCEPTION );
239     virtual REFERENCE<
240 				com::sun::star::ucb::XContentIdentifier > SAL_CALL
241 	queryContentIdentifier()
242 		throw( RUNTIME_EXCEPTION );
243     virtual REFERENCE<
244 				com::sun::star::ucb::XContent > SAL_CALL
245 	queryContent()
246 		throw( RUNTIME_EXCEPTION );
247 
248 	// XResultSet
249     virtual sal_Bool SAL_CALL
250 	next()
251 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
252     virtual sal_Bool SAL_CALL
253 	isBeforeFirst()
254 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
255     virtual sal_Bool SAL_CALL
256 	isAfterLast()
257 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
258     virtual sal_Bool SAL_CALL
259 	isFirst()
260 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
261     virtual sal_Bool SAL_CALL
262 	isLast()
263 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
264     virtual void SAL_CALL
265 	beforeFirst()
266 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
267     virtual void SAL_CALL
268 	afterLast()
269 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
270     virtual sal_Bool SAL_CALL
271 	first()
272 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
273     virtual sal_Bool SAL_CALL
274 	last()
275 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
276     virtual sal_Int32 SAL_CALL
277 	getRow()
278 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
279     virtual sal_Bool SAL_CALL
280 	absolute( sal_Int32 row )
281 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
282     virtual sal_Bool SAL_CALL
283 	relative( sal_Int32 rows )
284 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
285     virtual sal_Bool SAL_CALL
286 	previous()
287 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
288     virtual void SAL_CALL
289 	refreshRow()
290 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
291     virtual sal_Bool SAL_CALL
292 	rowUpdated()
293 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
294     virtual sal_Bool SAL_CALL
295 	rowInserted()
296 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
297     virtual sal_Bool SAL_CALL
298 	rowDeleted()
299 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
300     virtual REFERENCE<
301 				com::sun::star::uno::XInterface > SAL_CALL
302 	getStatement()
303 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
304 
305 	// XRow
306     virtual sal_Bool SAL_CALL
307 	wasNull() throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
308 
309     virtual rtl::OUString SAL_CALL
310 	getString( sal_Int32 columnIndex )
311 	throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
312 
313     virtual sal_Bool SAL_CALL
314 	getBoolean( sal_Int32 columnIndex )
315 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
316 
317 	virtual sal_Int8 SAL_CALL
318 	getByte( sal_Int32 columnIndex )
319 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
320 
321 	virtual sal_Int16 SAL_CALL
322 	getShort( sal_Int32 columnIndex )
323 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
324 
325 	virtual sal_Int32 SAL_CALL
326 	getInt( sal_Int32 columnIndex )
327 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
328 
329 	virtual sal_Int64 SAL_CALL
330 	getLong( sal_Int32 columnIndex )
331 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
332 
333 	virtual float SAL_CALL
334 	getFloat( sal_Int32 columnIndex )
335 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
336 
337 	virtual double SAL_CALL
338 	getDouble( sal_Int32 columnIndex )
339 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
340 
341 	virtual com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL
342 	getBytes( sal_Int32 columnIndex )
343 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
344 
345 	virtual com::sun::star::util::Date SAL_CALL
346 	getDate( sal_Int32 columnIndex )
347 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
348 
349 	virtual com::sun::star::util::Time SAL_CALL
350 	getTime( sal_Int32 columnIndex )
351 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
352 
353 	virtual com::sun::star::util::DateTime SAL_CALL
354 	getTimestamp( sal_Int32 columnIndex )
355 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
356 
357 	virtual REFERENCE<
358 				com::sun::star::io::XInputStream > SAL_CALL
359 	getBinaryStream( sal_Int32 columnIndex )
360 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
361 
362 	virtual REFERENCE<
363 				com::sun::star::io::XInputStream > SAL_CALL
364 	getCharacterStream( sal_Int32 columnIndex )
365 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
366 
367 	virtual com::sun::star::uno::Any SAL_CALL
368 	getObject( sal_Int32 columnIndex,
369 			   const REFERENCE<
370 			   	com::sun::star::container::XNameAccess >& typeMap )
371 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
372     virtual REFERENCE<
373 				com::sun::star::sdbc::XRef > SAL_CALL
374 	getRef( sal_Int32 columnIndex )
375 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
376     virtual REFERENCE<
377 				com::sun::star::sdbc::XBlob > SAL_CALL
378 	getBlob( sal_Int32 columnIndex )
379 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
380     virtual REFERENCE<
381 				com::sun::star::sdbc::XClob > SAL_CALL
382 	getClob( sal_Int32 columnIndex )
383 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
384     virtual REFERENCE<
385 				com::sun::star::sdbc::XArray > SAL_CALL
386 	getArray( sal_Int32 columnIndex )
387 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
388 
389 	// XCloseable
390     virtual void SAL_CALL
391 	close()
392 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
393 
394 	// XResultSetMetaDataSupplier
395 	virtual REFERENCE< XRESULTSETMETADATA > SAL_CALL
396 	getMetaData()
397 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
398 
399 
400 	// XPropertySet
401     virtual REFERENCE<
402 				com::sun::star::beans::XPropertySetInfo > SAL_CALL
403 	getPropertySetInfo()
404 		throw( RUNTIME_EXCEPTION );
405 
406     virtual void SAL_CALL
407 	setPropertyValue( const rtl::OUString& PropertyName,
408 					  const com::sun::star::uno::Any& Value )
409 		throw( com::sun::star::beans::UnknownPropertyException,
410 			   com::sun::star::beans::PropertyVetoException,
411 			   com::sun::star::lang::IllegalArgumentException,
412 			   com::sun::star::lang::WrappedTargetException,
413 			   RUNTIME_EXCEPTION );
414 
415     virtual com::sun::star::uno::Any SAL_CALL
416 	getPropertyValue( const rtl::OUString& PropertyName )
417 		throw( com::sun::star::beans::UnknownPropertyException,
418 		com::sun::star::lang::WrappedTargetException,
419 		RUNTIME_EXCEPTION );
420 
421     virtual void SAL_CALL
422 	addPropertyChangeListener( const rtl::OUString& PropertyName,
423 							   const REFERENCE<
424 							   		com::sun::star::beans::XPropertyChangeListener >& Listener )
425 		throw( com::sun::star::beans::UnknownPropertyException,
426 			   com::sun::star::lang::WrappedTargetException,
427 			   RUNTIME_EXCEPTION );
428 
429     virtual void SAL_CALL
430 	removePropertyChangeListener( const rtl::OUString& PropertyName,
431 								  const REFERENCE<
432 								  	com::sun::star::beans::XPropertyChangeListener >& Listener )
433 		throw( com::sun::star::beans::UnknownPropertyException,
434 			   com::sun::star::lang::WrappedTargetException,
435 			   RUNTIME_EXCEPTION );
436 
437     virtual void SAL_CALL
438 	addVetoableChangeListener( const rtl::OUString& PropertyName,
439 							   const REFERENCE<
440 							   		com::sun::star::beans::XVetoableChangeListener >& Listener )
441 		throw( com::sun::star::beans::UnknownPropertyException,
442 			   com::sun::star::lang::WrappedTargetException,
443 			   RUNTIME_EXCEPTION );
444 
445     virtual void SAL_CALL
446 	removeVetoableChangeListener( const rtl::OUString& PropertyName,
447 								  const REFERENCE<
448 								  	com::sun::star::beans::XVetoableChangeListener >& aListener )
449 		throw( com::sun::star::beans::UnknownPropertyException,
450 			   com::sun::star::lang::WrappedTargetException,
451 			   RUNTIME_EXCEPTION );
452 };
453 
454 #endif
455 
456