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 uno::Sequence< OUString > SAL_CALL OReportEngineJFree::getSupportedServiceNames( )
82 {
83  return { "com.sun.star.report.ReportEngine" };
84 }
85 
86 sal_Bool SAL_CALL OReportEngineJFree::supportsService(const OUString& ServiceName)
87 {
88  return cppu::supportsService(this, ServiceName);
89 }
90 
91 // XReportEngine
92  // Attributes
93 uno::Reference< report::XReportDefinition > SAL_CALL OReportEngineJFree::getReportDefinition()
94 {
95  ::osl::MutexGuard aGuard(m_aMutex);
96  return m_xReport;
97 }
98 
99 void SAL_CALL OReportEngineJFree::setReportDefinition( const uno::Reference< report::XReportDefinition >& _report )
100 {
101  if ( !_report.is() )
102  throw lang::IllegalArgumentException();
103  BoundListeners l;
104  {
105  ::osl::MutexGuard aGuard(m_aMutex);
106  if ( m_xReport != _report )
107  {
108  prepareSet(PROPERTY_REPORTDEFINITION, uno::makeAny(m_xReport), uno::makeAny(_report), &l);
109  m_xReport = _report;
110  }
111  }
112  l.notify();
113 }
114 
115 uno::Reference< task::XStatusIndicator > SAL_CALL OReportEngineJFree::getStatusIndicator()
116 {
117  ::osl::MutexGuard aGuard(m_aMutex);
118  return m_StatusIndicator;
119 }
120 
121 void SAL_CALL OReportEngineJFree::setStatusIndicator( const uno::Reference< task::XStatusIndicator >& _statusindicator )
122 {
124 }
125 
127 {
128  ::osl::MutexGuard aGuard(m_aMutex);
129  ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
130  if ( !m_xReport.is() || !m_xActiveConnection.is() )
131  throw lang::IllegalArgumentException();
132 
133  static constexpr OUStringLiteral s_sMediaType = u"MediaType";
134 
135  MimeConfigurationHelper aConfighelper(m_xContext);
136  const OUString sMimeType = m_xReport->getMimeType();
137  std::shared_ptr<const SfxFilter> pFilter = SfxFilter::GetDefaultFilter( aConfighelper.GetDocServiceNameFromMediaType(sMimeType) );
138  OUString sExt(".rpt");
139  if ( pFilter )
140  sExt = ::comphelper::string::stripStart(pFilter->GetDefaultExtension(), '*');
141 
142  uno::Reference< embed::XStorage > xTemp = OStorageHelper::GetTemporaryStorage(/*sFileTemp,embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE,*/ m_xContext);
143  utl::DisposableComponent aTemp(xTemp);
144  uno::Sequence< beans::PropertyValue > aEmpty;
145  uno::Reference< beans::XPropertySet> xStorageProp(xTemp,uno::UNO_QUERY);
146  if ( xStorageProp.is() )
147  {
148  xStorageProp->setPropertyValue( s_sMediaType, uno::makeAny(sMimeType));
149  }
150  m_xReport->storeToStorage(xTemp,aEmpty); // store to temp file because it may contain information which isn't in the database yet.
151 
152  uno::Sequence< beans::NamedValue > aConvertedProperties(8);
153  sal_Int32 nPos = 0;
154  aConvertedProperties[nPos].Name = "InputStorage";
155  aConvertedProperties[nPos++].Value <<= xTemp;
156  aConvertedProperties[nPos].Name = "OutputStorage";
157 
158  OUString sFileURL;
159  OUString sName = m_xReport->getCaption();
160  if ( sName.isEmpty() )
161  sName = m_xReport->getName();
162  {
163  ::utl::TempFile aTestFile(sName, false, &sExt);
164  if ( !aTestFile.IsValid() )
165  {
166  sName = RptResId(RID_STR_REPORT);
167  ::utl::TempFile aFile(sName, false, &sExt);
168  sFileURL = aFile.GetURL();
169  }
170  else
171  sFileURL = aTestFile.GetURL();
172  }
173 
174  uno::Reference< embed::XStorage > xOut = OStorageHelper::GetStorageFromURL(sFileURL,embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE, m_xContext);
175  utl::DisposableComponent aOut(xOut);
176  xStorageProp.set(xOut,uno::UNO_QUERY);
177  if ( xStorageProp.is() )
178  {
179  xStorageProp->setPropertyValue( s_sMediaType, uno::makeAny(sMimeType));
180  }
181 
182  aConvertedProperties[nPos++].Value <<= xOut;
183 
184  aConvertedProperties[nPos].Name = PROPERTY_REPORTDEFINITION;
185  aConvertedProperties[nPos++].Value <<= m_xReport;
186 
187  aConvertedProperties[nPos].Name = PROPERTY_ACTIVECONNECTION;
188  aConvertedProperties[nPos++].Value <<= m_xActiveConnection;
189 
190  aConvertedProperties[nPos].Name = PROPERTY_MAXROWS;
191  aConvertedProperties[nPos++].Value <<= m_nMaxRows;
192 
193  // some meta data
194  SvtUserOptions aUserOpts;
195  OUString sAuthor = aUserOpts.GetFirstName() +
196  " " +
197  aUserOpts.GetLastName();
198  aConvertedProperties[nPos].Name = "Author";
199  aConvertedProperties[nPos++].Value <<= sAuthor;
200 
201  aConvertedProperties[nPos].Name = "Title";
202  aConvertedProperties[nPos++].Value <<= m_xReport->getCaption();
203 
204  OUString sOutputName;
205 
206  // create job factory and initialize
207  const OUString sReportEngineServiceName = ::dbtools::getDefaultReportEngineServiceName(m_xContext);
208  uno::Reference<task::XJob> xJob(m_xContext->getServiceManager()->createInstanceWithContext(sReportEngineServiceName,m_xContext),uno::UNO_QUERY_THROW);
209  if ( !m_xReport->getCommand().isEmpty() )
210  {
211  xJob->execute(aConvertedProperties);
212  if ( xStorageProp.is() )
213  {
214  sOutputName = sFileURL;
215  }
216  }
217 
218  uno::Reference<embed::XTransactedObject> xTransact(xOut,uno::UNO_QUERY);
219  if ( !sOutputName.isEmpty() && xTransact.is() )
220  xTransact->commit();
221 
222  if ( sOutputName.isEmpty() )
223  throw lang::IllegalArgumentException();
224 
225  return sOutputName;
226 }
227 
228 // Methods
229 uno::Reference< frame::XModel > SAL_CALL OReportEngineJFree::createDocumentModel( )
230 {
231  return createDocumentAlive(nullptr,true);
232 }
233 
234 uno::Reference< frame::XModel > SAL_CALL OReportEngineJFree::createDocumentAlive( const uno::Reference< frame::XFrame >& _frame )
235 {
236  return createDocumentAlive(_frame,false);
237 }
238 
239 uno::Reference< frame::XModel > OReportEngineJFree::createDocumentAlive( const uno::Reference< frame::XFrame >& _frame,bool _bHidden )
240 {
241  uno::Reference< frame::XModel > xModel;
242  OUString sOutputName = getNewOutputName(); // starts implicitly the report generator
243  if ( !sOutputName.isEmpty() )
244  {
245  ::osl::MutexGuard aGuard(m_aMutex);
246  ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
247  uno::Reference<frame::XComponentLoader> xFrameLoad(_frame,uno::UNO_QUERY);
248  if ( !xFrameLoad.is() )
249  {
250  // if there is no frame given, find the right
251  xFrameLoad = frame::Desktop::create(m_xContext);
252  sal_Int32 const nFrameSearchFlag = frame::FrameSearchFlag::TASKS | frame::FrameSearchFlag::CREATE;
253  uno::Reference< frame::XFrame> xFrame = uno::Reference< frame::XFrame>(xFrameLoad,uno::UNO_QUERY_THROW)->findFrame("_blank",nFrameSearchFlag);
254  xFrameLoad.set( xFrame,uno::UNO_QUERY);
255  }
256 
257  if ( xFrameLoad.is() )
258  {
259  uno::Sequence < beans::PropertyValue > aArgs( _bHidden ? 3 : 2 );
260  sal_Int32 nLen = 0;
261  aArgs[nLen].Name = "AsTemplate";
262  aArgs[nLen++].Value <<= false;
263 
264  aArgs[nLen].Name = "ReadOnly";
265  aArgs[nLen++].Value <<= true;
266 
267  if ( _bHidden )
268  {
269  aArgs[nLen].Name = "Hidden";
270  aArgs[nLen++].Value <<= true;
271  }
272 
273  xModel.set( xFrameLoad->loadComponentFromURL(
274  sOutputName,
275  OUString(), // empty frame name
276  0,
277  aArgs
278  ),uno::UNO_QUERY);
279  }
280  }
281  return xModel;
282 }
283 
285 {
286  util::URL aRet;
287  uno::Reference< frame::XModel > xModel = createDocumentModel();
288  if ( xModel.is() )
289  {
290  ::osl::MutexGuard aGuard(m_aMutex);
291  ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
292  }
293  return aRet;
294 }
295 
297 {
298  ::osl::MutexGuard aGuard(m_aMutex);
299  ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
300 }
301 
302 uno::Reference< beans::XPropertySetInfo > SAL_CALL OReportEngineJFree::getPropertySetInfo( )
303 {
305 }
306 
307 void SAL_CALL OReportEngineJFree::setPropertyValue( const OUString& aPropertyName, const uno::Any& aValue )
308 {
309  ReportEnginePropertySet::setPropertyValue( aPropertyName, aValue );
310 }
311 
312 uno::Any SAL_CALL OReportEngineJFree::getPropertyValue( const OUString& PropertyName )
313 {
314  return ReportEnginePropertySet::getPropertyValue( PropertyName);
315 }
316 
317 void SAL_CALL OReportEngineJFree::addPropertyChangeListener( const OUString& aPropertyName, const uno::Reference< beans::XPropertyChangeListener >& xListener )
318 {
319  ReportEnginePropertySet::addPropertyChangeListener( aPropertyName, xListener );
320 }
321 
322 void SAL_CALL OReportEngineJFree::removePropertyChangeListener( const OUString& aPropertyName, const uno::Reference< beans::XPropertyChangeListener >& aListener )
323 {
324  ReportEnginePropertySet::removePropertyChangeListener( aPropertyName, aListener );
325 }
326 
327 void SAL_CALL OReportEngineJFree::addVetoableChangeListener( const OUString& PropertyName, const uno::Reference< beans::XVetoableChangeListener >& aListener )
328 {
329  ReportEnginePropertySet::addVetoableChangeListener( PropertyName, aListener );
330 }
331 
332 void SAL_CALL OReportEngineJFree::removeVetoableChangeListener( const OUString& PropertyName, const uno::Reference< beans::XVetoableChangeListener >& aListener )
333 {
335 }
336 
337 uno::Reference< sdbc::XConnection > SAL_CALL OReportEngineJFree::getActiveConnection()
338 {
339  return m_xActiveConnection;
340 }
341 
342 void SAL_CALL OReportEngineJFree::setActiveConnection( const uno::Reference< sdbc::XConnection >& _activeconnection )
343 {
344  if ( !_activeconnection.is() )
345  throw lang::IllegalArgumentException();
347 }
348 
349 ::sal_Int32 SAL_CALL OReportEngineJFree::getMaxRows()
350 {
351  ::osl::MutexGuard aGuard(m_aMutex);
352  return m_nMaxRows;
353 }
354 
355 void SAL_CALL OReportEngineJFree::setMaxRows( ::sal_Int32 MaxRows )
356 {
358 }
359 
360 } // namespace reportdesign
361 
362 
363 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
365  css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
366 {
367  return cppu::acquire(new reportdesign::OReportEngineJFree(context));
368 }
369 
370 /* 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
osl::Mutex m_aMutex
#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
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
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
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 >())
#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
OUString RptResId(const char *pId)
OReportEngineJFree(const OReportEngineJFree &)=delete
virtual void SAL_CALL addPropertyChangeListener(rtl::OUString const &propertyName, css::uno::Reference< css::beans::XPropertyChangeListener > const &listener) SAL_OVERRIDE
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * reportdesign_OReportEngineJFree_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
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
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