Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "dbmm_module.hxx"
21 : #include "dbmm_global.hrc"
22 : #include "migrationerror.hxx"
23 : #include "migrationlog.hxx"
24 :
25 : #include <comphelper/anytostring.hxx>
26 : #include <rtl/ustrbuf.hxx>
27 :
28 : #include <vector>
29 : #include <map>
30 : #include <list>
31 : #include <algorithm>
32 :
33 : namespace dbmm
34 : {
35 : // LibraryEntry
36 0 : struct LibraryEntry
37 : {
38 : ScriptType eType;
39 : OUString sOldName;
40 : OUString sNewName;
41 :
42 0 : LibraryEntry( const ScriptType& _eType, const OUString& _rOldName, const OUString& _rNewName )
43 : :eType( _eType )
44 : ,sOldName( _rOldName )
45 0 : ,sNewName( _rNewName )
46 : {
47 0 : }
48 : };
49 :
50 : // DocumentEntry
51 0 : struct DocumentEntry
52 : {
53 : SubDocumentType eType;
54 : OUString sName;
55 : ::std::vector< LibraryEntry > aMovedLibraries;
56 :
57 0 : DocumentEntry()
58 : :eType( eForm )
59 : ,sName()
60 0 : ,aMovedLibraries()
61 : {
62 0 : }
63 :
64 0 : DocumentEntry( const SubDocumentType _eType, const OUString& _rName )
65 : :eType( _eType )
66 0 : ,sName( _rName )
67 : {
68 0 : }
69 : };
70 :
71 : // DocumentLogs
72 : typedef ::std::map< DocumentID, DocumentEntry > DocumentLogs;
73 :
74 : // ErrorLog
75 : typedef ::std::list< MigrationError > ErrorLog;
76 :
77 : // MigrationLog_Data
78 0 : struct MigrationLog_Data
79 : {
80 : OUString sBackupLocation;
81 : DocumentLogs aDocumentLogs;
82 : ErrorLog aFailures;
83 : ErrorLog aWarnings;
84 : };
85 :
86 : // MigrationLog
87 0 : MigrationLog::MigrationLog()
88 0 : :m_pData( new MigrationLog_Data )
89 : {
90 0 : }
91 :
92 0 : MigrationLog::~MigrationLog()
93 : {
94 0 : }
95 :
96 0 : void MigrationLog::logFailure( const MigrationError& _rError )
97 : {
98 0 : m_pData->aFailures.push_back( _rError );
99 0 : }
100 :
101 0 : void MigrationLog::logRecoverable( const MigrationError& _rError )
102 : {
103 0 : m_pData->aWarnings.push_back( _rError );
104 0 : }
105 :
106 0 : bool MigrationLog::hadFailure() const
107 : {
108 0 : return !m_pData->aFailures.empty();
109 : }
110 :
111 0 : void MigrationLog::backedUpDocument( const OUString& _rNewDocumentLocation )
112 : {
113 0 : m_pData->sBackupLocation = _rNewDocumentLocation;
114 0 : }
115 :
116 0 : DocumentID MigrationLog::startedDocument( const SubDocumentType _eType, const OUString& _rName )
117 : {
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
123 : )
124 : {
125 : bAlreadyKnown = ( doc->second.eType == _eType ) && ( doc->second.sName == _rName );
126 : }
127 : OSL_ENSURE( !bAlreadyKnown, "MigrationLog::startedDocument: document is already known!" );
128 : #endif
129 :
130 0 : DocumentID nID = (DocumentID)( m_pData->aDocumentLogs.size() + 1 );
131 0 : while ( m_pData->aDocumentLogs.find( nID ) != m_pData->aDocumentLogs.end() )
132 0 : ++nID;
133 :
134 0 : m_pData->aDocumentLogs[ nID ] = DocumentEntry( _eType, _rName );
135 :
136 0 : return nID;
137 : }
138 :
139 0 : void MigrationLog::movedLibrary( const DocumentID _nDocID, const ScriptType _eScriptType,
140 : const OUString& _rOriginalLibName, const OUString& _rNewLibName )
141 : {
142 : OSL_ENSURE( m_pData->aDocumentLogs.find( _nDocID ) != m_pData->aDocumentLogs.end(),
143 : "MigrationLog::movedLibrary: document is not known!" );
144 :
145 0 : DocumentEntry& rDocEntry = m_pData->aDocumentLogs[ _nDocID ];
146 0 : rDocEntry.aMovedLibraries.push_back( LibraryEntry( _eScriptType, _rOriginalLibName, _rNewLibName ) );
147 0 : }
148 :
149 0 : void MigrationLog::finishedDocument( const DocumentID _nDocID )
150 : {
151 : OSL_ENSURE( m_pData->aDocumentLogs.find( _nDocID ) != m_pData->aDocumentLogs.end(),
152 : "MigrationLog::finishedDocument: document is not known!" );
153 :
154 0 : DocumentEntry& rDocEntry = m_pData->aDocumentLogs[ _nDocID ];
155 : (void)rDocEntry;
156 : // nothing to do here
157 0 : }
158 :
159 0 : const OUString& MigrationLog::getNewLibraryName( DocumentID _nDocID, ScriptType _eScriptType,
160 : const OUString& _rOriginalLibName ) const
161 : {
162 0 : static OUString s_sEmptyString;
163 :
164 0 : DocumentLogs::const_iterator docPos = m_pData->aDocumentLogs.find( _nDocID );
165 0 : if ( docPos == m_pData->aDocumentLogs.end() )
166 : {
167 : OSL_FAIL( "MigrationLog::getNewLibraryName: document is not known!" );
168 0 : return s_sEmptyString;
169 : }
170 :
171 0 : const DocumentEntry& rDocEntry( docPos->second );
172 0 : for ( ::std::vector< LibraryEntry >::const_iterator lib = rDocEntry.aMovedLibraries.begin();
173 0 : lib != rDocEntry.aMovedLibraries.end();
174 : ++lib
175 : )
176 : {
177 0 : if ( ( _eScriptType == lib->eType )
178 0 : && ( _rOriginalLibName == lib->sOldName )
179 : )
180 0 : return lib->sNewName;
181 : }
182 :
183 : OSL_FAIL( "MigrationLog::getNewLibraryName: doc is known, but library isn't!" );
184 0 : return s_sEmptyString;
185 : }
186 :
187 : namespace
188 : {
189 0 : static void lcl_appendErrorDescription( OUStringBuffer& _inout_rBuffer, const MigrationError& _rError )
190 : {
191 0 : const sal_Char* pAsciiErrorDescription( NULL );
192 0 : ::std::vector< OUString > aParameterNames;
193 0 : switch ( _rError.eType )
194 : {
195 : case ERR_OPENING_SUB_DOCUMENT_FAILED:
196 0 : pAsciiErrorDescription = "opening '#doc#' failed";
197 0 : aParameterNames.push_back(OUString("#doc#"));
198 0 : break;
199 :
200 : case ERR_CLOSING_SUB_DOCUMENT_FAILED:
201 0 : pAsciiErrorDescription = "closing '#doc#' failed";
202 0 : aParameterNames.push_back(OUString("#doc#"));
203 0 : break;
204 :
205 : case ERR_STORAGE_COMMIT_FAILED:
206 0 : pAsciiErrorDescription = "committing the changes for document '#doc#' failed";
207 0 : aParameterNames.push_back(OUString("#doc#"));
208 0 : break;
209 :
210 : case ERR_STORING_DATABASEDOC_FAILED:
211 0 : pAsciiErrorDescription = "storing the database document failed";
212 0 : break;
213 :
214 : case ERR_COLLECTING_DOCUMENTS_FAILED:
215 0 : pAsciiErrorDescription = "collecting the forms/reports of the database document failed";
216 0 : break;
217 :
218 : case ERR_UNEXPECTED_LIBSTORAGE_ELEMENT:
219 0 : pAsciiErrorDescription = "unexpected #lib# storage element in document '#doc#', named '#element#'";
220 0 : aParameterNames.push_back(OUString("#doc#"));
221 0 : aParameterNames.push_back(OUString("#libstore#"));
222 0 : aParameterNames.push_back(OUString("#element#"));
223 0 : break;
224 :
225 : case ERR_CREATING_DBDOC_SCRIPT_STORAGE_FAILED:
226 0 : pAsciiErrorDescription = "creating the database document's storage for #scripttype# scripts failed";
227 0 : aParameterNames.push_back(OUString("#scripttype#"));
228 0 : break;
229 :
230 : case ERR_COMMITTING_SCRIPT_STORAGES_FAILED:
231 0 : pAsciiErrorDescription = "saving the #scripttype# scripts for document '#doc#' failed";
232 0 : aParameterNames.push_back(OUString("#scripttype#"));
233 0 : aParameterNames.push_back(OUString("#doc#"));
234 0 : break;
235 :
236 : case ERR_GENERAL_SCRIPT_MIGRATION_FAILURE:
237 0 : pAsciiErrorDescription = "general error while migrating #scripttype# scripts of document '#doc#'";
238 0 : aParameterNames.push_back(OUString("#scripttype#"));
239 0 : aParameterNames.push_back(OUString("#doc#"));
240 0 : break;
241 :
242 : case ERR_GENERAL_MACRO_MIGRATION_FAILURE:
243 0 : pAsciiErrorDescription = "general error during macro migration of document '#doc#'";
244 0 : aParameterNames.push_back(OUString("#doc#"));
245 0 : break;
246 :
247 : case ERR_UNKNOWN_SCRIPT_TYPE:
248 0 : pAsciiErrorDescription = "unknown script type: #type#";
249 0 : aParameterNames.push_back(OUString("#type#"));
250 0 : break;
251 :
252 : case ERR_UNKNOWN_SCRIPT_LANGUAGE:
253 0 : pAsciiErrorDescription = "unknown script language: #lang#";
254 0 : aParameterNames.push_back(OUString("#lang#"));
255 0 : break;
256 :
257 : case ERR_UNKNOWN_SCRIPT_NAME_FORMAT:
258 0 : pAsciiErrorDescription = "unknown script name format: #script#";
259 0 : aParameterNames.push_back(OUString("#script#"));
260 0 : break;
261 :
262 : case ERR_SCRIPT_TRANSLATION_FAILURE:
263 0 : pAsciiErrorDescription = "analyzing/translating the script URL failed; script type: #type#; script: #code#";
264 0 : aParameterNames.push_back(OUString("#type#"));
265 0 : aParameterNames.push_back(OUString("#code#"));
266 0 : break;
267 :
268 : case ERR_INVALID_SCRIPT_DESCRIPTOR_FORMAT:
269 0 : pAsciiErrorDescription = "invalid script descriptor format";
270 0 : break;
271 :
272 : case ERR_ADJUSTING_DOCUMENT_EVENTS_FAILED:
273 0 : pAsciiErrorDescription = "adjusting events for document '#doc#' failed";
274 0 : aParameterNames.push_back(OUString("#doc#"));
275 0 : break;
276 :
277 : case ERR_ADJUSTING_DIALOG_EVENTS_FAILED:
278 0 : pAsciiErrorDescription = "adjusting events for dialog #lib#.#dlg# in document '#doc#' failed";
279 0 : aParameterNames.push_back(OUString("#doc#"));
280 0 : aParameterNames.push_back(OUString("#lib#"));
281 0 : aParameterNames.push_back(OUString("#dlg#"));
282 0 : break;
283 :
284 : case ERR_ADJUSTING_FORMCOMP_EVENTS_FAILED:
285 0 : pAsciiErrorDescription = "adjusting form component events for '#doc#' failed";
286 0 : aParameterNames.push_back(OUString("#doc#"));
287 0 : break;
288 :
289 : case ERR_BIND_SCRIPT_STORAGE_FAILED:
290 0 : pAsciiErrorDescription = "binding to the script storage failed for document '#doc#'";
291 0 : aParameterNames.push_back(OUString("#doc#"));
292 0 : break;
293 :
294 : case ERR_REMOVE_SCRIPTS_STORAGE_FAILED:
295 0 : pAsciiErrorDescription = "removing a scripts storage failed for document '#doc#'";
296 0 : aParameterNames.push_back(OUString("#doc#"));
297 0 : break;
298 :
299 : case ERR_DOCUMENT_BACKUP_FAILED:
300 0 : pAsciiErrorDescription = "backing up the document to #location# failed";
301 0 : aParameterNames.push_back(OUString("#location#"));
302 0 : break;
303 :
304 : case ERR_UNKNOWN_SCRIPT_FOLDER:
305 0 : pAsciiErrorDescription = "unknown script folder '#name#' in document '#doc#'";
306 0 : aParameterNames.push_back(OUString("#doc#"));
307 0 : aParameterNames.push_back(OUString("#name#"));
308 0 : break;
309 :
310 : case ERR_EXAMINING_SCRIPTS_FOLDER_FAILED:
311 0 : pAsciiErrorDescription = "examining the 'Scripts' folder failed for document '#doc#'";
312 0 : aParameterNames.push_back(OUString("#doc#"));
313 0 : break;
314 :
315 : case ERR_PASSWORD_VERIFICATION_FAILED:
316 0 : pAsciiErrorDescription = "password verification failed for document '#doc#', #libtype# library '#name#'";
317 0 : aParameterNames.push_back(OUString("#doc#"));
318 0 : aParameterNames.push_back(OUString("#libtype#"));
319 0 : aParameterNames.push_back(OUString("#name#"));
320 0 : break;
321 :
322 : case ERR_NEW_STYLE_REPORT:
323 0 : pAsciiErrorDescription = "#doc# could not be processed, since you don't have the Oracle Report Builder (TM) extension installed.";
324 0 : aParameterNames.push_back(OUString("#doc#"));
325 0 : break;
326 :
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
329 : }
330 : OSL_ENSURE( pAsciiErrorDescription, "lcl_appendErrorDescription: no error message!" );
331 0 : if ( pAsciiErrorDescription )
332 : {
333 0 : OUString sSubstituted( OUString::createFromAscii( pAsciiErrorDescription ) );
334 : OSL_ENSURE( aParameterNames.size() == _rError.aErrorDetails.size(),
335 : "lcl_appendErrorDescription: unexpected number of error message parameters!" );
336 :
337 0 : for ( size_t i=0; i < ::std::min( aParameterNames.size(), _rError.aErrorDetails.size() ); ++i )
338 : {
339 0 : sSubstituted = sSubstituted.replaceFirst(
340 0 : aParameterNames[i], _rError.aErrorDetails[i]);
341 : }
342 :
343 0 : _inout_rBuffer.append( sSubstituted );
344 0 : }
345 0 : }
346 :
347 0 : void lcl_describeErrors( OUStringBuffer& _rBuffer, const ErrorLog& _rErrors, const sal_uInt16 _nHeadingResId )
348 : {
349 0 : _rBuffer.appendAscii( "=== " );
350 0 : _rBuffer.append ( OUString( MacroMigrationResId( _nHeadingResId ) ) );
351 0 : _rBuffer.appendAscii( " ===\n" );
352 :
353 0 : OUString sException( MacroMigrationResId( STR_EXCEPTION ) );
354 :
355 0 : for ( ErrorLog::const_iterator error = _rErrors.begin();
356 0 : error != _rErrors.end();
357 : ++error
358 : )
359 : {
360 0 : _rBuffer.append( '-' );
361 0 : _rBuffer.append( ' ' );
362 0 : lcl_appendErrorDescription( _rBuffer, *error );
363 0 : _rBuffer.append( '\n' );
364 :
365 0 : if ( !error->aCaughtException.hasValue() )
366 0 : continue;
367 :
368 0 : _rBuffer.append( sException );
369 0 : _rBuffer.append( ::comphelper::anyToString( error->aCaughtException ) );
370 0 : _rBuffer.append( '\n' );
371 0 : _rBuffer.append( '\n' );
372 0 : }
373 0 : }
374 : }
375 :
376 0 : bool MigrationLog::movedAnyLibrary( const DocumentID _nDocID )
377 : {
378 0 : DocumentLogs::const_iterator docPos = m_pData->aDocumentLogs.find( _nDocID );
379 0 : if ( docPos == m_pData->aDocumentLogs.end() )
380 : {
381 : OSL_FAIL( "MigrationLog::movedAnyLibrary: document is not known!" );
382 0 : return false;
383 : }
384 0 : return !docPos->second.aMovedLibraries.empty();
385 : }
386 :
387 0 : OUString MigrationLog::getCompleteLog() const
388 : {
389 0 : OUStringBuffer aBuffer;
390 :
391 0 : if ( !m_pData->sBackupLocation.isEmpty() )
392 : {
393 0 : OUString sBackedUp( MacroMigrationResId( STR_SAVED_COPY_TO ) );
394 0 : sBackedUp = sBackedUp.replaceAll( "$location$", m_pData->sBackupLocation );
395 :
396 0 : aBuffer.append( "=== " + OUString( MacroMigrationResId( STR_DATABASE_DOCUMENT ) )
397 0 : + " ===\n" + sBackedUp + "\n\n");
398 : }
399 :
400 0 : if ( !m_pData->aFailures.empty() )
401 : {
402 0 : lcl_describeErrors( aBuffer, m_pData->aFailures
403 0 : , STR_ERRORS );
404 : }
405 : else
406 : {
407 0 : OUString sMovedLibTemplate( MacroMigrationResId( STR_MOVED_LIBRARY ) );
408 :
409 0 : for ( DocumentLogs::const_iterator doc = m_pData->aDocumentLogs.begin();
410 0 : doc != m_pData->aDocumentLogs.end();
411 : ++doc
412 : )
413 : {
414 0 : const DocumentEntry& rDoc( doc->second );
415 :
416 0 : if ( rDoc.aMovedLibraries.empty() )
417 0 : continue;
418 :
419 0 : OUString sDocTitle( MacroMigrationResId( rDoc.eType == eForm ? STR_FORM : STR_REPORT ) );
420 0 : sDocTitle = sDocTitle.replaceAll( "$name$", rDoc.sName );
421 :
422 0 : aBuffer.append( "=== " + sDocTitle + " ===\n" );
423 :
424 0 : for ( ::std::vector< LibraryEntry >::const_iterator lib = rDoc.aMovedLibraries.begin();
425 0 : lib != rDoc.aMovedLibraries.end();
426 : ++lib
427 : )
428 : {
429 0 : OUString sMovedLib( sMovedLibTemplate );
430 0 : sMovedLib = sMovedLib.replaceAll( "$type$", getScriptTypeDisplayName( lib->eType ) );
431 0 : sMovedLib = sMovedLib.replaceAll( "$old$", lib->sOldName );
432 0 : sMovedLib = sMovedLib.replaceAll( "$new$", lib->sNewName );
433 :
434 0 : aBuffer.append( sMovedLib + "\n" );
435 0 : }
436 :
437 0 : aBuffer.append( '\n' );
438 0 : }
439 : }
440 :
441 0 : if ( !m_pData->aWarnings.empty() )
442 : {
443 0 : lcl_describeErrors( aBuffer, m_pData->aWarnings, STR_WARNINGS );
444 : }
445 :
446 0 : return aBuffer.makeStringAndClear();
447 : }
448 :
449 : } // namespace dbmm
450 :
451 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|