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");
// Create the Romantic Comedies category under the Movies category.
Category romance = new Category("Romantic Comedies",
                                "Comedies with love stories");
romance.setDefaultParentCategory(movies);
// 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);
// Create the Drama category under the Movies category.
Category drama = new Category("Drama", "long, not funny stories");
// Make Drama a parent category (but not the default parent) of Titanic.
drama.addChild(titanic);
// Finally, make movies a parent of Drama
drama.setDefaultParentCategory(movies);

Example 11-1. Creating a new category

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

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, assuming its id is 123
Category category = new Category(new BigDecimal(123));
// Delete the Movies category.
category.deleteCategoryAndRemap();

Example 11-2. Category deletion

Figure 11-5. 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);
    // Makes Category C the parent of Category A
    c.addChild(a);
    // Sets Category B as the default parent of Category A
    a.setDefaultParentCategory(b);
}

Example 11-3. Adding more parent categories

Figure 11-6. 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);
a.setDefaultParentCategory(b.getDefaultParentCategory());

Example 11-4. Removing Parent Categories

Figure 11-7. 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);
// subcategories is a collection of Category objects
CategoryCollection subcategories = category.getChildren();

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.getObjects();

// 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