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 <sfx2/docfilt.hxx>
44 
45 namespace reportdesign
46 {
47 
48  using namespace com::sun::star;
49  using namespace comphelper;
50 
51 
52 OReportEngineJFree::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 
63 {
64 }
65 
67 
68 void SAL_CALL OReportEngineJFree::dispose()
69 {
71  cppu::WeakComponentImplHelperBase::dispose();
72  m_xActiveConnection.clear();
73 }
74 
76 {
77  return "com.sun.star.comp.report.OReportEngineJFree";
78 }
79 
80 
82 {
84 }
85 
87 {
88  uno::Sequence< OUString > aServices { "com.sun.star.report.ReportEngine" };
89 
90  return aServices;
91 }
92 
93 uno::Reference< uno::XInterface > OReportEngineJFree::create(uno::Reference< uno::XComponentContext > const & xContext)
94 {
95  return *(new OReportEngineJFree(xContext));
96 }
97 
98 
99 uno::Sequence< OUString > SAL_CALL OReportEngineJFree::getSupportedServiceNames( )
100 {
102 }
103 
104 sal_Bool SAL_CALL OReportEngineJFree::supportsService(const OUString& ServiceName)
105 {
106  return cppu::supportsService(this, ServiceName);
107 }
108 
109 // XReportEngine
110  // Attributes
111 uno::Reference< report::XReportDefinition > SAL_CALL OReportEngineJFree::getReportDefinition()
112 {
113  ::osl::MutexGuard aGuard(m_aMutex);
114  return m_xReport;
115 }
116 
117 void 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  {
127  m_xReport = _report;
128  }
129  }
130  l.notify();
131 }
132 
133 uno::Reference< task::XStatusIndicator > SAL_CALL OReportEngineJFree::getStatusIndicator()
134 {
135  ::osl::MutexGuard aGuard(m_aMutex);
136  return m_StatusIndicator;
137 }
138 
139 void SAL_CALL OReportEngineJFree::setStatusIndicator( const uno::Reference< task::XStatusIndicator >& _statusindicator )
140 {
142 }
143 
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  {
175  ::utl::TempFile aTestFile(sName, false, &sExt);
176  if ( !aTestFile.IsValid() )
177  {
178  sName = RptResId(RID_STR_REPORT);
179  ::utl::TempFile aFile(sName, false, &sExt);
180  sFileURL = aFile.GetURL();
181  }
182  else
183  sFileURL = aTestFile.GetURL();
184  }
185 
186  uno::Reference< embed::XStorage > xOut = OStorageHelper::GetStorageFromURL(sFileURL,embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE, m_xContext);
187  utl::DisposableComponent aOut(xOut);
188  xStorageProp.set(xOut,uno::UNO_QUERY);
189  if ( xStorageProp.is() )
190  {
191  xStorageProp->setPropertyValue( s_sMediaType, uno::Any(sMimeType));
192  }
193 
194  // some meta data
195  SvtUserOptions aUserOpts;
196  OUString sAuthor = aUserOpts.GetFirstName() +
197  " " +
198  aUserOpts.GetLastName();
199 
200  uno::Sequence< beans::NamedValue > aConvertedProperties{
201  {"InputStorage", uno::Any(xTemp) },
202  {"OutputStorage", uno::Any(xOut) },
206  {"Author", uno::Any(sAuthor) },
207  {"Title", uno::Any(m_xReport->getCaption()) }
208  };
209 
210  OUString sOutputName;
211 
212  // create job factory and initialize
213  const OUString sReportEngineServiceName = ::dbtools::getDefaultReportEngineServiceName(m_xContext);
214  uno::Reference<task::XJob> xJob(m_xContext->getServiceManager()->createInstanceWithContext(sReportEngineServiceName,m_xContext),uno::UNO_QUERY_THROW);
215  if ( !m_xReport->getCommand().isEmpty() )
216  {
217  xJob->execute(aConvertedProperties);
218  if ( xStorageProp.is() )
219  {
220  sOutputName = sFileURL;
221  }
222  }
223 
224  uno::Reference<embed::XTransactedObject> xTransact(xOut,uno::UNO_QUERY);
225  if ( !sOutputName.isEmpty() && xTransact.is() )
226  xTransact->commit();
227 
228  if ( sOutputName.isEmpty() )
229  throw lang::IllegalArgumentException();
230 
231  return sOutputName;
232 }
233 
234 // Methods
235 uno::Reference< frame::XModel > SAL_CALL OReportEngineJFree::createDocumentModel( )
236 {
237  return createDocumentAlive(nullptr,true);
238 }
239 
240 uno::Reference< frame::XModel > SAL_CALL OReportEngineJFree::createDocumentAlive( const uno::Reference< frame::XFrame >& _frame )
241 {
242  return createDocumentAlive(_frame,false);
243 }
244 
245 uno::Reference< frame::XModel > OReportEngineJFree::createDocumentAlive( const uno::Reference< frame::XFrame >& _frame,bool _bHidden )
246 {
247  uno::Reference< frame::XModel > xModel;
248  OUString sOutputName = getNewOutputName(); // starts implicitly the report generator
249  if ( !sOutputName.isEmpty() )
250  {
251  ::osl::MutexGuard aGuard(m_aMutex);
252  ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
253  uno::Reference<frame::XComponentLoader> xFrameLoad(_frame,uno::UNO_QUERY);
254  if ( !xFrameLoad.is() )
255  {
256  // if there is no frame given, find the right
257  xFrameLoad = frame::Desktop::create(m_xContext);
258  sal_Int32 const nFrameSearchFlag = frame::FrameSearchFlag::TASKS | frame::FrameSearchFlag::CREATE;
259  uno::Reference< frame::XFrame> xFrame = uno::Reference< frame::XFrame>(xFrameLoad,uno::UNO_QUERY_THROW)->findFrame("_blank",nFrameSearchFlag);
260  xFrameLoad.set( xFrame,uno::UNO_QUERY);
261  }
262 
263  if ( xFrameLoad.is() )
264  {
265  uno::Sequence < beans::PropertyValue > aArgs( _bHidden ? 3 : 2 );
266  auto pArgs = aArgs.getArray();
267  sal_Int32 nLen = 0;
268  pArgs[nLen].Name = "AsTemplate";
269  pArgs[nLen++].Value <<= false;
270 
271  pArgs[nLen].Name = "ReadOnly";
272  pArgs[nLen++].Value <<= true;
273 
274  if ( _bHidden )
275  {
276  pArgs[nLen].Name = "Hidden";
277  pArgs[nLen++].Value <<= true;
278  }
279 
280  xModel.set( xFrameLoad->loadComponentFromURL(
281  sOutputName,
282  OUString(), // empty frame name
283  0,
284  aArgs
285  ),uno::UNO_QUERY);
286  }
287  }
288  return xModel;
289 }
290 
292 {
293  util::URL aRet;
294  uno::Reference< frame::XModel > xModel = createDocumentModel();
295  if ( xModel.is() )
296  {
297  ::osl::MutexGuard aGuard(m_aMutex);
298  ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
299  }
300  return aRet;
301 }
302 
304 {
305  ::osl::MutexGuard aGuard(m_aMutex);
306  ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
307 }
308 
309 uno::Reference< beans::XPropertySetInfo > SAL_CALL OReportEngineJFree::getPropertySetInfo( )
310 {
312 }
313 
314 void SAL_CALL OReportEngineJFree::setPropertyValue( const OUString& aPropertyName, const uno::Any& aValue )
315 {
316  ReportEnginePropertySet::setPropertyValue( aPropertyName, aValue );
317 }
318 
319 uno::Any SAL_CALL OReportEngineJFree::getPropertyValue( const OUString& PropertyName )
320 {
321  return ReportEnginePropertySet::getPropertyValue( PropertyName);
322 }
323 
324 void SAL_CALL OReportEngineJFree::addPropertyChangeListener( const OUString& aPropertyName, const uno::Reference< beans::XPropertyChangeListener >& xListener )
325 {
326  ReportEnginePropertySet::addPropertyChangeListener( aPropertyName, xListener );
327 }
328 
329 void SAL_CALL OReportEngineJFree::removePropertyChangeListener( const OUString& aPropertyName, const uno::Reference< beans::XPropertyChangeListener >& aListener )
330 {
331  ReportEnginePropertySet::removePropertyChangeListener( aPropertyName, aListener );
332 }
333 
334 void SAL_CALL OReportEngineJFree::addVetoableChangeListener( const OUString& PropertyName, const uno::Reference< beans::XVetoableChangeListener >& aListener )
335 {
336  ReportEnginePropertySet::addVetoableChangeListener( PropertyName, aListener );
337 }
338 
339 void SAL_CALL OReportEngineJFree::removeVetoableChangeListener( const OUString& PropertyName, const uno::Reference< beans::XVetoableChangeListener >& aListener )
340 {
342 }
343 
344 uno::Reference< sdbc::XConnection > SAL_CALL OReportEngineJFree::getActiveConnection()
345 {
346  return m_xActiveConnection;
347 }
348 
349 void SAL_CALL OReportEngineJFree::setActiveConnection( const uno::Reference< sdbc::XConnection >& _activeconnection )
350 {
351  if ( !_activeconnection.is() )
352  throw lang::IllegalArgumentException();
354 }
355 
356 ::sal_Int32 SAL_CALL OReportEngineJFree::getMaxRows()
357 {
358  ::osl::MutexGuard aGuard(m_aMutex);
359  return m_nMaxRows;
360 }
361 
362 void SAL_CALL OReportEngineJFree::setMaxRows( ::sal_Int32 MaxRows )
363 {
365 }
366 
367 } // namespace reportdesign
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
#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 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:241
OUString const & GetURL() const
static std::shared_ptr< const SfxFilter > GetDefaultFilter(std::u16string_view rName)
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:181
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:187
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
css::uno::Reference< css::task::XStatusIndicator > m_StatusIndicator