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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_ucb.hxx"
30 
31 #include <cacheddynamicresultsetstub.hxx>
32 #include <com/sun/star/sdbc/XResultSet.hpp>
33 #include <cachedcontentresultsetstub.hxx>
34 #include <com/sun/star/ucb/ContentResultSetCapability.hpp>
35 #include <com/sun/star/ucb/XSortedDynamicResultSetFactory.hpp>
36 #include <osl/diagnose.h>
37 
38 using namespace com::sun::star::lang;
39 using namespace com::sun::star::sdbc;
40 using namespace com::sun::star::ucb;
41 using namespace com::sun::star::uno;
42 using namespace rtl;
43 
44 CachedDynamicResultSetStub::CachedDynamicResultSetStub(
45 		Reference< XDynamicResultSet > xOrigin
46 		, const Reference< XMultiServiceFactory > & xSMgr )
47 		: DynamicResultSetWrapper( xOrigin, xSMgr )
48 {
49 	OSL_ENSURE( m_xSMgr.is(), "need Multiservicefactory to create stub" );
50 	impl_init();
51 }
52 
53 CachedDynamicResultSetStub::~CachedDynamicResultSetStub()
54 {
55 	impl_deinit();
56 }
57 
58 //virtual
59 void SAL_CALL CachedDynamicResultSetStub
60 	::impl_InitResultSetOne( const Reference< XResultSet >& xResultSet )
61 {
62 	DynamicResultSetWrapper::impl_InitResultSetOne( xResultSet );
63 	OSL_ENSURE( m_xSourceResultOne.is(), "need source resultset" );
64 
65 	Reference< XResultSet > xStub(
66 		new CachedContentResultSetStub( m_xSourceResultOne ) );
67 
68 	osl::Guard< osl::Mutex > aGuard( m_aMutex );
69 	m_xMyResultOne = xStub;
70 }
71 
72 //virtual
73 void SAL_CALL CachedDynamicResultSetStub
74 	::impl_InitResultSetTwo( const Reference< XResultSet >& xResultSet )
75 {
76 	DynamicResultSetWrapper::impl_InitResultSetTwo( xResultSet );
77 	OSL_ENSURE( m_xSourceResultTwo.is(), "need source resultset" );
78 
79 	Reference< XResultSet > xStub(
80 		new CachedContentResultSetStub( m_xSourceResultTwo ) );
81 
82 	osl::Guard< osl::Mutex > aGuard( m_aMutex );
83 	m_xMyResultTwo = xStub;
84 }
85 
86 //--------------------------------------------------------------------------
87 // XInterface methods.
88 //--------------------------------------------------------------------------
89 XINTERFACE_COMMON_IMPL( CachedDynamicResultSetStub )
90 
91 Any SAL_CALL CachedDynamicResultSetStub
92 	::queryInterface( const Type&  rType )
93 	throw ( RuntimeException )
94 {
95 	//list all interfaces inclusive baseclasses of interfaces
96 
97 	Any aRet = DynamicResultSetWrapper::queryInterface( rType );
98 	if( aRet.hasValue() )
99 		return aRet;
100 
101 	aRet = cppu::queryInterface( rType,
102 				static_cast< XTypeProvider* >( this )
103 				, static_cast< XServiceInfo* >( this )
104 				);
105 	return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
106 }
107 
108 //--------------------------------------------------------------------------
109 // XTypeProvider methods.
110 //--------------------------------------------------------------------------
111 //list all interfaces exclusive baseclasses
112 XTYPEPROVIDER_IMPL_5( CachedDynamicResultSetStub
113 					, XTypeProvider
114 					, XServiceInfo
115 					, XDynamicResultSet
116 					, XDynamicResultSetListener
117 					, XSourceInitialization
118 					);
119 
120 //--------------------------------------------------------------------------
121 // XServiceInfo methods.
122 //--------------------------------------------------------------------------
123 
124 XSERVICEINFO_NOFACTORY_IMPL_1( CachedDynamicResultSetStub,
125 			 		   OUString::createFromAscii(
126 						"com.sun.star.comp.ucb.CachedDynamicResultSetStub" ),
127 			 		   OUString::createFromAscii(
128 						CACHED_DRS_STUB_SERVICE_NAME ) );
129 
130 //--------------------------------------------------------------------------
131 //--------------------------------------------------------------------------
132 // class CachedDynamicResultSetStubFactory
133 //--------------------------------------------------------------------------
134 //--------------------------------------------------------------------------
135 
136 CachedDynamicResultSetStubFactory::CachedDynamicResultSetStubFactory(
137 		const Reference< XMultiServiceFactory > & rSMgr )
138 {
139 	m_xSMgr = rSMgr;
140 }
141 
142 CachedDynamicResultSetStubFactory::~CachedDynamicResultSetStubFactory()
143 {
144 }
145 
146 //--------------------------------------------------------------------------
147 // CachedDynamicResultSetStubFactory XInterface methods.
148 //--------------------------------------------------------------------------
149 
150 XINTERFACE_IMPL_3( CachedDynamicResultSetStubFactory,
151 				   XTypeProvider,
152 				   XServiceInfo,
153 				   XCachedDynamicResultSetStubFactory );
154 
155 //--------------------------------------------------------------------------
156 // CachedDynamicResultSetStubFactory XTypeProvider methods.
157 //--------------------------------------------------------------------------
158 
159 XTYPEPROVIDER_IMPL_3( CachedDynamicResultSetStubFactory,
160 					  XTypeProvider,
161 				   	  XServiceInfo,
162 					  XCachedDynamicResultSetStubFactory );
163 
164 //--------------------------------------------------------------------------
165 // CachedDynamicResultSetStubFactory XServiceInfo methods.
166 //--------------------------------------------------------------------------
167 
168 XSERVICEINFO_IMPL_1( CachedDynamicResultSetStubFactory,
169  		   		 OUString::createFromAscii(
170 				 	"com.sun.star.comp.ucb.CachedDynamicResultSetStubFactory" ),
171  		   		 OUString::createFromAscii(
172 				 	CACHED_DRS_STUB_FACTORY_NAME ) );
173 
174 //--------------------------------------------------------------------------
175 // Service factory implementation.
176 //--------------------------------------------------------------------------
177 
178 ONE_INSTANCE_SERVICE_FACTORY_IMPL( CachedDynamicResultSetStubFactory );
179 
180 //--------------------------------------------------------------------------
181 // CachedDynamicResultSetStubFactory XCachedDynamicResultSetStubFactory methods.
182 //--------------------------------------------------------------------------
183 
184 //virtual
185 Reference< XDynamicResultSet > SAL_CALL CachedDynamicResultSetStubFactory
186 	::createCachedDynamicResultSetStub(
187 			const Reference< XDynamicResultSet > & Source )
188 			throw( RuntimeException )
189 {
190 	Reference< XDynamicResultSet > xRet;
191 	xRet = new CachedDynamicResultSetStub( Source, m_xSMgr );
192 	return xRet;
193 }
194 
195 //virtual
196 void SAL_CALL CachedDynamicResultSetStubFactory
197 	::connectToCache(
198 			  const Reference< XDynamicResultSet > & Source
199 			, const Reference< XDynamicResultSet > & TargetCache
200 			, const Sequence< NumberedSortingInfo > & SortingInfo
201 			, const Reference< XAnyCompareFactory > & CompareFactory
202 			)
203 			throw (  ListenerAlreadySetException
204 			, AlreadyInitializedException
205 			, RuntimeException )
206 {
207 	OSL_ENSURE( Source.is(), "a Source is needed" );
208 	OSL_ENSURE( TargetCache.is(), "a TargetCache is needed" );
209 
210 	Reference< XDynamicResultSet > xSource( Source );
211 	if( SortingInfo.getLength() &&
212 		!( xSource->getCapabilities() & ContentResultSetCapability::SORTED )
213 		)
214 	{
215 		Reference< XSortedDynamicResultSetFactory > xSortFactory;
216 		try
217 		{
218 			xSortFactory = Reference< XSortedDynamicResultSetFactory >(
219 				m_xSMgr->createInstance( OUString::createFromAscii(
220 					"com.sun.star.ucb.SortedDynamicResultSetFactory" ) ),
221 				UNO_QUERY );
222 		}
223 		catch ( Exception const & )
224 		{
225 		}
226 
227 		if( xSortFactory.is() )
228 		{
229 			Reference< XDynamicResultSet > xSorted(
230 				xSortFactory->createSortedDynamicResultSet(
231 					Source, SortingInfo, CompareFactory ) );
232 			if( xSorted.is() )
233 				xSource = xSorted;
234 		}
235 	}
236 
237 	Reference< XDynamicResultSet > xStub(
238 		new CachedDynamicResultSetStub( xSource, m_xSMgr ) );
239 
240 	Reference< XSourceInitialization > xTarget( TargetCache, UNO_QUERY );
241 	OSL_ENSURE( xTarget.is(), "Target must have interface XSourceInitialization" );
242 
243 	xTarget->setSource( xStub );
244 }
245 
246