[jboss-svn-commits] JBL Code SVN: r12094 - 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
Wed May 23 00:02:38 EDT 2007


Author: michael.neale at jboss.com
Date: 2007-05-23 00:02:38 -0400 (Wed, 23 May 2007)
New Revision: 12094

Modified:
   labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RulesRepository.java
   labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulesRepositoryTest.java
Log:
JBRULES-868 (package copy feature)

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	2007-05-23 03:51:46 UTC (rev 12093)
+++ labs/jbossrules/trunk/drools-repository/src/main/java/org/drools/repository/RulesRepository.java	2007-05-23 04:02:38 UTC (rev 12094)
@@ -1,5 +1,5 @@
 package org.drools.repository;
- 
+
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
@@ -102,7 +102,8 @@
     /**
      * This requires a JCR session be setup, and the repository be configured.
      */
-    public RulesRepository(Session session) {
+    public RulesRepository(
+                           Session session) {
         this.session = session;
     }
 
@@ -121,9 +122,7 @@
      *         node.
      * @throws RulesRepositoryException
      */
-    protected static Node addNodeIfNew(Node parent,
-                                       String nodeName,
-                                       String type) throws RulesRepositoryException {
+    protected static Node addNodeIfNew(Node parent, String nodeName, String type) throws RulesRepositoryException {
         Node node;
         try {
             node = parent.getNode( nodeName );
@@ -132,16 +131,13 @@
             try {
                 log.debug( new StringBuilder().append( "Adding new node of type: " ).append( type ).append( " named: " ).append( nodeName ).append( " to parent node named " ).append( parent.getName() ) );
 
-                node = parent.addNode( nodeName,
-                                       type );
+                node = parent.addNode( nodeName, type );
             } catch ( Exception e1 ) {
-                log.error( "Caught Exception",
-                           e );
+                log.error( "Caught Exception", e );
                 throw new RulesRepositoryException( e1 );
             }
         } catch ( Exception e ) {
-            log.error( "Caught Exception",
-                       e );
+            log.error( "Caught Exception", e );
             throw new RulesRepositoryException( e );
         }
         return node;
@@ -172,8 +168,7 @@
                     log.error( "Unable to correct repository corruption" );
                 }
             } catch ( Exception e ) {
-                log.error( "Caught Exception",
-                           e );
+                log.error( "Caught Exception", e );
                 throw new RulesRepositoryException( "Caught exception " + e.getClass().getName(),
                                                     e );
             }
@@ -186,155 +181,150 @@
         return folderNode;
     }
 
