sync master with lastest vba changes
[ooovba.git] / reportdesign / source / core / api / ReportEngineJFree.cxx
blob25bfdf6539275c38b4789d4eead77ac4f3ffa2e1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ReportEngineJFree.cxx,v $
10 * $Revision: 1.11 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include <com/sun/star/beans/PropertyValue.hpp>
31 #include "ReportEngineJFree.hxx"
32 #include <comphelper/enumhelper.hxx>
33 #include <comphelper/documentconstants.hxx>
34 #include <comphelper/storagehelper.hxx>
35 #include <connectivity/dbtools.hxx>
36 #include <com/sun/star/beans/PropertyAttribute.hpp>
37 #include <com/sun/star/beans/NamedValue.hpp>
38 #include <comphelper/sequence.hxx>
39 #include <com/sun/star/frame/XComponentLoader.hpp>
40 #include <com/sun/star/frame/FrameSearchFlag.hpp>
41 #include <com/sun/star/embed/XTransactedObject.hpp>
42 #include <com/sun/star/sdb/XCompletedExecution.hpp>
43 #include <com/sun/star/sdb/XSingleSelectQueryAnalyzer.hpp>
44 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
45 #include <com/sun/star/sdb/CommandType.hpp>
47 #include <com/sun/star/task/XInteractionHandler.hpp>
48 #include <com/sun/star/task/XJob.hpp>
49 #ifndef REPORTDESIGN_SHARED_CORESTRINGS_HRC
50 #include "corestrings.hrc"
51 #endif
52 #include <tools/debug.hxx>
53 #include <svtools/useroptions.hxx>
54 #include <unotools/tempfile.hxx>
55 #include <unotools/sharedunocomponent.hxx>
56 #include <comphelper/mimeconfighelper.hxx>
57 #include "Tools.hxx"
58 #include <comphelper/property.hxx>
59 #include <connectivity/CommonTools.hxx>
60 #include <rtl/ustrbuf.hxx>
61 #include <sfx2/docfilt.hxx>
62 // =============================================================================
63 namespace reportdesign
65 // =============================================================================
66 using namespace com::sun::star;
67 using namespace comphelper;
69 DBG_NAME( rpt_OReportEngineJFree )
70 // -----------------------------------------------------------------------------
71 OReportEngineJFree::OReportEngineJFree( const uno::Reference< uno::XComponentContext >& context)
72 :ReportEngineBase(m_aMutex)
73 ,ReportEnginePropertySet(context,static_cast< Implements >(IMPLEMENTS_PROPERTY_SET),uno::Sequence< ::rtl::OUString >())
74 ,m_xContext(context)
76 DBG_CTOR( rpt_OReportEngineJFree,NULL);
78 // -----------------------------------------------------------------------------
79 // TODO: VirtualFunctionFinder: This is virtual function!
80 //
81 OReportEngineJFree::~OReportEngineJFree()
83 DBG_DTOR( rpt_OReportEngineJFree,NULL);
85 //--------------------------------------------------------------------------
86 IMPLEMENT_FORWARD_XINTERFACE2(OReportEngineJFree,ReportEngineBase,ReportEnginePropertySet)
87 // -----------------------------------------------------------------------------
88 void SAL_CALL OReportEngineJFree::dispose() throw(uno::RuntimeException)
90 ReportEnginePropertySet::dispose();
91 cppu::WeakComponentImplHelperBase::dispose();
92 m_xActiveConnection.clear();
94 // -----------------------------------------------------------------------------
95 ::rtl::OUString OReportEngineJFree::getImplementationName_Static( ) throw(uno::RuntimeException)
97 return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.report.OReportEngineJFree"));
100 //--------------------------------------------------------------------------
101 ::rtl::OUString SAL_CALL OReportEngineJFree::getImplementationName( ) throw(uno::RuntimeException)
103 return getImplementationName_Static();
105 //--------------------------------------------------------------------------
106 uno::Sequence< ::rtl::OUString > OReportEngineJFree::getSupportedServiceNames_Static( ) throw(uno::RuntimeException)
108 uno::Sequence< ::rtl::OUString > aServices(1);
109 aServices.getArray()[0] = SERVICE_REPORTENGINE;
111 return aServices;
113 //------------------------------------------------------------------------------
114 uno::Reference< uno::XInterface > OReportEngineJFree::create(uno::Reference< uno::XComponentContext > const & xContext)
116 return *(new OReportEngineJFree(xContext));
119 //--------------------------------------------------------------------------
120 uno::Sequence< ::rtl::OUString > SAL_CALL OReportEngineJFree::getSupportedServiceNames( ) throw(uno::RuntimeException)
122 return getSupportedServiceNames_Static();
124 //------------------------------------------------------------------------------
125 sal_Bool SAL_CALL OReportEngineJFree::supportsService(const ::rtl::OUString& ServiceName) throw( uno::RuntimeException )
127 return ::comphelper::existsValue(ServiceName,getSupportedServiceNames_Static());
129 // -----------------------------------------------------------------------------
130 // XReportEngine
131 // Attributes
132 uno::Reference< report::XReportDefinition > SAL_CALL OReportEngineJFree::getReportDefinition() throw (uno::RuntimeException)
134 ::osl::MutexGuard aGuard(m_aMutex);
135 return m_xReport;
137 // -----------------------------------------------------------------------------
138 void SAL_CALL OReportEngineJFree::setReportDefinition( const uno::Reference< report::XReportDefinition >& _report ) throw (lang::IllegalArgumentException, uno::RuntimeException)
140 if ( !_report.is() )
141 throw lang::IllegalArgumentException();
142 BoundListeners l;
144 ::osl::MutexGuard aGuard(m_aMutex);
145 if ( m_xReport != _report )
147 prepareSet(PROPERTY_REPORTDEFINITION, uno::makeAny(m_xReport), uno::makeAny(_report), &l);
148 m_xReport = _report;
151 l.notify();
153 // -----------------------------------------------------------------------------
154 uno::Reference< task::XStatusIndicator > SAL_CALL OReportEngineJFree::getStatusIndicator() throw (uno::RuntimeException)
156 ::osl::MutexGuard aGuard(m_aMutex);
157 return m_StatusIndicator;
159 // -----------------------------------------------------------------------------
160 void SAL_CALL OReportEngineJFree::setStatusIndicator( const uno::Reference< task::XStatusIndicator >& _statusindicator ) throw (uno::RuntimeException)
162 set(PROPERTY_STATUSINDICATOR,_statusindicator,m_StatusIndicator);
164 // -----------------------------------------------------------------------------
165 ::rtl::OUString OReportEngineJFree::getNewOutputName()
167 ::rtl::OUString sOutputName;
169 ::osl::MutexGuard aGuard(m_aMutex);
170 ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
171 if ( !m_xReport.is() || !m_xActiveConnection.is() )
172 throw lang::IllegalArgumentException();
174 static const ::rtl::OUString s_sMediaType(RTL_CONSTASCII_USTRINGPARAM("MediaType"));
177 const uno::Reference< lang::XMultiServiceFactory > xFactory(m_xContext->getServiceManager(),uno::UNO_QUERY_THROW);
178 MimeConfigurationHelper aConfighelper(xFactory);
179 const ::rtl::OUString sMimeType = m_xReport->getMimeType();
180 const SfxFilter* pFilter = SfxFilter::GetDefaultFilter( aConfighelper.GetDocServiceNameFromMediaType(sMimeType) );
181 String sExt;
182 if ( pFilter )
184 sExt = pFilter->GetDefaultExtension();
185 sExt.EraseLeadingChars( '*' );
187 else
188 sExt = String::CreateFromAscii(".rpt");
190 uno::Reference< embed::XStorage > xTemp = OStorageHelper::GetTemporaryStorage(/*sFileTemp,embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE,*/uno::Reference< lang::XMultiServiceFactory >(m_xContext->getServiceManager(),uno::UNO_QUERY));
191 utl::DisposableComponent aTemp(xTemp);
192 uno::Sequence< beans::PropertyValue > aEmpty;
193 uno::Reference< beans::XPropertySet> xStorageProp(xTemp,uno::UNO_QUERY);
194 if ( xStorageProp.is() )
196 xStorageProp->setPropertyValue( s_sMediaType, uno::makeAny(sMimeType));
198 m_xReport->storeToStorage(xTemp,aEmpty); // store to temp file because it may contain information which aren't in the database yet.
200 uno::Sequence< beans::NamedValue > aConvertedProperties(7);
201 sal_Int32 nPos = 0;
202 aConvertedProperties[nPos].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("InputStorage"));
203 aConvertedProperties[nPos++].Value <<= xTemp;
204 aConvertedProperties[nPos].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OutputStorage"));
207 String sName = m_xReport->getCaption();
208 if ( !sName.Len() )
209 sName = m_xReport->getName();
210 ::utl::TempFile aFile(sName,sal_False,&sExt);
211 uno::Reference< embed::XStorage > xOut = OStorageHelper::GetStorageFromURL(aFile.GetURL(),embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE,uno::Reference< lang::XMultiServiceFactory >(m_xContext->getServiceManager(),uno::UNO_QUERY));
212 utl::DisposableComponent aOut(xOut);
213 xStorageProp.set(xOut,uno::UNO_QUERY);
214 if ( xStorageProp.is() )
216 xStorageProp->setPropertyValue( s_sMediaType, uno::makeAny(sMimeType));
219 aConvertedProperties[nPos++].Value <<= xOut;
221 aConvertedProperties[nPos].Name = PROPERTY_REPORTDEFINITION;
222 aConvertedProperties[nPos++].Value <<= m_xReport;
224 aConvertedProperties[nPos].Name = PROPERTY_ACTIVECONNECTION;
225 aConvertedProperties[nPos++].Value <<= m_xActiveConnection;
227 // some meta data
228 SvtUserOptions aUserOpts;
229 ::rtl::OUStringBuffer sAuthor(aUserOpts.GetFirstName());
230 sAuthor.appendAscii(" ");
231 sAuthor.append(aUserOpts.GetLastName());
232 static const ::rtl::OUString s_sAuthor(RTL_CONSTASCII_USTRINGPARAM("Author"));
233 aConvertedProperties[nPos].Name = s_sAuthor;
234 aConvertedProperties[nPos++].Value <<= sAuthor.makeStringAndClear();
236 static const ::rtl::OUString s_sTitle(RTL_CONSTASCII_USTRINGPARAM("Title"));
237 aConvertedProperties[nPos].Name = s_sTitle;
238 aConvertedProperties[nPos++].Value <<= m_xReport->getCaption();
240 // create job factory and initialize
241 const ::rtl::OUString sReportEngineServiceName = ::dbtools::getDefaultReportEngineServiceName(xFactory);
242 uno::Reference<task::XJob> xJob(m_xContext->getServiceManager()->createInstanceWithContext(sReportEngineServiceName,m_xContext),uno::UNO_QUERY_THROW);
243 if ( m_xReport->getCommand().getLength() )
245 xJob->execute(aConvertedProperties);
246 if ( xStorageProp.is() )
248 //xStorageProp->getPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("URL"))) >>= sOutputName;
249 sOutputName = aFile.GetURL();
253 uno::Reference<embed::XTransactedObject> xTransact(xOut,uno::UNO_QUERY);
254 if ( sOutputName.getLength() && xTransact.is() )
255 xTransact->commit();
257 if ( !sOutputName.getLength() )
258 throw lang::IllegalArgumentException();
260 catch(const uno::Exception& e)
262 (void)e; // helper to know what e contains
263 throw;
266 return sOutputName;
268 // -----------------------------------------------------------------------------
269 // Methods
270 uno::Reference< frame::XModel > SAL_CALL OReportEngineJFree::createDocumentModel( ) throw (lang::DisposedException, lang::IllegalArgumentException, uno::Exception, uno::RuntimeException)
272 return createDocumentAlive(NULL,true);
274 // -----------------------------------------------------------------------------
275 uno::Reference< frame::XModel > SAL_CALL OReportEngineJFree::createDocumentAlive( const uno::Reference< frame::XFrame >& _frame ) throw (lang::DisposedException, lang::IllegalArgumentException, uno::Exception, uno::RuntimeException)
277 return createDocumentAlive(_frame,false);
279 // -----------------------------------------------------------------------------
280 uno::Reference< frame::XModel > SAL_CALL OReportEngineJFree::createDocumentAlive( const uno::Reference< frame::XFrame >& _frame,bool _bHidden ) throw (lang::DisposedException, lang::IllegalArgumentException, uno::Exception, uno::RuntimeException)
282 uno::Reference< frame::XModel > xModel;
283 ::rtl::OUString sOutputName = getNewOutputName(); // starts implicite the report generator
284 if ( sOutputName.getLength() )
286 ::osl::MutexGuard aGuard(m_aMutex);
287 ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
288 uno::Reference<frame::XComponentLoader> xFrameLoad(_frame,uno::UNO_QUERY);
289 if ( !xFrameLoad.is() )
291 // if there is no frame given, find the right
292 xFrameLoad.set( m_xContext->getServiceManager()->createInstanceWithContext(
293 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Desktop"))
294 ,m_xContext)
295 ,uno::UNO_QUERY);
296 ::rtl::OUString sTarget = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("_blank"));
297 sal_Int32 nFrameSearchFlag = frame::FrameSearchFlag::TASKS | frame::FrameSearchFlag::CREATE;
298 uno::Reference< frame::XFrame> xFrame = uno::Reference< frame::XFrame>(xFrameLoad,uno::UNO_QUERY)->findFrame(sTarget,nFrameSearchFlag);
299 xFrameLoad.set( xFrame,uno::UNO_QUERY);
302 if ( xFrameLoad.is() )
304 uno::Sequence < beans::PropertyValue > aArgs( _bHidden ? 3 : 2 );
305 sal_Int32 nLen = 0;
306 aArgs[nLen].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("AsTemplate"));
307 aArgs[nLen++].Value <<= sal_False;
309 aArgs[nLen].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ReadOnly"));
310 aArgs[nLen++].Value <<= sal_True;
312 if ( _bHidden )
314 aArgs[nLen].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Hidden"));
315 aArgs[nLen++].Value <<= sal_True;
318 uno::Reference< lang::XMultiServiceFactory > xFac(m_xContext->getServiceManager(),uno::UNO_QUERY);
319 ::comphelper::MimeConfigurationHelper aHelper(xFac);
320 xModel.set( xFrameLoad->loadComponentFromURL(
321 sOutputName,
322 ::rtl::OUString(), // empty frame name
324 aArgs
325 ),uno::UNO_QUERY);
328 return xModel;
330 // -----------------------------------------------------------------------------
331 util::URL SAL_CALL OReportEngineJFree::createDocument( ) throw (lang::DisposedException, lang::IllegalArgumentException, uno::Exception, uno::RuntimeException)
333 util::URL aRet;
334 uno::Reference< frame::XModel > xModel = createDocumentModel();
335 if ( xModel.is() )
337 ::osl::MutexGuard aGuard(m_aMutex);
338 ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
340 return aRet;
342 // -----------------------------------------------------------------------------
343 void SAL_CALL OReportEngineJFree::interrupt( ) throw (lang::DisposedException, uno::Exception, uno::RuntimeException)
346 ::osl::MutexGuard aGuard(m_aMutex);
347 ::connectivity::checkDisposed(ReportEngineBase::rBHelper.bDisposed);
350 // -----------------------------------------------------------------------------
351 uno::Reference< beans::XPropertySetInfo > SAL_CALL OReportEngineJFree::getPropertySetInfo( ) throw(uno::RuntimeException)
353 return ReportEnginePropertySet::getPropertySetInfo();
355 // -------------------------------------------------------------------------
356 void SAL_CALL OReportEngineJFree::setPropertyValue( const ::rtl::OUString& aPropertyName, const uno::Any& aValue ) throw (beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
358 ReportEnginePropertySet::setPropertyValue( aPropertyName, aValue );
360 // -----------------------------------------------------------------------------
361 uno::Any SAL_CALL OReportEngineJFree::getPropertyValue( const ::rtl::OUString& PropertyName ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
363 return ReportEnginePropertySet::getPropertyValue( PropertyName);
365 // -----------------------------------------------------------------------------
366 void SAL_CALL OReportEngineJFree::addPropertyChangeListener( const ::rtl::OUString& aPropertyName, const uno::Reference< beans::XPropertyChangeListener >& xListener ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
368 ReportEnginePropertySet::addPropertyChangeListener( aPropertyName, xListener );
370 // -----------------------------------------------------------------------------
371 void SAL_CALL OReportEngineJFree::removePropertyChangeListener( const ::rtl::OUString& aPropertyName, const uno::Reference< beans::XPropertyChangeListener >& aListener ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
373 ReportEnginePropertySet::removePropertyChangeListener( aPropertyName, aListener );
375 // -----------------------------------------------------------------------------
376 void SAL_CALL OReportEngineJFree::addVetoableChangeListener( const ::rtl::OUString& PropertyName, const uno::Reference< beans::XVetoableChangeListener >& aListener ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
378 ReportEnginePropertySet::addVetoableChangeListener( PropertyName, aListener );
380 // -----------------------------------------------------------------------------
381 void SAL_CALL OReportEngineJFree::removeVetoableChangeListener( const ::rtl::OUString& PropertyName, const uno::Reference< beans::XVetoableChangeListener >& aListener ) throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
383 ReportEnginePropertySet::removeVetoableChangeListener( PropertyName, aListener );
385 // -----------------------------------------------------------------------------
386 uno::Reference< sdbc::XConnection > SAL_CALL OReportEngineJFree::getActiveConnection() throw (uno::RuntimeException)
388 return m_xActiveConnection;
390 // -----------------------------------------------------------------------------
391 void SAL_CALL OReportEngineJFree::setActiveConnection( const uno::Reference< sdbc::XConnection >& _activeconnection ) throw (lang::IllegalArgumentException, uno::RuntimeException)
393 if ( !_activeconnection.is() )
394 throw lang::IllegalArgumentException();
395 set(PROPERTY_ACTIVECONNECTION,_activeconnection,m_xActiveConnection);
397 // =============================================================================
398 } // namespace reportdesign
399 // =============================================================================