- corrects nullpointer bugs during the conversion of categories

git-svn-id: https://svn.libreccm.org/ccm/trunk@4302 8810af33-2d31-482b-a856-94f89814c4df
master
tosmers 2016-09-14 14:35:24 +00:00
parent 3706bbf3d1
commit 57940eedc4
2 changed files with 83 additions and 25 deletions

View File

@ -48,41 +48,26 @@ public class CategoryConversion {
List<com.arsdigita.categorization.Category> 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<com.arsdigita.categorization.Category> 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<com.arsdigita.categorization.Category> 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());
}
}
}
}

View File

@ -60,6 +60,10 @@ public class Category extends CcmObject {
private Category parentCategory;
private long categoryOrder;
// to avoid infinite recursion
private List<Long> 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<Long> getSubCategoriesId() {
return subCategoriesId;
}
public void setSubCategoriesId(List<Long> 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;
}
}