1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ReportEngineJFree.cxx,v $
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 <comphelper/sequence.hxx>
37 #include <comphelper/mimeconfighelper.hxx>
38 #include <comphelper/property.hxx>
39 #include <com/sun/star/beans/PropertyAttribute.hpp>
40 #include <com/sun/star/beans/NamedValue.hpp>
41 #include <com/sun/star/frame/XComponentLoader.hpp>
42 #include <com/sun/star/frame/FrameSearchFlag.hpp>
43 #include <com/sun/star/embed/XTransactedObject.hpp>
44 #include <com/sun/star/sdb/XCompletedExecution.hpp>
45 #include <com/sun/star/sdb/XSingleSelectQueryAnalyzer.hpp>
46 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
47 #include <com/sun/star/sdb/CommandType.hpp>
49 #include <com/sun/star/task/XInteractionHandler.hpp>
50 #include <com/sun/star/task/XJob.hpp>
52 #include <tools/debug.hxx>
53 #include <tools/urlobj.hxx>
54 #include <svtools/useroptions.hxx>
55 #include <unotools/tempfile.hxx>
56 #include <unotools/sharedunocomponent.hxx>
59 #include "corestrings.hrc"
60 #include "core_resource.hrc"
61 #include "core_resource.hxx"
63 #include <connectivity/CommonTools.hxx>
64 #include <rtl/ustrbuf.hxx>
65 #include <sfx2/docfilt.hxx>
66 // =============================================================================
67 namespace reportdesign
69 // =============================================================================
70 using namespace com::sun::star
;
71 using namespace comphelper
;
73 DBG_NAME( rpt_OReportEngineJFree
)
74 // -----------------------------------------------------------------------------
75 OReportEngineJFree::OReportEngineJFree( const uno::Reference
< uno::XComponentContext
>& context
)
76 :ReportEngineBase(m_aMutex
)
77 ,ReportEnginePropertySet(context
,static_cast< Implements
>(IMPLEMENTS_PROPERTY_SET
),uno::Sequence
< ::rtl::OUString
>())
81 DBG_CTOR( rpt_OReportEngineJFree
,NULL
);
83 // -----------------------------------------------------------------------------
84 // TODO: VirtualFunctionFinder: This is virtual function!
86 OReportEngineJFree::~OReportEngineJFree()
88 DBG_DTOR( rpt_OReportEngineJFree
,NULL
);
90 //--------------------------------------------------------------------------
91 IMPLEMENT_FORWARD_XINTERFACE2(OReportEngineJFree
,ReportEngineBase
,ReportEnginePropertySet
)
92 // -----------------------------------------------------------------------------
93 void SAL_CALL
OReportEngineJFree::dispose() throw(uno::RuntimeException
)
95 ReportEnginePropertySet::dispose();
96 cppu::WeakComponentImplHelperBase::dispose();
97 m_xActiveConnection
.clear();
99 // -----------------------------------------------------------------------------
100 ::rtl::OUString
OReportEngineJFree::getImplementationName_Static( ) throw(uno::RuntimeException
)
102 return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.report.OReportEngineJFree"));
105 //--------------------------------------------------------------------------
106 ::rtl::OUString SAL_CALL
OReportEngineJFree::getImplementationName( ) throw(uno::RuntimeException
)
108 return getImplementationName_Static();
110 //--------------------------------------------------------------------------
111 uno::Sequence
< ::rtl::OUString
> OReportEngineJFree::getSupportedServiceNames_Static( ) throw(uno::RuntimeException
)
113 uno::Sequence
< ::rtl::OUString
> aServices(1);
114 aServices
.getArray()[0] = SERVICE_REPORTENGINE
;
118 //------------------------------------------------------------------------------
119 uno::Reference
< uno::XInterface
> OReportEngineJFree::create(uno::Reference
< uno::XComponentContext
> const & xContext
)
121 return *(new OReportEngineJFree(xContext
));
124 //--------------------------------------------------------------------------
125 uno::Sequence
< ::rtl::OUString
> SAL_CALL
OReportEngineJFree::getSupportedServiceNames( ) throw(uno::RuntimeException
)
127 return getSupportedServiceNames_Static();
129 //------------------------------------------------------------------------------
130 sal_Bool SAL_CALL
OReportEngineJFree::supportsService(const ::rtl::OUString
& ServiceName
) throw( uno::RuntimeException
)
132 return ::comphelper::existsValue(ServiceName
,getSupportedServiceNames_Static());
134 // -----------------------------------------------------------------------------
137 uno::Reference
< report::XReportDefinition
> SAL_CALL
OReportEngineJFree::getReportDefinition() throw (uno::RuntimeException
)
139 ::osl::MutexGuard
aGuard(m_aMutex
);
142 // -----------------------------------------------------------------------------
143 void SAL_CALL
OReportEngineJFree::setReportDefinition( const uno::Reference
< report::XReportDefinition
>& _report
) throw (lang::IllegalArgumentException
, uno::RuntimeException
)
146 throw lang::IllegalArgumentException();
149 ::osl::MutexGuard
aGuard(m_aMutex
);
150 if ( m_xReport
!= _report
)
152 prepareSet(PROPERTY_REPORTDEFINITION
, uno::makeAny(m_xReport
), uno::makeAny(_report
), &l
);
158 // -----------------------------------------------------------------------------
159 uno::Reference
< task::XStatusIndicator
> SAL_CALL
OReportEngineJFree::getStatusIndicator() throw (uno::RuntimeException
)
161 ::osl::MutexGuard
aGuard(m_aMutex
);
162 return m_StatusIndicator
;
164 // -----------------------------------------------------------------------------
165 void SAL_CALL
OReportEngineJFree::setStatusIndicator( const uno::Reference
< task::XStatusIndicator
>& _statusindicator
) throw (uno::RuntimeException
)
167 set(PROPERTY_STATUSINDICATOR
,_statusindicator
,m_StatusIndicator
);
169 // -----------------------------------------------------------------------------
170 ::rtl::OUString
OReportEngineJFree::getNewOutputName()
172 ::rtl::OUString sOutputName
;
174 ::osl::MutexGuard
aGuard(m_aMutex
);
175 ::connectivity::checkDisposed(ReportEngineBase::rBHelper
.bDisposed
);
176 if ( !m_xReport
.is() || !m_xActiveConnection
.is() )
177 throw lang::IllegalArgumentException();
179 static const ::rtl::OUString
s_sMediaType(RTL_CONSTASCII_USTRINGPARAM("MediaType"));
182 const uno::Reference
< lang::XMultiServiceFactory
> xFactory(m_xContext
->getServiceManager(),uno::UNO_QUERY_THROW
);
183 MimeConfigurationHelper
aConfighelper(xFactory
);
184 const ::rtl::OUString sMimeType
= m_xReport
->getMimeType();
185 const SfxFilter
* pFilter
= SfxFilter::GetDefaultFilter( aConfighelper
.GetDocServiceNameFromMediaType(sMimeType
) );
189 sExt
= pFilter
->GetDefaultExtension();
190 sExt
.EraseLeadingChars( '*' );
193 sExt
= String::CreateFromAscii(".rpt");
195 uno::Reference
< embed::XStorage
> xTemp
= OStorageHelper::GetTemporaryStorage(/*sFileTemp,embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE,*/uno::Reference
< lang::XMultiServiceFactory
>(m_xContext
->getServiceManager(),uno::UNO_QUERY
));
196 utl::DisposableComponent
aTemp(xTemp
);
197 uno::Sequence
< beans::PropertyValue
> aEmpty
;
198 uno::Reference
< beans::XPropertySet
> xStorageProp(xTemp
,uno::UNO_QUERY
);
199 if ( xStorageProp
.is() )
201 xStorageProp
->setPropertyValue( s_sMediaType
, uno::makeAny(sMimeType
));
203 m_xReport
->storeToStorage(xTemp
,aEmpty
); // store to temp file because it may contain information which aren't in the database yet.
205 uno::Sequence
< beans::NamedValue
> aConvertedProperties(8);
207 aConvertedProperties
[nPos
].Name
= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("InputStorage"));
208 aConvertedProperties
[nPos
++].Value
<<= xTemp
;
209 aConvertedProperties
[nPos
].Name
= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OutputStorage"));
211 ::rtl::OUString sFileURL
;
212 String sName
= m_xReport
->getCaption();
214 sName
= m_xReport
->getName();
216 ::utl::TempFile
aTestFile(sName
,sal_False
,&sExt
);
217 if ( !aTestFile
.IsValid() )
219 sName
= RPT_RESSTRING(RID_STR_REPORT
,m_xContext
->getServiceManager());
220 ::utl::TempFile
aFile(sName
,sal_False
,&sExt
);
221 sFileURL
= aFile
.GetURL();
224 sFileURL
= aTestFile
.GetURL();
227 uno::Reference
< embed::XStorage
> xOut
= OStorageHelper::GetStorageFromURL(sFileURL
,embed::ElementModes::WRITE
| embed::ElementModes::TRUNCATE
,uno::Reference
< lang::XMultiServiceFactory
>(m_xContext
->getServiceManager(),uno::UNO_QUERY
));
228 utl::DisposableComponent
aOut(xOut
);
229 xStorageProp
.set(xOut
,uno::UNO_QUERY
);
230 if ( xStorageProp
.is() )
232 xStorageProp
->setPropertyValue( s_sMediaType
, uno::makeAny(sMimeType
));
235 aConvertedProperties
[nPos
++].Value
<<= xOut
;
237 aConvertedProperties
[nPos
].Name
= PROPERTY_REPORTDEFINITION
;
238 aConvertedProperties
[nPos
++].Value
<<= m_xReport
;
240 aConvertedProperties
[nPos
].Name
= PROPERTY_ACTIVECONNECTION
;
241 aConvertedProperties
[nPos
++].Value
<<= m_xActiveConnection
;
243 aConvertedProperties
[nPos
].Name
= PROPERTY_MAXROWS
;
244 aConvertedProperties
[nPos
++].Value
<<= m_nMaxRows
;
247 SvtUserOptions aUserOpts
;
248 ::rtl::OUStringBuffer
sAuthor(aUserOpts
.GetFirstName());
249 sAuthor
.appendAscii(" ");
250 sAuthor
.append(aUserOpts
.GetLastName());
251 static const ::rtl::OUString
s_sAuthor(RTL_CONSTASCII_USTRINGPARAM("Author"));
252 aConvertedProperties
[nPos
].Name
= s_sAuthor
;
253 aConvertedProperties
[nPos
++].Value
<<= sAuthor
.makeStringAndClear();
255 static const ::rtl::OUString
s_sTitle(RTL_CONSTASCII_USTRINGPARAM("Title"));
256 aConvertedProperties
[nPos
].Name
= s_sTitle
;
257 aConvertedProperties
[nPos
++].Value
<<= m_xReport
->getCaption();
259 // create job factory and initialize
260 const ::rtl::OUString sReportEngineServiceName
= ::dbtools::getDefaultReportEngineServiceName(xFactory
);
261 uno::Reference
<task::XJob
> xJob(m_xContext
->getServiceManager()->createInstanceWithContext(sReportEngineServiceName
,m_xContext
),uno::UNO_QUERY_THROW
);
262 if ( m_xReport
->getCommand().getLength() )
264 xJob
->execute(aConvertedProperties
);
265 if ( xStorageProp
.is() )
267 //xStorageProp->getPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("URL"))) >>= sOutputName;
268 sOutputName
= sFileURL
;
272 uno::Reference
<embed::XTransactedObject
> xTransact(xOut
,uno::UNO_QUERY
);
273 if ( sOutputName
.getLength() && xTransact
.is() )
276 if ( !sOutputName
.getLength() )
277 throw lang::IllegalArgumentException();
279 catch(const uno::Exception
& e
)
281 (void)e
; // helper to know what e contains
287 // -----------------------------------------------------------------------------
289 uno::Reference
< frame::XModel
> SAL_CALL
OReportEngineJFree::createDocumentModel( ) throw (lang::DisposedException
, lang::IllegalArgumentException
, uno::Exception
, uno::RuntimeException
)
291 return createDocumentAlive(NULL
,true);
293 // -----------------------------------------------------------------------------
294 uno::Reference
< frame::XModel
> SAL_CALL
OReportEngineJFree::createDocumentAlive( const uno::Reference
< frame::XFrame
>& _frame
) throw (lang::DisposedException
, lang::IllegalArgumentException
, uno::Exception
, uno::RuntimeException
)
296 return createDocumentAlive(_frame
,false);
298 // -----------------------------------------------------------------------------
299 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
)
301 uno::Reference
< frame::XModel
> xModel
;
302 ::rtl::OUString sOutputName
= getNewOutputName(); // starts implicite the report generator
303 if ( sOutputName
.getLength() )
305 ::osl::MutexGuard
aGuard(m_aMutex
);
306 ::connectivity::checkDisposed(ReportEngineBase::rBHelper
.bDisposed
);
307 uno::Reference
<frame::XComponentLoader
> xFrameLoad(_frame
,uno::UNO_QUERY
);
308 if ( !xFrameLoad
.is() )
310 // if there is no frame given, find the right
311 xFrameLoad
.set( m_xContext
->getServiceManager()->createInstanceWithContext(
312 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Desktop"))
315 ::rtl::OUString
sTarget(RTL_CONSTASCII_USTRINGPARAM("_blank"));
316 sal_Int32 nFrameSearchFlag
= frame::FrameSearchFlag::TASKS
| frame::FrameSearchFlag::CREATE
;
317 uno::Reference
< frame::XFrame
> xFrame
= uno::Reference
< frame::XFrame
>(xFrameLoad
,uno::UNO_QUERY
)->findFrame(sTarget
,nFrameSearchFlag
);
318 xFrameLoad
.set( xFrame
,uno::UNO_QUERY
);
321 if ( xFrameLoad
.is() )
323 uno::Sequence
< beans::PropertyValue
> aArgs( _bHidden
? 3 : 2 );
325 aArgs
[nLen
].Name
= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("AsTemplate"));
326 aArgs
[nLen
++].Value
<<= sal_False
;
328 aArgs
[nLen
].Name
= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ReadOnly"));
329 aArgs
[nLen
++].Value
<<= sal_True
;
333 aArgs
[nLen
].Name
= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Hidden"));
334 aArgs
[nLen
++].Value
<<= sal_True
;
337 uno::Reference
< lang::XMultiServiceFactory
> xFac(m_xContext
->getServiceManager(),uno::UNO_QUERY
);
338 /*::comphelper::MimeConfigurationHelper aHelper(xFac);*/
339 xModel
.set( xFrameLoad
->loadComponentFromURL(
341 ::rtl::OUString(), // empty frame name
349 // -----------------------------------------------------------------------------
350 util::URL SAL_CALL
OReportEngineJFree::createDocument( ) throw (lang::DisposedException
, lang::IllegalArgumentException
, uno::Exception
, uno::RuntimeException
)
353 uno::Reference
< frame::XModel
> xModel
= createDocumentModel();
356 ::osl::MutexGuard
aGuard(m_aMutex
);
357 ::connectivity::checkDisposed(ReportEngineBase::rBHelper
.bDisposed
);
361 // -----------------------------------------------------------------------------
362 void SAL_CALL
OReportEngineJFree::interrupt( ) throw (lang::DisposedException
, uno::Exception
, uno::RuntimeException
)
365 ::osl::MutexGuard
aGuard(m_aMutex
);
366 ::connectivity::checkDisposed(ReportEngineBase::rBHelper
.bDisposed
);
369 // -----------------------------------------------------------------------------
370 uno::Reference
< beans::XPropertySetInfo
> SAL_CALL
OReportEngineJFree::getPropertySetInfo( ) throw(uno::RuntimeException
)
372 return ReportEnginePropertySet::getPropertySetInfo();
374 // -------------------------------------------------------------------------
375 void SAL_CALL
OReportEngineJFree::setPropertyValue( const ::rtl::OUString
& aPropertyName
, const uno::Any
& aValue
) throw (beans::UnknownPropertyException
, beans::PropertyVetoException
, lang::IllegalArgumentException
, lang::WrappedTargetException
, uno::RuntimeException
)
377 ReportEnginePropertySet::setPropertyValue( aPropertyName
, aValue
);
379 // -----------------------------------------------------------------------------
380 uno::Any SAL_CALL
OReportEngineJFree::getPropertyValue( const ::rtl::OUString
& PropertyName
) throw (beans::UnknownPropertyException
, lang::WrappedTargetException
, uno::RuntimeException
)
382 return ReportEnginePropertySet::getPropertyValue( PropertyName
);
384 // -----------------------------------------------------------------------------
385 void SAL_CALL
OReportEngineJFree::addPropertyChangeListener( const ::rtl::OUString
& aPropertyName
, const uno::Reference
< beans::XPropertyChangeListener
>& xListener
) throw (beans::UnknownPropertyException
, lang::WrappedTargetException
, uno::RuntimeException
)
387 ReportEnginePropertySet::addPropertyChangeListener( aPropertyName
, xListener
);
389 // -----------------------------------------------------------------------------
390 void SAL_CALL
OReportEngineJFree::removePropertyChangeListener( const ::rtl::OUString
& aPropertyName
, const uno::Reference
< beans::XPropertyChangeListener
>& aListener
) throw (beans::UnknownPropertyException
, lang::WrappedTargetException
, uno::RuntimeException
)
392 ReportEnginePropertySet::removePropertyChangeListener( aPropertyName
, aListener
);
394 // -----------------------------------------------------------------------------
395 void SAL_CALL
OReportEngineJFree::addVetoableChangeListener( const ::rtl::OUString
& PropertyName
, const uno::Reference
< beans::XVetoableChangeListener
>& aListener
) throw (beans::UnknownPropertyException
, lang::WrappedTargetException
, uno::RuntimeException
)
397 ReportEnginePropertySet::addVetoableChangeListener( PropertyName
, aListener
);
399 // -----------------------------------------------------------------------------
400 void SAL_CALL
OReportEngineJFree::removeVetoableChangeListener( const ::rtl::OUString
& PropertyName
, const uno::Reference
< beans::XVetoableChangeListener
>& aListener
) throw (beans::UnknownPropertyException
, lang::WrappedTargetException
, uno::RuntimeException
)
402 ReportEnginePropertySet::removeVetoableChangeListener( PropertyName
, aListener
);
404 // -----------------------------------------------------------------------------
405 uno::Reference
< sdbc::XConnection
> SAL_CALL
OReportEngineJFree::getActiveConnection() throw (uno::RuntimeException
)
407 return m_xActiveConnection
;
409 // -----------------------------------------------------------------------------
410 void SAL_CALL
OReportEngineJFree::setActiveConnection( const uno::Reference
< sdbc::XConnection
>& _activeconnection
) throw (lang::IllegalArgumentException
, uno::RuntimeException
)
412 if ( !_activeconnection
.is() )
413 throw lang::IllegalArgumentException();
414 set(PROPERTY_ACTIVECONNECTION
,_activeconnection
,m_xActiveConnection
);
416 // -----------------------------------------------------------------------------
417 ::sal_Int32 SAL_CALL
OReportEngineJFree::getMaxRows() throw (uno::RuntimeException
)
419 ::osl::MutexGuard
aGuard(m_aMutex
);
422 // -----------------------------------------------------------------------------
423 void SAL_CALL
OReportEngineJFree::setMaxRows( ::sal_Int32 _MaxRows
) throw (uno::RuntimeException
)
425 set(PROPERTY_MAXROWS
,_MaxRows
,m_nMaxRows
);
427 // =============================================================================
428 } // namespace reportdesign
429 // =============================================================================