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>
22 #include <connectivity/dbtools.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 
34 #include <unotools/useroptions.hxx>
35 #include <unotools/tempfile.hxx>
37 
38 #include <strings.hxx>
39 #include <strings.hrc>
40 #include <core_resource.hxx>
41 
43 #include <rtl/ustrbuf.hxx>
44 #include <sfx2/docfilt.hxx>
45 
46 namespace reportdesign
47 {
48 
49  using namespace com::sun::star;
50  using namespace comphelper;
51 
52 
53 OReportEngineJFree::OReportEngineJFree( const uno::Reference< uno::XComponentContext >& context)
55 ,ReportEnginePropertySet(context,IMPLEMENTS_PROPERTY_SET,uno::Sequence< OUString >())
56 ,m_xContext(context)
57 ,m_nMaxRows(0)
58 {
59 }
60 
61 // TODO: VirtualFunctionFinder: This is virtual function!
62 
64 {
65 }
66 
68 
69 void SAL_CALL OReportEngineJFree::dispose()
70 {
72  cppu::WeakComponentImplHelperBase::dispose();
73  m_xActiveConnection.clear();
74 }
75 
77 {
78  return "com.sun.star.comp.report.OReportEngineJFree";
79 }
80 
81 
83 {
85 }
86 
88 {
89  uno::Sequence< OUString > aServices { "com.sun.star.report.ReportEngine" };
90 
91  return aServices;
92 }
93 
94 uno::Reference< uno::XInterface > OReportEngineJFree::create(uno::Reference< uno::XComponentContext > const & xContext)
95 {
96  return *(new OReportEngineJFree(xContext));
97 }
98 
99 
100 uno::Sequence< OUString > SAL_CALL OReportEngineJFree::getSupportedServiceNames( )
101 {
103 }
104 
105 sal_Bool SAL_CALL OReportEngineJFree::supportsService(const OUString& ServiceName)
106 {
107  return cppu::supportsService(this, ServiceName);
108 }
109 
110 // XReportEngine
111  // Attributes
112 uno::Reference< report::XReportDefinition > SAL_CALL OReportEngineJFree::getReportDefinition()
113 {
114  ::osl::MutexGuard aGuard(m_aMutex);
115  return m_xReport;
116 }
117 
118 void SAL_CALL OReportEngineJFree::setReportDefinition( const uno::Reference< report::XReportDefinition >& _report )
119 {
120  if ( !_report.is() )
121  throw lang::IllegalArgumentException();
122  BoundListeners l;
123  {
124  ::osl::MutexGuard aGuard(m_aMutex);
125  if ( m_xReport != _report )
126  {
127  prepareSet(PROPERTY_REPORTDEFINITION, uno::makeAny(m_xReport), uno::makeAny(_report), &l);
128  m_xReport = _report;
129  }
130  }
131  l.notify();
132 }
133 
134 uno::Reference< task::XStatusIndicator > SAL_CALL OReportEngineJFree::getStatusIndicator()
135 {
136  ::osl::MutexGuard aGuard(m_aMutex);
137  return m_StatusIndicator;
138 }
139 
140 void SAL_CALL OReportEngineJFree::setStatusIndicator( const uno::Reference< task::XStatusIndicator >& _statusindicator )
141 {
143 }
144 
146 {
147  ::osl::MutexGuard aGuard(m_aMutex);
148  ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
149  if ( !m_xReport.is() || !m_xActiveConnection.is() )
150  throw lang::IllegalArgumentException();
151 
152  static constexpr OUStringLiteral s_sMediaType = u"MediaType";
153 
154  MimeConfigurationHelper aConfighelper(m_xContext);
155  const OUString sMimeType = m_xReport->getMimeType();
156  std::shared_ptr<const SfxFilter> pFilter = SfxFilter::GetDefaultFilter( aConfighelper.GetDocServiceNameFromMediaType(sMimeType) );
157  OUString sExt(".rpt");
158  if ( pFilter )
159  sExt = ::comphelper::string::stripStart(pFilter->GetDefaultExtension(), '*');
160 
161  uno::Reference< embed::XStorage > xTemp = OStorageHelper::GetTemporaryStorage(/*sFileTemp,embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE,*/ m_xContext);
162  utl::DisposableComponent aTemp(xTemp);
163  uno::Sequence< beans::PropertyValue > aEmpty;
164  uno::Reference< beans::XPropertySet> xStorageProp(xTemp,uno::UNO_QUERY);
165  if ( xStorageProp.is() )
166  {
167  xStorageProp->setPropertyValue( s_sMediaType, uno::makeAny(sMimeType));
168  }
169  m_xReport->storeToStorage(xTemp,aEmpty); // store to temp file because it may contain information which isn't in the database yet.
170 
171  uno::Sequence< beans::NamedValue > aConvertedProperties(8);
172  sal_Int32 nPos = 0;
173  aConvertedProperties[nPos].Name = "InputStorage";
174  aConvertedProperties[nPos++].Value <<= xTemp;
175  aConvertedProperties[nPos].Name = "OutputStorage";
176 
177  OUString sFileURL;
178  OUString sName = m_xReport->getCaption();
179  if ( sName.isEmpty() )
180  sName = m_xReport->getName();
181  {
182  ::utl::TempFile aTestFile(sName, false, &sExt);
183  if ( !aTestFile.IsValid() )
184  {
185  sName = RptResId(RID_STR_REPORT);
186  ::utl::TempFile aFile(sName, false, &sExt);
187  sFileURL = aFile.GetURL();
188  }
189  else
190  sFileURL = aTestFile.GetURL();
191  }
192 
193  uno::Reference< embed::XStorage > xOut = OStorageHelper::GetStorageFromURL(sFileURL,embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE, m_xContext);
194  utl::DisposableComponent aOut(xOut);
195  xStorageProp.set(xOut,uno::UNO_QUERY);
196  if ( xStorageProp.is() )
197  {
198  xStorageProp->setPropertyValue( s_sMediaType, uno::makeAny(sMimeType));
199  }
200 
201  aConvertedProperties[nPos++].Value <<= xOut;
202 
203  aConvertedProperties[nPos].Name = PROPERTY_REPORTDEFINITION;
204  aConvertedProperties[nPos++].Value <<= m_xReport;
205 
206  aConvertedProperties[nPos].Name = PROPERTY_ACTIVECONNECTION;
207  aConvertedProperties[nPos++].Value <<= m_xActiveConnection;
208 
209  aConvertedProperties[nPos].Name = PROPERTY_MAXROWS;
210  aConvertedProperties[nPos++].Value <<= m_nMaxRows;
211 
212  // some meta data
213  SvtUserOptions aUserOpts;
214  OUString sAuthor = aUserOpts.GetFirstName() +
215  " " +
216  aUserOpts.GetLastName();
217  aConvertedProperties[nPos].Name = "Author";
218  aConvertedProperties[nPos++].Value <<= sAuthor;
219 
220  aConvertedProperties[nPos].Name = "Title";
221  aConvertedProperties[nPos++].Value <<= m_xReport->getCaption();
222 
223  OUString sOutputName;
224 
225  // create job factory and initialize
226  const OUString sReportEngineServiceName = ::dbtools::getDefaultReportEngineServiceName(m_xContext);
227  uno::Reference<task::XJob> xJob(m_xContext->getServiceManager()->createInstanceWithContext(sReportEngineServiceName,m_xContext),uno::UNO_QUERY_THROW);
228  if ( !m_xReport->getCommand().isEmpty() )
229  {
230  xJob->execute(aConvertedProperties);
231  if ( xStorageProp.is() )
232  {
233  sOutputName = sFileURL;
234  }
235  }
236 
237  uno::Reference<embed::XTransactedObject> xTransact(xOut,uno::UNO_QUERY);
238  if ( !sOutputName.isEmpty() && xTransact.is() )
239  xTransact->commit();
240 
241  if ( sOutputName.isEmpty() )
242  throw lang::IllegalArgumentException();
243 
244  return sOutputName;
245 }
246 
247 // Methods
248 uno::Reference< frame::XModel > SAL_CALL OReportEngineJFree::createDocumentModel( )
249 {
250  return createDocumentAlive(nullptr,true);
251 }
252 
253 uno::Reference< frame::XModel > SAL_CALL OReportEngineJFree::createDocumentAlive( const uno::Reference< frame::XFrame >& _frame )
254 {
255  return createDocumentAlive(_frame,false);
256 }
257 
258 uno::Reference< frame::XModel > OReportEngineJFree::createDocumentAlive( const uno::Reference< frame::XFrame >& _frame,bool _bHidden )
259 {
260  uno::Reference< frame::XModel > xModel;
261  OUString sOutputName = getNewOutputName(); // starts implicitly the report generator
262  if ( !sOutputName.isEmpty() )
263  {
264  ::osl::MutexGuard aGuard(m_aMutex);
265  ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
266  uno::Reference<frame::XComponentLoader> xFrameLoad(_frame,uno::UNO_QUERY);
267  if ( !xFrameLoad.is() )
268  {
269  // if there is no frame given, find the right
270  xFrameLoad = frame::Desktop::create(m_xContext);
271  sal_Int32 const nFrameSearchFlag = frame::FrameSearchFlag::TASKS | frame::FrameSearchFlag::CREATE;
272  uno::Reference< frame::XFrame> xFrame = uno::Reference< frame::XFrame>(xFrameLoad,uno::UNO_QUERY_THROW)->findFrame("_blank",nFrameSearchFlag);
273  xFrameLoad.set( xFrame,uno::UNO_QUERY);
274  }
275 
276  if ( xFrameLoad.is() )
277  {
278  uno::Sequence < beans::PropertyValue > aArgs( _bHidden ? 3 : 2 );
279  sal_Int32 nLen = 0;
280  aArgs[nLen].Name = "AsTemplate";
281  aArgs[nLen++].Value <<= false;
282 
283  aArgs[nLen].Name = "ReadOnly";
284  aArgs[nLen++].Value <<= true;
285 
286  if ( _bHidden )
287  {
288  aArgs[nLen].Name = "Hidden";
289  aArgs[nLen++].Value <<= true;
290  }
291 
292  xModel.set( xFrameLoad->loadComponentFromURL(
293  sOutputName,
294  OUString(), // empty frame name
295  0,
296  aArgs
297  ),uno::UNO_QUERY);
298  }
299  }
300  return xModel;
301 }
302 
304 {
305  util::URL aRet;
306  uno::Reference< frame::XModel > xModel = createDocumentModel();
307  if ( xModel.is() )
308  {
309  ::osl::MutexGuard aGuard(m_aMutex);
310  ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
311  }
312  return aRet;
313 }
314 
316 {
317  ::osl::MutexGuard aGuard(m_aMutex);
318  ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
319 }
320 
321 uno::Reference< beans::XPropertySetInfo > SAL_CALL OReportEngineJFree::getPropertySetInfo( )
322 {
324 }
325 
326 void SAL_CALL OReportEngineJFree::setPropertyValue( const OUString& aPropertyName, const uno::Any& aValue )
327 {
328  ReportEnginePropertySet::setPropertyValue( aPropertyName, aValue );
329 }
330 
331 uno::Any SAL_CALL OReportEngineJFree::getPropertyValue( const OUString& PropertyName )
332 {
333  return ReportEnginePropertySet::getPropertyValue( PropertyName);
334 }
335 
336 void SAL_CALL OReportEngineJFree::addPropertyChangeListener( const OUString& aPropertyName, const uno::Reference< beans::XPropertyChangeListener >& xListener )
337 {
338  ReportEnginePropertySet::addPropertyChangeListener( aPropertyName, xListener );
339 }
340 
341 void SAL_CALL OReportEngineJFree::removePropertyChangeListener( const OUString& aPropertyName, const uno::Reference< beans::XPropertyChangeListener >& aListener )
342 {
343  ReportEnginePropertySet::removePropertyChangeListener( aPropertyName, aListener );
344 }
345 
346 void SAL_CALL OReportEngineJFree::addVetoableChangeListener( const OUString& PropertyName, const uno::Reference< beans::XVetoableChangeListener >& aListener )
347 {
348  ReportEnginePropertySet::addVetoableChangeListener( PropertyName, aListener );
349 }
350 
351 void SAL_CALL OReportEngineJFree::removeVetoableChangeListener( const OUString& PropertyName, const uno::Reference< beans::XVetoableChangeListener >& aListener )
352 {
354 }
355 
356 uno::Reference< sdbc::XConnection > SAL_CALL OReportEngineJFree::getActiveConnection()
357 {
358  return m_xActiveConnection;
359 }
360 
361 void SAL_CALL OReportEngineJFree::setActiveConnection( const uno::Reference< sdbc::XConnection >& _activeconnection )
362 {
363  if ( !_activeconnection.is() )
364  throw lang::IllegalArgumentException();
366 }
367 
368 ::sal_Int32 SAL_CALL OReportEngineJFree::getMaxRows()
369 {
370  ::osl::MutexGuard aGuard(m_aMutex);
371  return m_nMaxRows;
372 }
373 
374 void SAL_CALL OReportEngineJFree::setMaxRows( ::sal_Int32 MaxRows )
375 {
377 }
378 
379 } // namespace reportdesign
380 
381 
382 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Reference< css::frame::XModel > SAL_CALL createDocumentAlive(const css::uno::Reference< css::frame::XFrame > &_frame) override
IMPLEMENT_FORWARD_XINTERFACE2(ChildWindowPane, ChildWindowPaneInterfaceBase, Pane)
OUString getNewOutputName()
returns the file url for a new model
virtual css::uno::Reference< css::report::XReportDefinition > SAL_CALL getReportDefinition() override
#define PROPERTY_REPORTDEFINITION
Definition: strings.hxx:49
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() SAL_OVERRIDE
::cppu::PropertySetMixin< css::report::XReportEngine > ReportEnginePropertySet
Reference< XFrame > xFrame
virtual void SAL_CALL interrupt() override
virtual css::uno::Any SAL_CALL getPropertyValue(const OUString &PropertyName) override
static css::uno::Reference< css::uno::XInterface > create(css::uno::Reference< css::uno::XComponentContext > const &xContext)
virtual void SAL_CALL removePropertyChangeListener(rtl::OUString const &propertyName, css::uno::Reference< css::beans::XPropertyChangeListener > const &listener) SAL_OVERRIDE
virtual css::util::URL SAL_CALL createDocument() override
static OUString getImplementationName_Static()
css::uno::Reference< css::uno::XComponentContext > m_xContext
::cppu::WeakComponentImplHelper< css::report::XReportEngine,css::lang::XServiceInfo > ReportEngineBase
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
bool IsValid() const
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
virtual void SAL_CALL setReportDefinition(const css::uno::Reference< css::report::XReportDefinition > &_reportdefinition) override
std::mutex m_aMutex
OUString GetDocServiceNameFromMediaType(const OUString &aMediaType)
virtual void SAL_CALL setPropertyValue(rtl::OUString const &propertyName, css::uno::Any const &value) SAL_OVERRIDE
const char * sName
virtual void SAL_CALL removePropertyChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &aListener) override
virtual OUString SAL_CALL getImplementationName() override
virtual void SAL_CALL setMaxRows(::sal_Int32 MaxRows) override
static std::shared_ptr< const SfxFilter > GetDefaultFilter(const OUString &rName)
static css::uno::Reference< css::embed::XStorage > GetTemporaryStorage(const css::uno::Reference< css::uno::XComponentContext > &rxContext=css::uno::Reference< css::uno::XComponentContext >())
OUString RptResId(TranslateId aId)
#define PROPERTY_MAXROWS
Definition: strings.hxx:248
OUString const & GetURL() const
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual css::uno::Reference< css::frame::XModel > SAL_CALL createDocumentModel() override
float u
unsigned char sal_Bool
static css::uno::Reference< css::embed::XStorage > GetStorageFromURL(const OUString &aURL, sal_Int32 nStorageMode, const css::uno::Reference< css::uno::XComponentContext > &rxContext=css::uno::Reference< css::uno::XComponentContext >())
virtual css::uno::Reference< css::task::XStatusIndicator > SAL_CALL getStatusIndicator() override
OReportEngineJFree(const OReportEngineJFree &)=delete
virtual void SAL_CALL addPropertyChangeListener(rtl::OUString const &propertyName, css::uno::Reference< css::beans::XPropertyChangeListener > const &listener) SAL_OVERRIDE
virtual void SAL_CALL removeVetoableChangeListener(const OUString &PropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > &aListener) override
OUString GetFirstName() const
virtual void SAL_CALL setPropertyValue(const OUString &aPropertyName, const css::uno::Any &aValue) override
void prepareSet(rtl::OUString const &propertyName, css::uno::Any const &oldValue, css::uno::Any const &newValue, BoundListeners *boundListeners)
virtual void SAL_CALL setActiveConnection(const css::uno::Reference< css::sdbc::XConnection > &_activeconnection) override
virtual void SAL_CALL addPropertyChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &xListener) override
OUString GetLastName() const
css::uno::Reference< css::sdbc::XConnection > m_xActiveConnection
virtual void SAL_CALL addVetoableChangeListener(const OUString &PropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > &aListener) override
virtual css::uno::Any SAL_CALL getPropertyValue(rtl::OUString const &propertyName) SAL_OVERRIDE
#define PROPERTY_STATUSINDICATOR
Definition: strings.hxx:187
virtual void SAL_CALL addVetoableChangeListener(rtl::OUString const &propertyName, css::uno::Reference< css::beans::XVetoableChangeListener > const &listener) SAL_OVERRIDE
Reference< XModel > xModel
void set(const OUString &_sProperty, const T &Value, T &_member)
virtual void SAL_CALL removeVetoableChangeListener(rtl::OUString const &propertyName, css::uno::Reference< css::beans::XVetoableChangeListener > const &listener) SAL_OVERRIDE
virtual ::sal_Int32 SAL_CALL getMaxRows() override
Reference< XComponentContext > m_xContext
#define PROPERTY_ACTIVECONNECTION
Definition: strings.hxx:193
static css::uno::Sequence< OUString > getSupportedServiceNames_Static()
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual void SAL_CALL setStatusIndicator(const css::uno::Reference< css::task::XStatusIndicator > &_statusindicator) override
virtual css::uno::Reference< css::sdbc::XConnection > SAL_CALL getActiveConnection() override
css::uno::Reference< css::report::XReportDefinition > m_xReport
sal_uInt16 nPos
css::uno::Reference< css::task::XStatusIndicator > m_StatusIndicator