LibreOffice Module reportdesign (master) 1
ReportEngineJFree.cxx
Go to the documentation of this file.
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>
24#include <comphelper/string.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>
31
32#include <com/sun/star/task/XJob.hpp>
33
35#include <unotools/tempfile.hxx>
37
38#include <strings.hxx>
39#include <strings.hrc>
40#include <core_resource.hxx>
41
43#include <sfx2/docfilt.hxx>
44
45namespace reportdesign
46{
47
48 using namespace com::sun::star;
49 using namespace comphelper;
50
51
52OReportEngineJFree::OReportEngineJFree( const uno::Reference< uno::XComponentContext >& context)
54,ReportEnginePropertySet(context,IMPLEMENTS_PROPERTY_SET,uno::Sequence< OUString >())
55,m_xContext(context)
56,m_nMaxRows(0)
57{
58}
59
60// TODO: VirtualFunctionFinder: This is virtual function!
61
62OReportEngineJFree::~OReportEngineJFree()
63{
64}
65
67
68void SAL_CALL OReportEngineJFree::dispose()
69{
70 ReportEnginePropertySet::dispose();
71 cppu::WeakComponentImplHelperBase::dispose();
72 m_xActiveConnection.clear();
73}
74
75OUString OReportEngineJFree::getImplementationName_Static( )
76{
77 return "com.sun.star.comp.report.OReportEngineJFree";
78}
79
80
81OUString SAL_CALL OReportEngineJFree::getImplementationName( )
82{
83 return getImplementationName_Static();
84}
85
86uno::Sequence< OUString > OReportEngineJFree::getSupportedServiceNames_Static( )
87{
88 uno::Sequence< OUString > aServices { "com.sun.star.report.ReportEngine" };
89
90 return aServices;
91}
92
93uno::Reference< uno::XInterface > OReportEngineJFree::create(uno::Reference< uno::XComponentContext > const & xContext)
94{
95 return *(new OReportEngineJFree(xContext));
96}
97
98
99uno::Sequence< OUString > SAL_CALL OReportEngineJFree::getSupportedServiceNames( )
100{
101 return getSupportedServiceNames_Static();
102}
103
104sal_Bool SAL_CALL OReportEngineJFree::supportsService(const OUString& ServiceName)
105{
107}
108
109// XReportEngine
110 // Attributes
111uno::Reference< report::XReportDefinition > SAL_CALL OReportEngineJFree::getReportDefinition()
112{
113 ::osl::MutexGuard aGuard(m_aMutex);
114 return m_xReport;
115}
116
117void SAL_CALL OReportEngineJFree::setReportDefinition( const uno::Reference< report::XReportDefinition >& _report )
118{
119 if ( !_report.is() )
120 throw lang::IllegalArgumentException();
121 BoundListeners l;
122 {
123 ::osl::MutexGuard aGuard(m_aMutex);
124 if ( m_xReport != _report )
125 {
126 prepareSet(PROPERTY_REPORTDEFINITION, uno::Any(m_xReport), uno::Any(_report), &l);
127 m_xReport = _report;
128 }
129 }
130 l.notify();
131}
132
133uno::Reference< task::XStatusIndicator > SAL_CALL OReportEngineJFree::getStatusIndicator()
134{
135 ::osl::MutexGuard aGuard(m_aMutex);
136 return m_StatusIndicator;
137}
138
139void SAL_CALL OReportEngineJFree::setStatusIndicator( const uno::Reference< task::XStatusIndicator >& _statusindicator )
140{
141 set(PROPERTY_STATUSINDICATOR,_statusindicator,m_StatusIndicator);
142}
143
144OUString OReportEngineJFree::getNewOutputName()
145{
146 ::osl::MutexGuard aGuard(m_aMutex);
147 ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
148 if ( !m_xReport.is() || !m_xActiveConnection.is() )
149 throw lang::IllegalArgumentException();
150
151 static constexpr OUStringLiteral s_sMediaType = u"MediaType";
152
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(".rpt");
157 if ( pFilter )
158 sExt = ::comphelper::string::stripStart(pFilter->GetDefaultExtension(), '*');
159
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() )
165 {
166 xStorageProp->setPropertyValue( s_sMediaType, uno::Any(sMimeType));
167 }
168 m_xReport->storeToStorage(xTemp,aEmpty); // store to temp file because it may contain information which isn't in the database yet.
169
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() )
176 {
177 ::utl::TempFileNamed aTestFile(sName, false, sExt);
178 if ( !aTestFile.IsValid() )
179 {
180 sName = RptResId(RID_STR_REPORT);
181 ::utl::TempFileNamed aFile(sName, false, sExt);
182 sFileURL = aFile.GetURL();
183 }
184 else
185 sFileURL = aTestFile.GetURL();
186 }
187
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() )
192 {
193 xStorageProp->setPropertyValue( s_sMediaType, uno::Any(sMimeType));
194 }
195
196 // some meta data
197 SvtUserOptions aUserOpts;
198 OUString sAuthor = aUserOpts.GetFirstName() +
199 " " +
200 aUserOpts.GetLastName();
201
202 uno::Sequence< beans::NamedValue > aConvertedProperties{
203 {"InputStorage", uno::Any(xTemp) },
204 {"OutputStorage", 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 {"Author", uno::Any(sAuthor) },
209 {"Title", uno::Any(m_xReport->getCaption()) }
210 };
211
212 OUString sOutputName;
213
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() )
218 {
219 xJob->execute(aConvertedProperties);
220 if ( xStorageProp.is() )
221 {
222 sOutputName = sFileURL;
223 }
224 }
225
226 uno::Reference<embed::XTransactedObject> xTransact(xOut,uno::UNO_QUERY);
227 if ( !sOutputName.isEmpty() && xTransact.is() )
228 xTransact->commit();
229
230 if ( sOutputName.isEmpty() )
231 throw lang::IllegalArgumentException();
232
233 return sOutputName;
234}
235
236// Methods
237uno::Reference< frame::XModel > SAL_CALL OReportEngineJFree::createDocumentModel( )
238{
239 return createDocumentAlive(nullptr,true);
240}
241
242uno::Reference< frame::XModel > SAL_CALL OReportEngineJFree::createDocumentAlive( const uno::Reference< frame::XFrame >& _frame )
243{
244 return createDocumentAlive(_frame,false);
245}
246
247uno::Reference< frame::XModel > OReportEngineJFree::createDocumentAlive( const uno::Reference< frame::XFrame >& _frame,bool _bHidden )
248{
249 uno::Reference< frame::XModel > xModel;
250 OUString sOutputName = getNewOutputName(); // starts implicitly the report generator
251 if ( !sOutputName.isEmpty() )
252 {
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() )
257 {
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("_blank",nFrameSearchFlag);
262 xFrameLoad.set( xFrame,uno::UNO_QUERY);
263 }
264
265 if ( xFrameLoad.is() )
266 {
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;
272
273 pArgs[nLen].Name = "ReadOnly";
274 pArgs[nLen++].Value <<= true;
275
276 if ( _bHidden )
277 {
278 pArgs[nLen].Name = "Hidden";
279 pArgs[nLen++].Value <<= true;
280 }
281
282 xModel.set( xFrameLoad->loadComponentFromURL(
283 sOutputName,
284 OUString(), // empty frame name
285 0,
286 aArgs
287 ),uno::UNO_QUERY);
288 }
289 }
290 return xModel;
291}
292
293util::URL SAL_CALL OReportEngineJFree::createDocument( )
294{
295 util::URL aRet;
296 uno::Reference< frame::XModel > xModel = createDocumentModel();
297 if ( xModel.is() )
298 {
299 ::osl::MutexGuard aGuard(m_aMutex);
300 ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
301 }
302 return aRet;
303}
304
305void SAL_CALL OReportEngineJFree::interrupt( )
306{
307 ::osl::MutexGuard aGuard(m_aMutex);
308 ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
309}
310
311uno::Reference< beans::XPropertySetInfo > SAL_CALL OReportEngineJFree::getPropertySetInfo( )
312{
313 return ReportEnginePropertySet::getPropertySetInfo();
314}
315
316void SAL_CALL OReportEngineJFree::setPropertyValue( const OUString& aPropertyName, const uno::Any& aValue )
317{
318 ReportEnginePropertySet::setPropertyValue( aPropertyName, aValue );
319}
320
321uno::Any SAL_CALL OReportEngineJFree::getPropertyValue( const OUString& PropertyName )
322{
323 return ReportEnginePropertySet::getPropertyValue( PropertyName);
324}
325
326void SAL_CALL OReportEngineJFree::addPropertyChangeListener( const OUString& aPropertyName, const uno::Reference< beans::XPropertyChangeListener >& xListener )
327{
328 ReportEnginePropertySet::addPropertyChangeListener( aPropertyName, xListener );
329}
330
331void SAL_CALL OReportEngineJFree::removePropertyChangeListener( const OUString& aPropertyName, const uno::Reference< beans::XPropertyChangeListener >& aListener )
332{
333 ReportEnginePropertySet::removePropertyChangeListener( aPropertyName, aListener );
334}
335
336void SAL_CALL OReportEngineJFree::addVetoableChangeListener( const OUString& PropertyName, const uno::Reference< beans::XVetoableChangeListener >& aListener )
337{
338 ReportEnginePropertySet::addVetoableChangeListener( PropertyName, aListener );
339}
340
341void SAL_CALL OReportEngineJFree::removeVetoableChangeListener( const OUString& PropertyName, const uno::Reference< beans::XVetoableChangeListener >& aListener )
342{
343 ReportEnginePropertySet::removeVetoableChangeListener( PropertyName, aListener );
344}
345
346uno::Reference< sdbc::XConnection > SAL_CALL OReportEngineJFree::getActiveConnection()
347{
348 return m_xActiveConnection;
349}
350
351void SAL_CALL OReportEngineJFree::setActiveConnection( const uno::Reference< sdbc::XConnection >& _activeconnection )
352{
353 if ( !_activeconnection.is() )
354 throw lang::IllegalArgumentException();
355 set(PROPERTY_ACTIVECONNECTION,_activeconnection,m_xActiveConnection);
356}
357
358::sal_Int32 SAL_CALL OReportEngineJFree::getMaxRows()
359{
360 ::osl::MutexGuard aGuard(m_aMutex);
361 return m_nMaxRows;
362}
363
364void SAL_CALL OReportEngineJFree::setMaxRows( ::sal_Int32 MaxRows )
365{
366 set(PROPERTY_MAXROWS,MaxRows,m_nMaxRows);
367}
368
369} // namespace reportdesign
370
371
372/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
static std::shared_ptr< const SfxFilter > GetDefaultFilter(std::u16string_view rName)
OUString GetFirstName() const
OUString GetLastName() const
OUString GetDocServiceNameFromMediaType(const OUString &aMediaType)
OReportEngineJFree(const OReportEngineJFree &)=delete
bool IsValid() const
OUString const & GetURL() const
OUString RptResId(TranslateId aId)
float u
OUString sName
std::mutex m_aMutex
void set(css::uno::UnoInterfaceReference const &value)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
::cppu::WeakComponentImplHelper< css::report::XReportEngine,css::lang::XServiceInfo > ReportEngineBase
::cppu::PropertySetMixin< css::report::XReportEngine > ReportEnginePropertySet
IMPLEMENT_FORWARD_XINTERFACE2(ChildWindowPane, ChildWindowPaneInterfaceBase, Pane)
constexpr OUStringLiteral PROPERTY_STATUSINDICATOR
Definition: strings.hxx:181
constexpr OUStringLiteral PROPERTY_ACTIVECONNECTION
Definition: strings.hxx:187
constexpr OUStringLiteral PROPERTY_MAXROWS
Definition: strings.hxx:241
constexpr OUStringLiteral PROPERTY_REPORTDEFINITION
Definition: strings.hxx:49
Reference< XFrame > xFrame
Reference< XModel > xModel
unsigned char sal_Bool