// 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
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.
// 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
You can delete a category in the following ways:
Deleting Leaf Category — If the category is a leaf, call Category.delete().
Remapping Child Categories — If you want to remove the current category and make all categories that were pointing to it point to its default parent (as in the example), call Category.deleteCategoryAndRemap().
Orphaning Children — If you want to delete the category without remapping any of the child categories, call Category.deleteCategoryAndOrphan().
Deleting Category Subtree — If you want to delete the entire subtree, including the passed-in category and all categories below it whose default ancestry can be traced to the root, call Category.deleteCategorySubtree().
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
// 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