1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2008 by Sun Microsystems, Inc.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* $RCSfile: migrationengine.cxx,v $
* $Revision: 1.4 $
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_dbaccess.hxx"
#include "dbmm_global.hrc"
#include "dbmm_module.hxx"
#include "dbmm_types.hxx"
#include "docerrorhandling.hxx"
#include "migrationengine.hxx"
#include "migrationprogress.hxx"
#include "migrationlog.hxx"
#include "progresscapture.hxx"
/** === begin UNO includes === **/
#include <com/sun/star/sdb/XFormDocumentsSupplier.hpp>
#include <com/sun/star/sdb/XReportDocumentsSupplier.hpp>
#include <com/sun/star/util/XCloseable.hpp>
#include <com/sun/star/frame/XModel.hpp>
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/ucb/XCommandProcessor.hpp>
#include <com/sun/star/embed/XComponentSupplier.hpp>
/** === end UNO includes === **/
#include <comphelper/string.hxx>
#include <comphelper/namedvaluecollection.hxx>
#include <tools/string.hxx>
#include <tools/diagnose_ex.h>
#include <rtl/ustrbuf.hxx>
#include <rtl/ref.hxx>
#include <vector>
#define DEFAULT_DOC_PROGRESS_RANGE 100000
//........................................................................
namespace dbmm
{
//........................................................................
/** === begin UNO using === **/
using ::com::sun::star::uno::Reference;
using ::com::sun::star::uno::XInterface;
using ::com::sun::star::uno::UNO_QUERY;
using ::com::sun::star::uno::UNO_QUERY_THROW;
using ::com::sun::star::uno::UNO_SET_THROW;
using ::com::sun::star::uno::Exception;
using ::com::sun::star::uno::RuntimeException;
using ::com::sun::star::uno::Any;
using ::com::sun::star::uno::makeAny;
using ::com::sun::star::sdb::XOfficeDatabaseDocument;
using ::com::sun::star::sdb::XFormDocumentsSupplier;
using ::com::sun::star::sdb::XReportDocumentsSupplier;
using ::com::sun::star::container::XNameAccess;
using ::com::sun::star::uno::Sequence;
using ::com::sun::star::util::XCloseable;
using ::com::sun::star::util::CloseVetoException;
using ::com::sun::star::lang::XComponent;
using ::com::sun::star::frame::XModel;
using ::com::sun::star::frame::XComponentLoader;
using ::com::sun::star::ucb::XCommandProcessor;
using ::com::sun::star::ucb::Command;
using ::com::sun::star::embed::XComponentSupplier;
using ::com::sun::star::task::XStatusIndicator;
/** === end UNO using === **/
//====================================================================
//= SubDocument
//====================================================================
struct SubDocument
{
Reference< XCommandProcessor > xCommandProcessor;
::rtl::OUString sHierarchicalName;
SubDocumentType eType;
SubDocument( const Reference< XCommandProcessor >& _rxCommandProcessor, const ::rtl::OUString& _rName, const SubDocumentType _eType )
:xCommandProcessor( _rxCommandProcessor )
,sHierarchicalName( _rName )
,eType( _eType )
{
}
};
typedef ::std::vector< SubDocument > SubDocuments;
//====================================================================
//= MigrationEngine_Impl - declaration
//====================================================================
class MigrationEngine_Impl
{
public:
MigrationEngine_Impl(
const ::comphelper::ComponentContext& _rContext,
const Reference< XOfficeDatabaseDocument >& _rxDocument,
IMigrationProgress& _rProgress,
MigrationLog& _rLogger
);
~MigrationEngine_Impl();
inline sal_Int32 getFormCount() const { return m_nFormCount; }
inline sal_Int32 getReportCount()const { return m_nReportCount; }
bool migrateAll();
private:
::comphelper::ComponentContext m_aContext;
const Reference< XOfficeDatabaseDocument > m_xDocument;
IMigrationProgress& m_rProgress;
MigrationLog& m_rLogger;
SubDocuments m_aSubDocs;
size_t m_nFormCount;
size_t m_nReportCount;
private:
/** collects a description of all sub documents of our database document
@return
<TRUE/> if and only if collecting the documents was successful
*/
bool impl_collectSubDocuments_nothrow();
/** reports the given error (usually an exception caught on the caller's side)
to the user, using the document's interaction handler, if any.
*/
void impl_reportError_nothrow( const Any& _rError ) const;
/** migrates the macros/scripts of the given sub document
*/
bool impl_handleDocument_nothrow( const SubDocument& _rDocument ) const;
};
//====================================================================
//= MigrationEngine_Impl - implementation
//====================================================================
//--------------------------------------------------------------------
MigrationEngine_Impl::MigrationEngine_Impl( const ::comphelper::ComponentContext& _rContext,
const Reference< XOfficeDatabaseDocument >& _rxDocument, IMigrationProgress& _rProgress, MigrationLog& _rLogger )
:m_aContext( _rContext )
,m_xDocument( _rxDocument )
,m_rProgress( _rProgress )
,m_rLogger( _rLogger )
,m_aSubDocs()
,m_nFormCount( 0 )
,m_nReportCount( 0 )
{
OSL_VERIFY( impl_collectSubDocuments_nothrow() );
}
//--------------------------------------------------------------------
MigrationEngine_Impl::~MigrationEngine_Impl()
{
}
//--------------------------------------------------------------------
bool MigrationEngine_Impl::migrateAll()
{
if ( m_aSubDocs.empty() )
{
OSL_ENSURE( false, "MigrationEngine_Impl::migrateAll: no forms/reports found!" );
// The whole migration wizard is not expected to be called when there are no forms/reports
// with macros, not to mention when there are no forms/reports at all.
return false;
}
// initialize global progress
sal_Int32 nOverallRange( m_aSubDocs.size() );
String sProgressSkeleton = String( MacroMigrationResId( STR_OVERALL_PROGRESS ) );
sProgressSkeleton.SearchAndReplaceAscii( "$overall$", String::CreateFromInt32( nOverallRange ) );
m_rProgress.start( nOverallRange );
for ( SubDocuments::const_iterator doc = m_aSubDocs.begin();
doc != m_aSubDocs.end();
++doc
)
{
sal_Int32 nOverallProgressValue( doc - m_aSubDocs.begin() + 1 );
// update overall progress text
::rtl::OUString sOverallProgress( sProgressSkeleton );
::comphelper::string::searchAndReplaceAsciiI( sOverallProgress, "$current$", ::rtl::OUString::valueOf( nOverallProgressValue ) );
m_rProgress.setOverallProgressText( sOverallProgress );
// migrate document
if ( !impl_handleDocument_nothrow( *doc ) )
return false;
// update overall progress vallue
m_rProgress.setOverallProgressValue( nOverallProgressValue );
}
return true;
}
//--------------------------------------------------------------------
namespace
{
size_t lcl_collectHierarchicalElementNames_throw(
const Reference< XNameAccess >& _rxContainer, const ::rtl::OUString& _rContainerLoc,
SubDocuments& _out_rDocs, const SubDocumentType _eType )
{
size_t nAddedElements = 0;
const ::rtl::OUString sHierarhicalBase(
_rContainerLoc.getLength() ? ::rtl::OUStringBuffer( _rContainerLoc ).appendAscii( "/" ).makeStringAndClear()
: ::rtl::OUString() );
Sequence< ::rtl::OUString > aElementNames( _rxContainer->getElementNames() );
for ( const ::rtl::OUString* elementName = aElementNames.getConstArray();
elementName != aElementNames.getConstArray() + aElementNames.getLength();
++elementName
)
{
Any aElement( _rxContainer->getByName( *elementName ) );
::rtl::OUString sElementName( ::rtl::OUStringBuffer( sHierarhicalBase ).append( *elementName ) );
Reference< XNameAccess > xSubContainer( aElement, UNO_QUERY );
if ( xSubContainer.is() )
{
nAddedElements += lcl_collectHierarchicalElementNames_throw( xSubContainer, sElementName, _out_rDocs, _eType );
}
else
{
Reference< XCommandProcessor > xCommandProcessor( aElement, UNO_QUERY );
OSL_ENSURE( xCommandProcessor.is(), "lcl_collectHierarchicalElementNames_throw: no container, and no comand processor? What *is* it, then?!" );
if ( xCommandProcessor.is() )
{
_out_rDocs.push_back( SubDocument( xCommandProcessor, sElementName, _eType ) );
++nAddedElements;
}
}
}
return nAddedElements;
}
}
//--------------------------------------------------------------------
bool MigrationEngine_Impl::impl_collectSubDocuments_nothrow()
{
try
{
::rtl::OUString sRootLocation;
Reference< XFormDocumentsSupplier > xSuppForms( m_xDocument, UNO_QUERY_THROW );
Reference< XNameAccess > xDocContainer( xSuppForms->getFormDocuments(), UNO_SET_THROW );
m_nFormCount = lcl_collectHierarchicalElementNames_throw( xDocContainer, sRootLocation, m_aSubDocs, eForm );
Reference< XReportDocumentsSupplier > xSuppReports( m_xDocument, UNO_QUERY_THROW );
xDocContainer.set( xSuppReports->getReportDocuments(), UNO_SET_THROW );
m_nReportCount = lcl_collectHierarchicalElementNames_throw( xDocContainer, sRootLocation, m_aSubDocs, eReport );
}
catch( const Exception& )
{
// TODO: check whether we can handle this error
DBG_UNHANDLED_EXCEPTION();
return false;
}
return true;
}
//--------------------------------------------------------------------
namespace
{
//................................................................
static void lcl_disposeComponent_nothrow( const Reference< XCommandProcessor >& _rxCommandProc )
{
OSL_PRECOND( _rxCommandProc.is(), "lcl_disposeComponent_nothrow: illegal object!" );
if ( !_rxCommandProc.is() )
return;
bool bCouldClose = false;
try
{
Command aCommand;
aCommand.Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "shutdown" ) );
OSL_VERIFY( _rxCommandProc->execute(
aCommand, _rxCommandProc->createCommandIdentifier(), NULL ) >>= bCouldClose );
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION();
}
if ( !bCouldClose )
{
;
// TODO: can we handle this somehow?
}
}
//................................................................
static Reference< XModel > lcl_loadSubDocument_nothrow( const SubDocument& _rDocument,
const Reference< XStatusIndicator >& _rxProgress )
{
Reference< XModel > xDocument;
try
{
::comphelper::NamedValueCollection aLoadArgs;
aLoadArgs.put( "Hidden", (sal_Bool)sal_True );
aLoadArgs.put( "StatusIndicator", _rxProgress );
Reference< XCommandProcessor > xCommandProcessor( _rDocument.xCommandProcessor, UNO_SET_THROW );
Command aCommand;
aCommand.Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "openDesign" ) );
aCommand.Argument <<= aLoadArgs.getPropertyValues();
Reference< XComponent > xDocComponent(
xCommandProcessor->execute(
aCommand, xCommandProcessor->createCommandIdentifier(), NULL
),
UNO_QUERY
);
OSL_ENSURE( xDocComponent.is(), "lcl_loadSubDocument_nothrow: no component loaded!" );
xDocument.set( xDocComponent, UNO_QUERY_THROW );
}
catch( const Exception& )
{
// TODO: how to proceed?
DBG_UNHANDLED_EXCEPTION();
}
return xDocument;
}
}
//--------------------------------------------------------------------
bool MigrationEngine_Impl::impl_handleDocument_nothrow( const SubDocument& _rDocument ) const
{
DocumentID nDocID = m_rLogger.startedDocument( _rDocument.eType, _rDocument.sHierarchicalName );
// start the progress
::rtl::OUString aProgress;
aProgress = String( MacroMigrationResId( _rDocument.eType == eForm ? STR_FORM : STR_REPORT ) );
::comphelper::string::searchAndReplaceAsciiI( aProgress, "$name$", _rDocument.sHierarchicalName );
m_rProgress.startObject( aProgress, ::rtl::OUString(), DEFAULT_DOC_PROGRESS_RANGE );
// load the document
::rtl::Reference< ProgressCapture > pStatusIndicator( new ProgressCapture( aProgress, m_rProgress ) );
Reference< XModel > xDocument( lcl_loadSubDocument_nothrow( _rDocument, pStatusIndicator.get() ) );
if ( !xDocument.is() )
{
pStatusIndicator->dispose();
m_rProgress.endObject();
m_rLogger.finishedDocument( nDocID, false );
// TODO: log the *reason* for the failure
return false;
}
// TODO
// clean up
lcl_disposeComponent_nothrow( _rDocument.xCommandProcessor );
pStatusIndicator->dispose();
// end the progress, just in case the ProgressCapture didn't receive the XStatusIndicator::end event
m_rProgress.endObject();
m_rLogger.finishedDocument( nDocID, true );
return true;
}
//--------------------------------------------------------------------
void MigrationEngine_Impl::impl_reportError_nothrow( const Any& _rError ) const
{
DocumentErrorHandling::reportError( m_aContext, m_xDocument, _rError );
}
//====================================================================
//= MigrationEngine
//====================================================================
//--------------------------------------------------------------------
MigrationEngine::MigrationEngine( const ::comphelper::ComponentContext& _rContext, const Reference< XOfficeDatabaseDocument >& _rxDocument,
IMigrationProgress& _rProgress, MigrationLog& _rLogger )
:m_pImpl( new MigrationEngine_Impl( _rContext, _rxDocument, _rProgress, _rLogger ) )
{
}
//--------------------------------------------------------------------
MigrationEngine::~MigrationEngine()
{
}
//--------------------------------------------------------------------
sal_Int32 MigrationEngine::getFormCount() const
{
return m_pImpl->getFormCount();
}
//--------------------------------------------------------------------
sal_Int32 MigrationEngine::getReportCount() const
{
return m_pImpl->getReportCount();
}
//--------------------------------------------------------------------
bool MigrationEngine::migrateAll()
{
return m_pImpl->migrateAll();
}
//........................................................................
} // namespace dbmm
//........................................................................
|