Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / ext / macromigration / migrationlog.cxx
blobe6699eea74c3539a204bbc3edf4a789b9ea00603
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include "dbmm_module.hxx"
21 #include "dbmm_global.hrc"
22 #include "migrationerror.hxx"
23 #include "migrationlog.hxx"
25 #include <comphelper/anytostring.hxx>
26 #include <rtl/ustrbuf.hxx>
28 #include <vector>
29 #include <map>
30 #include <list>
31 #include <algorithm>
33 namespace dbmm
35 // LibraryEntry
36 struct LibraryEntry
38 ScriptType eType;
39 OUString sOldName;
40 OUString sNewName;
42 LibraryEntry( const ScriptType& _eType, const OUString& _rOldName, const OUString& _rNewName )
43 :eType( _eType )
44 ,sOldName( _rOldName )
45 ,sNewName( _rNewName )
50 // DocumentEntry
51 struct DocumentEntry
53 SubDocumentType eType;
54 OUString sName;
55 ::std::vector< LibraryEntry > aMovedLibraries;
57 DocumentEntry()
58 :eType( eForm )
59 ,sName()
60 ,aMovedLibraries()
64 DocumentEntry( const SubDocumentType _eType, const OUString& _rName )
65 :eType( _eType )
66 ,sName( _rName )
71 // DocumentLogs
72 typedef ::std::map< DocumentID, DocumentEntry > DocumentLogs;
74 // ErrorLog
75 typedef ::std::list< MigrationError > ErrorLog;
77 // MigrationLog_Data
78 struct MigrationLog_Data
80 OUString sBackupLocation;
81 DocumentLogs aDocumentLogs;
82 ErrorLog aFailures;
83 ErrorLog aWarnings;
86 // MigrationLog
87 MigrationLog::MigrationLog()
88 :m_pData( new MigrationLog_Data )
92 MigrationLog::~MigrationLog()
96 void MigrationLog::logFailure( const MigrationError& _rError )
98 m_pData->aFailures.push_back( _rError );
101 void MigrationLog::logRecoverable( const MigrationError& _rError )
103 m_pData->aWarnings.push_back( _rError );
106 bool MigrationLog::hadFailure() const
108 return !m_pData->aFailures.empty();
111 void MigrationLog::backedUpDocument( const OUString& _rNewDocumentLocation )
113 m_pData->sBackupLocation = _rNewDocumentLocation;
116 DocumentID MigrationLog::startedDocument( const SubDocumentType _eType, const OUString& _rName )
118 #if OSL_DEBUG_LEVEL > 0
119 bool bAlreadyKnown = false;
120 for ( DocumentLogs::const_iterator doc = m_pData->aDocumentLogs.begin();
121 doc != m_pData->aDocumentLogs.end() && !bAlreadyKnown;
122 ++doc
125 bAlreadyKnown = ( doc->second.eType == _eType ) && ( doc->second.sName == _rName );
127 OSL_ENSURE( !bAlreadyKnown, "MigrationLog::startedDocument: document is already known!" );
128 #endif
130 DocumentID nID = (DocumentID)( m_pData->aDocumentLogs.size() + 1 );
131 while ( m_pData->aDocumentLogs.find( nID ) != m_pData->aDocumentLogs.end() )
132 ++nID;
134 m_pData->aDocumentLogs[ nID ] = DocumentEntry( _eType, _rName );
136 return nID;
139 void MigrationLog::movedLibrary( const DocumentID _nDocID, const ScriptType _eScriptType,
140 const OUString& _rOriginalLibName, const OUString& _rNewLibName )
142 OSL_ENSURE( m_pData->aDocumentLogs.find( _nDocID ) != m_pData->aDocumentLogs.end(),
143 "MigrationLog::movedLibrary: document is not known!" );
145 DocumentEntry& rDocEntry = m_pData->aDocumentLogs[ _nDocID ];
146 rDocEntry.aMovedLibraries.push_back( LibraryEntry( _eScriptType, _rOriginalLibName, _rNewLibName ) );
149 void MigrationLog::finishedDocument( const DocumentID _nDocID )
151 OSL_ENSURE( m_pData->aDocumentLogs.find( _nDocID ) != m_pData->aDocumentLogs.end(),
152 "MigrationLog::finishedDocument: document is not known!" );
154 DocumentEntry& rDocEntry = m_pData->aDocumentLogs[ _nDocID ];
155 (void)rDocEntry;
156 // nothing to do here
159 const OUString& MigrationLog::getNewLibraryName( DocumentID _nDocID, ScriptType _eScriptType,
160 const OUString& _rOriginalLibName ) const
162 static OUString s_sEmptyString;
164 DocumentLogs::const_iterator docPos = m_pData->aDocumentLogs.find( _nDocID );
165 if ( docPos == m_pData->aDocumentLogs.end() )
167 OSL_FAIL( "MigrationLog::getNewLibraryName: document is not known!" );
168 return s_sEmptyString;
171 const DocumentEntry& rDocEntry( docPos->second );
172 for ( ::std::vector< LibraryEntry >::const_iterator lib = rDocEntry.aMovedLibraries.begin();
173 lib != rDocEntry.aMovedLibraries.end();
174 ++lib
177 if ( ( _eScriptType == lib->eType )
178 && ( _rOriginalLibName == lib->sOldName )
180 return lib->sNewName;
183 OSL_FAIL( "MigrationLog::getNewLibraryName: doc is known, but library isn't!" );
184 return s_sEmptyString;
187 namespace
189 static void lcl_appendErrorDescription( OUStringBuffer& _inout_rBuffer, const MigrationError& _rError )
191 const sal_Char* pAsciiErrorDescription( NULL );
192 ::std::vector< OUString > aParameterNames;
193 switch ( _rError.eType )
195 case ERR_OPENING_SUB_DOCUMENT_FAILED:
196 pAsciiErrorDescription = "opening '#doc#' failed";
197 aParameterNames.push_back(OUString("#doc#"));
198 break;
200 case ERR_CLOSING_SUB_DOCUMENT_FAILED:
201 pAsciiErrorDescription = "closing '#doc#' failed";
202 aParameterNames.push_back(OUString("#doc#"));
203 break;
205 case ERR_STORAGE_COMMIT_FAILED:
206 pAsciiErrorDescription = "committing the changes for document '#doc#' failed";
207 aParameterNames.push_back(OUString("#doc#"));
208 break;
210 case ERR_STORING_DATABASEDOC_FAILED:
211 pAsciiErrorDescription = "storing the database document failed";
212 break;
214 case ERR_COLLECTING_DOCUMENTS_FAILED:
215 pAsciiErrorDescription = "collecting the forms/reports of the database document failed";
216 break;
218 case ERR_UNEXPECTED_LIBSTORAGE_ELEMENT:
219 pAsciiErrorDescription = "unexpected #lib# storage element in document '#doc#', named '#element#'";
220 aParameterNames.push_back(OUString("#doc#"));
221 aParameterNames.push_back(OUString("#libstore#"));
222 aParameterNames.push_back(OUString("#element#"));
223 break;
225 case ERR_CREATING_DBDOC_SCRIPT_STORAGE_FAILED:
226 pAsciiErrorDescription = "creating the database document's storage for #scripttype# scripts failed";
227 aParameterNames.push_back(OUString("#scripttype#"));
228 break;
230 case ERR_COMMITTING_SCRIPT_STORAGES_FAILED:
231 pAsciiErrorDescription = "saving the #scripttype# scripts for document '#doc#' failed";
232 aParameterNames.push_back(OUString("#scripttype#"));
233 aParameterNames.push_back(OUString("#doc#"));
234 break;
236 case ERR_GENERAL_SCRIPT_MIGRATION_FAILURE:
237 pAsciiErrorDescription = "general error while migrating #scripttype# scripts of document '#doc#'";
238 aParameterNames.push_back(OUString("#scripttype#"));
239 aParameterNames.push_back(OUString("#doc#"));
240 break;
242 case ERR_GENERAL_MACRO_MIGRATION_FAILURE:
243 pAsciiErrorDescription = "general error during macro migration of document '#doc#'";
244 aParameterNames.push_back(OUString("#doc#"));
245 break;
247 case ERR_UNKNOWN_SCRIPT_TYPE:
248 pAsciiErrorDescription = "unknown script type: #type#";
249 aParameterNames.push_back(OUString("#type#"));
250 break;
252 case ERR_UNKNOWN_SCRIPT_LANGUAGE:
253 pAsciiErrorDescription = "unknown script language: #lang#";
254 aParameterNames.push_back(OUString("#lang#"));
255 break;
257 case ERR_UNKNOWN_SCRIPT_NAME_FORMAT:
258 pAsciiErrorDescription = "unknown script name format: #script#";
259 aParameterNames.push_back(OUString("#script#"));
260 break;
262 case ERR_SCRIPT_TRANSLATION_FAILURE:
263 pAsciiErrorDescription = "analyzing/translating the script URL failed; script type: #type#; script: #code#";
264 aParameterNames.push_back(OUString("#type#"));
265 aParameterNames.push_back(OUString("#code#"));
266 break;
268 case ERR_INVALID_SCRIPT_DESCRIPTOR_FORMAT:
269 pAsciiErrorDescription = "invalid script descriptor format";
270 break;
272 case ERR_ADJUSTING_DOCUMENT_EVENTS_FAILED:
273 pAsciiErrorDescription = "adjusting events for document '#doc#' failed";
274 aParameterNames.push_back(OUString("#doc#"));
275 break;
277 case ERR_ADJUSTING_DIALOG_EVENTS_FAILED:
278 pAsciiErrorDescription = "adjusting events for dialog #lib#.#dlg# in document '#doc#' failed";
279 aParameterNames.push_back(OUString("#doc#"));
280 aParameterNames.push_back(OUString("#lib#"));
281 aParameterNames.push_back(OUString("#dlg#"));
282 break;
284 case ERR_ADJUSTING_FORMCOMP_EVENTS_FAILED:
285 pAsciiErrorDescription = "adjusting form component events for '#doc#' failed";
286 aParameterNames.push_back(OUString("#doc#"));
287 break;
289 case ERR_BIND_SCRIPT_STORAGE_FAILED:
290 pAsciiErrorDescription = "binding to the script storage failed for document '#doc#'";
291 aParameterNames.push_back(OUString("#doc#"));
292 break;
294 case ERR_REMOVE_SCRIPTS_STORAGE_FAILED:
295 pAsciiErrorDescription = "removing a scripts storage failed for document '#doc#'";
296 aParameterNames.push_back(OUString("#doc#"));
297 break;
299 case ERR_DOCUMENT_BACKUP_FAILED:
300 pAsciiErrorDescription = "backing up the document to #location# failed";
301 aParameterNames.push_back(OUString("#location#"));
302 break;
304 case ERR_UNKNOWN_SCRIPT_FOLDER:
305 pAsciiErrorDescription = "unknown script folder '#name#' in document '#doc#'";
306 aParameterNames.push_back(OUString("#doc#"));
307 aParameterNames.push_back(OUString("#name#"));
308 break;
310 case ERR_EXAMINING_SCRIPTS_FOLDER_FAILED:
311 pAsciiErrorDescription = "examining the 'Scripts' folder failed for document '#doc#'";
312 aParameterNames.push_back(OUString("#doc#"));
313 break;
315 case ERR_PASSWORD_VERIFICATION_FAILED:
316 pAsciiErrorDescription = "password verification failed for document '#doc#', #libtype# library '#name#'";
317 aParameterNames.push_back(OUString("#doc#"));
318 aParameterNames.push_back(OUString("#libtype#"));
319 aParameterNames.push_back(OUString("#name#"));
320 break;
322 case ERR_NEW_STYLE_REPORT:
323 pAsciiErrorDescription = "#doc# could not be processed, since you don't have the Oracle Report Builder (TM) extension installed.";
324 aParameterNames.push_back(OUString("#doc#"));
325 break;
327 // do *not* add a default case here: Without a default, some compilers will warn you when
328 // you miss a newly-introduced enum value here
330 OSL_ENSURE( pAsciiErrorDescription, "lcl_appendErrorDescription: no error message!" );
331 if ( pAsciiErrorDescription )
333 OUString sSubstituted( OUString::createFromAscii( pAsciiErrorDescription ) );
334 OSL_ENSURE( aParameterNames.size() == _rError.aErrorDetails.size(),
335 "lcl_appendErrorDescription: unexpected number of error message parameters!" );
337 for ( size_t i=0; i < ::std::min( aParameterNames.size(), _rError.aErrorDetails.size() ); ++i )
339 sSubstituted = sSubstituted.replaceFirst(
340 aParameterNames[i], _rError.aErrorDetails[i]);
343 _inout_rBuffer.append( sSubstituted );
347 void lcl_describeErrors( OUStringBuffer& _rBuffer, const ErrorLog& _rErrors, const sal_uInt16 _nHeadingResId )
349 _rBuffer.appendAscii( "=== " );
350 _rBuffer.append ( OUString( MacroMigrationResId( _nHeadingResId ) ) );
351 _rBuffer.appendAscii( " ===\n" );
353 OUString sException( MacroMigrationResId( STR_EXCEPTION ) );
355 for ( ErrorLog::const_iterator error = _rErrors.begin();
356 error != _rErrors.end();
357 ++error
360 _rBuffer.append( '-' );
361 _rBuffer.append( ' ' );
362 lcl_appendErrorDescription( _rBuffer, *error );
363 _rBuffer.append( '\n' );
365 if ( !error->aCaughtException.hasValue() )
366 continue;
368 _rBuffer.append( sException );
369 _rBuffer.append( ::comphelper::anyToString( error->aCaughtException ) );
370 _rBuffer.append( '\n' );
371 _rBuffer.append( '\n' );
376 bool MigrationLog::movedAnyLibrary( const DocumentID _nDocID )
378 DocumentLogs::const_iterator docPos = m_pData->aDocumentLogs.find( _nDocID );
379 if ( docPos == m_pData->aDocumentLogs.end() )
381 OSL_FAIL( "MigrationLog::movedAnyLibrary: document is not known!" );
382 return false;
384 return !docPos->second.aMovedLibraries.empty();
387 OUString MigrationLog::getCompleteLog() const
389 OUStringBuffer aBuffer;
391 if ( !m_pData->sBackupLocation.isEmpty() )
393 OUString sBackedUp( MacroMigrationResId( STR_SAVED_COPY_TO ) );
394 sBackedUp = sBackedUp.replaceAll( "$location$", m_pData->sBackupLocation );
396 aBuffer.append( "=== " + OUString( MacroMigrationResId( STR_DATABASE_DOCUMENT ) )
397 + " ===\n" + sBackedUp + "\n\n");
400 if ( !m_pData->aFailures.empty() )
402 lcl_describeErrors( aBuffer, m_pData->aFailures
403 , STR_ERRORS );
405 else
407 OUString sMovedLibTemplate( MacroMigrationResId( STR_MOVED_LIBRARY ) );
409 for ( DocumentLogs::const_iterator doc = m_pData->aDocumentLogs.begin();
410 doc != m_pData->aDocumentLogs.end();
411 ++doc
414 const DocumentEntry& rDoc( doc->second );
416 if ( rDoc.aMovedLibraries.empty() )
417 continue;
419 OUString sDocTitle( MacroMigrationResId( rDoc.eType == eForm ? STR_FORM : STR_REPORT ) );
420 sDocTitle = sDocTitle.replaceAll( "$name$", rDoc.sName );
422 aBuffer.append( "=== " + sDocTitle + " ===\n" );
424 for ( ::std::vector< LibraryEntry >::const_iterator lib = rDoc.aMovedLibraries.begin();
425 lib != rDoc.aMovedLibraries.end();
426 ++lib
429 OUString sMovedLib( sMovedLibTemplate );
430 sMovedLib = sMovedLib.replaceAll( "$type$", getScriptTypeDisplayName( lib->eType ) );
431 sMovedLib = sMovedLib.replaceAll( "$old$", lib->sOldName );
432 sMovedLib = sMovedLib.replaceAll( "$new$", lib->sNewName );
434 aBuffer.append( sMovedLib + "\n" );
437 aBuffer.append( '\n' );
441 if ( !m_pData->aWarnings.empty() )
443 lcl_describeErrors( aBuffer, m_pData->aWarnings, STR_WARNINGS );
446 return aBuffer.makeStringAndClear();
449 } // namespace dbmm
451 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */