001: package org.drools.repository;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import javax.jcr.Node;
007: import javax.jcr.NodeIterator;
008: import javax.jcr.RepositoryException;
009:
010: import org.apache.log4j.Logger;
011:
012: /**
013: * The TagItem class abstracts away details of the underlying JCR repository.
014: *
015: * @author btruitt
016: */
017: public class CategoryItem extends Item {
018: private Logger log = Logger.getLogger(CategoryItem.class);
019:
020: /**
021: * The name of the tag node type
022: */
023: public static final String TAG_NODE_TYPE_NAME = "drools:categoryNodeType";
024:
025: /**
026: * Constructs an object of type TagItem corresponding the specified node
027: * @param rulesRepository the rulesRepository that instantiated this object
028: * @param node the node to which this object corresponds
029: * @throws RulesRepositoryException
030: */
031: public CategoryItem(RulesRepository rulesRepository, Node node)
032: throws RulesRepositoryException {
033: super (rulesRepository, node);
034:
035: // try {
036: // //make sure this node is a tag node
037: // if(!(this.node.getPrimaryNodeType().getName().equals(TAG_NODE_TYPE_NAME))) {
038: // String message = this.node.getName() + " is not a node of type " + TAG_NODE_TYPE_NAME + ". It is a node of type: " + this.node.getPrimaryNodeType().getName();
039: // log.error(message);
040: // throw new RulesRepositoryException(message);
041: // }
042: // }
043: // catch(Exception e) {
044: // log.error("Caught exception: " + e);
045: // throw new RulesRepositoryException(e);
046: // }
047: }
048:
049: /**
050: * @return the full path of this tag, rooted at the tag area of the repository.
051: * @throws RulesRepositoryException
052: */
053: public String getFullPath() throws RulesRepositoryException {
054: try {
055:
056: StringBuffer returnString = new StringBuffer();
057: returnString.append(this .node.getName());
058: Node parentNode = this .node.getParent();
059: while (!parentNode.getName().equals(
060: RulesRepository.TAG_AREA)) {
061: returnString.insert(0, parentNode.getName() + "/");
062: parentNode = parentNode.getParent();
063: }
064: return returnString.toString();
065: } catch (Exception e) {
066: log.error("Caught Exception: " + e);
067: throw new RulesRepositoryException(e);
068: }
069: }
070:
071: /**
072: * @return a List of the immediate children of this tag
073: * @throws RulesRepositoryException
074: */
075: public List getChildTags() throws RulesRepositoryException {
076: List children = new ArrayList();
077:
078: try {
079: NodeIterator it = this .node.getNodes();
080: while (it.hasNext()) {
081: Node currentNode = it.nextNode();
082: children.add(new CategoryItem(this .rulesRepository,
083: currentNode));
084: }
085: } catch (Exception e) {
086: log.error("Caught Exception: " + e);
087: throw new RulesRepositoryException(e);
088: }
089:
090: return children;
091: }
092:
093: /**
094: * This will create a child category under this one
095: */
096: public CategoryItem addCategory(String name, String description) {
097: try {
098: Node child = this .node.addNode(name,
099: CategoryItem.TAG_NODE_TYPE_NAME);
100: this .rulesRepository.getSession().save();
101: return new CategoryItem(this .rulesRepository, child);
102: } catch (Exception e) {
103: if (e instanceof RuntimeException) {
104: throw (RuntimeException) e;
105: } else {
106: throw new RulesRepositoryException(e);
107: }
108: }
109:
110: }
111:
112: /**
113: * This will remove the category and any ones under it.
114: * This will only work if you have no current assets linked to the category.
115: */
116: public void remove() {
117: try {
118: if (this .node.getReferences().hasNext()) {
119: throw new RulesRepositoryException(
120: "The category still has some assets linked to it. You will need to remove the links so you can delete the cateogory.");
121: }
122: this .node.remove();
123: } catch (RepositoryException e) {
124: log.error(e);
125: }
126: }
127:
128: }
|