Skip to content
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

fix issue #65 : delete new questions in the target + issue #63 : porb… #66

Merged
merged 3 commits into from
Oct 16, 2017
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 @@ -3,6 +3,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -65,6 +66,8 @@ public void execute(OperationContext operationContext, ResultHandler resultHandl

// "replace-existing" attribute. Defaults to false.
boolean replaceExisting = filters.contains("replace-existing:true");
boolean deleteNewCategories = filters.contains("delete-newcategories:true");
boolean deleteNewQuestions = filters.contains("delete-newquestions:true");

// "create-space" attribute. Defaults to false.
boolean createSpace = filters.contains("create-space:true");
Expand All @@ -80,7 +83,7 @@ public void execute(OperationContext operationContext, ResultHandler resultHandl
FileEntry activitiesFile = getAndRemoveFileByPath(fileEntries, ActivitiesExportTask.FILENAME);

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

boolean isSpaceFAQ = categoryId.contains(Utils.CATE_SPACE_ID_PREFIX);
Expand Down Expand Up @@ -111,14 +114,14 @@ public void execute(OperationContext operationContext, ResultHandler resultHandl
}

@SuppressWarnings("unchecked")
private void importAnswerData(File file, File spaceMetadataFile, boolean replaceExisting, boolean createSpace) throws Exception {
private void importAnswerData(File file, File spaceMetadataFile, boolean replaceExisting, boolean deleteNewCategories, boolean deleteNewQuestions, boolean createSpace) throws Exception {
List<Object> objects = (List<Object>) deserializeObject(file, null, null);
// object i : the category, object i+1 : the list of category questions
for (int i=0 ; i<objects.size(); i+=2) {
Category category = (Category) objects.get(i);
String parentId = category.getPath().replace("/" + category.getId(), "");
String parentPath = getParentPath(category);
List<Question> questions = (List<Question>) objects.get(i+1);
Category parentCategory = faqService.getCategoryById(parentId);
Category parentCategory = faqService.getCategoryById(parentPath);
if (parentCategory == null) {
log.warn("Parent Answer Category of Category '" + category.getName() + "' doesn't exist, ignore import operation for this category.");
return;
Expand All @@ -135,17 +138,22 @@ private void importAnswerData(File file, File spaceMetadataFile, boolean replace
if (toReplaceCategory != null) {
if (replaceExisting ) {
log.info("Overwrite existing FAQ Category: '" + toReplaceCategory.getName() + "' (replace-existing=true)");
deleteActivities(category.getId(), null);
removeCategoryQuestions(category, questions);
deleteActivities(toReplaceCategory.getId(), null);
if(deleteNewQuestions) {
List<Question> targetQuestions = faqService.getAllQuestionsByCatetory(toReplaceCategory.getId(), AnswerExtension.EMPTY_FAQ_SETTIGNS).getAll();
removeCategoryQuestions(toReplaceCategory, targetQuestions);
} else {
removeCategoryQuestions(toReplaceCategory, questions);
}
} else {
log.info("Ignore existing FAQ Category: '" + category.getName() + "' (replace-existing=false)");
return;
}
if(!category.getName().equals(toReplaceCategory.getName())){
toReplaceCategory.setName(category.getName());
faqService.saveCategory(parentPath,category,false);
}
} else {
faqService.saveCategory(parentId, category, true);
faqService.saveCategory(parentPath, category, true);
}

// FIXME Exception swallowed FORUM-971, so we have to make test
Expand All @@ -168,6 +176,9 @@ private void importAnswerData(File file, File spaceMetadataFile, boolean replace
}
}
}
if(deleteNewCategories){
removeNewCategories(objects,category);
}
deleteActivities(category.getId(), questions);
}
}
Expand Down Expand Up @@ -288,5 +299,37 @@ private void removeCategoryQuestions(Category category, List<Question> questions
log.error("Fail to remove questions of category: "+category.getName(),e);
}
}
private void removeNewCategories(List<Object> objectList,Category category){
try {
Category toReplaceCategory = faqService.getCategoryById(category.getId());
if(toReplaceCategory==null){
return;
}
List<Category> targetCategoriesList = faqService.getSubCategories(category.getPath(),AnswerExtension.EMPTY_FAQ_SETTIGNS,true,null);
if(targetCategoriesList==null || targetCategoriesList.size()==0){
return;
}
List<Category> sourceCategoriesList = new ArrayList<Category>();
String categoryPath = category.getPath();
for(int i=0; i<objectList.size(); i+=2){
Category sub_category = (Category) objectList.get(i);
if(getParentPath(sub_category).equals(categoryPath)){
sourceCategoriesList.add(sub_category);
}
}
for(Category sub_category : targetCategoriesList){
if(!sourceCategoriesList.contains(sub_category)){
log.info("Delete the category: '" + sub_category.getName() + "' because it's only found here (delete-newcategories=true)");
deleteActivities(sub_category.getId(), null);
faqService.removeCategory(sub_category.getPath());
}
}
} catch (Exception e) {
log.error("Error when trying to delete new categories",e);
}
}

private String getParentPath(Category category){
return category.getPath().replace("/" + category.getId(), "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@ public AnswerDataReadResource(boolean isSpaceType) {
private String getFullPath(Category category){
String fullPath= category.getName();
Category cat = getParenCategory(category,faqService);
while (!cat.getName().equals("categories")){
fullPath = cat.getName()+"//"+fullPath;
cat = getParenCategory(cat,faqService);
try {
String rootCategoryName = faqService.getCategoryById(Utils.CATEGORY_HOME).getName();
while (!cat.getName().equals(rootCategoryName)){
fullPath = cat.getName()+"//"+fullPath;
cat = getParenCategory(cat,faqService);
}
} catch (Exception e) {
log.error("Error when trying to get the full path of the category :"+category.getName(), e);
}
return fullPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@
<label class="checkbox uiCheckbox"><input class="option" type="checkbox" name="/answer/public_EXPORT_filter/with-subcategories"
ng-model="optionsModel['/answer/public_EXPORT_filter/with-subcategories']"/><span>With Sub categories</span></label>
</div>
<div ng-show="mode == 'import' || mode == 'synchronize'">
<label class="checkbox uiCheckbox"><input class="option" type="checkbox" name="/answer/public_IMPORT_filter/delete-newcategories"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

founded should be found, check it everywhere in the PR

ng-model="optionsModel['/answer/public_IMPORT_filter/delete-newcategories']"><span>Delete new categories found in the target</span></label>
</div>
<div ng-show="mode == 'import' || mode == 'synchronize'">
<label class="checkbox uiCheckbox"><input class="option" type="checkbox" name="/answer/public_IMPORT_filter/delete-newquestions"
ng-model="optionsModel['/answer/public_IMPORT_filter/delete-newquestions']"><span>Delete new questions found in the target</span></label>
</div>
<div class="selected-resource-table" ng-show="mode != 'import'">
<table class="table table-striped table-condensed">
<tr ng-show="!(resources['/answer/public'] | filter:{selected:true}).length">
Expand Down Expand Up @@ -211,6 +219,14 @@
<label class="checkbox uiCheckbox"><input class="option" type="checkbox" name="/answer/space_EXPORT_filter/with-subcategories"
ng-model="optionsModel['/answer/space_EXPORT_filter/with-subcategories']"/><span>With Sub categories</span></label>
</div>
<div ng-show="mode == 'export' || mode == 'synchronize'">
<label class="checkbox uiCheckbox"><input class="option" type="checkbox" name="/answer/space_IMPORT_filter/delete-newcategories"
ng-model="optionsModel['/answer/space_IMPORT_filter/delete-newcategories']"/><span>Delete new categories found in the target</span></label>
</div>
<div ng-show="mode == 'export' || mode == 'synchronize'">
<label class="checkbox uiCheckbox"><input class="option" type="checkbox" name="/answer/space_IMPORT_filter/delete-newquestions"
ng-model="optionsModel['/answer/space_IMPORT_filter/delete-newquestions']"/><span>Delete new questions found in the target</span></label>
</div>
<div class="selected-resource-table" ng-show="mode != 'import'">
<table class="table table-striped table-condensed">
<tr ng-show="!(resources['/answer/space'] | filter:{selected:true}).length">
Expand Down