[jboss-svn-commits] JBL Code SVN: r8547 - 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
Fri Dec 22 11:51:14 EST 2006


Author: michael.neale at jboss.com
Date: 2006-12-22 11:51:09 -0500 (Fri, 22 Dec 2006)
New Revision: 8547

Modified:
   labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/CategorisableItem.java
   labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/AssetItemTest.java
Log:
added ability to update categories with one hit

Modified: labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/CategorisableItem.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/CategorisableItem.java	2006-12-22 16:13:29 UTC (rev 8546)
+++ labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/CategorisableItem.java	2006-12-22 16:51:09 UTC (rev 8547)
@@ -6,7 +6,13 @@
 import javax.jcr.Node;
 import javax.jcr.PathNotFoundException;
 import javax.jcr.Property;
+import javax.jcr.RepositoryException;
+import javax.jcr.UnsupportedRepositoryOperationException;
 import javax.jcr.Value;
+import javax.jcr.ValueFormatException;
+import javax.jcr.lock.LockException;
+import javax.jcr.nodetype.ConstraintViolationException;
+import javax.jcr.version.VersionException;
 
 /**
  * This contains logic for categorisable items
@@ -37,49 +43,77 @@
     public void addCategory(String tag) throws RulesRepositoryException {
         try {
             //make sure this object's node is the head version
-            checkIsUpdateable();                                       
-            
-            CategoryItem tagItem = this.rulesRepository.loadCategory(tag);
-                                    
+            checkIsUpdateable();
+
+            CategoryItem tagItem = this.rulesRepository.loadCategory( tag );
+
             //now set the tag property of the rule
             Property tagReferenceProperty;
             int i = 0;
             Value[] newTagValues = null;
             try {
-                tagReferenceProperty = this.node.getProperty(CATEGORY_PROPERTY_NAME);
+                tagReferenceProperty = this.node.getProperty( CATEGORY_PROPERTY_NAME );
                 Value[] oldTagValues = tagReferenceProperty.getValues();
-                
+
                 //first, make sure this tag wasn't already there. while we're at it, lets copy the array
-                newTagValues = new Value[oldTagValues.length + 1];                
-                for(i=0; i<oldTagValues.length; i++) {
-                    if(oldTagValues[i].getString().equals(tag)) {
-                        log.info("tag '" + tag + "' already existed for rule node: " + this.node.getName());
+                newTagValues = new Value[oldTagValues.length + 1];
+                for ( i = 0; i < oldTagValues.length; i++ ) {
+                    if ( oldTagValues[i].getString().equals( tag ) ) {
+                        log.info( "tag '" + tag + "' already existed for rule node: " + this.node.getName() );
                         return;
                     }
                     newTagValues[i] = oldTagValues[i];
                 }
-            }
-            catch(PathNotFoundException e) {
+            } catch ( PathNotFoundException e ) {
                 //the property doesn't exist yet, so create it in the finally block
-                newTagValues = new Value[1];                 
-            }
-            finally {   
-                if(newTagValues != null) {
-                    newTagValues[i] = this.node.getSession().getValueFactory().createValue(tagItem.getNode());
-                    this.node.checkout();
-                    this.node.setProperty(CATEGORY_PROPERTY_NAME, newTagValues);
+                newTagValues = new Value[1];
+            } finally {
+                if ( newTagValues != null ) {
+                    newTagValues[i] = this.node.getSession().getValueFactory().createValue( tagItem.getNode() );
+                    updateCategories( newTagValues );
+                } else {
+                    log.error( "reached expected path of execution when adding tag '" + tag + "' to ruleNode: " + this.node.getName() );
                 }
-                else {
-                    log.error("reached expected path of execution when adding tag '" + tag + "' to ruleNode: " + this.node.getName());
-                }
             }
+        } catch ( Exception e ) {
+            log.error( "Caught exception",
+                       e );
+            throw new RulesRepositoryException( e );
         }
-        catch(Exception e) {
-            log.error("Caught exception", e);
+    }
+
+    private void updateCategories(Value[] newTagValues) throws UnsupportedRepositoryOperationException,
+                                                       LockException,
+                                                       RepositoryException,
+                                                       ValueFormatException,
+                                                       VersionException,
+                                                       ConstraintViolationException {
+        this.node.checkout();
+        this.node.setProperty( CATEGORY_PROPERTY_NAME,
+                               newTagValues );
+    }
+
+    /**
+     * This method sets the categories in one hit, making the 
+     * ASSUMPTION that the categories were previously set up !
+     * (via CategoryItem of course !).
+     */
+    public void updateCategoryList(String[] categories) {
+        this.checkIsUpdateable();
+        try {
+            Value[] newCats = new Value[categories.length];
+            for ( int i = 0; i < categories.length; i++ ) {
+                CategoryItem item = this.rulesRepository.loadCategory( categories[i] );
+
+                newCats[i] = this.node.getSession().getValueFactory().createValue( item.getNode() );
+
+            }
+            updateCategories( newCats );
+        } catch ( RepositoryException e ) {
             throw new RulesRepositoryException(e);
         }
-    }   
-    
+    }
+
     /**
      * Gets a list of CategoryItem objects for this assets node.
      * 
@@ -87,30 +121,30 @@
      * @throws RulesRepositoryException
      */
     public List getCategories() throws RulesRepositoryException {
-        try {                            
+        try {
             Node ruleNode = getVersionContentNode();
-            
+
             List returnList = new ArrayList();
             try {
-                Property tagReferenceProperty = ruleNode.getProperty(CATEGORY_PROPERTY_NAME);
-                Value[] tagValues = tagReferenceProperty.getValues();                
-                for(int i=0; i<tagValues.length; i++) {
-                    Node tagNode = this.node.getSession().getNodeByUUID(tagValues[i].getString());
-                    CategoryItem tagItem = new CategoryItem(this.rulesRepository, tagNode);
-                    returnList.add(tagItem);
+                Property tagReferenceProperty = ruleNode.getProperty( CATEGORY_PROPERTY_NAME );
+                Value[] tagValues = tagReferenceProperty.getValues();
+                for ( int i = 0; i < tagValues.length; i++ ) {
+                    Node tagNode = this.node.getSession().getNodeByUUID( tagValues[i].getString() );
+                    CategoryItem tagItem = new CategoryItem( this.rulesRepository,
+                                                             tagNode );
+                    returnList.add( tagItem );
                 }
-            }
-            catch(PathNotFoundException e) {
+            } catch ( PathNotFoundException e ) {
                 //the property doesn't even exist yet, so just return nothing
             }
             return returnList;
+        } catch ( Exception e ) {
+            log.error( "Caught exception",
+                       e );
+            throw new RulesRepositoryException( e );
         }
-        catch(Exception e) {
-            log.error("Caught exception", e);
-            throw new RulesRepositoryException(e);
-        }
-    }  
-    
+    }
+
     /**
      * Removes the specified tag from this object's rule node.
      * 
@@ -120,65 +154,60 @@
     public void removeCategory(String tag) throws RulesRepositoryException {
         try {
             //make sure this object's node is the head version
-            if(this.node.getPrimaryNodeType().getName().equals("nt:version")) {
+            if ( this.node.getPrimaryNodeType().getName().equals( "nt:version" ) ) {
                 String message = "Error. Tags can only be removed from the head version of a rule node";
-                log.error(message);
-                throw new RulesRepositoryException(message);
-            }                                                   
-                                    
+                log.error( message );
+                throw new RulesRepositoryException( message );
+            }
+
             //now set the tag property of the rule
             Property tagReferenceProperty;
             int i = 0;
             int j = 0;
             Value[] newTagValues = null;
             try {
-                tagReferenceProperty = this.node.getProperty(CATEGORY_PROPERTY_NAME);
+                tagReferenceProperty = this.node.getProperty( CATEGORY_PROPERTY_NAME );
                 Value[] oldTagValues = tagReferenceProperty.getValues();
-                
+
                 //see if the tag was even there
                 boolean wasThere = false;
-                for(i=0; i<oldTagValues.length; i++) {
-                    Node tagNode = this.node.getSession().getNodeByUUID(oldTagValues[i].getString());
-                    if(tagNode.getName().equals(tag)) {                                                
+                for ( i = 0; i < oldTagValues.length; i++ ) {
+                    Node tagNode = this.node.getSession().getNodeByUUID( oldTagValues[i].getString() );
+                    if ( tagNode.getName().equals( tag ) ) {
                         wasThere = true;
                     }
                 }
-                
-                if(wasThere) {
+
+                if ( wasThere ) {
                     //copy the array, minus the specified tag
-                    newTagValues = new Value[oldTagValues.length + 1];                
-                    for(i=0; i<oldTagValues.length; i++) {
-                        Node tagNode = this.node.getSession().getNodeByUUID(oldTagValues[i].getString());
-                        if(!tagNode.getName().equals(tag)) {                                                         
+                    newTagValues = new Value[oldTagValues.length + 1];
+                    for ( i = 0; i < oldTagValues.length; i++ ) {
+                        Node tagNode = this.node.getSession().getNodeByUUID( oldTagValues[i].getString() );
+                        if ( !tagNode.getName().equals( tag ) ) {
                             newTagValues[j] = oldTagValues[i];
                             j++;
                         }
                     }
-                }
-                else {
+                } else {
                     return;
                 }
-            }
-            catch(PathNotFoundException e) {
+            } catch ( PathNotFoundException e ) {
                 //the property doesn't exist yet
-                return;             
-            }
-            finally {   
-                if(newTagValues != null) {
+                return;
+            } finally {
+                if ( newTagValues != null ) {
                     checkout();
-                    this.node.setProperty(CATEGORY_PROPERTY_NAME, newTagValues);
+                    this.node.setProperty( CATEGORY_PROPERTY_NAME,
+                                           newTagValues );
+                } else {
+                    log.error( "reached expected path of execution when removing tag '" + tag + "' from ruleNode: " + this.node.getName() );
                 }
-                else {
-                    log.error("reached expected path of execution when removing tag '" + tag + "' from ruleNode: " + this.node.getName());
-                }
             }
+        } catch ( Exception e ) {
+            log.error( "Caught exception",
+                       e );
+            throw new RulesRepositoryException( e );
         }
-        catch(Exception e) {
-            log.error("Caught exception", e);
-            throw new RulesRepositoryException(e);
-        }
     }
-    
 
-
 }

Modified: labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/AssetItemTest.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/AssetItemTest.java	2006-12-22 16:13:29 UTC (rev 8546)
+++ labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/AssetItemTest.java	2006-12-22 16:51:09 UTC (rev 8547)
@@ -136,6 +136,26 @@
             
     }
     
+    public void testUpdateCategories() {
+        getRepo().loadCategory( "/" ).addCategory( "testUpdateCategoriesOnAsset", "la" );
+        getRepo().loadCategory( "/" ).addCategory( "testUpdateCategoriesOnAsset2", "la" );
+        
+        AssetItem item = getRepo().loadDefaultPackage().addAsset( "testUpdateCategoriesOnAsset", "huhuhu" );
+        String[] cats = new String[] {"testUpdateCategoriesOnAsset", "testUpdateCategoriesOnAsset2"};
+        item.updateCategoryList( cats );
+        
+        item.checkin( "aaa" );
+        
+        item = getRepo().loadDefaultPackage().loadAsset( "testUpdateCategoriesOnAsset" );
+        assertEquals(2, item.getCategories().size());
+        
+        for ( Iterator iter = item.getCategories().iterator(); iter.hasNext(); ) {
+            CategoryItem cat = (CategoryItem) iter.next();            
+            assertTrue(cat.getName().startsWith( "testUpdateCategoriesOnAsset" ));            
+        }
+        
+    }
+    
     public void testFindRulesByCategory() throws Exception {
         
         getRepo().loadCategory( "/" ).addCategory( "testFindRulesByCat", "yeah" );




More information about the jboss-svn-commits mailing list