-//    MN: This is kept for future reference showing how to tie references
-//    to a specific version when
-//    sharing assets.
-//
-//    /**
-//     * Adds a Rule node in the repository using the content specified,
-//     associating it with
-//     * the specified DSL node
-//     *
-//     * @param ruleName the name of the rule
-//     * @param lhsContent the lhs of the rule
-//     * @param rhsContent the rhs of the rule
-//     * @param dslItem the dslItem encapsulting the dsl node to associate
-//     this rule node with
-//     * @paaram followDslHead whether or not to follow the head revision of
-//     the dsl node
-//     * @return a RuleItem object encapsulating the node that gets added
-//     * @throws RulesRepositoryException
-//     */
-//    public RuleItem addRule(String ruleName, String ruleContent, DslItem
-//                            dslItem, boolean followDslHead) throws RulesRepositoryException {
-//        Node folderNode = this.getAreaNode(RULE_AREA);
-//
-//        try {
-//            //create the node - see section 6.7.22.6 of the spec
-//            Node ruleNode = folderNode.addNode(ruleName,
-//                                               RuleItem.RULE_NODE_TYPE_NAME);
-//
-//            ruleNode.setProperty(RuleItem.TITLE_PROPERTY_NAME, ruleName);
-//            ruleNode.setProperty(RuleItem.RULE_CONTENT_PROPERTY_NAME,
-//                                 ruleContent);
-//            ruleNode.setProperty(RuleItem.DESCRIPTION_PROPERTY_NAME, "");
-//            ruleNode.setProperty(RuleItem.FORMAT_PROPERTY_NAME,
-//                                 RuleItem.RULE_FORMAT);
-//
-//
-//            if(followDslHead) {
-//                ruleNode.setProperty(RuleItem.DSL_PROPERTY_NAME, dslItem.getNode());
-//            }
-//            else {
-//                //tie the ruleNode to specifically the current version of the dslNode
-//                ruleNode.setProperty(RuleItem.DSL_PROPERTY_NAME,
-//                                     dslItem.getNode().getBaseVersion());
-//            }
-//
-//            Calendar lastModified = Calendar.getInstance();
-//            ruleNode.setProperty(RuleItem.LAST_MODIFIED_PROPERTY_NAME,
-//                                 lastModified);
-//
-//            session.save();
-//
-//            try {
-//                ruleNode.checkin();
-//            }
-//            catch(UnsupportedRepositoryOperationException e) {
-//                String message = "Error: Caught
-//                    UnsupportedRepositoryOperationException when attempting to checkin
-//                    rule: " + ruleNode.getName() + ". Are you sure your JCR repository
-//                    supports versioning? ";
-//                        log.error(message + e);
-//                throw new RulesRepositoryException(message, e);
-//            }
-//
-//            return new RuleItem(this, ruleNode);
-//        }
-//        catch(Exception e) {
-//            log.error("Caught Exception", e);
-//            throw new RulesRepositoryException(e);
-//        }
-//    }
-//
-//
-//    /**
-//     * Adds a Rule node in the repository using the content specified
-//     *
-//     * @param ruleName the name of the rule
-//     * @param lhsContent the lhs of the rule
-//     * @param rhsContent the rhs of the rule
-//     * @return a RuleItem object encapsulating the node that gets added
-//     * @throws RulesRepositoryException
-//     */
-//    public RuleItem addRule(String ruleName, String ruleContent) throws
-//    RulesRepositoryException {
-//        Node folderNode = this.getAreaNode(RULE_AREA);
-//
-//        try {
-//            //create the node - see section 6.7.22.6 of the spec
-//            Node ruleNode = folderNode.addNode(ruleName,
-//                                               RuleItem.RULE_NODE_TYPE_NAME);
-//
-//            ruleNode.setProperty(RuleItem.TITLE_PROPERTY_NAME, ruleName);
-//
-//
-//            ruleNode.setProperty(RuleItem.DESCRIPTION_PROPERTY_NAME, "");
-//            ruleNode.setProperty(RuleItem.FORMAT_PROPERTY_NAME,
-//                                 RuleItem.RULE_FORMAT);
-//            ruleNode.setProperty(RuleItem.RULE_CONTENT_PROPERTY_NAME,
-//                                 ruleContent);
-//
-//            ruleNode.setProperty( VersionableItem.CHECKIN_COMMENT, "Initial" );
-//
-//
-//            Calendar lastModified = Calendar.getInstance();
-//            ruleNode.setProperty(RuleItem.LAST_MODIFIED_PROPERTY_NAME,
-//                                 lastModified);
-//
-//            session.save();
-//
-//            try {
-//                ruleNode.checkin();
-//            }
-//            catch(UnsupportedRepositoryOperationException e) {
-//                String message = "Error: Caught
-//                    UnsupportedRepositoryOperationException when attempting to checkin
-//                    rule: " + ruleNode.getName() + ". Are you sure your JCR repository
-//                    supports versioning? ";
-//                        log.error(message + e);
-//                throw new RulesRepositoryException(message, e);
-//            }
-//
-//            return new RuleItem(this, ruleNode);
-//        }
-//        catch(Exception e) {
-//            log.error("Caught Exception", e);
-//            throw new RulesRepositoryException(e);
-//        }
-//    }
+    //    MN: This is kept for future reference showing how to tie references
+    //    to a specific version when
+    //    sharing assets.
+    //
+    //    /**
+    //     * Adds a Rule node in the repository using the content specified,
+    //     associating it with
+    //     * the specified DSL node
+    //     *
+    //     * @param ruleName the name of the rule
+    //     * @param lhsContent the lhs of the rule
+    //     * @param rhsContent the rhs of the rule
+    //     * @param dslItem the dslItem encapsulting the dsl node to associate
+    //     this rule node with
+    //     * @paaram followDslHead whether or not to follow the head revision of
+    //     the dsl node
+    //     * @return a RuleItem object encapsulating the node that gets added
+    //     * @throws RulesRepositoryException
+    //     */
+    //    public RuleItem addRule(String ruleName, String ruleContent, DslItem
+    //                            dslItem, boolean followDslHead) throws RulesRepositoryException {
+    //        Node folderNode = this.getAreaNode(RULE_AREA);
+    //
+    //        try {
+    //            //create the node - see section 6.7.22.6 of the spec
+    //            Node ruleNode = folderNode.addNode(ruleName,
+    //                                               RuleItem.RULE_NODE_TYPE_NAME);
+    //
+    //            ruleNode.setProperty(RuleItem.TITLE_PROPERTY_NAME, ruleName);
+    //            ruleNode.setProperty(RuleItem.RULE_CONTENT_PROPERTY_NAME,
+    //                                 ruleContent);
+    //            ruleNode.setProperty(RuleItem.DESCRIPTION_PROPERTY_NAME, "");
+    //            ruleNode.setProperty(RuleItem.FORMAT_PROPERTY_NAME,
+    //                                 RuleItem.RULE_FORMAT);
+    //
+    //
+    //            if(followDslHead) {
+    //                ruleNode.setProperty(RuleItem.DSL_PROPERTY_NAME, dslItem.getNode());
+    //            }
+    //            else {
+    //                //tie the ruleNode to specifically the current version of the dslNode
+    //                ruleNode.setProperty(RuleItem.DSL_PROPERTY_NAME,
+    //                                     dslItem.getNode().getBaseVersion());
+    //            }
+    //
+    //            Calendar lastModified = Calendar.getInstance();
+    //            ruleNode.setProperty(RuleItem.LAST_MODIFIED_PROPERTY_NAME,
+    //                                 lastModified);
+    //
+    //            session.save();
+    //
+    //            try {
+    //                ruleNode.checkin();
+    //            }
+    //            catch(UnsupportedRepositoryOperationException e) {
+    //                String message = "Error: Caught
+    //                    UnsupportedRepositoryOperationException when attempting to checkin
+    //                    rule: " + ruleNode.getName() + ". Are you sure your JCR repository
+    //                    supports versioning? ";
+    //                        log.error(message + e);
+    //                throw new RulesRepositoryException(message, e);
+    //            }
+    //
+    //            return new RuleItem(this, ruleNode);
+    //        }
+    //        catch(Exception e) {
+    //            log.error("Caught Exception", e);
+    //            throw new RulesRepositoryException(e);
+    //        }
+    //    }
+    //
+    //
+    //    /**
+    //     * Adds a Rule node in the repository using the content specified
+    //     *
+    //     * @param ruleName the name of the rule
+    //     * @param lhsContent the lhs of the rule
+    //     * @param rhsContent the rhs of the rule
+    //     * @return a RuleItem object encapsulating the node that gets added
+    //     * @throws RulesRepositoryException
+    //     */
+    //    public RuleItem addRule(String ruleName, String ruleContent) throws
+    //    RulesRepositoryException {
+    //        Node folderNode = this.getAreaNode(RULE_AREA);
+    //
+    //        try {
+    //            //create the node - see section 6.7.22.6 of the spec
+    //            Node ruleNode = folderNode.addNode(ruleName,
+    //                                               RuleItem.RULE_NODE_TYPE_NAME);
+    //
+    //            ruleNode.setProperty(RuleItem.TITLE_PROPERTY_NAME, ruleName);
+    //
+    //
+    //            ruleNode.setProperty(RuleItem.DESCRIPTION_PROPERTY_NAME, "");
+    //            ruleNode.setProperty(RuleItem.FORMAT_PROPERTY_NAME,
+    //                                 RuleItem.RULE_FORMAT);
+    //            ruleNode.setProperty(RuleItem.RULE_CONTENT_PROPERTY_NAME,
+    //                                 ruleContent);
+    //
+    //            ruleNode.setProperty( VersionableItem.CHECKIN_COMMENT, "Initial" );
+    //
+    //
+    //            Calendar lastModified = Calendar.getInstance();
+    //            ruleNode.setProperty(RuleItem.LAST_MODIFIED_PROPERTY_NAME,
+    //                                 lastModified);
+    //
+    //            session.save();
+    //
+    //            try {
+    //                ruleNode.checkin();
+    //            }
+    //            catch(UnsupportedRepositoryOperationException e) {
+    //                String message = "Error: Caught
+    //                    UnsupportedRepositoryOperationException when attempting to checkin
+    //                    rule: " + ruleNode.getName() + ". Are you sure your JCR repository
+    //                    supports versioning? ";
+    //                        log.error(message + e);
+    //                throw new RulesRepositoryException(message, e);
+    //            }
+    //
+    //            return new RuleItem(this, ruleNode);
+    //        }
+    //        catch(Exception e) {
+    //            log.error("Caught Exception", e);
+    //            throw new RulesRepositoryException(e);
+    //        }
+    //    }
 
     /**
      * This will copy an assets content to the new location.
      */
