Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 : #include <com/sun/star/beans/PropertyValue.hpp>
20 : #include "ReportEngineJFree.hxx"
21 : #include <comphelper/enumhelper.hxx>
22 : #include <comphelper/documentconstants.hxx>
23 : #include <comphelper/storagehelper.hxx>
24 : #include <connectivity/dbtools.hxx>
25 : #include <comphelper/sequence.hxx>
26 : #include <comphelper/mimeconfighelper.hxx>
27 : #include <comphelper/property.hxx>
28 : #include <comphelper/string.hxx>
29 : #include <com/sun/star/beans/PropertyAttribute.hpp>
30 : #include <com/sun/star/beans/NamedValue.hpp>
31 : #include <com/sun/star/frame/Desktop.hpp>
32 : #include <com/sun/star/frame/XComponentLoader.hpp>
33 : #include <com/sun/star/frame/FrameSearchFlag.hpp>
34 : #include <com/sun/star/embed/XTransactedObject.hpp>
35 : #include <com/sun/star/sdb/XCompletedExecution.hpp>
36 : #include <com/sun/star/sdb/XSingleSelectQueryAnalyzer.hpp>
37 : #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
38 : #include <com/sun/star/sdb/CommandType.hpp>
39 :
40 : #include <com/sun/star/task/XInteractionHandler.hpp>
41 : #include <com/sun/star/task/XJob.hpp>
42 :
43 : #include <tools/debug.hxx>
44 : #include <unotools/useroptions.hxx>
45 : #include <unotools/tempfile.hxx>
46 : #include <unotools/sharedunocomponent.hxx>
47 :
48 : #include "Tools.hxx"
49 : #include "corestrings.hrc"
50 : #include "core_resource.hrc"
51 : #include "core_resource.hxx"
52 :
53 : #include <connectivity/CommonTools.hxx>
54 : #include <rtl/ustrbuf.hxx>
55 : #include <sfx2/docfilt.hxx>
56 : // =============================================================================
57 : namespace reportdesign
58 : {
59 : // =============================================================================
60 : using namespace com::sun::star;
61 : using namespace comphelper;
62 :
63 : DBG_NAME( rpt_OReportEngineJFree )
64 : // -----------------------------------------------------------------------------
65 0 : OReportEngineJFree::OReportEngineJFree( const uno::Reference< uno::XComponentContext >& context)
66 : :ReportEngineBase(m_aMutex)
67 : ,ReportEnginePropertySet(context,static_cast< Implements >(IMPLEMENTS_PROPERTY_SET),uno::Sequence< ::rtl::OUString >())
68 : ,m_xContext(context)
69 0 : ,m_nMaxRows(0)
70 : {
71 : DBG_CTOR( rpt_OReportEngineJFree,NULL);
72 0 : }
73 : // -----------------------------------------------------------------------------
74 : // TODO: VirtualFunctionFinder: This is virtual function!
75 : //
76 0 : OReportEngineJFree::~OReportEngineJFree()
77 : {
78 : DBG_DTOR( rpt_OReportEngineJFree,NULL);
79 0 : }
80 : //--------------------------------------------------------------------------
81 0 : IMPLEMENT_FORWARD_XINTERFACE2(OReportEngineJFree,ReportEngineBase,ReportEnginePropertySet)
82 : // -----------------------------------------------------------------------------
83 0 : void SAL_CALL OReportEngineJFree::dispose() throw(uno::RuntimeException)
84 : {
85 0 : ReportEnginePropertySet::dispose();
86 0 : cppu::WeakComponentImplHelperBase::dispose();
87 0 : m_xActiveConnection.clear();
88 0 : }
89 : // -----------------------------------------------------------------------------
90 0 : ::rtl::OUString OReportEngineJFree::getImplementationName_Static( ) throw(uno::RuntimeException)
91 : {
92 0 : return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.report.OReportEngineJFree"));
93 : }
94 :
95 : //--------------------------------------------------------------------------
96 0 : ::rtl::OUString SAL_CALL OReportEngineJFree::getImplementationName( ) throw(uno::RuntimeException)
97 : {
98 0 : return getImplementationName_Static();
99 : }
100 : //--------------------------------------------------------------------------
101 0 : uno::Sequence< ::rtl::OUString > OReportEngineJFree::getSupportedServiceNames_Static( ) throw(uno::RuntimeException)
102 : {
103 0 : uno::Sequence< ::rtl::OUString > aServices(1);
104 0 : aServices.getArray()[0] = SERVICE_REPORTENGINE;
105 :
106 0 : return aServices;
107 : }
108 : //------------------------------------------------------------------------------
109 0 : uno::Reference< uno::XInterface > OReportEngineJFree::create(uno::Reference< uno::XComponentContext > const & xContext)
110 : {
111 0 : return *(new OReportEngineJFree(xContext));
112 : }
113 :
114 : //--------------------------------------------------------------------------
115 0 : uno::Sequence< ::rtl::OUString > SAL_CALL OReportEngineJFree::getSupportedServiceNames( ) throw(uno::RuntimeException)
116 : {
117 0 : return getSupportedServiceNames_Static();
118 : }
119 : //------------------------------------------------------------------------------
120 0 : sal_Bool SAL_CALL OReportEngineJFree::supportsService(const ::rtl::OUString& ServiceName) throw( uno::RuntimeException )
121 : {
122 0 : return ::comphelper::existsValue(ServiceName,getSupportedServiceNames_Static());
123 : }
124 : // -----------------------------------------------------------------------------
125 : // XReportEngine
126 : // Attributes
127 0 : uno::Reference< report::XReportDefinition > SAL_CALL OReportEngineJFree::getReportDefinition() throw (uno::RuntimeException)
128 : {
129 0 : ::osl::MutexGuard aGuard(m_aMutex);
130 0 : return m_xReport;
131 : }
132 : // -----------------------------------------------------------------------------
133 0 : void SAL_CALL OReportEngineJFree::setReportDefinition( const uno::Reference< report::XReportDefinition >& _report ) throw (lang::IllegalArgumentException, uno::RuntimeException)
134 : {
135 0 : if ( !_report.is() )
136 0 : throw lang::IllegalArgumentException();
137 0 : BoundListeners l;
138 : {
139 0 : ::osl::MutexGuard aGuard(m_aMutex);
140 0 : if ( m_xReport != _report )
141 : {
142 0 : prepareSet(PROPERTY_REPORTDEFINITION, uno::makeAny(m_xReport), uno::makeAny(_report), &l);
143 0 : m_xReport = _report;
144 0 : }
145 : }
146 0 : l.notify();
147 0 : }
148 : // -----------------------------------------------------------------------------
149 0 : uno::Reference< task::XStatusIndicator > SAL_CALL OReportEngineJFree::getStatusIndicator() throw (uno::RuntimeException)
150 : {
151 0 : ::osl::MutexGuard aGuard(m_aMutex);
152 0 : return m_StatusIndicator;
153 : }
154 : // -----------------------------------------------------------------------------
155 0 : void SAL_CALL OReportEngineJFree::setStatusIndicator( const uno::Reference< task::XStatusIndicator >& _statusindicator ) throw (uno::RuntimeException)
156 : {
157 0 : set(PROPERTY_STATUSINDICATOR,_statusindicator,m_StatusIndicator);
158 0 : }
159 : // -----------------------------------------------------------------------------
160 0 : ::rtl::OUString OReportEngineJFree::getNewOutputName()
161 : {
162 0 : ::rtl::OUString sOutputName;
163 : {
164 0 : ::osl::MutexGuard aGuard(m_aMutex);
165 0 : ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
166 0 : if ( !m_xReport.is() || !m_xActiveConnection.is() )
167 0 : throw lang::IllegalArgumentException();
168 :
169 0 : static const ::rtl::OUString s_sMediaType(RTL_CONSTASCII_USTRINGPARAM("MediaType"));
170 : try
171 : {
172 0 : const uno::Reference< lang::XMultiServiceFactory > xFactory(m_xContext->getServiceManager(),uno::UNO_QUERY_THROW);
173 0 : MimeConfigurationHelper aConfighelper(xFactory);
174 0 : const ::rtl::OUString sMimeType = m_xReport->getMimeType();
175 0 : const SfxFilter* pFilter = SfxFilter::GetDefaultFilter( aConfighelper.GetDocServiceNameFromMediaType(sMimeType) );
176 0 : String sExt;
177 0 : if ( pFilter )
178 0 : sExt = ::comphelper::string::stripStart(pFilter->GetDefaultExtension(), '*');
179 : else
180 0 : sExt = rtl::OUString(".rpt");
181 :
182 0 : uno::Reference< embed::XStorage > xTemp = OStorageHelper::GetTemporaryStorage(/*sFileTemp,embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE,*/ m_xContext);
183 0 : utl::DisposableComponent aTemp(xTemp);
184 0 : uno::Sequence< beans::PropertyValue > aEmpty;
185 0 : uno::Reference< beans::XPropertySet> xStorageProp(xTemp,uno::UNO_QUERY);
186 0 : if ( xStorageProp.is() )
187 : {
188 0 : xStorageProp->setPropertyValue( s_sMediaType, uno::makeAny(sMimeType));
189 : }
190 0 : m_xReport->storeToStorage(xTemp,aEmpty); // store to temp file because it may contain information which isn't in the database yet.
191 :
192 0 : uno::Sequence< beans::NamedValue > aConvertedProperties(8);
193 0 : sal_Int32 nPos = 0;
194 0 : aConvertedProperties[nPos].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("InputStorage"));
195 0 : aConvertedProperties[nPos++].Value <<= xTemp;
196 0 : aConvertedProperties[nPos].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OutputStorage"));
197 :
198 0 : ::rtl::OUString sFileURL;
199 0 : String sName = m_xReport->getCaption();
200 0 : if ( !sName.Len() )
201 0 : sName = m_xReport->getName();
202 : {
203 0 : ::utl::TempFile aTestFile(sName,sal_False,&sExt);
204 0 : if ( !aTestFile.IsValid() )
205 : {
206 0 : sName = RPT_RESSTRING(RID_STR_REPORT,m_xContext->getServiceManager());
207 0 : ::utl::TempFile aFile(sName,sal_False,&sExt);
208 0 : sFileURL = aFile.GetURL();
209 : }
210 : else
211 0 : sFileURL = aTestFile.GetURL();
212 : }
213 :
214 0 : uno::Reference< embed::XStorage > xOut = OStorageHelper::GetStorageFromURL(sFileURL,embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE, m_xContext);
215 0 : utl::DisposableComponent aOut(xOut);
216 0 : xStorageProp.set(xOut,uno::UNO_QUERY);
217 0 : if ( xStorageProp.is() )
218 : {
219 0 : xStorageProp->setPropertyValue( s_sMediaType, uno::makeAny(sMimeType));
220 : }
221 :
222 0 : aConvertedProperties[nPos++].Value <<= xOut;
223 :
224 0 : aConvertedProperties[nPos].Name = PROPERTY_REPORTDEFINITION;
225 0 : aConvertedProperties[nPos++].Value <<= m_xReport;
226 :
227 0 : aConvertedProperties[nPos].Name = PROPERTY_ACTIVECONNECTION;
228 0 : aConvertedProperties[nPos++].Value <<= m_xActiveConnection;
229 :
230 0 : aConvertedProperties[nPos].Name = PROPERTY_MAXROWS;
231 0 : aConvertedProperties[nPos++].Value <<= m_nMaxRows;
232 :
233 : // some meta data
234 0 : SvtUserOptions aUserOpts;
235 0 : ::rtl::OUStringBuffer sAuthor(aUserOpts.GetFirstName());
236 0 : sAuthor.appendAscii(" ");
237 0 : sAuthor.append(aUserOpts.GetLastName());
238 0 : static const ::rtl::OUString s_sAuthor(RTL_CONSTASCII_USTRINGPARAM("Author"));
239 0 : aConvertedProperties[nPos].Name = s_sAuthor;
240 0 : aConvertedProperties[nPos++].Value <<= sAuthor.makeStringAndClear();
241 :
242 0 : static const ::rtl::OUString s_sTitle(RTL_CONSTASCII_USTRINGPARAM("Title"));
243 0 : aConvertedProperties[nPos].Name = s_sTitle;
244 0 : aConvertedProperties[nPos++].Value <<= m_xReport->getCaption();
245 :
246 : // create job factory and initialize
247 0 : const ::rtl::OUString sReportEngineServiceName = ::dbtools::getDefaultReportEngineServiceName(xFactory);
248 0 : uno::Reference<task::XJob> xJob(m_xContext->getServiceManager()->createInstanceWithContext(sReportEngineServiceName,m_xContext),uno::UNO_QUERY_THROW);
249 0 : if ( !m_xReport->getCommand().isEmpty() )
250 : {
251 0 : xJob->execute(aConvertedProperties);
252 0 : if ( xStorageProp.is() )
253 : {
254 0 : sOutputName = sFileURL;
255 : }
256 : }
257 :
258 0 : uno::Reference<embed::XTransactedObject> xTransact(xOut,uno::UNO_QUERY);
259 0 : if ( !sOutputName.isEmpty() && xTransact.is() )
260 0 : xTransact->commit();
261 :
262 0 : if ( sOutputName.isEmpty() )
263 0 : throw lang::IllegalArgumentException();
264 : }
265 0 : catch(const uno::Exception& e)
266 : {
267 : (void)e; // helper to know what e contains
268 0 : throw;
269 0 : }
270 : }
271 0 : return sOutputName;
272 : }
273 : // -----------------------------------------------------------------------------
274 : // Methods
275 0 : uno::Reference< frame::XModel > SAL_CALL OReportEngineJFree::createDocumentModel( ) throw (lang::DisposedException, lang::IllegalArgumentException, uno::Exception, uno::RuntimeException)
276 : {
277 0 : return createDocumentAlive(NULL,true);
278 : }
279 : // -----------------------------------------------------------------------------
280 0 : uno::Reference< frame::XModel > SAL_CALL OReportEngineJFree::createDocumentAlive( const uno::Reference< frame::XFrame >& _frame ) throw (lang::DisposedException, lang::IllegalArgumentException, uno::Exception, uno::RuntimeException)
281 : {
282 0 : return createDocumentAlive(_frame,false);
283 : }
284 : // -----------------------------------------------------------------------------
285 0 : uno::Reference< frame::XModel > SAL_CALL OReportEngineJFree::createDocumentAlive( const uno::Reference< frame::XFrame >& _frame,bool _bHidden ) throw (lang::DisposedException, lang::IllegalArgumentException, uno::Exception, uno::RuntimeException)
286 : {
287 0 : uno::Reference< frame::XModel > xModel;
288 0 : ::rtl::OUString sOutputName = getNewOutputName(); // starts implicite the report generator
289 0 : if ( !sOutputName.isEmpty() )
290 : {
291 0 : ::osl::MutexGuard aGuard(m_aMutex);
292 0 : ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
293 0 : uno::Reference<frame::XComponentLoader> xFrameLoad(_frame,uno::UNO_QUERY);
294 0 : if ( !xFrameLoad.is() )
295 : {
296 : // if there is no frame given, find the right
297 0 : xFrameLoad.set( frame::Desktop::create(m_xContext), uno::UNO_QUERY);
298 0 : ::rtl::OUString sTarget(RTL_CONSTASCII_USTRINGPARAM("_blank"));
299 0 : sal_Int32 nFrameSearchFlag = frame::FrameSearchFlag::TASKS | frame::FrameSearchFlag::CREATE;
300 0 : uno::Reference< frame::XFrame> xFrame = uno::Reference< frame::XFrame>(xFrameLoad,uno::UNO_QUERY)->findFrame(sTarget,nFrameSearchFlag);
301 0 : xFrameLoad.set( xFrame,uno::UNO_QUERY);
302 : }
303 :
304 0 : if ( xFrameLoad.is() )
305 : {
306 0 : uno::Sequence < beans::PropertyValue > aArgs( _bHidden ? 3 : 2 );
307 0 : sal_Int32 nLen = 0;
308 0 : aArgs[nLen].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("AsTemplate"));
309 0 : aArgs[nLen++].Value <<= sal_False;
310 :
311 0 : aArgs[nLen].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ReadOnly"));
312 0 : aArgs[nLen++].Value <<= sal_True;
313 :
314 0 : if ( _bHidden )
315 : {
316 0 : aArgs[nLen].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Hidden"));
317 0 : aArgs[nLen++].Value <<= sal_True;
318 : }
319 :
320 0 : uno::Reference< lang::XMultiServiceFactory > xFac(m_xContext->getServiceManager(),uno::UNO_QUERY);
321 0 : xModel.set( xFrameLoad->loadComponentFromURL(
322 : sOutputName,
323 : ::rtl::OUString(), // empty frame name
324 : 0,
325 : aArgs
326 0 : ),uno::UNO_QUERY);
327 0 : }
328 : }
329 0 : return xModel;
330 : }
331 : // -----------------------------------------------------------------------------
332 0 : util::URL SAL_CALL OReportEngineJFree::createDocument( ) throw (lang::DisposedException, lang::IllegalArgumentException, uno::Exception, uno::RuntimeException)
333 : {
334 0 : util::URL aRet;
335 0 : uno::Reference< frame::XModel > xModel = createDocumentModel();
336 0 : if ( xModel.is() )
337 : {
338 0 : ::osl::MutexGuard aGuard(m_aMutex);
339 0 : ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
340 : }
341 0 : return aRet;
342 : }
343 : // -----------------------------------------------------------------------------
344 0 : void SAL_CALL OReportEngineJFree::interrupt( ) throw (lang::DisposedException, uno::Exception, uno::RuntimeException)
345 : {
346 : {
347 0 : ::osl::MutexGuard aGuard(m_aMutex);
348 0 : ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
349 : }
350 0 : }
351 : // -----------------------------------------------------------------------------
352 0 : uno::Reference< beans::XPropertySetInfo > SAL_CALL OReportEngineJFree::getPropertySetInfo( ) throw(uno::RuntimeException)
353 : {
354 0 : return ReportEnginePropertySet::getPropertySetInfo();
355 : }
356 : // -------------------------------------------------------------------------
357 0 : void SAL_CALL OReportEngineJFree::setPropertyValue( const ::rtl::OUString& aPropertyName, const uno::Any& aValue ) throw (beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
358 : {
359 0 : ReportEnginePropertySet::setPropertyValue( aPropertyName, aValue );
360 0 : }
361 : // -----------------------------------------------------------------------------
362 0 : uno::Any SAL_CALL OReportEngineJFree::getPropertyValue( const ::rtl::OUString& PropertyName ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
363 : {
364 0 : return ReportEnginePropertySet::getPropertyValue( PropertyName);
365 : }
366 : // -----------------------------------------------------------------------------
367 0 : void SAL_CALL OReportEngineJFree::addPropertyChangeListener( const ::rtl::OUString& aPropertyName, const uno::Reference< beans::XPropertyChangeListener >& xListener ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
368 : {
369 0 : ReportEnginePropertySet::addPropertyChangeListener( aPropertyName, xListener );
370 0 : }
371 : // -----------------------------------------------------------------------------
372 0 : void SAL_CALL OReportEngineJFree::removePropertyChangeListener( const ::rtl::OUString& aPropertyName, const uno::Reference< beans::XPropertyChangeListener >& aListener ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
373 : {
374 0 : ReportEnginePropertySet::removePropertyChangeListener( aPropertyName, aListener );
375 0 : }
376 : // -----------------------------------------------------------------------------
377 0 : void SAL_CALL OReportEngineJFree::addVetoableChangeListener( const ::rtl::OUString& PropertyName, const uno::Reference< beans::XVetoableChangeListener >& aListener ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
378 : {
379 0 : ReportEnginePropertySet::addVetoableChangeListener( PropertyName, aListener );
380 0 : }
381 : // -----------------------------------------------------------------------------
382 0 : void SAL_CALL OReportEngineJFree::removeVetoableChangeListener( const ::rtl::OUString& PropertyName, const uno::Reference< beans::XVetoableChangeListener >& aListener ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
383 : {
384 0 : ReportEnginePropertySet::removeVetoableChangeListener( PropertyName, aListener );
385 0 : }
386 : // -----------------------------------------------------------------------------
387 0 : uno::Reference< sdbc::XConnection > SAL_CALL OReportEngineJFree::getActiveConnection() throw (uno::RuntimeException)
388 : {
389 0 : return m_xActiveConnection;
390 : }
391 : // -----------------------------------------------------------------------------
392 0 : void SAL_CALL OReportEngineJFree::setActiveConnection( const uno::Reference< sdbc::XConnection >& _activeconnection ) throw (lang::IllegalArgumentException, uno::RuntimeException)
393 : {
394 0 : if ( !_activeconnection.is() )
395 0 : throw lang::IllegalArgumentException();
396 0 : set(PROPERTY_ACTIVECONNECTION,_activeconnection,m_xActiveConnection);
397 0 : }
398 : // -----------------------------------------------------------------------------
399 0 : ::sal_Int32 SAL_CALL OReportEngineJFree::getMaxRows() throw (uno::RuntimeException)
400 : {
401 0 : ::osl::MutexGuard aGuard(m_aMutex);
402 0 : return m_nMaxRows;
403 : }
404 : // -----------------------------------------------------------------------------
405 0 : void SAL_CALL OReportEngineJFree::setMaxRows( ::sal_Int32 _MaxRows ) throw (uno::RuntimeException)
406 : {
407 0 : set(PROPERTY_MAXROWS,_MaxRows,m_nMaxRows);
408 0 : }
409 : // =============================================================================
410 : } // namespace reportdesign
411 : // =============================================================================
412 :
413 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|