cid#1607171 Data race condition
[LibreOffice.git] / reportdesign / source / core / api / ReportEngineJFree.cxx
blob1d0ee4af1b402ba85bd8481232f06dcd1020109e
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/storagehelper.hxx>
22 #include <connectivity/dbtools.hxx>
23 #include <comphelper/mimeconfighelper.hxx>
24 #include <comphelper/string.hxx>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <com/sun/star/beans/NamedValue.hpp>
27 #include <com/sun/star/frame/Desktop.hpp>
28 #include <com/sun/star/frame/XComponentLoader.hpp>
29 #include <com/sun/star/frame/FrameSearchFlag.hpp>
30 #include <com/sun/star/embed/XTransactedObject.hpp>
32 #include <com/sun/star/task/XJob.hpp>
34 #include <unotools/useroptions.hxx>
35 #include <unotools/tempfile.hxx>
36 #include <unotools/sharedunocomponent.hxx>
38 #include <strings.hxx>
39 #include <strings.hrc>
40 #include <core_resource.hxx>
42 #include <connectivity/CommonTools.hxx>
43 #include <sfx2/docfilt.hxx>
45 namespace reportdesign
48 using namespace com::sun::star;
49 using namespace comphelper;
52 OReportEngineJFree::OReportEngineJFree( const uno::Reference< uno::XComponentContext >& context)
53 :ReportEngineBase(m_aMutex)
54 ,ReportEnginePropertySet(context,IMPLEMENTS_PROPERTY_SET,uno::Sequence< OUString >())
55 ,m_xContext(context)
56 ,m_nMaxRows(0)
60 // TODO: VirtualFunctionFinder: This is virtual function!
62 OReportEngineJFree::~OReportEngineJFree()
66 IMPLEMENT_FORWARD_XINTERFACE2(OReportEngineJFree,ReportEngineBase,ReportEnginePropertySet)
68 void SAL_CALL OReportEngineJFree::dispose()
70 ReportEnginePropertySet::dispose();
71 cppu::WeakComponentImplHelperBase::dispose();
72 m_xActiveConnection.clear();
75 OUString OReportEngineJFree::getImplementationName_Static( )
77 return u"com.sun.star.comp.report.OReportEngineJFree"_ustr;
81 OUString SAL_CALL OReportEngineJFree::getImplementationName( )
83 return getImplementationName_Static();
86 uno::Sequence< OUString > OReportEngineJFree::getSupportedServiceNames_Static( )
88 uno::Sequence< OUString > aServices { u"com.sun.star.report.ReportEngine"_ustr };
90 return aServices;
93 uno::Reference< uno::XInterface > OReportEngineJFree::create(uno::Reference< uno::XComponentContext > const & xContext)
95 return *(new OReportEngineJFree(xContext));
99 uno::Sequence< OUString > SAL_CALL OReportEngineJFree::getSupportedServiceNames( )
101 return getSupportedServiceNames_Static();
104 sal_Bool SAL_CALL OReportEngineJFree::supportsService(const OUString& ServiceName)
106 return cppu::supportsService(this, ServiceName);
109 // XReportEngine
110 // Attributes
111 uno::Reference< report::XReportDefinition > SAL_CALL OReportEngineJFree::getReportDefinition()
113 ::osl::MutexGuard aGuard(m_aMutex);
114 return m_xReport;
117 void SAL_CALL OReportEngineJFree::setReportDefinition( const uno::Reference< report::XReportDefinition >& _report )
119 if ( !_report.is() )
120 throw lang::IllegalArgumentException();
121 BoundListeners l;
123 ::osl::MutexGuard aGuard(m_aMutex);
124 if ( m_xReport != _report )
126 prepareSet(PROPERTY_REPORTDEFINITION, uno::Any(m_xReport), uno::Any(_report), &l);
127 m_xReport = _report;
130 l.notify();
133 uno::Reference< task::XStatusIndicator > SAL_CALL OReportEngineJFree::getStatusIndicator()
135 ::osl::MutexGuard aGuard(m_aMutex);
136 return m_StatusIndicator;
139 void SAL_CALL OReportEngineJFree::setStatusIndicator( const uno::Reference< task::XStatusIndicator >& _statusindicator )
141 set(PROPERTY_STATUSINDICATOR,_statusindicator,m_StatusIndicator);
144 OUString OReportEngineJFree::getNewOutputName()
146 ::osl::MutexGuard aGuard(m_aMutex);
147 ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
148 if ( !m_xReport.is() || !m_xActiveConnection.is() )
149 throw lang::IllegalArgumentException();
151 static constexpr OUString s_sMediaType = u"MediaType"_ustr;
153 MimeConfigurationHelper aConfighelper(m_xContext);
154 const OUString sMimeType = m_xReport->getMimeType();
155 std::shared_ptr<const SfxFilter> pFilter = SfxFilter::GetDefaultFilter( aConfighelper.GetDocServiceNameFromMediaType(sMimeType) );
156 OUString sExt(u".rpt"_ustr);
157 if ( pFilter )
158 sExt = ::comphelper::string::stripStart(pFilter->GetDefaultExtension(), '*');
160 uno::Reference< embed::XStorage > xTemp = OStorageHelper::GetTemporaryStorage(/*sFileTemp,embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE,*/ m_xContext);
161 utl::DisposableComponent aTemp(xTemp);
162 uno::Sequence< beans::PropertyValue > aEmpty;
163 uno::Reference< beans::XPropertySet> xStorageProp(xTemp,uno::UNO_QUERY);
164 if ( xStorageProp.is() )
166 xStorageProp->setPropertyValue( s_sMediaType, uno::Any(sMimeType));
168 m_xReport->storeToStorage(xTemp,aEmpty); // store to temp file because it may contain information which isn't in the database yet.
170 OUString sFileURL;
171 OUString sName = m_xReport->getCaption();
172 if ( sName.isEmpty() )
173 sName = m_xReport->getName();
174 sFileURL = ::utl::CreateTempURL(sName, false, sExt);
175 if ( sFileURL.isEmpty() )
177 ::utl::TempFileNamed aTestFile(sName, false, sExt);
178 if ( !aTestFile.IsValid() )
180 sName = RptResId(RID_STR_REPORT);
181 ::utl::TempFileNamed aFile(sName, false, sExt);
182 sFileURL = aFile.GetURL();
184 else
185 sFileURL = aTestFile.GetURL();
188 uno::Reference< embed::XStorage > xOut = OStorageHelper::GetStorageFromURL(sFileURL,embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE, m_xContext);
189 utl::DisposableComponent aOut(xOut);
190 xStorageProp.set(xOut,uno::UNO_QUERY);
191 if ( xStorageProp.is() )
193 xStorageProp->setPropertyValue( s_sMediaType, uno::Any(sMimeType));
196 // some meta data
197 SvtUserOptions aUserOpts;
198 OUString sAuthor = aUserOpts.GetFirstName() +
199 " " +
200 aUserOpts.GetLastName();
202 uno::Sequence< beans::NamedValue > aConvertedProperties{
203 {u"InputStorage"_ustr, uno::Any(xTemp) },
204 {u"OutputStorage"_ustr, uno::Any(xOut) },
205 {PROPERTY_REPORTDEFINITION, uno::Any(m_xReport) },
206 {PROPERTY_ACTIVECONNECTION, uno::Any(m_xActiveConnection) },
207 {PROPERTY_MAXROWS, uno::Any(m_nMaxRows) },
208 {u"Author"_ustr, uno::Any(sAuthor) },
209 {u"Title"_ustr, uno::Any(m_xReport->getCaption()) }
212 OUString sOutputName;
214 // create job factory and initialize
215 const OUString sReportEngineServiceName = ::dbtools::getDefaultReportEngineServiceName(m_xContext);
216 uno::Reference<task::XJob> xJob(m_xContext->getServiceManager()->createInstanceWithContext(sReportEngineServiceName,m_xContext),uno::UNO_QUERY_THROW);
217 if ( !m_xReport->getCommand().isEmpty() )
219 xJob->execute(aConvertedProperties);
220 if ( xStorageProp.is() )
222 sOutputName = sFileURL;
226 uno::Reference<embed::XTransactedObject> xTransact(xOut,uno::UNO_QUERY);
227 if ( !sOutputName.isEmpty() && xTransact.is() )
228 xTransact->commit();
230 if ( sOutputName.isEmpty() )
231 throw lang::IllegalArgumentException();
233 return sOutputName;
236 // Methods
237 uno::Reference< frame::XModel > SAL_CALL OReportEngineJFree::createDocumentModel( )
239 return createDocumentAlive(nullptr,true);
242 uno::Reference< frame::XModel > SAL_CALL OReportEngineJFree::createDocumentAlive( const uno::Reference< frame::XFrame >& _frame )
244 return createDocumentAlive(_frame,false);
247 uno::Reference< frame::XModel > OReportEngineJFree::createDocumentAlive( const uno::Reference< frame::XFrame >& _frame,bool _bHidden )
249 uno::Reference< frame::XModel > xModel;
250 OUString sOutputName = getNewOutputName(); // starts implicitly the report generator
251 if ( !sOutputName.isEmpty() )
253 ::osl::MutexGuard aGuard(m_aMutex);
254 ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
255 uno::Reference<frame::XComponentLoader> xFrameLoad(_frame,uno::UNO_QUERY);
256 if ( !xFrameLoad.is() )
258 // if there is no frame given, find the right
259 xFrameLoad = frame::Desktop::create(m_xContext);
260 sal_Int32 const nFrameSearchFlag = frame::FrameSearchFlag::TASKS | frame::FrameSearchFlag::CREATE;
261 uno::Reference< frame::XFrame> xFrame = uno::Reference< frame::XFrame>(xFrameLoad,uno::UNO_QUERY_THROW)->findFrame(u"_blank"_ustr,nFrameSearchFlag);
262 xFrameLoad.set( xFrame,uno::UNO_QUERY);
265 if ( xFrameLoad.is() )
267 uno::Sequence < beans::PropertyValue > aArgs( _bHidden ? 3 : 2 );
268 auto pArgs = aArgs.getArray();
269 sal_Int32 nLen = 0;
270 pArgs[nLen].Name = "AsTemplate";
271 pArgs[nLen++].Value <<= false;
273 pArgs[nLen].Name = "ReadOnly";
274 pArgs[nLen++].Value <<= true;
276 if ( _bHidden )
278 pArgs[nLen].Name = "Hidden";
279 pArgs[nLen++].Value <<= true;
282 xModel.set( xFrameLoad->loadComponentFromURL(
283 sOutputName,
284 OUString(), // empty frame name
286 aArgs
287 ),uno::UNO_QUERY);
290 return xModel;
293 util::URL SAL_CALL OReportEngineJFree::createDocument( )
295 util::URL aRet;
296 uno::Reference< frame::XModel > xModel = createDocumentModel();
297 if ( xModel.is() )
299 ::osl::MutexGuard aGuard(m_aMutex);
300 ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
302 return aRet;
305 void SAL_CALL OReportEngineJFree::interrupt( )
307 ::osl::MutexGuard aGuard(m_aMutex);
308 ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
311 uno::Reference< beans::XPropertySetInfo > SAL_CALL OReportEngineJFree::getPropertySetInfo( )
313 return ReportEnginePropertySet::getPropertySetInfo();
316 void SAL_CALL OReportEngineJFree::setPropertyValue( const OUString& aPropertyName, const uno::Any& aValue )
318 ReportEnginePropertySet::setPropertyValue( aPropertyName, aValue );
321 uno::Any SAL_CALL OReportEngineJFree::getPropertyValue( const OUString& PropertyName )
323 return ReportEnginePropertySet::getPropertyValue( PropertyName);
326 void SAL_CALL OReportEngineJFree::addPropertyChangeListener( const OUString& aPropertyName, const uno::Reference< beans::XPropertyChangeListener >& xListener )
328 ReportEnginePropertySet::addPropertyChangeListener( aPropertyName, xListener );
331 void SAL_CALL OReportEngineJFree::removePropertyChangeListener( const OUString& aPropertyName, const uno::Reference< beans::XPropertyChangeListener >& aListener )
333 ReportEnginePropertySet::removePropertyChangeListener( aPropertyName, aListener );
336 void SAL_CALL OReportEngineJFree::addVetoableChangeListener( const OUString& PropertyName, const uno::Reference< beans::XVetoableChangeListener >& aListener )
338 ReportEnginePropertySet::addVetoableChangeListener( PropertyName, aListener );
341 void SAL_CALL OReportEngineJFree::removeVetoableChangeListener( const OUString& PropertyName, const uno::Reference< beans::XVetoableChangeListener >& aListener )
343 ReportEnginePropertySet::removeVetoableChangeListener( PropertyName, aListener );
346 uno::Reference< sdbc::XConnection > SAL_CALL OReportEngineJFree::getActiveConnection()
348 return m_xActiveConnection;
351 void SAL_CALL OReportEngineJFree::setActiveConnection( const uno::Reference< sdbc::XConnection >& _activeconnection )
353 if ( !_activeconnection.is() )
354 throw lang::IllegalArgumentException();
355 set(PROPERTY_ACTIVECONNECTION,_activeconnection,m_xActiveConnection);
358 ::sal_Int32 SAL_CALL OReportEngineJFree::getMaxRows()
360 ::osl::MutexGuard aGuard(m_aMutex);
361 return m_nMaxRows;
364 void SAL_CALL OReportEngineJFree::setMaxRows( ::sal_Int32 MaxRows )
366 set(PROPERTY_MAXROWS,MaxRows,m_nMaxRows);
369 } // namespace reportdesign
372 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */