11.2. Categorization Scenarios

11.2.1. Creating a new Category

// Creates the Movies category at the top level with a description
Category movies = new Category("Movies", "long television shows");
movies.save();
// Create the Romantic Comedies category under the Movies category.
Category romance = new Category("Romantic Comedies",
                                "Comedies with love stories");
romance.setDefaultParentCategory(movies);
romance.save();
// Create the Titanic category under the Romantic Comedies category.
Category titanic = new Category("Titanic", "A category for large movies");
// set this as the default category
titanic.setDefaultParentCategory(romance);
titanic.save();
// Create the Drama category under the Movies category.
Category drama = new Category("Drama", "long, not funny stories");
drama.save();
// Make Drama a parent category (but not the default parent) of Titanic.
drama.addChild(titanic);
drama.save();
// Finally, make movies a parent of Drama
drama.setDefaultParentCategory(movies);
drama.save();

Example 11-1. Creating a new category

Figure 11-1. Category hierarchy before and after code execution

In Example 11-1, notice that whenever a new item is instantiated, it must be explicitly saved. This allows developers to easily back out of changes in the middle of the transaction.

Additionally, adding the "Titanic" category to two categories looks the same as adding it to a single category. The first category to which an item (category or other ACSObject) is added is explicitly added as the default category. If a category/item only has one parent, that parent is implicitly the default.

11.2.2. Deleting a Category

// Fetch the Movies category from the database.
Category category = new Category(new OID(123));
// Delete the Movies category.
category.deleteCategoryAndRemap();

Example 11-2. Category deletion

Figure 11-2. Category hierarchy before and after deleting a category

You can delete a category in the following ways:

11.2.3. Adding more Parent Categories

Category a = new Category(new OID(123));
Category b = new Category(new OID(124));
Category c = new Category(new OID(125));
if ( a != null && b != null && c != null ) {
// Makes Category B the parent of Category A
b.addChild(a);
b.save();
// Makes Category C the parent of Category A
c.addChild(a);
c.save();
// Sets Category B as the default parent of Category A
a.setDefaultParentCategory(b);
a.save();
}

Example 11-3. Adding more parent categories

Figure 11-3. Category hierarchy before and after adding parent categories.

11.2.4. Removing Parent Categories

// Fetch category A from the database.
Category a = new Category(new OID(123));
// Fetch the parent category
Category b = a.getDefaultParentCategory();
b.removeChild(a);
b.save();
a.setDefaultParentCategory(b.getDefaultParentCategory());

Example 11-4. Removing Parent Categories

Figure 11-4. Category Hierarchy before and after removing parent categories

11.2.5. Retrieving all Subcategories of a given Category

// assume that we have the Category OID (call it categoryOID)
Category category = new Category(categoryOID);
// the Collection will be a Collection of Category objects
Collection subcategories = category.getChildCategories();

Example 11-5. Retrieving Subcategories of a given Category

11.2.6. Retrieving all child objects of a given Category

// assume that we have the Category OID
// (call it categoryOID)
Category category = new Category(categoryOID);
// the Collection will be a Collection of
// CategorizedObjects
Collection childObjects = category.getChildObject();

// print out the toString for each object
// and its parents
Iterator childIterator = childObjects.iterator();
while (childIterator.hasNext()) {
CategorizedObject object =
 (CategorizedObject) childIterator.next();
System.out.println("object = " + object.getObject());
Iterator parents = object.getParentCategories().iterator();
while(parents.hasNext()) {
System.out.println("parent = " +
 ((Category) parents.next()).toString());
}
}

Example 11-6. Retrieving child objects of a given Category