Skip to content

Commit

Permalink
fix issue #52 : Various issues in staging-extension-2.3.15
Browse files Browse the repository at this point in the history
  • Loading branch information
anoissi committed Jan 2, 2017
1 parent 8ab1420 commit a7f53e0
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@

import java.util.ArrayList;
import java.util.List;
import javax.jcr.Node;

import org.apache.poi.util.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.exoplatform.faq.service.Category;
import org.exoplatform.faq.service.FAQService;
import org.exoplatform.faq.service.FileAttachment;
import org.exoplatform.faq.service.Question;
import org.exoplatform.faq.service.QuestionPageList;
import org.exoplatform.faq.service.Utils;
import org.exoplatform.management.answer.AnswerExtension;
import org.exoplatform.management.common.InputStreamWrapper;
import org.exoplatform.management.common.exportop.AbstractExportOperationHandler;
import org.exoplatform.management.common.exportop.AbstractJCRExportOperationHandler;
import org.exoplatform.management.common.exportop.ActivityExportOperationInterface;
import org.exoplatform.management.common.exportop.JCRNodeExportTask;
import org.exoplatform.management.common.exportop.SpaceMetadataExportTask;
import org.exoplatform.portal.config.UserACL;
import org.exoplatform.social.core.activity.model.ExoSocialActivity;
Expand All @@ -53,7 +52,7 @@
* @author <a href="mailto:bkhanfir@exoplatform.com">Boubaker Khanfir</a>
* @version $Revision$
*/
public class AnswerDataExportResource extends AbstractExportOperationHandler implements ActivityExportOperationInterface {
public class AnswerDataExportResource extends AbstractJCRExportOperationHandler implements ActivityExportOperationInterface {

final private static Logger log = LoggerFactory.getLogger(AnswerDataExportResource.class);

Expand All @@ -79,15 +78,14 @@ public void execute(OperationContext operationContext, ResultHandler resultHandl
identityManager = operationContext.getRuntimeContext().getRuntimeComponent(IdentityManager.class);
identityStorage = operationContext.getRuntimeContext().getRuntimeComponent(IdentityStorage.class);

String name = operationContext.getAttributes().getValue("filter");

List<String> names = operationContext.getAttributes().getValues("filter");
String excludeSpaceMetadataString = operationContext.getAttributes().getValue("exclude-space-metadata");
boolean exportSpaceMetadata = excludeSpaceMetadataString == null || excludeSpaceMetadataString.trim().equalsIgnoreCase("false");

List<ExportTask> exportTasks = new ArrayList<ExportTask>();

try {
if (name == null || name.isEmpty()) {
if (names == null || names.isEmpty()) {
log.info("Exporting all FAQ of type: " + (isSpaceType ? "Spaces" : "Public"));
List<Category> categories = faqService.getAllCategories();
for (Category category : categories) {
Expand All @@ -101,28 +99,30 @@ public void execute(OperationContext operationContext, ResultHandler resultHandl
exportAnswer(exportTasks, category, space, exportSpaceMetadata);
}
} else {
if (isSpaceType) {
Space space = spaceService.getSpaceByDisplayName(name);
String groupName = space.getGroupId().replace(SpaceUtils.SPACE_GROUP + "/", "");

Category category = faqService.getCategoryById(Utils.CATE_SPACE_ID_PREFIX + groupName);
if (category != null) {
exportAnswer(exportTasks, category, space, exportSpaceMetadata);
} else {
log.info("Cannot find Answer Category of Space: " + space.getDisplayName());
}
} else {
if (name.equals(AnswerExtension.ROOT_CATEGORY)) {
Category defaultCategory = faqService.getCategoryById(Utils.CATEGORY_HOME);
// Export questions from root category
exportAnswer(exportTasks, defaultCategory, null, exportSpaceMetadata);
for (String name : names) {
if (isSpaceType) {
Space space = spaceService.getSpaceByDisplayName(name);
String groupName = space.getGroupId().replace(SpaceUtils.SPACE_GROUP + "/", "");

Category category = faqService.getCategoryById(Utils.CATE_SPACE_ID_PREFIX + groupName);
if (category != null) {
exportAnswer(exportTasks, category, space, exportSpaceMetadata);
} else {
log.info("Cannot find Answer Category of Space: " + space.getDisplayName());
}
} else {
List<Category> categories = faqService.getAllCategories();
for (Category category : categories) {
if (!category.getName().equals(name)) {
continue;
if (name.equals(AnswerExtension.ROOT_CATEGORY)) {
Category defaultCategory = faqService.getCategoryById(Utils.CATEGORY_HOME);
// Export questions from root category
exportAnswer(exportTasks, defaultCategory, null, exportSpaceMetadata);
} else {
List<Category> categories = faqService.getAllCategories();
for (Category category : categories) {
if (!category.getName().equals(name)) {
continue;
}
exportAnswer(exportTasks, category, null, exportSpaceMetadata);
}
exportAnswer(exportTasks, category, null, exportSpaceMetadata);
}
}
}
Expand All @@ -134,22 +134,15 @@ public void execute(OperationContext operationContext, ResultHandler resultHandl
}

private void exportAnswer(List<ExportTask> exportTasks, Category category, Space space, boolean exportSpaceMetadata) throws Exception {
QuestionPageList questionsPageList = faqService.getAllQuestionsByCatetory(category.getId(), AnswerExtension.EMPTY_FAQ_SETTIGNS);
List<Question> questions = questionsPageList.getAll();
for (Question question : questions) {
if (question.getAttachMent() != null && !question.getAttachMent().isEmpty()) {
List<FileAttachment> attachments = question.getAttachMent();
for (FileAttachment fileAttachment : attachments) {
InputStreamWrapper inputStream = new InputStreamWrapper(IOUtils.toByteArray(fileAttachment.getInputStream()));
fileAttachment.setInputStream(inputStream);
}
}
}
exportTasks.add(new AnswerExportTask(type, category, questions));
// Export category
exportTasks.add(new AnswerExportTask(type, category));
// Export category metadata
exportTasks.add(new CategoryMetaDataExportTask(category, type));

categoryThreadLocal.set(category);
// In case of minimal profile
if (activityManager != null) {
// Export activities metadata
String prefix = "answer/" + type + "/" + category.getId() + "/";
exportActivities(exportTasks, space == null ? ((category.getModerators() == null || category.getModerators().length == 0) ? userACL.getSuperUser() : category.getModerators()[0])
: space.getPrettyName(), prefix, ANSWER_ACTIVITY_TYPE);
Expand All @@ -158,7 +151,7 @@ private void exportAnswer(List<ExportTask> exportTasks, Category category, Space
if (space == null) {
log.warn("Should export space DATA but it is null");
} else {
prefix = "answer/space/" + category.getId() + "/";
// Export space metadata
exportTasks.add(new SpaceMetadataExportTask(space, prefix));
}
}
Expand Down Expand Up @@ -204,4 +197,15 @@ public boolean isActivityValid(ExoSocialActivity activity) throws Exception {
return categoryThreadLocal.get().getId().equals(question.getCategoryId());
}
}

@Override
protected void addJCRNodeExportTask(Node childNode, List<ExportTask> subNodesExportTask, boolean recursive, String... params) {
if (params.length != 4) {
log.warn("Cannot add Answer Export Task, 4 parameters was expected, got: " + ArrayUtils.toString(params));
return;
}
String entryPath = "answer/" + type + "/" + (params[2] == null || params[2].isEmpty() ? params[1] : params[2]);
JCRNodeExportTask exportTask = new JCRNodeExportTask(repositoryService, params[0], params[3], entryPath, recursive, true);
subNodesExportTask.add(exportTask);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.exoplatform.management.answer.operations;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -76,12 +78,12 @@ public void execute(OperationContext operationContext, ResultHandler resultHandl

for (String categoryId : contentsByOwner.keySet()) {
List<FileEntry> fileEntries = contentsByOwner.get(categoryId);
FileEntry spaceMetadataFile = getAndRemoveFileByPath(fileEntries, SpaceMetadataExportTask.FILENAME);
FileEntry activitiesFile = getAndRemoveFileByPath(fileEntries, ActivitiesExportTask.FILENAME);
FileEntry spaceMetadataFile = getAndRemoveFileByPathIfAnswer(fileEntries, categoryId + "/" + SpaceMetadataExportTask.FILENAME);
FileEntry activitiesFile = getAndRemoveFileByPathIfAnswer(fileEntries, categoryId + "/" + ActivitiesExportTask.FILENAME);
FileEntry categoryMetadata = getAndRemoveFileByPathIfAnswer(fileEntries, categoryId + CategoryMetaDataExportTask.FILENAME);
FileEntry categoryContent = getAndRemoveFileByPathIfAnswer(fileEntries, categoryId + AnswerExportTask.FILENAME);

for (FileEntry fileEntry : fileEntries) {
importAnswerData(fileEntry.getFile(), spaceMetadataFile == null ? null : spaceMetadataFile.getFile(), replaceExisting, createSpace);
}
importAnswerData(categoryContent.getFile(), spaceMetadataFile == null ? null : spaceMetadataFile.getFile(), categoryMetadata.getFile(), replaceExisting, createSpace);

boolean isSpaceFAQ = categoryId.contains(Utils.CATE_SPACE_ID_PREFIX);
if (isSpaceFAQ) {
Expand Down Expand Up @@ -111,11 +113,10 @@ public void execute(OperationContext operationContext, ResultHandler resultHandl
}

@SuppressWarnings("unchecked")
private void importAnswerData(File file, File spaceMetadataFile, boolean replaceExisting, boolean createSpace) throws Exception {
List<Object> objects = (List<Object>) deserializeObject(file, null, null);
Category category = (Category) objects.get(0);
private void importAnswerData(File file, File spaceMetadataFile, File CategoryMetadataFile, boolean replaceExisting, boolean createSpace) throws Exception {
InputStream inputStream = new FileInputStream(file);
Category category = deserializeObject(CategoryMetadataFile, null, null);
String parentId = category.getPath().replace("/" + category.getId(), "");
List<Question> questions = (List<Question>) objects.get(1);
Category parentCategory = faqService.getCategoryById(parentId);
if (parentCategory == null) {
log.warn("Parent Answer Category of Category '" + category.getName() + "' doesn't exist, ignore import operation for this category.");
Expand All @@ -131,7 +132,7 @@ private void importAnswerData(File file, File spaceMetadataFile, boolean replace

Category toReplaceCategory = faqService.getCategoryById(category.getId());
if (toReplaceCategory != null) {
if (replaceExisting) {
if (replaceExisting && category.getName() != AnswerExtension.ROOT_CATEGORY) {
log.info("Overwrite existing FAQ Category: '" + toReplaceCategory.getName() + "' (replace-existing=true)");
deleteActivities(category.getId(), null);
faqService.removeCategory(category.getPath());
Expand All @@ -145,30 +146,13 @@ private void importAnswerData(File file, File spaceMetadataFile, boolean replace
return;
}
}

faqService.saveCategory(parentId, category, true);
// Import JCR data
faqService.importData(parentId, inputStream, false);

// FIXME Exception swallowed FORUM-971, so we have to make test
if (faqService.getCategoryById(category.getId()) == null) {
throw new RuntimeException("Category isn't imported");
}

for (Question question : questions) {
faqService.saveQuestion(question, true, AnswerExtension.EMPTY_FAQ_SETTIGNS);
if (question.getAnswers() != null) {
for (Answer answer : question.getAnswers()) {
answer.setNew(true);
faqService.saveAnswer(question.getPath(), answer, true);
}
}
if (question.getComments() != null) {
for (Comment comment : question.getComments()) {
comment.setNew(true);
faqService.saveComment(question.getPath(), comment, question.getLanguage());
}
}
}
deleteActivities(category.getId(), questions);
}

private void deleteActivities(String categoryId, List<Question> questions) throws Exception {
Expand All @@ -188,18 +172,11 @@ public String getManagedFilesPrefix() {

@Override
public boolean isUnKnownFileFormat(String filePath) {
return !filePath.endsWith(AnswerExportTask.FILENAME) && !filePath.endsWith(SpaceMetadataExportTask.FILENAME) && !filePath.endsWith(ActivitiesExportTask.FILENAME);
return !filePath.endsWith(AnswerExportTask.FILENAME) && !filePath.endsWith(SpaceMetadataExportTask.FILENAME) && !filePath.endsWith(ActivitiesExportTask.FILENAME) && !filePath.endsWith(CategoryMetaDataExportTask.FILENAME);
}

@Override
public boolean addSpecialFile(List<FileEntry> fileEntries, String filePath, File file) {
if (filePath.endsWith(SpaceMetadataExportTask.FILENAME)) {
fileEntries.add(new FileEntry(SpaceMetadataExportTask.FILENAME, file));
return true;
} else if (filePath.endsWith(ActivitiesExportTask.FILENAME)) {
fileEntries.add(new FileEntry(ActivitiesExportTask.FILENAME, file));
return true;
}
return false;
}

Expand Down Expand Up @@ -274,4 +251,24 @@ public boolean isActivityNotValid(ExoSocialActivity activity, ExoSocialActivity
return false;
}

public final static FileEntry getAndRemoveFileByPathIfAnswer(List<FileEntry> fileEntries, String nodePath) {
Iterator<FileEntry> iterator = fileEntries.iterator();
while (iterator.hasNext()) {
FileEntry fileEntry = (FileEntry) iterator.next();
String tmpNodePath = fileEntry.getNodePath();
String correctNodePath = "";
if (tmpNodePath.contains(AnswerExtension.PUBLIC_FAQ_TYPE)) {
correctNodePath = tmpNodePath.replace("answer/" + AnswerExtension.PUBLIC_FAQ_TYPE + "/", "");
} else if (tmpNodePath.contains(AnswerExtension.FAQ_TEMPLATE)) {
correctNodePath = tmpNodePath.replace("answer/" + AnswerExtension.FAQ_TEMPLATE + "/", "");
} else if (tmpNodePath.contains(AnswerExtension.SPACE_FAQ_TYPE)) {
correctNodePath = tmpNodePath.replace("answer/" + AnswerExtension.SPACE_FAQ_TYPE + "/", "");
}
if (correctNodePath.equals(nodePath)) {
iterator.remove();
return fileEntry;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,33 @@ public void execute(OperationContext operationContext, ResultHandler resultHandl
try {
List<Category> categories = faqService.getAllCategories();
for (Category category : categories) {
if ((isSpaceType && !category.getId().startsWith(Utils.CATE_SPACE_ID_PREFIX)) || (!isSpaceType && category.getId().startsWith(Utils.CATE_SPACE_ID_PREFIX))) {
continue;
}
String name = category.getName();
if (isSpaceType) {
String spaceGroupId = SpaceUtils.SPACE_GROUP + "/" + category.getId().replace(Utils.CATE_SPACE_ID_PREFIX, "");
Space space = spaceService.getSpaceByGroupId(spaceGroupId);
if (space == null) {
if (isParent(category)) {
if ((isSpaceType && !category.getId().startsWith(Utils.CATE_SPACE_ID_PREFIX)) || (!isSpaceType && category.getId().startsWith(Utils.CATE_SPACE_ID_PREFIX))) {
continue;
}
name = space.getDisplayName();
String name = category.getName();
if (isSpaceType) {
String spaceGroupId = SpaceUtils.SPACE_GROUP + "/" + category.getId().replace(Utils.CATE_SPACE_ID_PREFIX, "");
Space space = spaceService.getSpaceByGroupId(spaceGroupId);
if (space == null) {
continue;
}
name = space.getDisplayName();
}
children.add(name);
}
children.add(name);
}
if (!isSpaceType) {
children.add(AnswerExtension.ROOT_CATEGORY);
}
} catch (Exception e) {
log.error("Error while listing FAQ categories.", e);
}
resultHandler.completed(new ReadResourceModel("All FAQ:", children));
}

public boolean isParent(Category category) {
String isParent = category.getPath().replace("/" + category.getId(), "");
if (isParent.equals("categories")) {
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,30 @@

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.Session;

import org.chromattic.common.collection.Collections;
import org.exoplatform.faq.service.Answer;
import org.exoplatform.container.PortalContainer;
import org.exoplatform.faq.service.Category;
import org.exoplatform.faq.service.Question;
import org.gatein.common.logging.Logger;
import org.gatein.common.logging.LoggerFactory;
import org.exoplatform.faq.service.FAQService;
import org.gatein.management.api.operation.model.ExportTask;

import com.thoughtworks.xstream.XStream;

/**
* @author <a href="mailto:bkhanfir@exoplatform.com">Boubaker Khanfir</a>
* @version $Revision$
*/
public class AnswerExportTask implements ExportTask {
public static final String FILENAME = "/category.xml";
final private static Logger log = LoggerFactory.getLogger(AnswerDataImportResource.class);

private final String type;
private final Category category;
private final List<Question> questions;

public AnswerExportTask(String type, Category category, List<Question> questions) {
public AnswerExportTask(String type, Category category) {
this.category = category;
this.type = type;
this.questions = questions;
}

@Override
Expand All @@ -57,19 +55,13 @@ public static String getEntryPath(String type, String id) {

@Override
public void export(OutputStream outputStream) throws IOException {
XStream xStream = new XStream();
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");

for (Question question : questions) {
for (Answer answer : question.getAnswers()) {
if (answer.getLanguage() == null) {
answer.setLanguage(question.getLanguage());
}
}
FAQService faqService = (FAQService) PortalContainer.getInstance().getComponentInstanceOfType(FAQService.class);
try {
Node categoryNode = faqService.getCategoryNodeById(category.getId());
Session session = categoryNode.getSession();
session.exportSystemView(categoryNode.getPath(), outputStream, false, false);
} catch (Exception e) {
log.error("Fail to export data:", e);
}

List<Object> objects = Collections.list(category, questions);
xStream.toXML(objects, writer);
writer.flush();
}
}
Loading

0 comments on commit a7f53e0

Please sign in to comment.