[jboss-svn-commits] JBL Code SVN: r9625 - in labs/jbossrules/trunk/drools-repository/src: main/resources/node_type_definitions and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Feb 20 00:19:46 EST 2007


Author: michael.neale at jboss.com
Date: 2007-02-20 00:19:46 -0500 (Tue, 20 Feb 2007)
New Revision: 9625

Modified:
   labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/AssetItem.java
   labs/jbossrules/trunk/drools-repository/src/main/resources/node_type_definitions/rule_node_type.cnd
   labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/AssetItemTest.java
Log:
JBRULES-668 model upload

Modified: labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/AssetItem.java
===================================================================
--- labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/AssetItem.java	2007-02-20 01:07:34 UTC (rev 9624)
+++ labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/AssetItem.java	2007-02-20 05:19:46 UTC (rev 9625)
@@ -1,22 +1,14 @@
 package org.drools.repository;
 
-import java.util.ArrayList;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.Calendar;
 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.ValueFormatException;
-import javax.jcr.lock.LockException;
-import javax.jcr.nodetype.ConstraintViolationException;
-import javax.jcr.version.VersionException;
 
 import org.apache.log4j.Logger;
 
@@ -34,8 +26,9 @@
     public static final String RULE_NODE_TYPE_NAME          = "drools:ruleNodeType";
 
     public static final String CONTENT_PROPERTY_NAME        = "drools:content";
-
-
+    public static final String CONTENT_PROPERTY_BINARY_NAME = "drools:binaryContent";
+    public static final String CONTENT_PROPERTY_ATTACHMENT_FILENAME = "drools:attachmentFileName";
+    
     /**
      * The name of the date effective property on the rule node type
      */
@@ -47,6 +40,8 @@
     public static final String DATE_EXPIRED_PROPERTY_NAME   = "drools:dateExpired";
 
     public static final String PACKAGE_NAME_PROPERTY        = "drools:packageName";
+    
+    
 
     /**
      * Constructs a RuleItem object, setting its node attribute to the specified node.
@@ -79,9 +74,8 @@
     }
 
     /**
-     * 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.
+     * returns the string contents of the rule node.
+     * If this is a binary asset, this will return null (use getBinaryContent instead).
      */
     public String getContent() throws RulesRepositoryException {
         try {
@@ -99,6 +93,70 @@
             throw new RulesRepositoryException( e );
         }
     }
+    
+    /**
+     * If this asset contains binary data, this is how you return it. 
+     * Otherwise it will return null.
+     */
+    public InputStream getBinaryContentAttachment() {
+        try {
+            Node ruleNode = getVersionContentNode();
+            if ( ruleNode.hasProperty( CONTENT_PROPERTY_BINARY_NAME ) ) {
+                Property data = ruleNode.getProperty( CONTENT_PROPERTY_BINARY_NAME );
+                return data.getStream();
+            } else {
+                return null;
+            }
+        } catch ( Exception e ) {
+            log.error( "Caught Exception",
+                       e );
+            throw new RulesRepositoryException( e );
+        }        
+    }
+    
+    /** Get the name of the "file" attachment, if one is set. Null otherwise */
+    public String getBinaryContentAttachmentFileName() {
+        return getStringProperty( CONTENT_PROPERTY_ATTACHMENT_FILENAME );
+    }
+    
+    /**
+     * This is a convenience method for returning the binary data as a byte array.
+     */
+    public byte[] getBinaryContentAsBytes() {
+        try {
+            Node ruleNode = getVersionContentNode();
+            if ( ruleNode.hasProperty( CONTENT_PROPERTY_BINARY_NAME ) ) {
+                Property data = ruleNode.getProperty( CONTENT_PROPERTY_BINARY_NAME );
+                InputStream in = data.getStream();
+                
+                // Create the byte array to hold the data
+                byte[] bytes = new byte[(int) data.getLength()];
+            
+                // Read in the bytes
+                int offset = 0;
+                int numRead = 0;
+                while (offset < bytes.length
+                       && (numRead=in.read(bytes, offset, bytes.length-offset)) >= 0) {
+                    offset += numRead;
+                }
+            
+                // Ensure all the bytes have been read in
+                if (offset < bytes.length) {
+                    throw new RulesRepositoryException("Could not completely read asset "+ getName());
+                }
+            
+                // Close the input stream and return bytes
+                in.close();   
+                return bytes;
+            } else {
+                return null;
+            }
+        } catch ( Exception e ) {
+            log.error( e );
+            if (e instanceof RuntimeException) throw (RuntimeException) e;
+            throw new RulesRepositoryException( e );
+        }  
+    }
 
 
     /**
@@ -182,9 +240,11 @@
     }
 
     /**
-     * This will update the rules content (checking it out if it is not already).
+     * This will update the asset's 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).
+     * This is only used if the asset is a textual asset. For binary, use the updateBinaryContent method
+     * instead. 
      */
     public AssetItem updateContent(String newRuleContent) throws RulesRepositoryException {
         checkout();
@@ -193,11 +253,32 @@
                                    newRuleContent );
             return this;
         } catch ( RepositoryException e ) {
-            log.error( "Caught Exception",
-                       e );
+            log.error( "Unable to update the asset content", e );
             throw new RulesRepositoryException( e );
         }
     }
