From 57940eedc4635d1c720d9f5f6dd6b51222382222 Mon Sep 17 00:00:00 2001 From: tosmers Date: Wed, 14 Sep 2016 14:35:24 +0000 Subject: [PATCH] [UPDATE] - corrects nullpointer bugs during the conversion of categories git-svn-id: https://svn.libreccm.org/ccm/trunk@4302 8810af33-2d31-482b-a856-94f89814c4df --- .../categorization/CategoryConversion.java | 63 +++++++++++++------ .../modules/core/categorization/Category.java | 45 +++++++++++-- 2 files changed, 83 insertions(+), 25 deletions(-) diff --git a/ccm-core/src/com/arsdigita/portation/conversion/core/categorization/CategoryConversion.java b/ccm-core/src/com/arsdigita/portation/conversion/core/categorization/CategoryConversion.java index 24e1a6453..86277a329 100644 --- a/ccm-core/src/com/arsdigita/portation/conversion/core/categorization/CategoryConversion.java +++ b/ccm-core/src/com/arsdigita/portation/conversion/core/categorization/CategoryConversion.java @@ -48,41 +48,26 @@ public class CategoryConversion { List trunkCategories = com .arsdigita.categorization.Category.getAllObjectCategories(); - createCategoryAndSetAssociations(trunkCategories); + createCategoryAndCategorizations(trunkCategories); + setRingAssociations(trunkCategories); } /** - * Creates the equivalent ng-class of the {@code Category} and restores + * Creates the equivalent ng-class of the {@link Category} and restores * the associations to other classes. * * @param trunkCategories List of all * {@link com.arsdigita.categorization.Category}s * from this old trunk-system. */ - private static void createCategoryAndSetAssociations( + private static void createCategoryAndCategorizations( List trunkCategories) { for (com.arsdigita.categorization.Category trunkCategory : trunkCategories) { + // create categories Category category = new Category(trunkCategory); - - // set parent and opposed association - Category parentCategory = null; - try { - com.arsdigita.categorization.Category defaultParent = - trunkCategory.getDefaultParentCategory(); - - if (defaultParent != null) { - parentCategory = NgCollection.categories.get( - defaultParent.getID().longValue()); - } - } catch (Exception e) {} - if (parentCategory != null) { - category.setParentCategory(parentCategory); - parentCategory.addSubCategory(category); - } - // categorizations only for category typed objects CategorizedCollection categorizedCollection = trunkCategory .getObjects(com.arsdigita.categorization.Category @@ -118,4 +103,42 @@ public class CategoryConversion { } } } + + /** + * Method for setting the parent {@link Category} on the one side and the + * sub-{@link Category}s on the other side. + * + * @param trunkCategories List of all + * {@link com.arsdigita.categorization.Category}s + * from this old trunk-system. + */ + private static void setRingAssociations( + List trunkCategories) { + for (com.arsdigita.categorization.Category + trunkCategory : trunkCategories) { + + Category category = NgCollection.categories.get(trunkCategory + .getID().longValue()); + + // set parent and opposed association + Category parentCategory = null; + try { + com.arsdigita.categorization.Category defaultParent = + trunkCategory.getDefaultParentCategory(); + + if (defaultParent != null) { + parentCategory = NgCollection.categories.get( + defaultParent.getID().longValue()); + } + } catch (Exception e) {} + if (parentCategory != null) { + //category.setParentCategory(parentCategory); + //parentCategory.addSubCategory(category); + + // to avoid infinite recursion + category.setParentCategoryId(parentCategory.getObjectId()); + parentCategory.addSubCategoryId(category.getObjectId()); + } + } + } } diff --git a/ccm-core/src/com/arsdigita/portation/modules/core/categorization/Category.java b/ccm-core/src/com/arsdigita/portation/modules/core/categorization/Category.java index c03227c7e..365802b42 100644 --- a/ccm-core/src/com/arsdigita/portation/modules/core/categorization/Category.java +++ b/ccm-core/src/com/arsdigita/portation/modules/core/categorization/Category.java @@ -60,6 +60,10 @@ public class Category extends CcmObject { private Category parentCategory; private long categoryOrder; + // to avoid infinite recursion + private List subCategoriesId; + private long parentCategoryId; + public Category(final com.arsdigita.categorization.Category trunkCategory) { super(trunkCategory); @@ -71,12 +75,18 @@ public class Category extends CcmObject { trunkCategory.getCategoryLocalizationCollection(); if (categoryLocalizationCollection != null && categoryLocalizationCollection.next()) { - CategoryLocalization categoryLocalization = trunkCategory - .getCategoryLocalizationCollection().getCategoryLocalization(); - Locale locale = new Locale(categoryLocalization.getLocale()); - this.title.addValue(locale, categoryLocalization.getName()); - this.description.addValue(locale, categoryLocalization.getDescription()); + CategoryLocalization categoryLocalization = + categoryLocalizationCollection.getCategoryLocalization(); + + if (categoryLocalization != null && categoryLocalization + .getLocale() != null) { + Locale locale = new Locale(categoryLocalization.getLocale()); + if (categoryLocalization.getName() != null) + this.title.addValue(locale, categoryLocalization.getName()); + if (categoryLocalization.getDescription() != null) + this.description.addValue(locale, categoryLocalization.getDescription()); + } } this.enabled = trunkCategory.isEnabled(); @@ -208,4 +218,29 @@ public class Category extends CcmObject { public void setCategoryOrder(final long categoryOrder) { this.categoryOrder = categoryOrder; } + + + public List getSubCategoriesId() { + return subCategoriesId; + } + + public void setSubCategoriesId(List subCategoriesId) { + this.subCategoriesId = subCategoriesId; + } + + public void addSubCategoryId(final long subCategoryId) { + this.subCategoriesId.add(subCategoryId); + } + + public void removeSubCategoryId(final long subCategoryId) { + this.subCategoriesId.remove(subCategoryId); + } + + public long getParentCategoryId() { + return parentCategoryId; + } + + public void setParentCategoryId(long parentCategoryId) { + this.parentCategoryId = parentCategoryId; + } }