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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Nov 3 06:00:09 EST 2006


Author: michael.neale at jboss.com
Date: 2006-11-03 05:59:56 -0500 (Fri, 03 Nov 2006)
New Revision: 7359

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/RulePackageItem.java
   labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RulesRepository.java
   labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/VersionableItem.java
   labs/jbossrules/trunk/drools-repository/src/main/resources/node_type_definitions/rule_node_type.cnd
   labs/jbossrules/trunk/drools-repository/src/main/resources/node_type_definitions/versionable_node_type.cnd
   labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RuleItemTestCase.java
   labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulePackageItemTestCase.java
   labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulesRepositoryTestCase.java
Log:
added move support, added meta data

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-11-03 10:52:23 UTC (rev 7358)
+++ labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RuleItem.java	2006-11-03 10:59:56 UTC (rev 7359)
@@ -5,11 +5,17 @@
 import java.util.Iterator;
 import java.util.List;
 
+import javax.jcr.AccessDeniedException;
+import javax.jcr.ItemExistsException;
+import javax.jcr.ItemNotFoundException;
 import javax.jcr.Node;
 import javax.jcr.PathNotFoundException;
 import javax.jcr.Property;
 import javax.jcr.RepositoryException;
 import javax.jcr.Value;
+import javax.jcr.lock.LockException;
+import javax.jcr.nodetype.ConstraintViolationException;
+import javax.jcr.version.VersionException;
 
 import org.apache.log4j.Logger;
 
