[jboss-svn-commits] JBL Code SVN: r6231 - in labs/jbossrules/trunk/drools-repository/src: main/java/org/drools/repository test/java/org/drools/repository

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Sep 14 13:12:03 EDT 2006


Author: michael.neale at jboss.com
Date: 2006-09-14 13:11:55 -0400 (Thu, 14 Sep 2006)
New Revision: 6231

Modified:
   labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/CategoryItem.java
   labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RuleItem.java
   labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RulesRepository.java
   labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/CategoryItemTestCase.java
   labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RuleItemTestCase.java
   labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulesRepositoryTestCase.java
Log:
added more category functionality

Modified: labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/CategoryItem.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/CategoryItem.java	2006-09-14 15:22:25 UTC (rev 6230)
+++ labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/CategoryItem.java	2006-09-14 17:11:55 UTC (rev 6231)
@@ -5,9 +5,6 @@
 
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
-import javax.jcr.RepositoryException;
-import javax.jcr.version.Version;
-import javax.jcr.version.VersionIterator;
 
 import org.apache.log4j.Logger;
 
@@ -93,20 +90,22 @@
     }
 
     /**
-     * Gets a TagItem object encapsulating the specified child tag. If the child tag 
-     * doesn't exist, it is created.
-     * 
-     * @param tagName the name of the child tag to get or add
-     * @return a TagItem encapsulating the specified child tag
-     * @throws RulesRepositoryException
+     * This will create a child category under this one
      */
-    public CategoryItem getChildTag(String tagName) throws RulesRepositoryException {
+    public CategoryItem addCategory(String name,
+                                    String description) {
         try {
-            return this.rulesRepository.getOrCreateCategory(this.getFullPath() + "/" + tagName);
+            Node child = this.node.addNode( name, CategoryItem.TAG_NODE_TYPE_NAME );
+            this.rulesRepository.getSession().save();
+            return new CategoryItem(this.rulesRepository, child);
+        } catch (Exception e) {
+            if (e instanceof RuntimeException) {
+                throw (RuntimeException) e;
+            } else {
+                throw new RulesRepositoryException(e);
+            }
         }
-        catch(Exception e) {
-            log.error("Caught Exception: " + e);
-            throw new RulesRepositoryException(e);
-        }
-    }        
+
+    }
+    
 }

Modified: labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RuleItem.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RuleItem.java	2006-09-14 15:22:25 UTC (rev 6230)
+++ labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RuleItem.java	2006-09-14 17:11:55 UTC (rev 6231)
@@ -301,7 +301,7 @@
             //make sure this object's node is the head version
             checkIsUpdateable();                                       
             
-            CategoryItem tagItem = this.rulesRepository.getOrCreateCategory(tag);
+            CategoryItem tagItem = this.rulesRepository.loadCategory(tag);
                                     
             //now set the tag property of the rule
             Property tagReferenceProperty;

Modified: labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RulesRepository.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RulesRepository.java	2006-09-14 15:22:25 UTC (rev 6230)
+++ labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RulesRepository.java	2006-09-14 17:11:55 UTC (rev 6231)
@@ -689,9 +689,7 @@
     }
         
     /**
-     * Gets a TagItem object that encapsulates the node for the specified tag name.  If the tag does
-     * not already exist in the repository when this is called, it is first added to the repository
-     * and then returned.
+     * This will return a category for the given category path.
      * 
      * @param tagName the name of the tag to get. If the tag to get is within a heirarchy of
      *                tag nodes, specify the full path to the tag node of interest (e.g. if
@@ -699,7 +697,7 @@
      * @return a TagItem object encapsulating the node for the tag in the repository
      * @throws RulesRepositoryException 
      */
