Skip to content

fixing into the migration wysiwyg adjustment a potential wysiwyg loss due to an unexpected wysiwyg management behavior (the copy of a publication for example) #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

class WysiwygDocumentMerger implements Callable<Long> {
Expand Down Expand Up @@ -79,20 +81,42 @@ private Long mergeWysiwyg() {
for (String basename : basenames) {
console.printMessage("Adjusting wysiwyg for basename " + basename);
// Getting documents ordered by their names
List<SimpleDocument> documents = service.listWysiwygForBasename(basename, componentId);
if (!documents.isEmpty()) {
if (documents.size() > 1) {
console.printMessage(
"There is " + documents.size() + " documents to handle for basename " + basename);
SimpleDocument merge = getMergeDocument(documents, basename);
documents.remove(merge);
if (mergeDocuments(merge, documents)) {
nbAdjustedDocuments++;
List<SimpleDocument> documentsStartingWithBasename =
service.listWysiwygForBasename(basename, componentId);
if (!documentsStartingWithBasename.isEmpty()) {

// Splitting the previous list by foreignKey
Map<String, List<SimpleDocument>> documentListByForeignKey =
new HashMap<String, List<SimpleDocument>>();
for (SimpleDocument document : documentsStartingWithBasename) {
String foreignKey = document.getForeignId();
List<SimpleDocument> foreignKeyDocuments = documentListByForeignKey.get(foreignKey);
if (foreignKeyDocuments == null) {
foreignKeyDocuments = new ArrayList<SimpleDocument>();
documentListByForeignKey.put(foreignKey, foreignKeyDocuments);
}
} else {
console.printMessage("There is 1 document to handle for basename " + basename);
if (copyIntoRightLanguageLocation(documents.get(0))) {
nbAdjustedDocuments++;
foreignKeyDocuments.add(document);
}

// Process each list of documents by unique foreignKey
for (Map.Entry<String, List<SimpleDocument>> entry : documentListByForeignKey.entrySet()) {
List<SimpleDocument> documents = entry.getValue();
if (documents.size() > 1) {
console.printMessage(
"There is " + documents.size() + " documents to handle for basename " + basename +
" and foreignId " + entry.getKey());
SimpleDocument merge = getMergeDocument(documents, basename);
documents.remove(merge);
if (mergeDocuments(merge, documents)) {
nbAdjustedDocuments++;
}
} else {
console.printMessage(
"There is 1 document to handle for basename " + basename + " and foreignId " +
entry.getKey());
if (copyIntoRightLanguageLocation(documents.get(0))) {
nbAdjustedDocuments++;
}
}
}
}
Expand Down Expand Up @@ -146,7 +170,9 @@ public int compare(final SimpleDocument sd1, final SimpleDocument sd2) {
if (basename.equals(FilenameUtils.getBaseName(document.getFilename()))) {
return document;
}
if (theFirstWhichNameHasDefaultLanguageSuffix == null) {
if (theFirstWhichNameHasDefaultLanguageSuffix == null ||
!theFirstWhichNameHasDefaultLanguageSuffix.getLanguage()
.equals(ConverterUtil.defaultLanguage)) {
theFirstWhichNameHasDefaultLanguageSuffix = document;
}
}
Expand All @@ -166,7 +192,8 @@ private boolean copyIntoRightLanguageLocation(SimpleDocument document) {
String languageFromFilename =
ConverterUtil.checkLanguage(ConverterUtil.extractLanguage(document.getFilename()));
// Adjusting location of content that is in other language than this of default language
if (!languageFromFilename.equals(document.getLanguage())) {
if (document.getLanguage().equals(ConverterUtil.defaultLanguage) &&
!languageFromFilename.equals(document.getLanguage())) {
String documentLanguageBeforeAdjustment = document.getLanguage();
File contentToCopy = new File(document.getAttachmentPath());
if (contentToCopy.exists() && contentToCopy.isFile()) {
Expand Down Expand Up @@ -203,7 +230,8 @@ private boolean renameWithRightLanguageSuffix(SimpleDocument documentToRename) {
String languageFromFilename =
ConverterUtil.checkLanguage(ConverterUtil.extractLanguage(documentToRename.getFilename()));
// Renaming document that is in other language than this of default language
if (!languageFromFilename.equals(documentToRename.getLanguage())) {
if (documentToRename.getLanguage().equals(ConverterUtil.defaultLanguage) &&
!languageFromFilename.equals(documentToRename.getLanguage())) {
File contentToRename = new File(documentToRename.getAttachmentPath());
if (contentToRename.exists() && contentToRename.isFile()) {
documentToRename.setFilename(ConverterUtil
Expand Down Expand Up @@ -238,7 +266,8 @@ private boolean mergeDocuments(SimpleDocument document, List<SimpleDocument> doc
List<SimpleDocument> documentsToDelete = new ArrayList<SimpleDocument>();
for (SimpleDocument documentToMerge : documents) {
String lang = ConverterUtil.extractLanguage(documentToMerge.getFilename());
if (!StringUtil.isDefined(lang)) {
if (!StringUtil.isDefined(lang) ||
!documentToMerge.getLanguage().equals(ConverterUtil.defaultLanguage)) {
lang = documentToMerge.getLanguage();
}
documentTarget.setAttachment(
Expand Down Expand Up @@ -322,13 +351,18 @@ private boolean mergeDocuments(SimpleDocument document, List<SimpleDocument> doc
}
}
for (SimpleDocument documentToDelete : documentsToDelete) {
try {
service.deleteAttachment(documentToDelete);
adjustmentDone = true;
console.printMessage("Node " + documentToDelete.getNodeName() + " has been deleted");
} catch (Exception exc) {
// In case the node was already deleted, an exception is thrown...
// So it is catched here.
if (documentTarget.getNodeName().equals(documentToDelete.getNodeName())) {
console.printWarning("Node " + documentToDelete.getNodeName() +
" has finally not been deleted (because it was the selected target)");
} else {
try {
service.deleteAttachment(documentToDelete);
adjustmentDone = true;
console.printMessage("Node " + documentToDelete.getNodeName() + " has been deleted");
} catch (Exception exc) {
// In case the node was already deleted, an exception is thrown...
// So it is catched here.
}
}
}
return adjustmentDone;
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/org/silverpeas/util/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,19 @@
*/
package org.silverpeas.util;

import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
import org.silverpeas.dbbuilder.util.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.silverpeas.dbbuilder.util.Configuration;

/**
* Console into which messages are displayed. It wraps the source into which messages are printed
* out.
Expand Down Expand Up @@ -107,16 +106,17 @@ public synchronized void printTrace(String msg) {
}

private synchronized void printMessage(String msg, boolean isError) {
String message = "Thread " + Thread.currentThread().getId() + " - " + (msg != null ? msg : "");
if (null != logBuffer) {
logBuffer.print(msg);
logBuffer.print(message);
}
if (echoAsDotEnabled) {
System.out.print(".");
} else {
System.out.println(newline + msg + newline);
System.out.println(newline + message + newline);
}
if (!isError) {
logger.info(msg);
logger.info(message);
}
}

Expand Down