@@ -20,90 +26,82 @@
  * @author btruitt
  */
 public class RuleItem extends CategorisableItem {
-    private Logger log = Logger.getLogger(RuleItem.class);
-    
-    
-    
+    private Logger             log                            = Logger.getLogger( RuleItem.class );
+
     /**
      * The name of the DSL property on the rule node type
      */
-    public static final String DSL_PROPERTY_NAME = "drools:dslReference";
-    
+    public static final String DSL_PROPERTY_NAME              = "drools:dslReference";
 
     /**
      * The name of the rule node type
      */
-    public static final String RULE_NODE_TYPE_NAME = "drools:ruleNodeType";
-    
-    public static final String RULE_CONTENT_PROPERTY_NAME = "drools:content";
+    public static final String RULE_NODE_TYPE_NAME            = "drools:ruleNodeType";
 
+    public static final String RULE_CONTENT_PROPERTY_NAME     = "drools:content";
+
     public static final String RULE_CONTENT_URI_PROPERTY_NAME = "drools:contentURI";
-        
-    
-                 
-    
+
     /**
      * The name of the date effective property on the rule node type
      */
-    public static final String DATE_EFFECTIVE_PROPERTY_NAME = "drools:dateEffective";
-    
+    public static final String DATE_EFFECTIVE_PROPERTY_NAME   = "drools:dateEffective";
+
     /**
      * The name of the date expired property on the rule node type
      */
-    public static final String DATE_EXPIRED_PROPERTY_NAME = "drools:dateExpired";                        
-    
+    public static final String DATE_EXPIRED_PROPERTY_NAME     = "drools:dateExpired";
+
+    public static final String PACKAGE_NAME_PROPERTY = "drools:packageName";
+
     /**
-     * The name of the rule language property on the rule node type
-     */
-    public static final String RULE_LANGUAGE_PROPERTY_NAME = "drools:ruleLanguage";    
-    
-    /**
      * Constructs a RuleItem object, setting its node attribute to the specified node.
      * 
      * @param rulesRepository the rulesRepository that instantiated this object
      * @param node the node in the repository that this RuleItem corresponds to
      * @throws RulesRepositoryException 
      */
-    public RuleItem(RulesRepository rulesRepository, Node node) throws RulesRepositoryException {
-        super(rulesRepository, node);
-        
+    public RuleItem(RulesRepository rulesRepository,
+                    Node node) throws RulesRepositoryException {
+        super( rulesRepository,
+               node );
+
         try {
             //make sure this node is a rule node       
-            if(!(this.node.getPrimaryNodeType().getName().equals(RULE_NODE_TYPE_NAME) ||
-                 isHistoricalVersion())) {
+            if ( !(this.node.getPrimaryNodeType().getName().equals( RULE_NODE_TYPE_NAME ) || isHistoricalVersion()) ) {
                 String message = this.node.getName() + " is not a node of type " + RULE_NODE_TYPE_NAME + " nor nt:version. It is a node of type: " + this.node.getPrimaryNodeType().getName();
-                log.error(message);
-                throw new RulesRepositoryException(message);
-            }    
+                log.error( message );
+                throw new RulesRepositoryException( message );
+            }
+        } catch ( Exception e ) {
+            log.error( "Caught exception",
+                       e );
+            throw new RulesRepositoryException( e );
         }
-        catch(Exception e) {
-            log.error("Caught exception", e);
-            throw new RulesRepositoryException(e);
-        }
     }
-    
 
-    
     /**
-     * returns the contents of the rule node
+     * returns the contents of the rule node.
+     * It there is a URI, this may need to access the external resource
+     * to grab/sync the latest, but in any case, it should be the real content.
      */
     public String getRuleContent() throws RulesRepositoryException {
-        try {                        
+        try {
             Node ruleNode = getVersionContentNode();
-            if (ruleNode.hasProperty( RULE_CONTENT_PROPERTY_NAME ) ) {
-                Property data = ruleNode.getProperty(RULE_CONTENT_PROPERTY_NAME);
+            if ( ruleNode.hasProperty( RULE_CONTENT_PROPERTY_NAME ) ) {
+                Property data = ruleNode.getProperty( RULE_CONTENT_PROPERTY_NAME );
                 return data.getValue().getString();
-                
+
             } else {
                 return null;
             }
+        } catch ( Exception e ) {
+            log.error( "Caught Exception",
+                       e );
+            throw new RulesRepositoryException( e );
         }
-        catch(Exception e) {
-            log.error("Caught Exception", e);
-            throw new RulesRepositoryException(e);
-        }
     }
-    
+
     /**
      * returns the URI for where the rules content is stored.
      * Rule content may be stored in an external repository, 
@@ -111,43 +109,42 @@
      * how to get to the exact version that maps to this rule node.
      */
     public String getRuleContentURI() throws RulesRepositoryException {
-        try {                        
+        try {
             Node ruleNode = getVersionContentNode();
-            if (ruleNode.hasProperty( RULE_CONTENT_URI_PROPERTY_NAME )) {
-                Property data = ruleNode.getProperty(RULE_CONTENT_URI_PROPERTY_NAME);
+            if ( ruleNode.hasProperty( RULE_CONTENT_URI_PROPERTY_NAME ) ) {
+                Property data = ruleNode.getProperty( RULE_CONTENT_URI_PROPERTY_NAME );
                 return data.getValue().getString();
             } else {
                 return "";
             }
-            
+
+        } catch ( Exception e ) {
+            log.error( "Caught Exception",
+                       e );
+            throw new RulesRepositoryException( e );
         }
-        catch(Exception e) {
-            log.error("Caught Exception", e);
-            throw new RulesRepositoryException(e);
-        }
     }
-        
+
     /**
      * @return the date the rule becomes effective
      * @throws RulesRepositoryException
      */
     public Calendar getDateEffective() throws RulesRepositoryException {
-        try {                        
+        try {
             Node ruleNode = getVersionContentNode();
-                        
-            Property dateEffectiveProperty = ruleNode.getProperty(DATE_EFFECTIVE_PROPERTY_NAME);
+
+            Property dateEffectiveProperty = ruleNode.getProperty( DATE_EFFECTIVE_PROPERTY_NAME );
             return dateEffectiveProperty.getDate();
-        }
-        catch(PathNotFoundException e) {
+        } catch ( PathNotFoundException e ) {
             // doesn't have this property
             return null;
+        } catch ( Exception e ) {
+            log.error( "Caught Exception",
+                       e );
+            throw new RulesRepositoryException( e );
         }
-        catch(Exception e) {
-            log.error("Caught Exception", e);
-            throw new RulesRepositoryException(e);
-        }
     }
-    
+
     /**
      * Creates a new version of this object's rule node, updating the effective date for the
      * rule node. 
@@ -157,41 +154,37 @@
      */
     public void updateDateEffective(Calendar newDateEffective) throws RulesRepositoryException {
         checkIsUpdateable();
-        checkout();        
-        try {                                    
-            this.node.setProperty(DATE_EFFECTIVE_PROPERTY_NAME, newDateEffective);
+        checkout();
+        try {
+            this.node.setProperty( DATE_EFFECTIVE_PROPERTY_NAME,
+                                   newDateEffective );
+        } catch ( RepositoryException e ) {
+            log.error( "Caught Exception",
+                       e );
+            throw new RulesRepositoryException( e );
         }
-        catch(RepositoryException e) {
-            log.error("Caught Exception", e);
-            throw new RulesRepositoryException(e);
-        }
     }
 
-
-    
     /**
      * @return the date the rule becomes expired
      * @throws RulesRepositoryException
      */
     public Calendar getDateExpired() throws RulesRepositoryException {
-        try {                        
+        try {
             Node ruleNode = getVersionContentNode();
-                        
-            Property dateExpiredProperty = ruleNode.getProperty(DATE_EXPIRED_PROPERTY_NAME);
+
+            Property dateExpiredProperty = ruleNode.getProperty( DATE_EXPIRED_PROPERTY_NAME );
             return dateExpiredProperty.getDate();
-        }
-        catch(PathNotFoundException e) {
+        } catch ( PathNotFoundException e ) {
             // doesn't have this property
             return null;
+        } catch ( Exception e ) {
+            log.error( "Caught Exception",
+                       e );
+            throw new RulesRepositoryException( e );
         }
-        catch(Exception e) {
-            log.error("Caught Exception", e);
-            throw new RulesRepositoryException(e);
-        }
     }
 
-
-    
     /**
      * Creates a new version of this object's rule node, updating the expired date for the
      * rule node. 
@@ -201,72 +194,50 @@
      */
     public void updateDateExpired(Calendar newDateExpired) throws RulesRepositoryException {
         checkout();
-        
-        try {                                    
-            this.node.setProperty(DATE_EXPIRED_PROPERTY_NAME, newDateExpired);
+
+        try {
+            this.node.setProperty( DATE_EXPIRED_PROPERTY_NAME,
+                                   newDateExpired );
+        } catch ( Exception e ) {
+            log.error( "Caught Exception",
+                       e );
+            throw new RulesRepositoryException( e );
         }
-        catch(Exception e) {
-            log.error("Caught Exception", e);
-            throw new RulesRepositoryException(e);
-        }
     }
-    
+
     /**
-     * @return the rule language of this object's rune node
-     * @throws RulesRepositoryException
-     */
-    public String getRuleLanguage() throws RulesRepositoryException {
-        try {                        
-            Node ruleNode = getVersionContentNode();
-                        
-            Property ruleLanguageProperty = ruleNode.getProperty(RULE_LANGUAGE_PROPERTY_NAME);
-            return ruleLanguageProperty.getValue().getString();
-        }
-        catch(Exception e) {
-            log.error("Caught Exception", e);
-            throw new RulesRepositoryException(e);
-        }
-    }
-    
-    /**
      * This will update the rules content (checking it out if it is not already).
      * This will not save the session or create a new version of the node 
      * (this has to be done seperately, as several properties may change as part of one edit).
      */
     public RuleItem updateRuleContent(String newRuleContent) throws RulesRepositoryException {
-        checkout();        
-        try {                                    
-            this.node.setProperty(RULE_CONTENT_PROPERTY_NAME, newRuleContent);
+        checkout();
+        try {
+            this.node.setProperty( RULE_CONTENT_PROPERTY_NAME,
+                                   newRuleContent );
             return this;
+        } catch ( RepositoryException e ) {
+            log.error( "Caught Exception",
+                       e );
+            throw new RulesRepositoryException( e );
         }
-        catch(RepositoryException e) {
-            log.error("Caught Exception", e);
-            throw new RulesRepositoryException(e);
-        }
     }
-    
+
     /**
      * The URI represents a location for 
      */
     public void updateRuleContentURI(String newURI) throws RulesRepositoryException {
-        checkout();        
-        try {                                    
-            this.node.setProperty(RULE_CONTENT_URI_PROPERTY_NAME, newURI);
+        checkout();
+        try {
+            this.node.setProperty( RULE_CONTENT_URI_PROPERTY_NAME,
+                                   newURI );
+        } catch ( RepositoryException e ) {
+            log.error( "Caught Exception",
+                       e );
+            throw new RulesRepositoryException( e );
         }
-        catch(RepositoryException e) {
-            log.error("Caught Exception", e);
-            throw new RulesRepositoryException(e);
-        }
-    }        
-    
+    }
 
-
-
-
-    
-
-    
-
     /**
      * Gets a DslItem object corresponding to the DSL reference from the node that this object
      * encapsulates.
@@ -277,89 +248,96 @@
      */
     public DslItem getDsl() throws RulesRepositoryException {
         try {
-            Property dslProperty = getVersionContentNode().getProperty(DSL_PROPERTY_NAME);
-            Node dslNode = this.node.getSession().getNodeByUUID(dslProperty.getString());
-            return new DslItem(this.rulesRepository, dslNode);
-        }
-        catch(PathNotFoundException e) {
+            Property dslProperty = getVersionContentNode().getProperty( DSL_PROPERTY_NAME );
+            Node dslNode = this.node.getSession().getNodeByUUID( dslProperty.getString() );
+            return new DslItem( this.rulesRepository,
+                                dslNode );
+        } catch ( PathNotFoundException e ) {
             //not set
             return null;
+        } catch ( Exception e ) {
+            log.error( "Caught exception",
+                       e );
+            throw new RulesRepositoryException( e );
         }
-        catch(Exception e) {
-            log.error("Caught exception", e);
-            throw new RulesRepositoryException(e);
-        }
     }
-    
+
     /**
      * Nicely formats the information contained by the node that this object encapsulates
      */
-    public String toString() {                
+    public String toString() {
         try {
             StringBuffer returnString = new StringBuffer();
-            returnString.append("Content of rule item named '" + this.getName() + "':\n");
-            returnString.append("Content: " + this.getRuleContent() + "\n");
-            returnString.append("Content URI: " + this.getRuleContentURI() + "\n");
-            returnString.append("------\n");
-            
-            returnString.append("Date Effective: " + this.getDateEffective() + "\n");
-            returnString.append("Date Expired: " + this.getDateExpired() + "\n");
-            returnString.append("Rule Language: " + this.getRuleLanguage() + "\n");
-            returnString.append("Version Name: " + this.getVersionName() + "\n");
-            returnString.append("------\n");
-            
-            returnString.append("Rule state: ");
+            returnString.append( "Content of rule item named '" + this.getName() + "':\n" );
+            returnString.append( "Content: " + this.getRuleContent() + "\n" );
+            returnString.append( "Content URI: " + this.getRuleContentURI() + "\n" );
+            returnString.append( "------\n" );
+
+            returnString.append( "Date Effective: " + this.getDateEffective() + "\n" );
+            returnString.append( "Date Expired: " + this.getDateExpired() + "\n" );
+            returnString.append( "Version Name: " + this.getVersionName() + "\n" );
+            returnString.append( "------\n" );
+
+            returnString.append( "Rule state: " );
             StateItem stateItem = this.getState();
-            if(stateItem != null) {
-                returnString.append(this.getState().getName() + "\n");
+            if ( stateItem != null ) {
+                returnString.append( this.getState().getName() + "\n" );
+            } else {
+                returnString.append( "NO STATE SET FOR THIS NODE\n" );
             }
-            else {
-                returnString.append("NO STATE SET FOR THIS NODE\n");
-            }            
-            returnString.append("------\n");
-            
-            returnString.append("Rule tags:\n");
-            for(Iterator it=this.getCategories().iterator(); it.hasNext();) {
-                CategoryItem currentTag = (CategoryItem)it.next();
-                returnString.append(currentTag.getName() + "\n");
+            returnString.append( "------\n" );
+
+            returnString.append( "Rule tags:\n" );
+            for ( Iterator it = this.getCategories().iterator(); it.hasNext(); ) {
+                CategoryItem currentTag = (CategoryItem) it.next();
+                returnString.append( currentTag.getName() + "\n" );
             }
-            returnString.append("--------------\n");
+            returnString.append( "--------------\n" );
             return returnString.toString();
+        } catch ( Exception e ) {
+            throw new RulesRepositoryException( e );
         }
-        catch(Exception e) {         
-            throw new RulesRepositoryException(e);
-        }
     }
-        
+
     public VersionableItem getPrecedingVersion() throws RulesRepositoryException {
         try {
             Node precedingVersionNode = this.getPrecedingVersionNode();
-            if(precedingVersionNode != null) {
-                return new RuleItem(this.rulesRepository, precedingVersionNode);
-            }
-            else {
+            if ( precedingVersionNode != null ) {
+                return new RuleItem( this.rulesRepository,
+                                     precedingVersionNode );
+            } else {
                 return null;
             }
-        }        
-        catch(Exception e) {
-            log.error("Caught exception", e);
-            throw new RulesRepositoryException(e);
-        }               
+        } catch ( Exception e ) {
+            log.error( "Caught exception",
+                       e );
+            throw new RulesRepositoryException( e );
+        }
     }
 
     public VersionableItem getSucceedingVersion() throws RulesRepositoryException {
         try {
             Node succeedingVersionNode = this.getSucceedingVersionNode();
-            if(succeedingVersionNode != null) {
-                return new RuleItem(this.rulesRepository, succeedingVersionNode);
-            }
-            else {
+            if ( succeedingVersionNode != null ) {
+                return new RuleItem( this.rulesRepository,
+                                     succeedingVersionNode );
+            } else {
                 return null;
             }
-        }        
-        catch(Exception e) {
-            log.error("Caught exception", e);
-            throw new RulesRepositoryException(e);
+        } catch ( Exception e ) {
+            log.error( "Caught exception",
+                       e );
+            throw new RulesRepositoryException( e );
         }
-    }           
+    }
+
+    /**
+     * Get the name of the enclosing package.
+     * As assets are stored in versionable subfolders, this means walking up 2 levels in the 
+     * hierarchy to get to the enclosing "package" node.
+     */
+    public String getPackageName() {
+            return super.getStringProperty( PACKAGE_NAME_PROPERTY );
+    }
+
 }
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RulePackageItem.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RulePackageItem.java	2006-11-03 10:52:23 UTC (rev 7358)
+++ labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RulePackageItem.java	2006-11-03 10:59:56 UTC (rev 7359)
@@ -118,18 +118,20 @@
                                   "Initial" );
 
             Calendar lastModified = Calendar.getInstance();
-            ruleNode.setProperty( RuleItem.LAST_MODIFIED_PROPERTY_NAME,
-                                  lastModified );
             
+            ruleNode.setProperty( RuleItem.LAST_MODIFIED_PROPERTY_NAME, lastModified );            
+            ruleNode.setProperty( RuleItem.CREATION_DATE_PROPERTY, lastModified );
             
+            ruleNode.setProperty( RuleItem.PACKAGE_NAME_PROPERTY, this.getName() );
             
-
-            RuleItem rule = new RuleItem( this.rulesRepository,
-                                          ruleNode );
+            RuleItem rule = new RuleItem( this.rulesRepository, ruleNode );
+            
             if (initialCategory != null) {
                 rule.addCategory( initialCategory );
             }
-            //rule.setState( "Draft" );
+            
+            
+            
             this.rulesRepository.save();
             
             rule.checkin( "Initial" );
@@ -157,6 +159,8 @@
         }
     }
 
+    
+    
     // The following should be kept for reference on how to add a reference that 
     //is either locked to a version or follows head - FOR SHARING RULES
     //    /**

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-11-03 10:52:23 UTC (rev 7358)
+++ labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RulesRepository.java	2006-11-03 10:59:56 UTC (rev 7359)
@@ -613,6 +613,7 @@
             Calendar lastModified = Calendar.getInstance();
             rulePackageNode.setProperty(RulePackageItem.LAST_MODIFIED_PROPERTY_NAME, lastModified);
             
+            rulePackageNode.setProperty( RulePackageItem.CREATION_DATE_PROPERTY, lastModified );
             
             this.session.save();
             
@@ -685,6 +686,8 @@
     }
 
     
+    
+    
     /**
      * This will retrieve a list of RuleItem objects - that are allocated to the 
      * provided category.
@@ -754,6 +757,38 @@
     }
 
 
+    /**
+     * This moves a rule asset from one package to another, preserving history etc etc.
+     * 
+     * @param newPackage The destination package.
+     * @param uuid The UUID of the rule
+     * @param explanation The reason (which will be added as the checkin message).
+     */
+    public void moveRuleItemPackage(String newPackage, String uuid, String explanation) {
+        try {
+            RuleItem item = loadRuleByUUID( uuid );
+            String oldPackage = item.getPackageName();
+            RulePackageItem sourcePkg = loadRulePackage( oldPackage );
+            RulePackageItem destPkg = loadRulePackage( newPackage );
+            
+            String sourcePath = item.node.getPath();
+            String destPath = destPkg.node.getPath() + "/" + RulePackageItem.RULES_FOLDER_NAME + "/" + item.getName(); 
+            
+            this.session.move(sourcePath , destPath );
+            
+            item.checkout();
+            item.node.setProperty( RuleItem.PACKAGE_NAME_PROPERTY, newPackage );
+            
+            item.checkin( explanation );            
+            sourcePkg.checkin( explanation );
+            destPkg.checkin( explanation );
+            
+        } catch ( RepositoryException e ) {
+            throw new RulesRepositoryException(e);
+        }
+        
+        
+    }
 
 
 

Modified: labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/VersionableItem.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/VersionableItem.java	2006-11-03 10:52:23 UTC (rev 7358)
+++ labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/VersionableItem.java	2006-11-03 10:59:56 UTC (rev 7359)
@@ -8,6 +8,7 @@
 import javax.jcr.RepositoryException;
 import javax.jcr.UnsupportedRepositoryOperationException;
 import javax.jcr.Value;
+import javax.jcr.version.Version;
 
 import org.drools.repository.util.VersionNumberGenerator;
 
@@ -47,13 +48,15 @@
     /**
      * The name of the state property on the rule node type
      */
-    public static final String STATE_PROPERTY_NAME = "drools:stateReference";
-    
+    public static final String STATE_PROPERTY_NAME            = "drools:stateReference";
+
     /**
      * The name of the tag property on the rule node type
      */
-    public static final String CATEGORY_PROPERTY_NAME              = "drools:categoryReference";
+    public static final String CATEGORY_PROPERTY_NAME         = "drools:categoryReference";
 
+    public static final String CREATION_DATE_PROPERTY         = "drools:createdDate";
+
     /**
      * The possible formats for the format property of the node
      */
@@ -229,44 +232,50 @@
                               TITLE_PROPERTY_NAME );
     }
 
-
     public void updateLastContributor(String contibName) {
-        updateStringProperty( contibName, LAST_CONTRIBUTOR_PROPERTY_NAME );
+        updateStringProperty( contibName,
+                              LAST_CONTRIBUTOR_PROPERTY_NAME );
     }
 
-    
     public void updateType(String type) {
-        updateStringProperty( type, TYPE_PROPERTY_NAME );
+        updateStringProperty( type,
+                              TYPE_PROPERTY_NAME );
     }
-    
+
     public void updateExternalSource(String source) {
-        updateStringProperty( source, SOURCE_PROPERTY_NAME );
+        updateStringProperty( source,
+                              SOURCE_PROPERTY_NAME );
     }
-    
+
     public void updateSubject(String sub) {
-        updateStringProperty( sub, SUBJECT_PROPERTY_NAME );
+        updateStringProperty( sub,
+                              SUBJECT_PROPERTY_NAME );
     }
-    
+
     public void updateExternalRelation(String rel) {
-        updateStringProperty( rel, RELATION_PROPERTY_NAME );
+        updateStringProperty( rel,
+                              RELATION_PROPERTY_NAME );
     }
-    
+
     public void updateRights(String rights) {
-        updateStringProperty( rights, RIGHTS_PROPERTY_NAME );
+        updateStringProperty( rights,
+                              RIGHTS_PROPERTY_NAME );
     }
-    
+
     public void updateCoverage(String cov) {
-        updateStringProperty( cov, COVERAGE_PROPERTY_NAME );
+        updateStringProperty( cov,
+                              COVERAGE_PROPERTY_NAME );
     }
-    
+
     public void updatePublisher(String pub) {
-        updateStringProperty( pub, PUBLISHER_PROPERTY_NAME );
+        updateStringProperty( pub,
+                              PUBLISHER_PROPERTY_NAME );
     }
-    
+
     /**
      * update a text field.
      */
-    private void updateStringProperty(String value,
+    protected void updateStringProperty(String value,
                                       String prop) {
         try {
             checkIsUpdateable();
@@ -279,7 +288,7 @@
                                    lastModified );
 
         } catch ( Exception e ) {
-            if (e instanceof RuntimeException) {
+            if ( e instanceof RuntimeException ) {
                 throw (RuntimeException) e;
             }
             throw new RulesRepositoryException( e );
@@ -294,15 +303,7 @@
      * @throws RulesRepositoryException
      */
     public String getDescription() throws RulesRepositoryException {
-        try {
-
-            Property data = getVersionContentNode().getProperty( DESCRIPTION_PROPERTY_NAME );
-            return data.getValue().getString();
-        } catch ( Exception e ) {
-            log.error( "Caught Exception",
-                       e );
-            throw new RulesRepositoryException( e );
-        }
+            return getStringProperty( DESCRIPTION_PROPERTY_NAME );
     }
 
     /**
@@ -310,15 +311,17 @@
      * can provide an implementation of VersionNumberGenerator if needed).
      */
     public String getVersionNumber() {
-        try {
-            if ( getVersionContentNode().hasProperty( VERSION_NUMBER_PROPERTY_NAME ) ) {
-                return getVersionContentNode().getProperty( VERSION_NUMBER_PROPERTY_NAME ).getString();
-            } else {
-                return null;
-            }
-        } catch ( RepositoryException e ) {
-            throw new RulesRepositoryException( e );
-        }
+//        try {
+//            if ( getVersionContentNode().hasProperty( VERSION_NUMBER_PROPERTY_NAME ) ) {
+//                return getVersionContentNode().getProperty( VERSION_NUMBER_PROPERTY_NAME ).getString();
+//            } else {
+//                return null;
+//            }
+//        } catch ( RepositoryException e ) {
+//            throw new RulesRepositoryException( e );
+//        }
+        
+        return getStringProperty( VERSION_NUMBER_PROPERTY_NAME );
     }
 
     /**
@@ -341,7 +344,6 @@
      */
     public Calendar getLastModified() throws RulesRepositoryException {
         try {
-
             Property lastModifiedProperty = getVersionContentNode().getProperty( LAST_MODIFIED_PROPERTY_NAME );
             return lastModifiedProperty.getDate();
         } catch ( Exception e ) {
@@ -398,9 +400,10 @@
             throw new RulesRepositoryException( e );
         }
     }
-    
+
     public void updateFormat(String newFormat) {
-        this.updateStringProperty( newFormat, FORMAT_PROPERTY_NAME );
+        this.updateStringProperty( newFormat,
+                                   FORMAT_PROPERTY_NAME );
     }
 
     /**
@@ -411,18 +414,23 @@
     public Node getVersionContentNode() throws RepositoryException,
                                        PathNotFoundException {
         if ( this.contentNode == null ) {
-
-            if ( this.node.getPrimaryNodeType().getName().equals( "nt:version" ) ) {
-                contentNode = this.node.getNode( "jcr:frozenNode" );
-            } else {
-                contentNode = this.node;
-            }
-
+            this.contentNode = getRealContentFromVersion(this.node);
         }
-
         return contentNode;
     }
 
+    /**
+     * This deals with a node which *may* be a version, if it is, it grabs the frozen copy.
+     */
+    protected Node getRealContentFromVersion(Node node) throws RepositoryException,
+                                            PathNotFoundException {
+        if ( node.getPrimaryNodeType().getName().equals( "nt:version" ) ) {
+            return node.getNode( "jcr:frozenNode" );
+        } else {
+            return node;
+        }
+    }
+
     /** 
      * Need to get the name from the content node, not the version node
      * if it is in fact a version ! 
@@ -504,7 +512,6 @@
         }
     }
 
-    
     /**
      * Sets this object's rule node's state property to refer to the specified state node
      * 
@@ -513,19 +520,19 @@
      */
     public void updateState(String stateName) throws RulesRepositoryException {
         try {
-            
+
             //now set the state property of the rule                              
             checkout();
-            
-            StateItem stateItem = this.rulesRepository.getState(stateName);
-            this.updateState(stateItem);
+
+            StateItem stateItem = this.rulesRepository.getState( stateName );
+            this.updateState( stateItem );
+        } catch ( Exception e ) {
+            log.error( "Caught exception",
+                       e );
+            throw new RulesRepositoryException( e );
         }
-        catch(Exception e) {
-            log.error("Caught exception", e);
-            throw new RulesRepositoryException(e);
-        }
     }
-    
+
     /**
      * Sets this object's rule node's state property to refer to the specified StateItem's node
      * 
@@ -537,22 +544,23 @@
         checkIsUpdateable();
         try {
             //make sure this node is a rule node
-            if(this.node.getPrimaryNodeType().getName().equals("nt:version")) {
+            if ( this.node.getPrimaryNodeType().getName().equals( "nt:version" ) ) {
                 String message = "Error. States can only be set for the head version of a rule node";
-                log.error(message);
-                throw new RulesRepositoryException(message);
-            } 
-            
+                log.error( message );
+                throw new RulesRepositoryException( message );
+            }
+
             //now set the state property of the rule                              
             checkout();
-            this.node.setProperty(STATE_PROPERTY_NAME, stateItem.getNode());
+            this.node.setProperty( STATE_PROPERTY_NAME,
+                                   stateItem.getNode() );
+        } catch ( Exception e ) {
+            log.error( "Caught exception",
+                       e );
+            throw new RulesRepositoryException( e );
         }
-        catch(Exception e) {
-            log.error("Caught exception", e);
-            throw new RulesRepositoryException(e);
-        }
     }
-    
+
     /**
      * Gets StateItem object corresponding to the state property of this object's node
      * 
@@ -563,74 +571,73 @@
     public StateItem getState() throws RulesRepositoryException {
         try {
             Node content = getVersionContentNode();
-            Property stateProperty = content.getProperty(STATE_PROPERTY_NAME);
-            Node stateNode = this.rulesRepository.getSession().getNodeByUUID(stateProperty.getString());
-            return new StateItem(this.rulesRepository, stateNode);
-        }
-        catch(PathNotFoundException e) {
+            Property stateProperty = content.getProperty( STATE_PROPERTY_NAME );
+            Node stateNode = this.rulesRepository.getSession().getNodeByUUID( stateProperty.getString() );
+            return new StateItem( this.rulesRepository,
+                                  stateNode );
+        } catch ( PathNotFoundException e ) {
             //not set
             return null;
+        } catch ( Exception e ) {
+            log.error( "Caught exception",
+                       e );
+            throw new RulesRepositoryException( e );
         }
-        catch(Exception e) {
-            log.error("Caught exception", e);
-            throw new RulesRepositoryException(e);
-        }
     }
-    
+
     /**
      * This will return the current state item as a displayable thing.
      * If there is no state, it will be an empty string.
      */
     public String getStateDescription() {
         StateItem state = this.getState();
-        if (state == null) {
+        if ( state == null ) {
             return "";
         } else {
             return state.getName();
         }
-    }    
-    
+    }
+
     /** Compare this rules state with some other state */
     public boolean sameState(StateItem other) {
         StateItem thisState = getState();
-        if (thisState == other) {
+        if ( thisState == other ) {
             return true;
-        } else if (thisState != null){
+        } else if ( thisState != null ) {
             return thisState.equals( other );
         } else {
             return false;
         }
     }
-    
-    
+
     /**
      * Returns the last contributors name.
      */
     public String getLastContributor() {
         return getStringProperty( LAST_CONTRIBUTOR_PROPERTY_NAME );
     }
-    
+
     /**
      * This is the person who initially created the resource.
      */
     public String getCreator() {
         return getStringProperty( CREATOR_PROPERTY_NAME );
     }
-    
+
     /**
      * This is the Dublin Core field of type (a broad classification of resource type).
      */
     public String getType() {
-       return getStringProperty( TYPE_PROPERTY_NAME ); 
+        return getStringProperty( TYPE_PROPERTY_NAME );
     }
-    
+
     /**
      * This is the source of the asset/rule. Ie a human description of where it came from.
      */
     public String getExternalSource() {
-        return getStringProperty( SOURCE_PROPERTY_NAME );       
+        return getStringProperty( SOURCE_PROPERTY_NAME );
     }
-    
+
     /**
      * Typically, 
      * Subject will be expressed as keywords, 
@@ -646,23 +653,22 @@
     public String getExternalRelation() {
         return getStringProperty( RELATION_PROPERTY_NAME );
     }
-    
+
     /**
      * Optionally contains any copyright/ownership rights for the asset.
      */
     public String getRights() {
         return getStringProperty( RIGHTS_PROPERTY_NAME );
     }
-    
+
     /**
      * Typically, Coverage will include spatial location 
      * (a place name or geographic coordinates), temporal period (a period label, date, or date range) or jurisdiction (such as a named administrative entity). Recommended best practice is to select a value from a controlled vocabulary (for example, the Thesaurus of Geographic Names [TGN]) and to use, where appropriate, named places or time periods in preference to numeric identifiers such as sets of coordinates or date ranges.
      */
     public String getCoverage() {
-        return getStringProperty( COVERAGE_PROPERTY_NAME );        
+        return getStringProperty( COVERAGE_PROPERTY_NAME );
     }
-    
-    
+
     /**
      *  Examples of Publisher include a person, an organization, or a service. 
      *  Typically, the name of a Publisher should be used to indicate the entity.
@@ -671,20 +677,32 @@
         return getStringProperty( PUBLISHER_PROPERTY_NAME );
     }
 
-    private String getStringProperty(String property) {
+    /**
+     * This returns the date/time on which the asset was "ORIGINALLY CREATED".
+     * Kinda handy if you want to know how old something is.
+     */
+    public Calendar getCreatedDate() {
+        Property prop;
         try {
-            Node theNode = getVersionContentNode();
-            Property data = theNode.getProperty( property );
-            return data.getValue().getString();
+            prop = getVersionContentNode().getProperty( CREATION_DATE_PROPERTY );
+            return prop.getDate();
+        } catch ( RepositoryException e ) {
+            throw new RulesRepositoryException( e );
+        }
 
-        } catch ( Exception e ) {
-            if ( e instanceof RuntimeException ) {
-                throw (RuntimeException) e;
+    }
 
+    protected String getStringProperty(String property) {
+        try {
+            Node theNode = getVersionContentNode();
+            if ( theNode.hasProperty( property ) ) {
+                Property data = theNode.getProperty( property );
+                return data.getValue().getString();
             } else {
-                throw new RulesRepositoryException( e );
+                return "";
             }
+        } catch ( RepositoryException e ) {
+            throw new RulesRepositoryException( e );
         }
     }
-
 }

Modified: labs/jbossrules/trunk/drools-repository/src/main/resources/node_type_definitions/rule_node_type.cnd
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/main/resources/node_type_definitions/rule_node_type.cnd	2006-11-03 10:52:23 UTC (rev 7358)
+++ labs/jbossrules/trunk/drools-repository/src/main/resources/node_type_definitions/rule_node_type.cnd	2006-11-03 10:59:56 UTC (rev 7359)
@@ -27,9 +27,9 @@
 
 - drools:dateExpired (date)
   
-- drools:ruleLanguage (string)
-  = 'DRL'
-  mandatory autocreated
+- drools:packageName (string)
+  mandatory  
+  
 
 - drools:dslReference (reference)
   copy 

Modified: labs/jbossrules/trunk/drools-repository/src/main/resources/node_type_definitions/versionable_node_type.cnd
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/main/resources/node_type_definitions/versionable_node_type.cnd	2006-11-03 10:52:23 UTC (rev 7358)
+++ labs/jbossrules/trunk/drools-repository/src/main/resources/node_type_definitions/versionable_node_type.cnd	2006-11-03 10:59:56 UTC (rev 7359)
@@ -43,7 +43,8 @@
 //for incrementing (or user defined) unique version numbers
 - drools:versionNumber (string)
   
-
+//to remember when it was created  
+- drools:createdDate (date)  
   
 - drools:checkinComment (string)
 

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-11-03 10:52:23 UTC (rev 7358)
+++ labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RuleItemTestCase.java	2006-11-03 10:59:56 UTC (rev 7359)
@@ -17,8 +17,9 @@
         return getRepo().loadDefaultRulePackage();
     }
     
-    public void testRuleItem() {
-            //calls constructor
+    public void testRuleItemCreation() throws Exception {
+                
+            Calendar now = Calendar.getInstance();
         
             RuleItem ruleItem1 = getDefaultPackage().addRule("testRuleItem", "test content");
             
@@ -26,6 +27,14 @@
             assertNotNull(ruleItem1.getNode());
             assertEquals("testRuleItem", ruleItem1.getName());
         
+            assertNotNull(ruleItem1.getCreatedDate());
+            assertTrue(now.before( ruleItem1.getCreatedDate() ));
+            
+            String packName = getDefaultPackage().getName();
+
+            assertEquals(packName, ruleItem1.getPackageName());
+            
+            
         //try constructing with node of wrong type
         try {
             
@@ -286,17 +295,6 @@
         }
     }
     
-    public void testGetRuleLanguage() {
-        try {
-            RuleItem ruleItem1 = getRepo().loadDefaultRulePackage().addRule("testGetRuleLanguage", "test content");
-           
-            //it should be initialized to 'DRL'
-            assertEquals("DRL", ruleItem1.getRuleLanguage());                        
-        }
-        catch(Exception e) {
-            fail("Caught unexpected exception: " + e);
-        }
-    }
     
     public void testSaveAndCheckinDescriptionAndTitle() throws Exception {
             RuleItem ruleItem1 = getRepo().loadDefaultRulePackage().addRule("testGetDescription", "");
@@ -333,8 +331,14 @@
     }
     
     public void testGetPrecedingVersion() {
+        
+            getRepo().loadCategory( "/" ).addCategory( "foo", "ka" );
             RuleItem ruleItem1 = getRepo().loadDefaultRulePackage().addRule("testGetPrecedingVersion", "descr");
-            assertTrue(ruleItem1.getPrecedingVersion() == null);   
+            assertTrue(ruleItem1.getPrecedingVersion() == null);
+            
+            
+            
+            ruleItem1.addCategory( "foo" );
             ruleItem1.updateRuleContent( "test content" );
             ruleItem1.checkin( "boo" );
             
@@ -342,17 +346,27 @@
             RuleItem predecessorRuleItem = (RuleItem) ruleItem1.getPrecedingVersion();
             assertNotNull(predecessorRuleItem);            
             
+            
+            
             ruleItem1.updateRuleContent("new content");
             ruleItem1.updateRuleContentURI( "foobar" );
             ruleItem1.checkin( "two changes" );
             
             predecessorRuleItem = (RuleItem) ruleItem1.getPrecedingVersion();
             assertNotNull(predecessorRuleItem);
+            assertEquals(1, predecessorRuleItem.getCategories().size());
+            CategoryItem cat = (CategoryItem) predecessorRuleItem.getCategories().get( 0 );
+            assertEquals("foo", cat.getName());
+            
+            
             assertEquals("test content", predecessorRuleItem.getRuleContent());
             
+            assertEquals("descr", predecessorRuleItem.getDescription());
             
+            assertEquals("default", predecessorRuleItem.getPackageName());
             
             
+            
             ruleItem1.updateRuleContent("newer lhs");
             ruleItem1.checkin( "another" );
             
@@ -495,6 +509,9 @@
         assertEquals("b", ruleItem.getCoverage());
         assertEquals("me", ruleItem.getLastContributor());
         
+        assertEquals("", ruleItem.getExternalRelation());
+        assertEquals("", ruleItem.getExternalSource());
+        
     }
     
     public void testGetFormat() {        

Modified: labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulePackageItemTestCase.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulePackageItemTestCase.java	2006-11-03 10:52:23 UTC (rev 7358)
+++ labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulePackageItemTestCase.java	2006-11-03 10:59:56 UTC (rev 7359)
@@ -282,7 +282,7 @@
     }
 
     
-    private List iteratorToList(Iterator it) {
+    List iteratorToList(Iterator it) {
         List list = new ArrayList();
         while(it.hasNext()) {
             list.add( it.next() );

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-11-03 10:52:23 UTC (rev 7358)
+++ labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulesRepositoryTestCase.java	2006-11-03 10:59:56 UTC (rev 7359)
@@ -1,7 +1,9 @@
 package org.drools.repository;
 
+import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Iterator;
+import java.util.List;
 
 import javax.jcr.NodeIterator;
 
@@ -192,4 +194,43 @@
             assertTrue(found);
             
     }
+    
+    public void testMoveRulePackage() throws Exception {
+        RulesRepository repo = RepositorySession.getRepository();
+        RulePackageItem pkg = repo.createRulePackage( "testMove", "description" );
+        RuleItem r = pkg.addRule( "testMove", "description" );
+        
+        assertEquals("testMove", r.getPackageName());
+        
+        repo.save();
+        
+        assertEquals(1, iteratorToList( pkg.getRules()).size());
+        
+        repo.createRulePackage( "testMove2", "description" );
+        repo.moveRuleItemPackage( "testMove2", r.node.getUUID(), "explanation" );
+        
+        pkg = repo.loadRulePackage( "testMove" );
+        assertEquals(0, iteratorToList( pkg.getRules() ).size());
+        
+        pkg = repo.loadRulePackage( "testMove2" );
+        assertEquals(1, iteratorToList( pkg.getRules() ).size());
+        
+        r = (RuleItem) pkg.getRules().next();
+        assertEquals("testMove", r.getName());
+        assertEquals("testMove2", r.getPackageName());
+        assertEquals("explanation", r.getCheckinComment());
+        
+        RuleItem p = (RuleItem) r.getPrecedingVersion();
+        assertEquals("testMove", p.getPackageName());
+        assertEquals("Initial", p.getCheckinComment());
+        
+    }
+    
+    List iteratorToList(Iterator it) {
+        List list = new ArrayList();
+        while(it.hasNext()) {
+            list.add( it.next() );
+        }
+        return list;
+    }
 }




More information about the jboss-svn-commits mailing list