From a121efe40a2afc395ebf2b0d17dc4bf46a91089c Mon Sep 17 00:00:00 2001 From: tosmers Date: Wed, 5 Jul 2017 17:33:37 +0000 Subject: [PATCH] [TRUNK][FEATURE] - sorts the categories to guarantee, that the parents will be listed before their childs in the export git-svn-id: https://svn.libreccm.org/ccm/trunk@4842 8810af33-2d31-482b-a856-94f89814c4df --- .../arsdigita/portation/cmd/ExportHelper.java | 43 ++++++++-------- .../portation/conversion/MainConverter.java | 7 ++- .../portation/conversion/NgCollection.java | 50 +++++++++++++++++-- 3 files changed, 74 insertions(+), 26 deletions(-) diff --git a/ccm-core/src/com/arsdigita/portation/cmd/ExportHelper.java b/ccm-core/src/com/arsdigita/portation/cmd/ExportHelper.java index 4ac0b0556..ca5b03131 100644 --- a/ccm-core/src/com/arsdigita/portation/cmd/ExportHelper.java +++ b/ccm-core/src/com/arsdigita/portation/cmd/ExportHelper.java @@ -51,28 +51,6 @@ class ExportHelper { pathName = path; } - static void exportCategories() { - System.out.printf("\tExporting categories..."); - CategoryMarshaller categoryMarshaller = new - CategoryMarshaller(); - categoryMarshaller.prepare(Format.XML, pathName, - "categories", indentation); - categoryMarshaller.exportList(new ArrayList<>( - NgCollection.categories.values())); - System.out.printf("\t\tdone.\n"); - } - - static void exportCategorizations() { - System.out.printf("\tExporting categorizations..."); - CategorizationMarshaller categorizationMarshaller = new - CategorizationMarshaller(); - categorizationMarshaller.prepare(Format.XML, pathName, - "categorizations", indentation); - categorizationMarshaller.exportList(new ArrayList<>( - NgCollection.categorizations.values())); - System.out.printf("\tdone.\n"); - } - static void exportUsers() { System.out.printf("\tExporting users..."); UserMarshaller userMarshaller = new UserMarshaller(); @@ -125,6 +103,27 @@ class ExportHelper { System.out.printf("\tdone.\n"); } + static void exportCategories() { + System.out.printf("\tExporting categories..."); + CategoryMarshaller categoryMarshaller = new + CategoryMarshaller(); + categoryMarshaller.prepare(Format.XML, pathName, + "categories", indentation); + categoryMarshaller.exportList(NgCollection.getSortedCategories()); + System.out.printf("\t\tdone.\n"); + } + + static void exportCategorizations() { + System.out.printf("\tExporting categorizations..."); + CategorizationMarshaller categorizationMarshaller = new + CategorizationMarshaller(); + categorizationMarshaller.prepare(Format.XML, pathName, + "categorizations", indentation); + categorizationMarshaller.exportList(new ArrayList<>( + NgCollection.categorizations.values())); + System.out.printf("\tdone.\n"); + } + static void exportWorkflowTemplates() { System.out.printf("\tExporting workflow templates..."); WorkflowTemplateMarshaller workflowTemplateMarshaller = new diff --git a/ccm-core/src/com/arsdigita/portation/conversion/MainConverter.java b/ccm-core/src/com/arsdigita/portation/conversion/MainConverter.java index 5e5c15db3..04cf81705 100644 --- a/ccm-core/src/com/arsdigita/portation/conversion/MainConverter.java +++ b/ccm-core/src/com/arsdigita/portation/conversion/MainConverter.java @@ -45,12 +45,17 @@ public class MainConverter { * already been created. */ public static void startConversionToNg() { - CategoryConversion.convertAll(); UserConversion.convertAll(); GroupConversion.convertAll(); + RoleConversion.convertAll(); + + CategoryConversion.convertAll(); + System.out.println(NgCollection.categories.toString()); + WorkflowConversion.convertAll(); AssignableTaskConversion.convertAll(); + PermissionConversion.convertAll(); //Verify permissions diff --git a/ccm-core/src/com/arsdigita/portation/conversion/NgCollection.java b/ccm-core/src/com/arsdigita/portation/conversion/NgCollection.java index dffbf5395..572d4fd04 100644 --- a/ccm-core/src/com/arsdigita/portation/conversion/NgCollection.java +++ b/ccm-core/src/com/arsdigita/portation/conversion/NgCollection.java @@ -33,9 +33,9 @@ import com.arsdigita.portation.modules.core.workflow.Task; import com.arsdigita.portation.modules.core.workflow.TaskAssignment; import com.arsdigita.portation.modules.core.workflow.Workflow; import com.arsdigita.portation.modules.core.workflow.WorkflowTemplate; +import com.arsdigita.util.Assert; -import java.util.HashMap; -import java.util.Map; +import java.util.*; /** * Storage class for all ng-objects after conversion. This also helps for an @@ -47,7 +47,7 @@ import java.util.Map; public class NgCollection { public static Map ccmObjects = new HashMap<>(); - public static Map categories = new HashMap<>(); + public static Map categories = new TreeMap<>(); public static Map categorizations = new HashMap<>(); public static Map parties = new HashMap<>(); @@ -70,4 +70,48 @@ public class NgCollection { * Private constructor to prevent the instantiation of this class. */ private NgCollection() {} + + /** + * Sorts values of category-map to ensure that the parent-categories will + * be listed befor their childs in the export file. + * + * Runs once over the unsorted list and iterates over each their parents + * to add them to the sorted list. After being added to sorted the + * category will be removed from the unsorted list and therefore ignored + * in this foreach run. + * + * @return a sorted array list of categories + */ + public static ArrayList getSortedCategories() { + ArrayList unsorted = new ArrayList<>(categories.values()); + ArrayList sorted = new ArrayList<>(unsorted.size()); + + for (Category anUnsorted : unsorted) { + addTree(unsorted, sorted, anUnsorted); + } + + Assert.assertEquals(unsorted.size(), sorted.size()); + + return sorted; + + } + + /** + * Helper method to recursively add all parent categories before their + * childs. + * + * @param unsorted the unsorted list of categories + * @param sorted the sorted list of categories + * @param category the current category in the unsorted list + */ + private static void addTree(ArrayList unsorted, + ArrayList sorted, + Category category) { + if (category.getParentCategory() != null + && unsorted.contains(category.getParentCategory())) { + addTree(unsorted, sorted, category.getParentCategory()); + } + sorted.add(category); + unsorted.remove(category); + } }