-    public String copyAsset(String uuidSource,
-                            String destinationPackage,
-                            String destinationName) {
+    public String copyAsset(String uuidSource, String destinationPackage, String destinationName) {
         try {
             AssetItem source = loadAssetByUUID( uuidSource );
             String sourcePath = source.getNode().getPath();
 
             String destPath = this.getAreaNode( RULE_PACKAGE_AREA ).getPath() + "/" + destinationPackage + "/" + PackageItem.ASSET_FOLDER_NAME + "/" + destinationName;
-            this.session.getWorkspace().copy( sourcePath,
-                                              destPath );
+            this.session.getWorkspace().copy( sourcePath, destPath );
             AssetItem dest = loadPackage( destinationPackage ).loadAsset( destinationName );
-            dest.updateStringProperty( destinationPackage,
-                                       AssetItem.PACKAGE_NAME_PROPERTY );
+            dest.updateStringProperty( destinationPackage, AssetItem.PACKAGE_NAME_PROPERTY );
             dest.checkin( "Copied from " + source.getPackageName() + "/" + source.getName() );
             return dest.getUUID();
         } catch ( RepositoryException e ) {
-            log.error( "Unable to copy asset.",
-                       e );
+            log.error( "Unable to copy asset.", e );
             throw new RulesRepositoryException( e );
         }
     }
@@ -355,12 +345,11 @@
             return new PackageItem( this,
                                     rulePackageNode );
         } catch ( RepositoryException e ) {
-            log.error( "Unable to load a rule package. ",
-                       e );
+            log.error( "Unable to load a rule package. ", e );
 
-                throw new RulesRepositoryException( "Unable to load a rule package. ",
-                                                    e );
-            
+            throw new RulesRepositoryException( "Unable to load a rule package. ",
+                                                e );
+
         }
     }
 
@@ -376,8 +365,7 @@
         }
     }
 
-    public PackageItem loadPackageSnapshot(String packageName,
-                                           String snapshotName) {
+    public PackageItem loadPackageSnapshot(String packageName, String snapshotName) {
         try {
             Node n = this.getAreaNode( PACKAGE_SNAPSHOT_AREA ).getNode( packageName ).getNode( snapshotName );
             return new PackageItem( this,
@@ -392,15 +380,13 @@
      * This will copy the package to the snapshot area. Creating a copy for
      * deployment, etc.
      */
-    public void createPackageSnapshot(String packageName,
-                                      String snapshotName) {
+    public void createPackageSnapshot(String packageName, String snapshotName) {
         log.debug( "Creating snapshot for [" + packageName + "] called [" + snapshotName + "]" );
         try {
             Node snaps = this.getAreaNode( PACKAGE_SNAPSHOT_AREA );
 
             if ( !snaps.hasNode( packageName ) ) {
-                snaps.addNode( packageName,
-                               "nt:folder" );
+                snaps.addNode( packageName, "nt:folder" );
                 save();
             }
 
@@ -413,11 +399,9 @@
 
             String source = rulePackageNode.getPath();
 
-            this.session.getWorkspace().copy( source,
-                                              newName );
+            this.session.getWorkspace().copy( source, newName );
         } catch ( RepositoryException e ) {
-            log.error( "Unable to create snapshot",
-                       e );
+            log.error( "Unable to create snapshot", e );
             throw new RulesRepositoryException( e );
         }
     }
@@ -425,8 +409,7 @@
     /**
      * This will remove the specified snapshot.
      */
-    public void removePackageSnapshot(String packageName,
-                                      String snapshotName) {
+    public void removePackageSnapshot(String packageName, String snapshotName) {
         log.debug( "Removing snapshot for [" + packageName + "] called [" + snapshotName + "]" );
         try {
             Node snaps = this.getAreaNode( PACKAGE_SNAPSHOT_AREA );
@@ -443,8 +426,7 @@
 
             save();
         } catch ( RepositoryException e ) {
-            log.error( "Unable to remove snapshot",
-                       e );
+            log.error( "Unable to remove snapshot", e );
             throw new RulesRepositoryException( e );
         }
     }
@@ -459,9 +441,7 @@
      * @param newName
      *            The new label. The old one is left intact.
      */
-    public void copyPackageSnapshot(String packageName,
-                                    String snapshotName,
-                                    String newName) {
+    public void copyPackageSnapshot(String packageName, String snapshotName, String newName) {
         log.debug( "Creating snapshot for [" + packageName + "] called [" + snapshotName + "]" );
         try {
             Node snaps = this.getAreaNode( PACKAGE_SNAPSHOT_AREA );
@@ -472,11 +452,9 @@
 
             String destinationPath = pkgSnaps.getPath() + "/" + newName;
 
-            this.session.getWorkspace().copy( sourceNode.getPath(),
-                                              destinationPath );
+            this.session.getWorkspace().copy( sourceNode.getPath(), destinationPath );
         } catch ( RepositoryException e ) {
-            log.error( "Unable to create snapshot",
-                       e );
+            log.error( "Unable to create snapshot", e );
             throw new RulesRepositoryException( e );
         }
     }
@@ -491,8 +469,7 @@
             if ( folderNode.hasNode( DEFAULT_PACKAGE ) ) {
                 return loadPackage( DEFAULT_PACKAGE );
             } else {
-                return createPackage( DEFAULT_PACKAGE,
-                                      "" );
+                return createPackage( DEFAULT_PACKAGE, "" );
             }
         } catch ( RepositoryException e ) {
             throw new RulesRepositoryException( e );
@@ -514,8 +491,7 @@
             return new PackageItem( this,
                                     rulePackageNode );
         } catch ( Exception e ) {
-            log.error( "Unable to load a rule package by UUID. ",
-                       e );
+            log.error( "Unable to load a rule package by UUID. ", e );
             if ( e instanceof RuntimeException ) {
                 throw (RuntimeException) e;
             } else {
@@ -533,24 +509,19 @@
      * @param headVersion
      * @param comment
      */
-    public void restoreHistoricalAsset(AssetItem versionToRestore,
-                                       AssetItem headVersion,
-                                       String comment) {
+    public void restoreHistoricalAsset(AssetItem versionToRestore, AssetItem headVersion, String comment) {
 
         long oldVersionNumber = headVersion.getVersionNumber();
 
         Version v = (Version) versionToRestore.getNode();
         try {
-            headVersion.getNode().restore( v,
-                                           true );
+            headVersion.getNode().restore( v, true );
             AssetItem newHead = loadAssetByUUID( headVersion.getUUID() );
             newHead.checkout();
-            newHead.getNode().setProperty( VersionableItem.VERSION_NUMBER_PROPERTY_NAME,
-                                           oldVersionNumber );
+            newHead.getNode().setProperty( VersionableItem.VERSION_NUMBER_PROPERTY_NAME, oldVersionNumber );
             newHead.checkin( comment );
         } catch ( RepositoryException e ) {
-            log.error( "Unable to restore version of asset.",
-                       e );
+            log.error( "Unable to restore version of asset.", e );
             throw new RulesRepositoryException( e );
         }
     }
@@ -564,8 +535,7 @@
             return new AssetItem( this,
                                   rulePackageNode );
         } catch ( RepositoryException e ) {
-            log.error( "Unable to load a rule asset by UUID.",
-                       e );
+            log.error( "Unable to load a rule asset by UUID.", e );
             throw new RulesRepositoryException( e );
         }
 
@@ -581,31 +551,23 @@
      * @return a PackageItem, encapsulating the created node
      * @throws RulesRepositoryException
      */
-    public PackageItem createPackage(String name,
-                                     String description) throws RulesRepositoryException {
+    public PackageItem createPackage(String name, String description) throws RulesRepositoryException {
         Node folderNode = this.getAreaNode( RULE_PACKAGE_AREA );
 
         try {
             // create the node - see section 6.7.22.6 of the spec
-            Node rulePackageNode = folderNode.addNode( name,
-                                                       PackageItem.RULE_PACKAGE_TYPE_NAME );
+            Node rulePackageNode = folderNode.addNode( name, PackageItem.RULE_PACKAGE_TYPE_NAME );
 
-            rulePackageNode.addNode( PackageItem.ASSET_FOLDER_NAME,
-                                     "drools:versionableAssetFolder" );
+            rulePackageNode.addNode( PackageItem.ASSET_FOLDER_NAME, "drools:versionableAssetFolder" );
 
-            rulePackageNode.setProperty( PackageItem.TITLE_PROPERTY_NAME,
-                                         name );
+            rulePackageNode.setProperty( PackageItem.TITLE_PROPERTY_NAME, name );
 
-            rulePackageNode.setProperty( AssetItem.DESCRIPTION_PROPERTY_NAME,
-                                         description );
-            rulePackageNode.setProperty( AssetItem.FORMAT_PROPERTY_NAME,
-                                         PackageItem.PACKAGE_FORMAT );
-            rulePackageNode.setProperty( PackageItem.CREATOR_PROPERTY_NAME,
-                                         this.session.getUserID() );
+            rulePackageNode.setProperty( AssetItem.DESCRIPTION_PROPERTY_NAME, description );
+            rulePackageNode.setProperty( AssetItem.FORMAT_PROPERTY_NAME, PackageItem.PACKAGE_FORMAT );
+            rulePackageNode.setProperty( PackageItem.CREATOR_PROPERTY_NAME, this.session.getUserID() );
 
             Calendar lastModified = Calendar.getInstance();
-            rulePackageNode.setProperty( PackageItem.LAST_MODIFIED_PROPERTY_NAME,
-                                         lastModified );
+            rulePackageNode.setProperty( PackageItem.LAST_MODIFIED_PROPERTY_NAME, lastModified );
 
             PackageItem item = new PackageItem( this,
                                                 rulePackageNode );
@@ -616,8 +578,7 @@
             throw new RulesRepositoryException( "A package name must be unique.",
                                                 e );
         } catch ( RepositoryException e ) {
-            log.error( "Error when creating a new rule package",
-                       e );
+            log.error( "Error when creating a new rule package", e );
             throw new RulesRepositoryException( e );
         }
 
@@ -655,9 +616,7 @@
     public StateItem createState(String name) {
         try {
             Node folderNode = this.getAreaNode( STATE_AREA );
-            Node stateNode = RulesRepository.addNodeIfNew( folderNode,
-                                                           name,
-                                                           StateItem.STATE_NODE_TYPE_NAME );
+            Node stateNode = RulesRepository.addNodeIfNew( folderNode, name, StateItem.STATE_NODE_TYPE_NAME );
             log.debug( "Created the status [" + name + "]" );
             return new StateItem( this,
                                   stateNode );
@@ -716,8 +675,7 @@
      * returned (you will have to delve into the rules deepest darkest history
      * yourself... mahahahaha).
      */
-    public List findAssetsByCategory(String categoryTag,
-                                     boolean seekArchivedAsset) throws RulesRepositoryException {
+    public List findAssetsByCategory(String categoryTag, boolean seekArchivedAsset) throws RulesRepositoryException {
 
         CategoryItem item = this.loadCategory( categoryTag );
         List results = new ArrayList();
@@ -742,10 +700,9 @@
      * TODO: Comment
      */
     public List findAssetsByCategory(String categoryTag) throws RulesRepositoryException {
-        return this.findAssetsByCategory( categoryTag,
-                                          false );
+        return this.findAssetsByCategory( categoryTag, false );
     }
-    
+
     /**
      * TODO: comment 
      * @return
@@ -753,8 +710,10 @@
      * @throws PathNotFoundException
      * @throws RepositoryException
      */
-    public byte[] exportRulesRepository() throws IOException, PathNotFoundException, RepositoryException {
-        
+    public byte[] exportRulesRepository() throws IOException,
+                                         PathNotFoundException,
+                                         RepositoryException {
+
         ByteArrayOutputStream bout = new ByteArrayOutputStream();
         ZipOutputStream zout = new ZipOutputStream( bout );
 
@@ -764,22 +723,24 @@
         zout.finish();
         return bout.toByteArray();
     }
-    
-    public byte[] dumpRepositoryXml() throws PathNotFoundException, IOException, RepositoryException {
+
+    public byte[] dumpRepositoryXml() throws PathNotFoundException,
+                                     IOException,
+                                     RepositoryException {
         ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
         session.refresh( false );
-        session.exportSystemView( "/" + RULES_REPOSITORY_NAME, byteOut , false, false );
+        session.exportSystemView( "/" + RULES_REPOSITORY_NAME, byteOut, false, false );
         return byteOut.toByteArray();
     }
-    
+
     /**
      * 
      * @param byteArray
      */
     public void importRulesRepository(byte[] byteArray) {
         try {
-            new RulesRepositoryAdministrator(this.session).clearRulesRepository();
-            this.session.getWorkspace().importXML( "/" , new ByteArrayInputStream(byteArray), ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
+            new RulesRepositoryAdministrator( this.session ).clearRulesRepository();
+            this.session.getWorkspace().importXML( "/", new ByteArrayInputStream( byteArray ), ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW );
             session.save();
         } catch ( RepositoryException e ) {
             e.printStackTrace();
@@ -808,8 +769,7 @@
 
         try {
             if ( !folderNode.hasNode( DEFAULT_PACKAGE ) ) {
-                createPackage( DEFAULT_PACKAGE,
-                               "The default rule package" );
+                createPackage( DEFAULT_PACKAGE, "The default rule package" );
                 folderNode = this.getAreaNode( RULE_PACKAGE_AREA );
             }
             return new PackageIterator( this,
@@ -853,24 +813,20 @@
      * @param explanation
      *            The reason (which will be added as the checkin message).
      */
-    public void moveRuleItemPackage(String newPackage,
-                                    String uuid,
-                                    String explanation) {
+    public void moveRuleItemPackage(String newPackage, String uuid, String explanation) {
         try {
             AssetItem item = loadAssetByUUID( uuid );
             String oldPackage = item.getPackageName();
-            PackageItem sourcePkg = loadPackage( oldPackage );
+
             PackageItem destPkg = loadPackage( newPackage );
 
             String sourcePath = item.node.getPath();
             String destPath = destPkg.node.getPath() + "/" + PackageItem.ASSET_FOLDER_NAME + "/" + item.getName();
 
-            this.session.move( sourcePath,
-                               destPath );
+            this.session.move( sourcePath, destPath );
 
             item.checkout();
-            item.node.setProperty( AssetItem.PACKAGE_NAME_PROPERTY,
-                                   newPackage );
+            item.node.setProperty( AssetItem.PACKAGE_NAME_PROPERTY, newPackage );
 
             item.checkin( explanation );
 
@@ -905,8 +861,7 @@
     /**
      * This will search assets, looking for matches against the name.
      */
-    public AssetItemIterator findAssetsByName(String name,
-                                              boolean seekArchived) {
+    public AssetItemIterator findAssetsByName(String name, boolean seekArchived) {
         try {
 
             String sql = "SELECT " + AssetItem.TITLE_PROPERTY_NAME + ", " + AssetItem.DESCRIPTION_PROPERTY_NAME + ", " + AssetItem.CONTENT_PROPERTY_ARCHIVE_FLAG + " FROM " + AssetItem.RULE_NODE_TYPE_NAME;
@@ -915,8 +870,7 @@
 
             if ( seekArchived == false ) sql += " AND " + AssetItem.CONTENT_PROPERTY_ARCHIVE_FLAG + " = 'false'";
 
-            Query q = this.session.getWorkspace().getQueryManager().createQuery( sql,
-                                                                                 Query.SQL );
+            Query q = this.session.getWorkspace().getQueryManager().createQuery( sql, Query.SQL );
 
             QueryResult res = q.execute();
 
@@ -930,8 +884,7 @@
     }
 
     public AssetItemIterator findAssetsByName(String name) {
-        return this.findAssetsByName( name,
-                                      false );
+        return this.findAssetsByName( name, false );
     }
 
     /**
@@ -953,4 +906,27 @@
         }
         return (StateItem[]) states.toArray( new StateItem[states.size()] );
     }
+
+    /**
+     * Copy a package to the target name.
+     */
+    public void copyPackage(String sourcePackageName, String destPackageName) {
+        PackageItem source = loadPackage( sourcePackageName );
+        String sourcePath;
+        
+        try {
+            sourcePath = source.getNode().getPath();
+
+            String destPath = source.getNode().getParent().getPath() + "/" + destPackageName;
+            if ( this.getAreaNode( RULE_PACKAGE_AREA ).hasNode( destPackageName ) ) {
+                throw new RulesRepositoryException( "Destination already exists." );
+            }
+            this.session.getWorkspace().copy( sourcePath, destPath );
+
+        } catch ( RepositoryException e ) {
+            log.error( e );
+            throw new RulesRepositoryException(e);
+        }
+
+    }
 }
\ No newline at end of file

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	2007-05-23 03:51:46 UTC (rev 12093)
+++ labs/jbossrules/trunk/drools-repository/src/test/java/org/drools/repository/RulesRepositoryTest.java	2007-05-23 04:02:38 UTC (rev 12094)
@@ -329,6 +329,33 @@
         assertFalse(uuid.equals( item.getUUID() ));
     }
     
+    public void testCopyPackage() throws Exception {
+        RulesRepository repo = RepositorySessionUtil.getRepository();
+        PackageItem source = repo.createPackage( "testCopyPackage", "asset" );
+        AssetItem item = source.addAsset( "testCopyPackage", "desc" );
+        item.updateContent( "la" );
+        item.checkin( "" );
+        repo.save();
+        
+        repo.copyPackage( "testCopyPackage", "testCopyPackage2" );
+        PackageItem dest = repo.loadPackage( "testCopyPackage2" );
+        assertNotNull(dest);
+        assertFalse( source.getUUID().equals( dest.getUUID() ));
+        
+        assertEquals(1, iteratorToList( dest.getAssets()).size());
+        
+        try {
+            repo.copyPackage( "testCopyPackage", "testCopyPackage2" );
+            fail("should not be able to copy when existing.");
+            
+        } catch (RulesRepositoryException e) {
+            assertNotNull(e.getMessage());
+        }
+        
+        
+    }
+    
+    
     public void testListStates()  {
         RulesRepository repo = RepositorySessionUtil.getRepository();
         StateItem[] items = repo.listStates();




More information about the jboss-svn-commits mailing list