+    
+    /**
+     * If the asset is a binary asset, then use this to update the content
+     * (do NOT use text).
+     */
+    public AssetItem updateBinaryContentAttachment(InputStream data) {
+        checkout();
+        try {
+            this.node.setProperty( CONTENT_PROPERTY_BINARY_NAME, data );            
+            return this;
+        } catch (RepositoryException e ) {
+            log.error( "Unable to update the assets binary content", e );
+            throw new RulesRepositoryException( e );
+        }
+    }
+    
+    /**
+     * Optionally set the filename to be associated with the binary content.
+     */
+    public void updateBinaryContentAttachmentFileName(String name) {
+        updateStringProperty( name, CONTENT_PROPERTY_ATTACHMENT_FILENAME );
+    }
 
 
 

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	2007-02-20 01:07:34 UTC (rev 9624)
+++ labs/jbossrules/trunk/drools-repository/src/main/resources/node_type_definitions/rule_node_type.cnd	2007-02-20 05:19:46 UTC (rev 9625)
@@ -16,6 +16,8 @@
 
 // Properties:
 - drools:content (string)
+- drools:binaryContent (binary)
+- drools:attachmentFileName (string)
   
 // use this to capture fields which are not known ahead of time 
 - * (undefined)

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	2007-02-20 01:07:34 UTC (rev 9624)
+++ labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/AssetItemTest.java	2007-02-20 05:19:46 UTC (rev 9625)
@@ -1,5 +1,7 @@
 package org.drools.repository;
 
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
 import java.util.Calendar;
 import java.util.Iterator;
 import java.util.List;
@@ -598,11 +600,14 @@
         
     }
     
-    public void testGetFormat() {        
+    public void testGetFormat() throws Exception {        
             AssetItem ruleItem1 = getRepo().loadDefaultPackage().addAsset("testGetFormat", "test content");
-            
+            ruleItem1.updateContent( "la" );
             assertEquals(AssetItem.DEFAULT_CONTENT_FORMAT, ruleItem1.getFormat());     
             
+            assertTrue(ruleItem1.getNode().hasProperty( AssetItem.CONTENT_PROPERTY_NAME ));
+            assertFalse(ruleItem1.getNode().hasProperty( AssetItem.CONTENT_PROPERTY_BINARY_NAME ));            
+            
             ruleItem1.updateFormat( "blah" );
             assertEquals("blah", ruleItem1.getFormat());
     }        
@@ -626,4 +631,28 @@
         
     }
     
+    public void testBinaryAsset() throws Exception {
+        AssetItem item = getRepo().loadDefaultPackage().addAsset( "testBinaryAsset", "yeah" );
+        String data = "abc 123";
+        ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes());
+        item.updateBinaryContentAttachment( in );
+        item.updateBinaryContentAttachmentFileName( "x.x" );
+        in.close();
+        
+        assertFalse(item.getNode().hasProperty( AssetItem.CONTENT_PROPERTY_NAME ));
+        assertTrue(item.getNode().hasProperty( AssetItem.CONTENT_PROPERTY_BINARY_NAME ));
+        item.checkin( "lalalala" );
+        
+        
+        
+        item = getRepo().loadDefaultPackage().loadAsset( "testBinaryAsset" );
+        InputStream in2 = item.getBinaryContentAttachment();
+        assertNotNull(in2);
+        
+        byte[] data2 = item.getBinaryContentAsBytes();
+        assertEquals(data, new String(data2));
+        assertEquals("x.x", item.getBinaryContentAttachmentFileName());
+        
+    }
+    
 }




More information about the jboss-svn-commits mailing list