[jboss-svn-commits] JBL Code SVN: r19613 - in labs/jbossrules/trunk/drools-repository/src: test/java/org/drools/repository and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Apr 18 00:38:56 EDT 2008


Author: michael.neale at jboss.com
Date: 2008-04-18 00:38:55 -0400 (Fri, 18 Apr 2008)
New Revision: 19613

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/RulesRepository.java
   labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/AssetItemTest.java
   labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulesRepositoryTest.java
Log:
small tweaks

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	2008-04-17 22:26:55 UTC (rev 19612)
+++ labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/CategorisableItem.java	2008-04-18 04:38:55 UTC (rev 19613)
@@ -18,7 +18,7 @@
 /**
  * This contains logic for categorisable items
  * (not all versionably items are categorisable).
- * 
+ *
  * @author michael neale
  *
  */
@@ -32,14 +32,14 @@
 
     /**
      * Adds the specified tag to this object's node. Tags are stored as nodes in a tag area of
-     * the repository. If the specified tag does not already have a corresponding node, a node is 
+     * the repository. If the specified tag does not already have a corresponding node, a node is
      * created for it.
-     * 
+     *
      * Please note that this is mainly intended for rule related assets, not packages
-     * (although it could be used). 
-     *  
+     * (although it could be used).
+     *
      * @param tag the tag to add to the rule. rules can have multiple tags
-     * @throws RulesRepositoryException 
+     * @throws RulesRepositoryException
      */
     public void addCategory(String tag) throws RulesRepositoryException {
         try {
@@ -95,7 +95,7 @@
     }
 
     /**
-     * This method sets the categories in one hit, making the 
+     * This method sets the categories in one hit, making the
      * ASSUMPTION that the categories were previously set up !
      * (via CategoryItem of course !).
      */
@@ -117,15 +117,23 @@
 
     /**
      * Gets a list of CategoryItem objects for this assets node.
-     * 
-     * @return a list of TagItem objects for each tag on the rule. If there are no tags, an empty list. 
+     *
+     * @return a list of TagItem objects for each tag on the rule. If there are no tags, an empty list.
      * @throws RulesRepositoryException
      */
-    public List getCategories() throws RulesRepositoryException {
-        try {
+    public List<CategoryItem> getCategories() throws RulesRepositoryException {
+    	final List<CategoryItem> cats = new ArrayList<CategoryItem>();
+        doList(new Accum() {
+			public void add(CategoryItem c) {
+				cats.add(c);
+			}
+        });
+        return cats;
+    }
+
+	private void doList(Accum ac) {
+		try {
             Node ruleNode = getVersionContentNode();
-
-            List returnList = new ArrayList();
             try {
                 Property tagReferenceProperty = ruleNode.getProperty( CATEGORY_PROPERTY_NAME );
                 Value[] tagValues = tagReferenceProperty.getValues();
@@ -134,7 +142,8 @@
                         Node tagNode = this.node.getSession().getNodeByUUID( tagValues[i].getString() );
                         CategoryItem tagItem = new CategoryItem( this.rulesRepository,
                                                                  tagNode );
-                        returnList.add( tagItem );
+                        ac.add(tagItem);
+
                     } catch (ItemNotFoundException e) {
                         //ignore
                         log.debug( "Was unable to load a category by UUID - must have been removed." );
@@ -143,18 +152,41 @@
             } catch ( PathNotFoundException e ) {
                 //the property doesn't even exist yet, so just return nothing
             }
-            return returnList;
         } catch ( RepositoryException e ) {
             log.error( "Error loading cateories", e );
             throw new RulesRepositoryException( e );
         }
+	}
+
+    /**
+     * This will show a summary list of categories.
+     */
+    public String getCategorySummary() {
+    	final StringBuilder sum = new StringBuilder();
+    	doList(new Accum() {
+    		int count = 0;
+			public void add(CategoryItem c) {
+				count++;
+				if (count == 4) {
+					sum.append("...");
+				} else if (count < 4){
+					sum.append(c.getName());
+					sum.append(' ');
+				}
+			}
+    	});
+    	return sum.toString();
     }
 
+    static interface Accum {
+    	void add(CategoryItem c);
+    }
+
     /**
      * Removes the specified tag from this object's rule node.
-     * 
+     *
      * @param tag the tag to remove from the rule
-     * @throws RulesRepositoryException 
+     * @throws RulesRepositoryException
      */
     public void removeCategory(String tag) throws RulesRepositoryException {
         try {

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	2008-04-17 22:26:55 UTC (rev 19612)
+++ labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RulesRepository.java	2008-04-18 04:38:55 UTC (rev 19613)
@@ -889,6 +889,27 @@
     }
 
     /**
+     * Rename a category.
+     * @param originalPath The full path to the category.
+     * @param newName The new name (just the name, not the path).
+     */
+    public void renameCategory(String originalPath, String newName) {
+    	try {
+	    	CategoryItem cat = loadCategory(originalPath);
+	        Node node = cat.getNode();
+	        String sourcePath = node.getPath();
+	        String destPath = node.getParent().getPath() + "/" + newName;
+	        this.session.move(sourcePath, destPath);
+	        save();
+    	} catch (RepositoryException e) {
+    		log.error(e);
+    		throw new RulesRepositoryException(e);
+    	}
+    }
+
+
+
+    /**
      * This will rename a package and apply the change immediately.
      * @return the UUID of the package
      */

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	2008-04-17 22:26:55 UTC (rev 19612)
+++ labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/AssetItemTest.java	2008-04-18 04:38:55 UTC (rev 19613)
@@ -212,6 +212,12 @@
         ruleItem1.updateContent( "foo" );
         ruleItem1.checkin( "latest" );
 
+
+        assertTrue(ruleItem1.getCategories().size() > 0);
+        assertNotNull(ruleItem1.getCategorySummary());
+        assertEquals("testAddTagTestTag testAddTagTestTag2 ", ruleItem1.getCategorySummary());
+
+
         result = getRepo().findAssetsByCategory( "testAddTagTestTag",0, -1 ).assets;
 
         assertEquals(1, result.size());
@@ -225,6 +231,7 @@
 
 
 
+
     }
 
 

Modified: labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulesRepositoryTest.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulesRepositoryTest.java	2008-04-17 22:26:55 UTC (rev 19612)
+++ labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulesRepositoryTest.java	2008-04-18 04:38:55 UTC (rev 19613)
@@ -41,6 +41,46 @@
 
     }
 
+    public void testCategoryRename() throws Exception {
+        RulesRepository repo = RepositorySessionUtil.getRepository();
+
+        CategoryItem root = repo.loadCategory("/");
+        root.addCategory("testCatRename", "");
+        repo.loadCategory("testCatRename").addCategory("testRename", "");
+
+        repo.renameCategory("testCatRename/testRename", "testAnother");
+
+        CategoryItem cat = repo.loadCategory("testCatRename/testAnother");
+        assertNotNull(cat);
+        try {
+        	repo.loadCategory("testCatRename/testRename");
+        	fail("should not exist.");
+        } catch (RulesRepositoryException e) {
+        	assertNotNull(e.getMessage());
+        }
+
+        PackageItem pkg = repo.createPackage("testCategoryRename", "");
+        AssetItem asset = pkg.addAsset("fooBar", "");
+        asset.addCategory("testCatRename");
+        asset.addCategory("testCatRename/testAnother");
+        asset.checkin("");
+
+
+
+        cat = repo.loadCategory("testCatRename/testAnother");
+        AssetPageList as = repo.findAssetsByCategory("testCatRename/testAnother", 0, -1);
+        assertEquals(1, as.totalSize);
+        assertEquals("fooBar",((AssetItem) as.assets.get(0)).getName());
+
+
+        repo.renameCategory("testCatRename/testAnother", "testYetAnother");
+        as = repo.findAssetsByCategory("testCatRename/testYetAnother", 0, -1);
+        assertEquals(1, as.totalSize);
+        assertEquals("fooBar",((AssetItem) as.assets.get(0)).getName());
+
+
+    }
+
     public void testAddVersionARule() throws Exception {
         RulesRepository repo = RepositorySessionUtil.getRepository();
         PackageItem pack = repo.createPackage( "testAddVersionARule", "description" );




More information about the jboss-svn-commits mailing list