-    public CategoryItem getOrCreateCategory(String tagName) throws RulesRepositoryException {
+    public CategoryItem loadCategory(String tagName) throws RulesRepositoryException {
         if (tagName == null || "".equals( tagName )) {
             throw new RuntimeException("Empty category name not permitted.");
         }
@@ -713,14 +711,17 @@
             while(tok.hasMoreTokens()) {                                
                 String currentTagName = tok.nextToken();
                 
-                tagNode = RulesRepository.addNodeIfNew(folderNode, currentTagName, CategoryItem.TAG_NODE_TYPE_NAME);
+                tagNode = folderNode.getNode( currentTagName ) ; 
+                //MN was this: RulesRepository.addNodeIfNew(folderNode, currentTagName, CategoryItem.TAG_NODE_TYPE_NAME);
                 folderNode = tagNode;
             }             
                                     
             return new CategoryItem(this, tagNode);
         }
         catch(Exception e) {
-            log.error("Caught Exception: " + e);
+            if (e instanceof PathNotFoundException) {
+                throw new RulesRepositoryException("Unable to load the category : [" + tagName + "] does not exist.", e);
+            }
             throw new RulesRepositoryException(e);
         }
     }
@@ -734,7 +735,7 @@
      */
     public List findRulesByTag(String categoryTag) throws RulesRepositoryException {
         
-        CategoryItem item = this.getOrCreateCategory( categoryTag );
+        CategoryItem item = this.loadCategory( categoryTag );
         List results = new ArrayList();
         try {
             PropertyIterator it = item.getNode().getReferences();
@@ -771,4 +772,7 @@
     public Session getSession() {
         return this.session;
     }
+
+
+
 }
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/CategoryItemTestCase.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/CategoryItemTestCase.java	2006-09-14 15:22:25 UTC (rev 6230)
+++ labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/CategoryItemTestCase.java	2006-09-14 17:11:55 UTC (rev 6231)
@@ -13,25 +13,31 @@
     
     
     public void testTagItem() {
-            CategoryItem tagItem1 = getRepo().getOrCreateCategory("TestTag");
+        
+            final CategoryItem root = getRepo().loadCategory( "/" );
+            
+            root.addCategory( "TestTag", "nothing to see" );
+            
+            
+            CategoryItem tagItem1 = getRepo().loadCategory("TestTag");
             assertNotNull(tagItem1);
             assertEquals("TestTag", tagItem1.getName());                        
             
-            CategoryItem tagItem2 = getRepo().getOrCreateCategory("TestTag");
+            CategoryItem tagItem2 = getRepo().loadCategory("TestTag");
             assertNotNull(tagItem2);
             assertEquals("TestTag", tagItem2.getName());
             assertEquals(tagItem1, tagItem2);
             
-            List originalCats = getRepo().getOrCreateCategory( "/" ).getChildTags(); //listCategoryNames();
+            List originalCats = getRepo().loadCategory( "/" ).getChildTags(); //listCategoryNames();
             assertTrue(originalCats.size() > 0);            
             
-            CategoryItem root = (CategoryItem) originalCats.get( 0 );
-            assertNotNull(root.getName());
-            assertNotNull(root.getFullPath());
+            CategoryItem rootCat = (CategoryItem) originalCats.get( 0 );
+            assertNotNull(rootCat.getName());
+            assertNotNull(rootCat.getFullPath());
             
-            getRepo().getOrCreateCategory( "FootestTagItem" );
+            root.addCategory( "FootestTagItem", "nothing" );
             
-            List cats = getRepo().getOrCreateCategory( "/" ).getChildTags();
+            List cats = root.getChildTags();
             assertEquals(originalCats.size() + 1, cats.size());            
             
             boolean found = false;
@@ -46,41 +52,48 @@
     
     }    
     
-    public void testGetChildTags() {
-        try {            
-            CategoryItem tagItem1 = getRepo().getOrCreateCategory("TestTag");
+    public void testCreateCateories() throws Exception {
+        RulesRepository repo = getRepo();
+        
+        //load the root
+        CategoryItem root = repo.loadCategory( "/" );
+        
+        CategoryItem item = root.addCategory("testCreateCategories", "this is a top level one");
+        assertEquals("testCreateCategories", item.getName());
+        assertEquals("testCreateCategories", item.getFullPath());
+        
+        item = repo.loadCategory( "testCreateCategories" );
+        assertEquals("testCreateCategories", item.getName());
+        
+        
+    }
+    
+    public void testGetChildTags() {        
+            CategoryItem tagItem1 = getRepo().loadCategory("TestTag");
             assertNotNull(tagItem1);
             assertEquals("TestTag", tagItem1.getName());                        
             
             List childTags = tagItem1.getChildTags();
             assertNotNull(childTags);
-            assertEquals(0, childTags.size());
+            assertEquals(0, childTags.size());            
+
+            tagItem1.addCategory( "TestChildTag1", "description" );            
             
-            CategoryItem childTagItem1 = tagItem1.getChildTag("TestChildTag1");
-            assertNotNull(childTagItem1);
-            assertEquals("TestChildTag1", childTagItem1.getName());
-            
             childTags = tagItem1.getChildTags();
             assertNotNull(childTags);
             assertEquals(1, childTags.size());
             assertEquals("TestChildTag1", ((CategoryItem)childTags.get(0)).getName());
             
-            CategoryItem childTagItem2 = tagItem1.getChildTag("TestChildTag2");
-            assertNotNull(childTagItem2);
-            assertEquals("TestChildTag2", childTagItem2.getName());
+            tagItem1.addCategory( "AnotherChild", "ignore me" );
             
             childTags = tagItem1.getChildTags();
             assertNotNull(childTags);
             assertEquals(2, childTags.size());
-        }
-        catch(Exception e) {
-            fail("Unexpected Exception caught: " + e);
-        }
     }
     
     public void testGetChildTag() {
-        try {            
-            CategoryItem tagItem1 = getRepo().getOrCreateCategory("testGetChildTag");
+            CategoryItem root = getRepo().loadCategory( "/" );
+            CategoryItem tagItem1 = root.addCategory("testGetChildTag", "yeah");
             assertNotNull(tagItem1);
             assertEquals("testGetChildTag", tagItem1.getName());                        
             
@@ -89,40 +102,35 @@
             assertNotNull(childTags);
             assertEquals(0, childTags.size());
             
-            CategoryItem childTagItem1 = tagItem1.getChildTag("TestChildTag1");
+            CategoryItem childTagItem1 = tagItem1.addCategory("TestChildTag1", "woo");
             assertNotNull(childTagItem1);
             assertEquals("TestChildTag1", childTagItem1.getName());
                         
             //test that if already there, it is returned
-            CategoryItem childTagItem2 = tagItem1.getChildTag("TestChildTag1");
+            CategoryItem childTagItem2 = getRepo().loadCategory( "testGetChildTag/TestChildTag1");
             assertNotNull(childTagItem2);
             assertEquals("TestChildTag1", childTagItem2.getName());
             assertEquals(childTagItem1, childTagItem2);
-        }
-        catch(Exception e) {
-            fail("Unexpected Exception caught: " + e);
-        }
     }
     
     public void testGetFullPath() {
-        try {            
-            CategoryItem tagItem1 = getRepo().getOrCreateCategory("TestTag");
+        
+            CategoryItem root = getRepo().loadCategory( "/" );
+        
+            CategoryItem tagItem1 = root.addCategory("testGetFullPath", "foo");
             assertNotNull(tagItem1);
-            assertEquals("TestTag", tagItem1.getFullPath());                        
+            assertEquals("testGetFullPath", tagItem1.getFullPath());                        
                                     
-            CategoryItem childTagItem1 = tagItem1.getChildTag("TestChildTag1");
+            CategoryItem childTagItem1 = tagItem1.addCategory("TestChildTag1", "foo");
             assertNotNull(childTagItem1);
-            assertEquals("TestTag/TestChildTag1", childTagItem1.getFullPath());
+            assertEquals("testGetFullPath/TestChildTag1", childTagItem1.getFullPath());
                         
-            CategoryItem childTagItem2 = childTagItem1.getChildTag("TestChildTag2");
+            CategoryItem childTagItem2 = childTagItem1.addCategory("TestChildTag2", "wee");
             assertNotNull(childTagItem2);
-            assertEquals("TestTag/TestChildTag1/TestChildTag2", childTagItem2.getFullPath());
+            assertEquals("testGetFullPath/TestChildTag1/TestChildTag2", childTagItem2.getFullPath());
             
             
-        }
-        catch(Exception e) {
-            fail("Unexpected Exception caught: " + e);
-        }
+
     }
 
     private RulesRepository getRepo() {

Modified: labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RuleItemTestCase.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RuleItemTestCase.java	2006-09-14 15:22:25 UTC (rev 6230)
+++ labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RuleItemTestCase.java	2006-09-14 17:11:55 UTC (rev 6231)
@@ -92,11 +92,14 @@
     public void testCategories() {
             RuleItem ruleItem1 = getRepo().addRule("testAddTag", "test lhs content", "test rhs content");
             
+            getRepo().loadCategory( "/" ).addCategory( "testAddTagTestTag", "description" );
+            
             ruleItem1.addCategory("testAddTagTestTag");
             List tags = ruleItem1.getCategories();
             assertEquals(1, tags.size());
             assertEquals("testAddTagTestTag", ((CategoryItem)tags.get(0)).getName());
             
+            getRepo().loadCategory( "/" ).addCategory( "testAddTagTestTag2", "description" );            
             ruleItem1.addCategory("testAddTagTestTag2");
             tags = ruleItem1.getCategories();
             assertEquals(2, tags.size());   
@@ -128,17 +131,23 @@
     public void testRemoveTag() {
             RuleItem ruleItem1 = getRepo().addRule("testRemoveTag", "test lhs content", "test rhs content");
             
-            ruleItem1.addCategory("TestTag");                                    
-            ruleItem1.removeCategory("TestTag");
+            getRepo().loadCategory( "/" ).addCategory( "TestRemoveCategory", "description" );
+            
+            ruleItem1.addCategory("TestRemoveCategory");   
             List tags = ruleItem1.getCategories();
+            assertEquals(1, tags.size());
+            ruleItem1.removeCategory("TestRemoveCategory");
+            tags = ruleItem1.getCategories();
             assertEquals(0, tags.size());
-            
-            ruleItem1.addCategory("TestTag2");                                    
-            ruleItem1.addCategory("TestTag3");
-            ruleItem1.removeCategory("TestTag2");
+
+            getRepo().loadCategory( "/" ).addCategory( "TestRemoveCategory2", "description" );
+            getRepo().loadCategory( "/" ).addCategory( "TestRemoveCategory3", "description" );
+            ruleItem1.addCategory("TestRemoveCategory2");                                    
+            ruleItem1.addCategory("TestRemoveCategory3");
+            ruleItem1.removeCategory("TestRemoveCategory2");
             tags = ruleItem1.getCategories();
             assertEquals(1, tags.size());
-            assertEquals("TestTag3", ((CategoryItem)tags.get(0)).getName());            
+            assertEquals("TestRemoveCategory3", ((CategoryItem)tags.get(0)).getName());            
 
     }
 
@@ -149,6 +158,8 @@
             assertNotNull(tags);
             assertEquals(0, tags.size());
             
+            getRepo().loadCategory( "/" ).addCategory( "testGetTagsTestTag", "description" );
+            
             ruleItem1.addCategory("testGetTagsTestTag");                                    
             tags = ruleItem1.getCategories();
             assertEquals(1, tags.size());

Modified: labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulesRepositoryTestCase.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulesRepositoryTestCase.java	2006-09-14 15:22:25 UTC (rev 6230)
+++ labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulesRepositoryTestCase.java	2006-09-14 17:11:55 UTC (rev 6231)
@@ -132,19 +132,21 @@
     }
 
     public void testGetTag() {
-        RulesRepository rulesRepository = RepositorySession.getRepository();
-            CategoryItem tagItem1 = rulesRepository.getOrCreateCategory("testGetTag");
+            RulesRepository rulesRepository = RepositorySession.getRepository();
+            
+            CategoryItem root = rulesRepository.loadCategory( "/" );
+            CategoryItem tagItem1 = root.addCategory( "testGetTag", "ho");
             assertNotNull(tagItem1);
             assertEquals("testGetTag", tagItem1.getName());
             assertEquals("testGetTag", tagItem1.getFullPath());
             
-            CategoryItem tagItem2 = rulesRepository.getOrCreateCategory("testGetTag");
+            CategoryItem tagItem2 = rulesRepository.loadCategory("testGetTag");
             assertNotNull(tagItem2);
             assertEquals("testGetTag", tagItem2.getName());
             assertEquals(tagItem1, tagItem2);
             
             //now test getting a tag down in the tag hierarchy
-            CategoryItem tagItem3 = rulesRepository.getOrCreateCategory("testGetTag/TestChildTag1");
+            CategoryItem tagItem3 = tagItem2.addCategory( "TestChildTag1", "ka");
             assertNotNull(tagItem3);
             assertEquals("TestChildTag1", tagItem3.getName());
             assertEquals("testGetTag/TestChildTag1", tagItem3.getFullPath());                                   




More information about the jboss-svn-commits mailing list