bump product version to 4.1.6.2
[LibreOffice.git] / reportdesign / source / core / api / ReportEngineJFree.cxx
blobe99d1440df61324b4ac7c36a83e17acf76aca89d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
40 #include <com/sun/star/task/XInteractionHandler.hpp>
41 #include <com/sun/star/task/XJob.hpp>
43 #include <tools/debug.hxx>
44 #include <unotools/useroptions.hxx>
45 #include <unotools/tempfile.hxx>
46 #include <unotools/sharedunocomponent.hxx>
48 #include "Tools.hxx"
49 #include "corestrings.hrc"
50 #include "core_resource.hrc"
51 #include "core_resource.hxx"
53 #include <connectivity/CommonTools.hxx>
54 #include <rtl/ustrbuf.hxx>
55 #include <sfx2/docfilt.hxx>
56 // =============================================================================
57 namespace reportdesign
59 // =============================================================================
60 using namespace com::sun::star;
61 using namespace comphelper;
63 DBG_NAME( rpt_OReportEngineJFree )
64 // -----------------------------------------------------------------------------
65 OReportEngineJFree::OReportEngineJFree( const uno::Reference< uno::XComponentContext >& context)
66 :ReportEngineBase(m_aMutex)
67 ,ReportEnginePropertySet(context,static_cast< Implements >(IMPLEMENTS_PROPERTY_SET),uno::Sequence< OUString >())
68 ,m_xContext(context)
69 ,m_nMaxRows(0)
71 DBG_CTOR( rpt_OReportEngineJFree,NULL);
73 // -----------------------------------------------------------------------------
74 // TODO: VirtualFunctionFinder: This is virtual function!
76 OReportEngineJFree::~OReportEngineJFree()
78 DBG_DTOR( rpt_OReportEngineJFree,NULL);
80 //--------------------------------------------------------------------------
81 IMPLEMENT_FORWARD_XINTERFACE2(OReportEngineJFree,ReportEngineBase,ReportEnginePropertySet)
82 // -----------------------------------------------------------------------------
83 void SAL_CALL OReportEngineJFree::dispose() throw(uno::RuntimeException)
85 ReportEnginePropertySet::dispose();
86 cppu::WeakComponentImplHelperBase::dispose();
87 m_xActiveConnection.clear();
89 // -----------------------------------------------------------------------------
90 OUString OReportEngineJFree::getImplementationName_Static( ) throw(uno::RuntimeException)
92 return OUString("com.sun.star.comp.report.OReportEngineJFree");
95 //--------------------------------------------------------------------------
96 OUString SAL_CALL OReportEngineJFree::getImplementationName( ) throw(uno::RuntimeException)
98 return getImplementationName_Static();
100 //--------------------------------------------------------------------------
101 uno::Sequence< OUString > OReportEngineJFree::getSupportedServiceNames_Static( ) throw(uno::RuntimeException)
103 uno::Sequence< OUString > aServices(1);
104 aServices.getArray()[0] = OUString("com.sun.star.report.ReportEngine");
106 return aServices;
108 //------------------------------------------------------------------------------
109 uno::Reference< uno::XInterface > OReportEngineJFree::create(uno::Reference< uno::XComponentContext > const & xContext)
111 return *(new OReportEngineJFree(xContext));
114 //--------------------------------------------------------------------------
115 uno::Sequence< OUString > SAL_CALL OReportEngineJFree::getSupportedServiceNames( ) throw(uno::RuntimeException)
117 return getSupportedServiceNames_Static();
119 //------------------------------------------------------------------------------
120 sal_Bool SAL_CALL OReportEngineJFree::supportsService(const OUString& ServiceName) throw( uno::RuntimeException )
122 return ::comphelper::existsValue(ServiceName,getSupportedServiceNames_Static());
124 // -----------------------------------------------------------------------------
125 // XReportEngine
126 // Attributes
127 uno::Reference< report::XReportDefinition > SAL_CALL OReportEngineJFree::getReportDefinition() throw (uno::RuntimeException)
129 ::osl::MutexGuard aGuard(m_aMutex);
130 return m_xReport;
132 // -----------------------------------------------------------------------------
133 void SAL_CALL OReportEngineJFree::setReportDefinition( const uno::Reference< report::XReportDefinition >& _report ) throw (lang::IllegalArgumentException, uno::RuntimeException)
135 if ( !_report.is() )
136 throw lang::IllegalArgumentException();
137 BoundListeners l;
139 ::osl::MutexGuard aGuard(m_aMutex);
140 if ( m_xReport != _report )
142 prepareSet(PROPERTY_REPORTDEFINITION, uno::makeAny(m_xReport), uno::makeAny(_report), &l);
143 m_xReport = _report;
146 l.notify();
148 // -----------------------------------------------------------------------------
149 uno::Reference< task::XStatusIndicator > SAL_CALL OReportEngineJFree::getStatusIndicator() throw (uno::RuntimeException)
151 ::osl::MutexGuard aGuard(m_aMutex);
152 return m_StatusIndicator;
154 // -----------------------------------------------------------------------------
155 void SAL_CALL OReportEngineJFree::setStatusIndicator( const uno::Reference< task::XStatusIndicator >& _statusindicator ) throw (uno::RuntimeException)
157 set(PROPERTY_STATUSINDICATOR,_statusindicator,m_StatusIndicator);
159 // -----------------------------------------------------------------------------
160 OUString OReportEngineJFree::getNewOutputName()
162 OUString sOutputName;
164 ::osl::MutexGuard aGuard(m_aMutex);
165 ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
166 if ( !m_xReport.is() || !m_xActiveConnection.is() )
167 throw lang::IllegalArgumentException();
169 static const OUString s_sMediaType("MediaType");
172 MimeConfigurationHelper aConfighelper(m_xContext);
173 const OUString sMimeType = m_xReport->getMimeType();
174 const SfxFilter* pFilter = SfxFilter::GetDefaultFilter( aConfighelper.GetDocServiceNameFromMediaType(sMimeType) );
175 String sExt;
176 if ( pFilter )
177 sExt = ::comphelper::string::stripStart(pFilter->GetDefaultExtension(), '*');
178 else
179 sExt = OUString(".rpt");
181 uno::Reference< embed::XStorage > xTemp = OStorageHelper::GetTemporaryStorage(/*sFileTemp,embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE,*/ m_xContext);
182 utl::DisposableComponent aTemp(xTemp);
183 uno::Sequence< beans::PropertyValue > aEmpty;
184 uno::Reference< beans::XPropertySet> xStorageProp(xTemp,uno::UNO_QUERY);
185 if ( xStorageProp.is() )
187 xStorageProp->setPropertyValue( s_sMediaType, uno::makeAny(sMimeType));
189 m_xReport->storeToStorage(xTemp,aEmpty); // store to temp file because it may contain information which isn't in the database yet.
191 uno::Sequence< beans::NamedValue > aConvertedProperties(8);
192 sal_Int32 nPos = 0;
193 aConvertedProperties[nPos].Name = OUString("InputStorage");
194 aConvertedProperties[nPos++].Value <<= xTemp;
195 aConvertedProperties[nPos].Name = OUString("OutputStorage");
197 OUString sFileURL;
198 String sName = m_xReport->getCaption();
199 if ( !sName.Len() )
200 sName = m_xReport->getName();
202 ::utl::TempFile aTestFile(sName,sal_False,&sExt);
203 if ( !aTestFile.IsValid() )
205 sName = RPT_RESSTRING(RID_STR_REPORT,m_xContext->getServiceManager());
206 ::utl::TempFile aFile(sName,sal_False,&sExt);
207 sFileURL = aFile.GetURL();
209 else
210 sFileURL = aTestFile.GetURL();
213 uno::Reference< embed::XStorage > xOut = OStorageHelper::GetStorageFromURL(sFileURL,embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE, m_xContext);
214 utl::DisposableComponent aOut(xOut);
215 xStorageProp.set(xOut,uno::UNO_QUERY);
216 if ( xStorageProp.is() )
218 xStorageProp->setPropertyValue( s_sMediaType, uno::makeAny(sMimeType));
221 aConvertedProperties[nPos++].Value <<= xOut;
223 aConvertedProperties[nPos].Name = PROPERTY_REPORTDEFINITION;
224 aConvertedProperties[nPos++].Value <<= m_xReport;
226 aConvertedProperties[nPos].Name = PROPERTY_ACTIVECONNECTION;
227 aConvertedProperties[nPos++].Value <<= m_xActiveConnection;
229 aConvertedProperties[nPos].Name = PROPERTY_MAXROWS;
230 aConvertedProperties[nPos++].Value <<= m_nMaxRows;
232 // some meta data
233 SvtUserOptions aUserOpts;
234 OUStringBuffer sAuthor(aUserOpts.GetFirstName());
235 sAuthor.appendAscii(" ");
236 sAuthor.append(aUserOpts.GetLastName());
237 static const OUString s_sAuthor("Author");
238 aConvertedProperties[nPos].Name = s_sAuthor;
239 aConvertedProperties[nPos++].Value <<= sAuthor.makeStringAndClear();
241 static const OUString s_sTitle("Title");
242 aConvertedProperties[nPos].Name = s_sTitle;
243 aConvertedProperties[nPos++].Value <<= m_xReport->getCaption();
245 // create job factory and initialize
246 const OUString sReportEngineServiceName = ::dbtools::getDefaultReportEngineServiceName(m_xContext);
247 uno::Reference<task::XJob> xJob(m_xContext->getServiceManager()->createInstanceWithContext(sReportEngineServiceName,m_xContext),uno::UNO_QUERY_THROW);
248 if ( !m_xReport->getCommand().isEmpty() )
250 xJob->execute(aConvertedProperties);
251 if ( xStorageProp.is() )
253 sOutputName = sFileURL;
257 uno::Reference<embed::XTransactedObject> xTransact(xOut,uno::UNO_QUERY);
258 if ( !sOutputName.isEmpty() && xTransact.is() )
259 xTransact->commit();
261 if ( sOutputName.isEmpty() )
262 throw lang::IllegalArgumentException();
264 catch(const uno::Exception& e)
266 (void)e; // helper to know what e contains
267 throw;
270 return sOutputName;
272 // -----------------------------------------------------------------------------
273 // Methods
274 uno::Reference< frame::XModel > SAL_CALL OReportEngineJFree::createDocumentModel( ) throw (lang::DisposedException, lang::IllegalArgumentException, uno::Exception, uno::RuntimeException)
276 return createDocumentAlive(NULL,true);
278 // -----------------------------------------------------------------------------
279 uno::Reference< frame::XModel > SAL_CALL OReportEngineJFree::createDocumentAlive( const uno::Reference< frame::XFrame >& _frame ) throw (lang::DisposedException, lang::IllegalArgumentException, uno::Exception, uno::RuntimeException)
281 return createDocumentAlive(_frame,false);
283 // -----------------------------------------------------------------------------
284 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 uno::Reference< frame::XModel > xModel;
287 OUString sOutputName = getNewOutputName(); // starts implicite the report generator
288 if ( !sOutputName.isEmpty() )
290 ::osl::MutexGuard aGuard(m_aMutex);
291 ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
292 uno::Reference<frame::XComponentLoader> xFrameLoad(_frame,uno::UNO_QUERY);
293 if ( !xFrameLoad.is() )
295 // if there is no frame given, find the right
296 xFrameLoad.set( frame::Desktop::create(m_xContext), uno::UNO_QUERY);
297 OUString sTarget("_blank");
298 sal_Int32 nFrameSearchFlag = frame::FrameSearchFlag::TASKS | frame::FrameSearchFlag::CREATE;
299 uno::Reference< frame::XFrame> xFrame = uno::Reference< frame::XFrame>(xFrameLoad,uno::UNO_QUERY)->findFrame(sTarget,nFrameSearchFlag);
300 xFrameLoad.set( xFrame,uno::UNO_QUERY);
303 if ( xFrameLoad.is() )
305 uno::Sequence < beans::PropertyValue > aArgs( _bHidden ? 3 : 2 );
306 sal_Int32 nLen = 0;
307 aArgs[nLen].Name = OUString("AsTemplate");
308 aArgs[nLen++].Value <<= sal_False;
310 aArgs[nLen].Name = OUString("ReadOnly");
311 aArgs[nLen++].Value <<= sal_True;
313 if ( _bHidden )
315 aArgs[nLen].Name = OUString("Hidden");
316 aArgs[nLen++].Value <<= sal_True;
319 uno::Reference< lang::XMultiServiceFactory > xFac(m_xContext->getServiceManager(),uno::UNO_QUERY);
320 xModel.set( xFrameLoad->loadComponentFromURL(
321 sOutputName,
322 OUString(), // empty frame name
324 aArgs
325 ),uno::UNO_QUERY);
328 return xModel;
330 // -----------------------------------------------------------------------------
331 util::URL SAL_CALL OReportEngineJFree::createDocument( ) throw (lang::DisposedException, lang::IllegalArgumentException, uno::Exception, uno::RuntimeException)
333 util::URL aRet;
334 uno::Reference< frame::XModel > xModel = createDocumentModel();
335 if ( xModel.is() )
337 ::osl::MutexGuard aGuard(m_aMutex);
338 ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
340 return aRet;
342 // -----------------------------------------------------------------------------
343 void SAL_CALL OReportEngineJFree::interrupt( ) throw (lang::DisposedException, uno::Exception, uno::RuntimeException)
346 ::osl::MutexGuard aGuard(m_aMutex);
347 ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
350 // -----------------------------------------------------------------------------
351 uno::Reference< beans::XPropertySetInfo > SAL_CALL OReportEngineJFree::getPropertySetInfo( ) throw(uno::RuntimeException)
353 return ReportEnginePropertySet::getPropertySetInfo();
355 // -------------------------------------------------------------------------
356 void SAL_CALL OReportEngineJFree::setPropertyValue( const OUString& aPropertyName, const uno::Any& aValue ) throw (beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
358 ReportEnginePropertySet::setPropertyValue( aPropertyName, aValue );
360 // -----------------------------------------------------------------------------
361 uno::Any SAL_CALL OReportEngineJFree::getPropertyValue( const OUString& PropertyName ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
363 return ReportEnginePropertySet::getPropertyValue( PropertyName);
365 // -----------------------------------------------------------------------------
366 void SAL_CALL OReportEngineJFree::addPropertyChangeListener( const OUString& aPropertyName, const uno::Reference< beans::XPropertyChangeListener >& xListener ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
368 ReportEnginePropertySet::addPropertyChangeListener( aPropertyName, xListener );
370 // -----------------------------------------------------------------------------
371 void SAL_CALL OReportEngineJFree::removePropertyChangeListener( const OUString& aPropertyName, const uno::Reference< beans::XPropertyChangeListener >& aListener ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
373 ReportEnginePropertySet::removePropertyChangeListener( aPropertyName, aListener );
375 // -----------------------------------------------------------------------------
376 void SAL_CALL OReportEngineJFree::addVetoableChangeListener( const OUString& PropertyName, const uno::Reference< beans::XVetoableChangeListener >& aListener ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
378 ReportEnginePropertySet::addVetoableChangeListener( PropertyName, aListener );
380 // -----------------------------------------------------------------------------
381 void SAL_CALL OReportEngineJFree::removeVetoableChangeListener( const OUString& PropertyName, const uno::Reference< beans::XVetoableChangeListener >& aListener ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
383 ReportEnginePropertySet::removeVetoableChangeListener( PropertyName, aListener );
385 // -----------------------------------------------------------------------------
386 uno::Reference< sdbc::XConnection > SAL_CALL OReportEngineJFree::getActiveConnection() throw (uno::RuntimeException)
388 return m_xActiveConnection;
390 // -----------------------------------------------------------------------------
391 void SAL_CALL OReportEngineJFree::setActiveConnection( const uno::Reference< sdbc::XConnection >& _activeconnection ) throw (lang::IllegalArgumentException, uno::RuntimeException)
393 if ( !_activeconnection.is() )
394 throw lang::IllegalArgumentException();
395 set(PROPERTY_ACTIVECONNECTION,_activeconnection,m_xActiveConnection);
397 // -----------------------------------------------------------------------------
398 ::sal_Int32 SAL_CALL OReportEngineJFree::getMaxRows() throw (uno::RuntimeException)
400 ::osl::MutexGuard aGuard(m_aMutex);
401 return m_nMaxRows;
403 // -----------------------------------------------------------------------------
404 void SAL_CALL OReportEngineJFree::setMaxRows( ::sal_Int32 _MaxRows ) throw (uno::RuntimeException)
406 set(PROPERTY_MAXROWS,_MaxRows,m_nMaxRows);
408 // =============================================================================
409 } // namespace reportdesign
410 // =============================================================================
412 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */