1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include <com/sun/star/beans/PropertyValue.hpp>
20 #include "ReportEngineJFree.hxx"
21 #include <comphelper/enumhelper.hxx>
22 #include <comphelper/documentconstants.hxx>
23 #include <comphelper/storagehelper.hxx>
24 #include <connectivity/dbtools.hxx>
25 #include <comphelper/mimeconfighelper.hxx>
26 #include <comphelper/property.hxx>
27 #include <comphelper/string.hxx>
28 #include <cppuhelper/supportsservice.hxx>
29 #include <com/sun/star/beans/PropertyAttribute.hpp>
30 #include <com/sun/star/beans/NamedValue.hpp>
31 #include <com/sun/star/frame/Desktop.hpp>
32 #include <com/sun/star/frame/XComponentLoader.hpp>
33 #include <com/sun/star/frame/FrameSearchFlag.hpp>
34 #include <com/sun/star/embed/XTransactedObject.hpp>
35 #include <com/sun/star/sdb/XCompletedExecution.hpp>
36 #include <com/sun/star/sdb/XSingleSelectQueryAnalyzer.hpp>
37 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
38 #include <com/sun/star/sdb/CommandType.hpp>
40 #include <com/sun/star/task/XInteractionHandler.hpp>
41 #include <com/sun/star/task/XJob.hpp>
43 #include <tools/debug.hxx>
44 #include <unotools/useroptions.hxx>
45 #include <unotools/tempfile.hxx>
46 #include <unotools/sharedunocomponent.hxx>
49 #include "corestrings.hrc"
50 #include "core_resource.hrc"
51 #include "core_resource.hxx"
53 #include <connectivity/CommonTools.hxx>
54 #include <rtl/ustrbuf.hxx>
55 #include <sfx2/docfilt.hxx>
57 namespace reportdesign
60 using namespace com::sun::star
;
61 using namespace comphelper
;
64 OReportEngineJFree::OReportEngineJFree( const uno::Reference
< uno::XComponentContext
>& context
)
65 :ReportEngineBase(m_aMutex
)
66 ,ReportEnginePropertySet(context
,static_cast< Implements
>(IMPLEMENTS_PROPERTY_SET
),uno::Sequence
< OUString
>())
72 // TODO: VirtualFunctionFinder: This is virtual function!
74 OReportEngineJFree::~OReportEngineJFree()
78 IMPLEMENT_FORWARD_XINTERFACE2(OReportEngineJFree
,ReportEngineBase
,ReportEnginePropertySet
)
80 void SAL_CALL
OReportEngineJFree::dispose() throw(uno::RuntimeException
, std::exception
)
82 ReportEnginePropertySet::dispose();
83 cppu::WeakComponentImplHelperBase::dispose();
84 m_xActiveConnection
.clear();
87 OUString
OReportEngineJFree::getImplementationName_Static( ) throw(uno::RuntimeException
)
89 return OUString("com.sun.star.comp.report.OReportEngineJFree");
93 OUString SAL_CALL
OReportEngineJFree::getImplementationName( ) throw(uno::RuntimeException
, std::exception
)
95 return getImplementationName_Static();
98 uno::Sequence
< OUString
> OReportEngineJFree::getSupportedServiceNames_Static( ) throw(uno::RuntimeException
)
100 uno::Sequence
< OUString
> aServices(1);
101 aServices
[0] = "com.sun.star.report.ReportEngine";
106 uno::Reference
< uno::XInterface
> OReportEngineJFree::create(uno::Reference
< uno::XComponentContext
> const & xContext
)
108 return *(new OReportEngineJFree(xContext
));
112 uno::Sequence
< OUString
> SAL_CALL
OReportEngineJFree::getSupportedServiceNames( ) throw(uno::RuntimeException
, std::exception
)
114 return getSupportedServiceNames_Static();
117 sal_Bool SAL_CALL
OReportEngineJFree::supportsService(const OUString
& ServiceName
) throw( uno::RuntimeException
, std::exception
)
119 return cppu::supportsService(this, ServiceName
);
124 uno::Reference
< report::XReportDefinition
> SAL_CALL
OReportEngineJFree::getReportDefinition() throw (uno::RuntimeException
, std::exception
)
126 ::osl::MutexGuard
aGuard(m_aMutex
);
130 void SAL_CALL
OReportEngineJFree::setReportDefinition( const uno::Reference
< report::XReportDefinition
>& _report
) throw (lang::IllegalArgumentException
, uno::RuntimeException
, std::exception
)
133 throw lang::IllegalArgumentException();
136 ::osl::MutexGuard
aGuard(m_aMutex
);
137 if ( m_xReport
!= _report
)
139 prepareSet(PROPERTY_REPORTDEFINITION
, uno::makeAny(m_xReport
), uno::makeAny(_report
), &l
);
146 uno::Reference
< task::XStatusIndicator
> SAL_CALL
OReportEngineJFree::getStatusIndicator() throw (uno::RuntimeException
, std::exception
)
148 ::osl::MutexGuard
aGuard(m_aMutex
);
149 return m_StatusIndicator
;
152 void SAL_CALL
OReportEngineJFree::setStatusIndicator( const uno::Reference
< task::XStatusIndicator
>& _statusindicator
) throw (uno::RuntimeException
, std::exception
)
154 set(PROPERTY_STATUSINDICATOR
,_statusindicator
,m_StatusIndicator
);
157 OUString
OReportEngineJFree::getNewOutputName()
159 OUString sOutputName
;
161 ::osl::MutexGuard
aGuard(m_aMutex
);
162 ::connectivity::checkDisposed(ReportEngineBase::rBHelper
.bDisposed
);
163 if ( !m_xReport
.is() || !m_xActiveConnection
.is() )
164 throw lang::IllegalArgumentException();
166 static const char s_sMediaType
[] = "MediaType";
169 MimeConfigurationHelper
aConfighelper(m_xContext
);
170 const OUString sMimeType
= m_xReport
->getMimeType();
171 const SfxFilter
* pFilter
= SfxFilter::GetDefaultFilter( aConfighelper
.GetDocServiceNameFromMediaType(sMimeType
) );
172 OUString
sExt(".rpt");
174 sExt
= ::comphelper::string::stripStart(pFilter
->GetDefaultExtension(), '*');
176 uno::Reference
< embed::XStorage
> xTemp
= OStorageHelper::GetTemporaryStorage(/*sFileTemp,embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE,*/ m_xContext
);
177 utl::DisposableComponent
aTemp(xTemp
);
178 uno::Sequence
< beans::PropertyValue
> aEmpty
;
179 uno::Reference
< beans::XPropertySet
> xStorageProp(xTemp
,uno::UNO_QUERY
);
180 if ( xStorageProp
.is() )
182 xStorageProp
->setPropertyValue( s_sMediaType
, uno::makeAny(sMimeType
));
184 m_xReport
->storeToStorage(xTemp
,aEmpty
); // store to temp file because it may contain information which isn't in the database yet.
186 uno::Sequence
< beans::NamedValue
> aConvertedProperties(8);
188 aConvertedProperties
[nPos
].Name
= "InputStorage";
189 aConvertedProperties
[nPos
++].Value
<<= xTemp
;
190 aConvertedProperties
[nPos
].Name
= "OutputStorage";
193 OUString sName
= m_xReport
->getCaption();
194 if ( sName
.isEmpty() )
195 sName
= m_xReport
->getName();
197 ::utl::TempFile
aTestFile(sName
, false, &sExt
);
198 if ( !aTestFile
.IsValid() )
200 sName
= RPT_RESSTRING(RID_STR_REPORT
,m_xContext
->getServiceManager());
201 ::utl::TempFile
aFile(sName
, false, &sExt
);
202 sFileURL
= aFile
.GetURL();
205 sFileURL
= aTestFile
.GetURL();
208 uno::Reference
< embed::XStorage
> xOut
= OStorageHelper::GetStorageFromURL(sFileURL
,embed::ElementModes::WRITE
| embed::ElementModes::TRUNCATE
, m_xContext
);
209 utl::DisposableComponent
aOut(xOut
);
210 xStorageProp
.set(xOut
,uno::UNO_QUERY
);
211 if ( xStorageProp
.is() )
213 xStorageProp
->setPropertyValue( s_sMediaType
, uno::makeAny(sMimeType
));
216 aConvertedProperties
[nPos
++].Value
<<= xOut
;
218 aConvertedProperties
[nPos
].Name
= PROPERTY_REPORTDEFINITION
;
219 aConvertedProperties
[nPos
++].Value
<<= m_xReport
;
221 aConvertedProperties
[nPos
].Name
= PROPERTY_ACTIVECONNECTION
;
222 aConvertedProperties
[nPos
++].Value
<<= m_xActiveConnection
;
224 aConvertedProperties
[nPos
].Name
= PROPERTY_MAXROWS
;
225 aConvertedProperties
[nPos
++].Value
<<= m_nMaxRows
;
228 SvtUserOptions aUserOpts
;
229 OUStringBuffer
sAuthor(aUserOpts
.GetFirstName());
230 sAuthor
.appendAscii(" ");
231 sAuthor
.append(aUserOpts
.GetLastName());
232 static const char s_sAuthor
[] = "Author";
233 aConvertedProperties
[nPos
].Name
= s_sAuthor
;
234 aConvertedProperties
[nPos
++].Value
<<= sAuthor
.makeStringAndClear();
236 static const char s_sTitle
[] = "Title";
237 aConvertedProperties
[nPos
].Name
= s_sTitle
;
238 aConvertedProperties
[nPos
++].Value
<<= m_xReport
->getCaption();
240 // create job factory and initialize
241 const OUString sReportEngineServiceName
= ::dbtools::getDefaultReportEngineServiceName(m_xContext
);
242 uno::Reference
<task::XJob
> xJob(m_xContext
->getServiceManager()->createInstanceWithContext(sReportEngineServiceName
,m_xContext
),uno::UNO_QUERY_THROW
);
243 if ( !m_xReport
->getCommand().isEmpty() )
245 xJob
->execute(aConvertedProperties
);
246 if ( xStorageProp
.is() )
248 sOutputName
= sFileURL
;
252 uno::Reference
<embed::XTransactedObject
> xTransact(xOut
,uno::UNO_QUERY
);
253 if ( !sOutputName
.isEmpty() && xTransact
.is() )
256 if ( sOutputName
.isEmpty() )
257 throw lang::IllegalArgumentException();
259 catch(const uno::Exception
& e
)
261 (void)e
; // helper to know what e contains
269 uno::Reference
< frame::XModel
> SAL_CALL
OReportEngineJFree::createDocumentModel( ) throw (lang::DisposedException
, lang::IllegalArgumentException
, uno::Exception
, uno::RuntimeException
, std::exception
)
271 return createDocumentAlive(NULL
,true);
274 uno::Reference
< frame::XModel
> SAL_CALL
OReportEngineJFree::createDocumentAlive( const uno::Reference
< frame::XFrame
>& _frame
) throw (lang::DisposedException
, lang::IllegalArgumentException
, uno::Exception
, uno::RuntimeException
, std::exception
)
276 return createDocumentAlive(_frame
,false);
279 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
)
281 uno::Reference
< frame::XModel
> xModel
;
282 OUString sOutputName
= getNewOutputName(); // starts implicite the report generator
283 if ( !sOutputName
.isEmpty() )
285 ::osl::MutexGuard
aGuard(m_aMutex
);
286 ::connectivity::checkDisposed(ReportEngineBase::rBHelper
.bDisposed
);
287 uno::Reference
<frame::XComponentLoader
> xFrameLoad(_frame
,uno::UNO_QUERY
);
288 if ( !xFrameLoad
.is() )
290 // if there is no frame given, find the right
291 xFrameLoad
.set( frame::Desktop::create(m_xContext
), uno::UNO_QUERY
);
292 OUString
sTarget("_blank");
293 sal_Int32 nFrameSearchFlag
= frame::FrameSearchFlag::TASKS
| frame::FrameSearchFlag::CREATE
;
294 uno::Reference
< frame::XFrame
> xFrame
= uno::Reference
< frame::XFrame
>(xFrameLoad
,uno::UNO_QUERY
)->findFrame(sTarget
,nFrameSearchFlag
);
295 xFrameLoad
.set( xFrame
,uno::UNO_QUERY
);
298 if ( xFrameLoad
.is() )
300 uno::Sequence
< beans::PropertyValue
> aArgs( _bHidden
? 3 : 2 );
302 aArgs
[nLen
].Name
= "AsTemplate";
303 aArgs
[nLen
++].Value
<<= sal_False
;
305 aArgs
[nLen
].Name
= "ReadOnly";
306 aArgs
[nLen
++].Value
<<= sal_True
;
310 aArgs
[nLen
].Name
= "Hidden";
311 aArgs
[nLen
++].Value
<<= sal_True
;
314 uno::Reference
< lang::XMultiServiceFactory
> xFac(m_xContext
->getServiceManager(),uno::UNO_QUERY
);
315 xModel
.set( xFrameLoad
->loadComponentFromURL(
317 OUString(), // empty frame name
326 util::URL SAL_CALL
OReportEngineJFree::createDocument( ) throw (lang::DisposedException
, lang::IllegalArgumentException
, uno::Exception
, uno::RuntimeException
, std::exception
)
329 uno::Reference
< frame::XModel
> xModel
= createDocumentModel();
332 ::osl::MutexGuard
aGuard(m_aMutex
);
333 ::connectivity::checkDisposed(ReportEngineBase::rBHelper
.bDisposed
);
338 void SAL_CALL
OReportEngineJFree::interrupt( ) throw (lang::DisposedException
, uno::Exception
, uno::RuntimeException
, std::exception
)
341 ::osl::MutexGuard
aGuard(m_aMutex
);
342 ::connectivity::checkDisposed(ReportEngineBase::rBHelper
.bDisposed
);
346 uno::Reference
< beans::XPropertySetInfo
> SAL_CALL
OReportEngineJFree::getPropertySetInfo( ) throw(uno::RuntimeException
, std::exception
)
348 return ReportEnginePropertySet::getPropertySetInfo();
351 void SAL_CALL
OReportEngineJFree::setPropertyValue( const OUString
& aPropertyName
, const uno::Any
& aValue
) throw (beans::UnknownPropertyException
, beans::PropertyVetoException
, lang::IllegalArgumentException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
)
353 ReportEnginePropertySet::setPropertyValue( aPropertyName
, aValue
);
356 uno::Any SAL_CALL
OReportEngineJFree::getPropertyValue( const OUString
& PropertyName
) throw (beans::UnknownPropertyException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
)
358 return ReportEnginePropertySet::getPropertyValue( PropertyName
);
361 void SAL_CALL
OReportEngineJFree::addPropertyChangeListener( const OUString
& aPropertyName
, const uno::Reference
< beans::XPropertyChangeListener
>& xListener
) throw (beans::UnknownPropertyException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
)
363 ReportEnginePropertySet::addPropertyChangeListener( aPropertyName
, xListener
);
366 void SAL_CALL
OReportEngineJFree::removePropertyChangeListener( const OUString
& aPropertyName
, const uno::Reference
< beans::XPropertyChangeListener
>& aListener
) throw (beans::UnknownPropertyException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
)
368 ReportEnginePropertySet::removePropertyChangeListener( aPropertyName
, aListener
);
371 void SAL_CALL
OReportEngineJFree::addVetoableChangeListener( const OUString
& PropertyName
, const uno::Reference
< beans::XVetoableChangeListener
>& aListener
) throw (beans::UnknownPropertyException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
)
373 ReportEnginePropertySet::addVetoableChangeListener( PropertyName
, aListener
);
376 void SAL_CALL
OReportEngineJFree::removeVetoableChangeListener( const OUString
& PropertyName
, const uno::Reference
< beans::XVetoableChangeListener
>& aListener
) throw (beans::UnknownPropertyException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
)
378 ReportEnginePropertySet::removeVetoableChangeListener( PropertyName
, aListener
);
381 uno::Reference
< sdbc::XConnection
> SAL_CALL
OReportEngineJFree::getActiveConnection() throw (uno::RuntimeException
, std::exception
)
383 return m_xActiveConnection
;
386 void SAL_CALL
OReportEngineJFree::setActiveConnection( const uno::Reference
< sdbc::XConnection
>& _activeconnection
) throw (lang::IllegalArgumentException
, uno::RuntimeException
, std::exception
)
388 if ( !_activeconnection
.is() )
389 throw lang::IllegalArgumentException();
390 set(PROPERTY_ACTIVECONNECTION
,_activeconnection
,m_xActiveConnection
);
393 ::sal_Int32 SAL_CALL
OReportEngineJFree::getMaxRows() throw (uno::RuntimeException
, std::exception
)
395 ::osl::MutexGuard
aGuard(m_aMutex
);
399 void SAL_CALL
OReportEngineJFree::setMaxRows( ::sal_Int32 _MaxRows
) throw (uno::RuntimeException
, std::exception
)
401 set(PROPERTY_MAXROWS
,_MaxRows
,m_nMaxRows
);
404 } // namespace reportdesign
407 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */