update dev300-m58
[ooovba.git] / dbaccess / source / ext / macromigration / migrationlog.cxx
blob94bd930bc51e5755085c38eb3e1bc7fde836bcd7
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: migrationlog.cxx,v $
10 * $Revision: 1.4.2.8 $
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 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #include "dbmm_module.hxx"
35 #include "dbmm_global.hrc"
36 #include "migrationerror.hxx"
37 #include "migrationlog.hxx"
39 /** === begin UNO includes === **/
40 /** === end UNO includes === **/
42 #include <comphelper/anytostring.hxx>
43 #include <comphelper/string.hxx>
44 #include <tools/string.hxx>
45 #include <rtl/ustrbuf.hxx>
47 #include <vector>
48 #include <map>
49 #include <list>
51 //........................................................................
52 namespace dbmm
54 //........................................................................
56 /** === begin UNO using === **/
57 /** === end UNO using === **/
59 //====================================================================
60 //= LibraryEntry
61 //====================================================================
62 struct LibraryEntry
64 ScriptType eType;
65 ::rtl::OUString sOldName;
66 ::rtl::OUString sNewName;
68 LibraryEntry()
69 :eType( eBasic )
70 ,sOldName()
71 ,sNewName()
75 LibraryEntry( const ScriptType& _eType, const ::rtl::OUString& _rOldName, const ::rtl::OUString& _rNewName )
76 :eType( _eType )
77 ,sOldName( _rOldName )
78 ,sNewName( _rNewName )
83 //====================================================================
84 //= DocumentEntry
85 //====================================================================
86 struct DocumentEntry
88 SubDocumentType eType;
89 ::rtl::OUString sName;
90 ::std::vector< LibraryEntry > aMovedLibraries;
92 DocumentEntry()
93 :eType( eForm )
94 ,sName()
95 ,aMovedLibraries()
99 DocumentEntry( const SubDocumentType _eType, const ::rtl::OUString& _rName )
100 :eType( _eType )
101 ,sName( _rName )
106 //====================================================================
107 //= DocumentLogs
108 //====================================================================
109 typedef ::std::map< DocumentID, DocumentEntry > DocumentLogs;
111 //====================================================================
112 //= ErrorLog
113 //====================================================================
114 typedef ::std::list< MigrationError > ErrorLog;
116 //====================================================================
117 //= MigrationLog_Data
118 //====================================================================
119 struct MigrationLog_Data
121 ::rtl::OUString sBackupLocation;
122 DocumentLogs aDocumentLogs;
123 ErrorLog aFailures;
124 ErrorLog aWarnings;
127 //====================================================================
128 //= MigrationLog
129 //====================================================================
130 //--------------------------------------------------------------------
131 MigrationLog::MigrationLog()
132 :m_pData( new MigrationLog_Data )
136 //--------------------------------------------------------------------
137 MigrationLog::~MigrationLog()
141 //--------------------------------------------------------------------
142 void MigrationLog::logFailure( const MigrationError& _rError )
144 m_pData->aFailures.push_back( _rError );
147 //--------------------------------------------------------------------
148 void MigrationLog::logRecoverable( const MigrationError& _rError )
150 m_pData->aWarnings.push_back( _rError );
153 //--------------------------------------------------------------------
154 bool MigrationLog::hadFailure() const
156 return !m_pData->aFailures.empty();
159 //--------------------------------------------------------------------
160 void MigrationLog::backedUpDocument( const ::rtl::OUString& _rNewDocumentLocation )
162 m_pData->sBackupLocation = _rNewDocumentLocation;
165 //--------------------------------------------------------------------
166 DocumentID MigrationLog::startedDocument( const SubDocumentType _eType, const ::rtl::OUString& _rName )
168 #if OSL_DEBUG_LEVEL > 0
169 bool bAlreadyKnown = false;
170 for ( DocumentLogs::const_iterator doc = m_pData->aDocumentLogs.begin();
171 doc != m_pData->aDocumentLogs.end() && !bAlreadyKnown;
172 ++doc
175 bAlreadyKnown = ( doc->second.eType == _eType ) && ( doc->second.sName == _rName );
177 OSL_ENSURE( !bAlreadyKnown, "MigrationLog::startedDocument: document is already known!" );
178 #endif
180 DocumentID nID = (DocumentID)( m_pData->aDocumentLogs.size() + 1 );
181 while ( m_pData->aDocumentLogs.find( nID ) != m_pData->aDocumentLogs.end() )
182 ++nID;
184 m_pData->aDocumentLogs[ nID ] = DocumentEntry( _eType, _rName );
186 return nID;
189 //--------------------------------------------------------------------
190 void MigrationLog::movedLibrary( const DocumentID _nDocID, const ScriptType _eScriptType,
191 const ::rtl::OUString& _rOriginalLibName, const ::rtl::OUString& _rNewLibName )
193 OSL_ENSURE( m_pData->aDocumentLogs.find( _nDocID ) != m_pData->aDocumentLogs.end(),
194 "MigrationLog::movedLibrary: document is not known!" );
196 DocumentEntry& rDocEntry = m_pData->aDocumentLogs[ _nDocID ];
197 rDocEntry.aMovedLibraries.push_back( LibraryEntry( _eScriptType, _rOriginalLibName, _rNewLibName ) );
200 //--------------------------------------------------------------------
201 void MigrationLog::finishedDocument( const DocumentID _nDocID )
203 OSL_ENSURE( m_pData->aDocumentLogs.find( _nDocID ) != m_pData->aDocumentLogs.end(),
204 "MigrationLog::finishedDocument: document is not known!" );
206 DocumentEntry& rDocEntry = m_pData->aDocumentLogs[ _nDocID ];
207 (void)rDocEntry;
208 // nothing to do here
211 //--------------------------------------------------------------------
212 const ::rtl::OUString& MigrationLog::getNewLibraryName( DocumentID _nDocID, ScriptType _eScriptType,
213 const ::rtl::OUString& _rOriginalLibName ) const
215 static ::rtl::OUString s_sEmptyString;
217 DocumentLogs::const_iterator docPos = m_pData->aDocumentLogs.find( _nDocID );
218 if ( docPos == m_pData->aDocumentLogs.end() )
220 OSL_ENSURE( false, "MigrationLog::getNewLibraryName: document is not known!" );
221 return s_sEmptyString;
224 const DocumentEntry& rDocEntry( docPos->second );
225 for ( ::std::vector< LibraryEntry >::const_iterator lib = rDocEntry.aMovedLibraries.begin();
226 lib != rDocEntry.aMovedLibraries.end();
227 ++lib
230 if ( ( _eScriptType == lib->eType )
231 && ( _rOriginalLibName == lib->sOldName )
233 return lib->sNewName;
236 OSL_ENSURE( false, "MigrationLog::getNewLibraryName: doc is known, but library isn't!" );
237 return s_sEmptyString;
240 //--------------------------------------------------------------------
241 namespace
243 //----------------------------------------------------------------
244 static void lcl_appendErrorDescription( ::rtl::OUStringBuffer& _inout_rBuffer, const MigrationError& _rError )
246 const sal_Char* pAsciiErrorDescription( NULL );
247 ::std::vector< const sal_Char* > aAsciiParameterNames;
248 switch ( _rError.eType )
250 case ERR_OPENING_SUB_DOCUMENT_FAILED:
251 pAsciiErrorDescription = "opening '#doc#' failed";
252 aAsciiParameterNames.push_back( "#doc#" );
253 break;
255 case ERR_CLOSING_SUB_DOCUMENT_FAILED:
256 pAsciiErrorDescription = "closing '#doc#' failed";
257 aAsciiParameterNames.push_back( "#doc#" );
258 break;
260 case ERR_STORAGE_COMMIT_FAILED:
261 pAsciiErrorDescription = "committing the changes for document '#doc#' failed";
262 aAsciiParameterNames.push_back( "#doc#" );
263 break;
265 case ERR_STORING_DATABASEDOC_FAILED:
266 pAsciiErrorDescription = "storing the database document failed";
267 break;
269 case ERR_COLLECTING_DOCUMENTS_FAILED:
270 pAsciiErrorDescription = "collecting the forms/reports of the database document failed";
271 break;
273 case ERR_UNEXPECTED_LIBSTORAGE_ELEMENT:
274 pAsciiErrorDescription = "unexpected #lib# storage element in document '#doc#', named '#element#'";
275 aAsciiParameterNames.push_back( "#doc#" );
276 aAsciiParameterNames.push_back( "#libstore#" );
277 aAsciiParameterNames.push_back( "#element#" );
278 break;
280 case ERR_CREATING_DBDOC_SCRIPT_STORAGE_FAILED:
281 pAsciiErrorDescription = "creating the database document's storage for #scripttype# scripts failed";
282 aAsciiParameterNames.push_back( "#scripttype#" );
283 break;
285 case ERR_COMMITTING_SCRIPT_STORAGES_FAILED:
286 pAsciiErrorDescription = "saving the #scripttype# scripts for document '#doc#' failed";
287 aAsciiParameterNames.push_back( "#scripttype#" );
288 aAsciiParameterNames.push_back( "#doc#" );
289 break;
291 case ERR_GENERAL_SCRIPT_MIGRATION_FAILURE:
292 pAsciiErrorDescription = "general error while migrating #scripttype# scripts of document '#doc#'";
293 aAsciiParameterNames.push_back( "#scripttype#" );
294 aAsciiParameterNames.push_back( "#doc#" );
295 break;
297 case ERR_GENERAL_MACRO_MIGRATION_FAILURE:
298 pAsciiErrorDescription = "general error during macro migration of document '#doc#'";
299 aAsciiParameterNames.push_back( "#doc#" );
300 break;
302 case ERR_UNKNOWN_SCRIPT_TYPE:
303 pAsciiErrorDescription = "unknown script type: #type#";
304 aAsciiParameterNames.push_back( "#type#" );
305 break;
307 case ERR_UNKNOWN_SCRIPT_LANGUAGE:
308 pAsciiErrorDescription = "unknown script language: #lang#";
309 aAsciiParameterNames.push_back( "#lang#" );
310 break;
312 case ERR_UNKNOWN_SCRIPT_NAME_FORMAT:
313 pAsciiErrorDescription = "unknown script name format: #script#";
314 aAsciiParameterNames.push_back( "#script#" );
315 break;
317 case ERR_SCRIPT_TRANSLATION_FAILURE:
318 pAsciiErrorDescription = "analyzing/translating the script URL failed; script type: #type#; script: #code#";
319 aAsciiParameterNames.push_back( "#type#" );
320 aAsciiParameterNames.push_back( "#code#" );
321 break;
323 case ERR_INVALID_SCRIPT_DESCRIPTOR_FORMAT:
324 pAsciiErrorDescription = "invalid script descriptor format";
325 break;
327 case ERR_ADJUSTING_DOCUMENT_EVENTS_FAILED:
328 pAsciiErrorDescription = "adjusting events for document '#doc#' failed";
329 aAsciiParameterNames.push_back( "#doc#" );
330 break;
332 case ERR_ADJUSTING_DIALOG_EVENTS_FAILED:
333 pAsciiErrorDescription = "adjusting events for dialog #lib#.#dlg# in document '#doc#' failed";
334 aAsciiParameterNames.push_back( "#doc#" );
335 aAsciiParameterNames.push_back( "#lib#" );
336 aAsciiParameterNames.push_back( "#dlg#" );
337 break;
339 case ERR_ADJUSTING_FORMCOMP_EVENTS_FAILED:
340 pAsciiErrorDescription = "adjusting form component events for '#doc#' failed";
341 aAsciiParameterNames.push_back( "#doc#" );
342 break;
344 case ERR_BIND_SCRIPT_STORAGE_FAILED:
345 pAsciiErrorDescription = "binding to the script storage failed for document '#doc#'";
346 aAsciiParameterNames.push_back( "#doc#" );
347 break;
349 case ERR_REMOVE_SCRIPTS_STORAGE_FAILED:
350 pAsciiErrorDescription = "removing a scripts storage failed for document '#doc#'";
351 aAsciiParameterNames.push_back( "#doc#" );
352 break;
354 case ERR_DOCUMENT_BACKUP_FAILED:
355 pAsciiErrorDescription = "backing up the document to #location# failed";
356 aAsciiParameterNames.push_back( "#location#" );
357 break;
359 case ERR_UNKNOWN_SCRIPT_FOLDER:
360 pAsciiErrorDescription = "unknown script folder '#name#' in document '#doc#'";
361 aAsciiParameterNames.push_back( "#doc#" );
362 aAsciiParameterNames.push_back( "#name#" );
363 break;
365 case ERR_EXAMINING_SCRIPTS_FOLDER_FAILED:
366 pAsciiErrorDescription = "examining the 'Scripts' folder failed for document '#doc#'";
367 aAsciiParameterNames.push_back( "#doc#" );
368 break;
370 case ERR_PASSWORD_VERIFICATION_FAILED:
371 pAsciiErrorDescription = "password verification failed for document '#doc#', #libtype# library '#name#'";
372 aAsciiParameterNames.push_back( "#doc#" );
373 aAsciiParameterNames.push_back( "#libtype#" );
374 aAsciiParameterNames.push_back( "#name#" );
375 break;
377 case ERR_NEW_STYLE_REPORT:
378 pAsciiErrorDescription = "#doc# could not be processed, since you don't have the Sun Report Builder (TM) extension installed.";
379 aAsciiParameterNames.push_back( "#doc#" );
380 break;
382 // do *not* add a default case here: Without a default, some compilers will warn you when
383 // you miss a newly-introduced enum value here
385 OSL_ENSURE( pAsciiErrorDescription, "lcl_appendErrorDescription: no error message!" );
386 if ( pAsciiErrorDescription )
388 ::rtl::OUString sSubstituted( ::rtl::OUString::createFromAscii( pAsciiErrorDescription ) );
389 OSL_ENSURE( aAsciiParameterNames.size() == _rError.aErrorDetails.size(),
390 "lcl_appendErrorDescription: unexpected number of error message parameters!" );
392 for ( size_t i=0; i < ::std::min( aAsciiParameterNames.size(), _rError.aErrorDetails.size() ); ++i )
394 ::comphelper::string::searchAndReplaceAsciiI( sSubstituted, aAsciiParameterNames[i],
395 _rError.aErrorDetails[i] );
398 _inout_rBuffer.append( sSubstituted );
402 //----------------------------------------------------------------
403 void lcl_describeErrors( ::rtl::OUStringBuffer& _rBuffer, const ErrorLog& _rErrors, const USHORT _nHeadingResId )
405 _rBuffer.appendAscii( "=== " );
406 _rBuffer.append ( String( MacroMigrationResId( _nHeadingResId ) ) );
407 _rBuffer.appendAscii( " ===\n" );
409 String sException( MacroMigrationResId( STR_EXCEPTION ) );
411 for ( ErrorLog::const_iterator error = _rErrors.begin();
412 error != _rErrors.end();
413 ++error
416 _rBuffer.append( sal_Unicode( '-' ) );
417 _rBuffer.append( sal_Unicode( ' ' ) );
418 lcl_appendErrorDescription( _rBuffer, *error );
419 _rBuffer.append( sal_Unicode( '\n' ) );
421 if ( !error->aCaughtException.hasValue() )
422 continue;
424 _rBuffer.append( sException );
425 _rBuffer.append( ::comphelper::anyToString( error->aCaughtException ) );
426 _rBuffer.append( sal_Unicode( '\n' ) );
427 _rBuffer.append( sal_Unicode( '\n' ) );
432 //--------------------------------------------------------------------
433 bool MigrationLog::movedAnyLibrary( const DocumentID _nDocID )
435 DocumentLogs::const_iterator docPos = m_pData->aDocumentLogs.find( _nDocID );
436 if ( docPos == m_pData->aDocumentLogs.end() )
438 OSL_ENSURE( false, "MigrationLog::movedAnyLibrary: document is not known!" );
439 return false;
441 return !docPos->second.aMovedLibraries.empty();
444 //--------------------------------------------------------------------
445 ::rtl::OUString MigrationLog::getCompleteLog() const
447 ::rtl::OUStringBuffer aBuffer;
449 if ( m_pData->sBackupLocation.getLength() )
451 String sBackedUp( MacroMigrationResId( STR_SAVED_COPY_TO ) );
452 sBackedUp.SearchAndReplaceAllAscii( "$location$", m_pData->sBackupLocation );
454 aBuffer.appendAscii( "=== " );
455 aBuffer.append ( String( MacroMigrationResId( STR_DATABASE_DOCUMENT ) ) );
456 aBuffer.appendAscii( " ===\n" );
457 aBuffer.append ( sBackedUp );
458 aBuffer.appendAscii( "\n\n" );
461 if ( !m_pData->aFailures.empty() )
463 lcl_describeErrors( aBuffer, m_pData->aFailures
464 , STR_ERRORS );
466 else
468 String sMovedLibTemplate( MacroMigrationResId( STR_MOVED_LIBRARY ) );
470 for ( DocumentLogs::const_iterator doc = m_pData->aDocumentLogs.begin();
471 doc != m_pData->aDocumentLogs.end();
472 ++doc
475 const DocumentEntry& rDoc( doc->second );
477 if ( rDoc.aMovedLibraries.empty() )
478 continue;
480 String sDocTitle( MacroMigrationResId( rDoc.eType == eForm ? STR_FORM : STR_REPORT ) );
481 sDocTitle.SearchAndReplaceAllAscii( "$name$", rDoc.sName );
483 aBuffer.appendAscii( "=== " );
484 aBuffer.append ( sDocTitle );
485 aBuffer.appendAscii( " ===\n" );
487 for ( ::std::vector< LibraryEntry >::const_iterator lib = rDoc.aMovedLibraries.begin();
488 lib != rDoc.aMovedLibraries.end();
489 ++lib
492 String sMovedLib( sMovedLibTemplate );
493 sMovedLib.SearchAndReplaceAllAscii( "$type$", getScriptTypeDisplayName( lib->eType ) );
494 sMovedLib.SearchAndReplaceAllAscii( "$old$", lib->sOldName );
495 sMovedLib.SearchAndReplaceAllAscii( "$new$", lib->sNewName );
497 aBuffer.append( sMovedLib );
498 aBuffer.append( sal_Unicode( '\n' ) );
501 aBuffer.append( sal_Unicode( '\n' ) );
505 if ( !m_pData->aWarnings.empty() )
507 lcl_describeErrors( aBuffer, m_pData->aWarnings, STR_WARNINGS );
510 return aBuffer.makeStringAndClear();
513 //........................................................................
514 } // namespace dbmm
515 //........................................................................