Author: mir
Date: Thu Mar  4 14:12:04 2010
New Revision: 918996

URL: http://svn.apache.org/viewvc?rev=918996&view=rev
Log:
CLEREZZA-143: implemented renaming capability in platform.content.hierarchy

Modified:
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyManager.java
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyNode.java
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyService.java
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyUtils.java
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/test/java/org/apache/clerezza/platform/content/hierarchy/HierarchyTest.java

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyManager.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyManager.java?rev=918996&r1=918995&r2=918996&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyManager.java
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyManager.java
 Thu Mar  4 14:12:04 2010
@@ -165,20 +165,24 @@
 
        /**
         * Moves the node into the specified collection as the member at
-        * the specified position pos.
+        * the specified position pos. 
         * This resource method has the path "move"
         *
         * @param oldPos
         * @param newPos
+        * @param newName The new name of the moved node. This parameter is 
optionally.
         * @return if sucessfully moved then a Created (201) HTTP Response is 
returned.
         *              if the specified targetCollection is not a collection 
then a
-        *              Conflict (409) HTTP Response is returned.
+        *              Conflict (409) HTTP Response is returned. If the node 
to be moved or
+        *              the target collection does not exist, then a Not Found 
(404) HTTP
+        *              Response is returned.
         */
        @POST
        @Path("move")
        public Response move(@FormParam(value = "nodeUri") UriRef nodeUri,
                        @FormParam(value = "targetCollection") UriRef 
targetCollection,
-                       @FormParam(value = "pos") Integer newPos) {
+                       @FormParam(value = "pos") Integer newPos,
+                       @FormParam(value = "newName") String newName) {
                HierarchyNode hierarchyNode;
                CollectionNode targetCollectionNode;
                try {
@@ -191,12 +195,11 @@
                                        targetCollection.getUnicodeString() + " 
is not a Collection.").
                                        type(MediaType.TEXT_PLAIN_TYPE).build();
                }
-
                try {
-                       hierarchyNode = 
hierarchyNode.move(targetCollectionNode, newPos);
+                       hierarchyNode = 
hierarchyNode.move(targetCollectionNode, newName, newPos);
                } catch (NodeAlreadyExistsException ex) {
                        return Response.status(Response.Status.CONFLICT).entity(
-                                       targetCollection.getUnicodeString() + " 
already exists in " +
+                                       nodeUri.getUnicodeString() + " already 
exists in " +
                                        "collection.").
                                        type(MediaType.TEXT_PLAIN_TYPE).build();
                } catch (IllegalMoveException ex) {
@@ -206,6 +209,34 @@
                return Response.created(
                                
URI.create(hierarchyNode.getNode().getUnicodeString())).build();
        }
+       
+       /**
+        * Renames the specified node to the given name. E.g. if you rename 
+        * "http://localhost:8080/foo/bar"; to "test" the new URI will be 
+        * "http://localhost:8080/foo/test";.
+        * This resource method has the path "rename".
+        *
+        * @param oldPos
+        * @param newPos
+        * @param newName The new name of the moved node. This parameter is 
optionally.
+        * @return if sucessfully renamed then a Created (201) HTTP Response is 
returned.
+        *              If the node does not exist, then a Not Found (404) HTTP 
Response is
+        *              returned.
+        */
+       @POST
+       @Path("rename")
+       public Response rename(@FormParam(value = "nodeUri") UriRef nodeUri,
+                       @FormParam(value = "newName") String newName) {
+               try {
+                       HierarchyNode hierarchyNode = 
hierarchyService.getHierarchyNode(nodeUri);
+                       CollectionNode parent = hierarchyNode.getParent();
+                       int pos = parent.getMembers().indexOf(hierarchyNode);
+                       return move(nodeUri, parent.getNode(), pos, newName);
+               } catch (NodeDoesNotExistException ex) {
+                       return 
Response.status(Response.Status.NOT_FOUND).build();
+               }
+               
+       }
 
        /**
         * Returns a graph containng the graph context of the specified 
collection

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyNode.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyNode.java?rev=918996&r1=918995&r2=918996&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyNode.java
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyNode.java
 Thu Mar  4 14:12:04 2010
@@ -19,11 +19,15 @@
 package org.apache.clerezza.platform.content.hierarchy;
 
 import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 import org.apache.clerezza.rdf.core.NonLiteral;
 import org.apache.clerezza.rdf.core.Resource;
 import org.apache.clerezza.rdf.core.TripleCollection;
 import org.apache.clerezza.rdf.core.UriRef;
 import org.apache.clerezza.rdf.utils.GraphNode;
+import org.apache.clerezza.utils.UriException;
+import org.apache.clerezza.utils.UriUtil;
 
 /**
  * This class represents a node in a hierarchy and provides methods for
@@ -91,33 +95,65 @@
         */
        public HierarchyNode move(CollectionNode newParentCollection, int pos) 
                        throws NodeAlreadyExistsException, IllegalMoveException 
{
+               return this.move(newParentCollection, null, pos);
+       }
                
+       /**
+        * Moves this node into the specified parent collection at the specified
+        * position pos. Optionally you can specifiy a new name.
+        *
+        * @param newParentCollection the collection into which this node 
should be moved
+        * @param newName the new name of the moved node. Can be null.
+        * @param pos the member position of the moved node in the members list 
of
+        * the new parent collection.
+        * @throws NodeAlreadyExistsException is thrown if the new parent 
collection
+        *              already contains a hierarchy node with the same name.
+        * @throws IllegalMoveException is thrown if the move operation is not 
allowed
+        * @return the HierarchyNode at the new location
+        */
+       public HierarchyNode move(CollectionNode newParentCollection, String 
newName, int pos)
+                       throws NodeAlreadyExistsException, IllegalMoveException 
{
+               String name;
+               try {
+                       name = newName == null ? getName() : 
UriUtil.encodePartlyEncodedPath(newName, "UTF-8");
+               } catch (UriException ex) {
+                       throw new RuntimeException(ex);
+               }
                if (newParentCollection.equals(getParent())) {
                        UriRef nodeUri = getNode();
                        List<Resource> membersRdfList = 
newParentCollection.getMembersRdf();
                        int oldPos = membersRdfList.indexOf(nodeUri);           
        
                        if (oldPos < pos) {
                                pos -= 1;
-                       }
-                       if (oldPos == pos) {
-                               return this;
                        }                       
-                       membersRdfList.remove(nodeUri);
-                       membersRdfList.add(pos, nodeUri);
-                       return this;
-               }
-               String newName = 
newParentCollection.getNode().getUnicodeString() +
-                               getName();              
+                       if (name.equals(getName())) {                           
+                               if (oldPos != pos) {
+                                       membersRdfList.remove(nodeUri);
+                                       membersRdfList.add(pos, nodeUri);
+                               }                               
+                               return this;
+                       }
+               }               
+               String newUriString = 
newParentCollection.getNode().getUnicodeString() +
+                               name;
                if (this instanceof CollectionNode) {
-                       newName += "/";
+                       newUriString += "/";
                }
-               UriRef newUri = new UriRef(newName);    
-               if (newParentCollection.getMembersRdf().contains(newUri)) {
+               UriRef newUri = new UriRef(newUriString);
+               String alternativeUriString = newUriString.endsWith("/") ? 
+                       newUriString.substring(0, newUriString.length() - 1) : 
newUriString + "/";
+               UriRef alternativeUri = new UriRef(alternativeUriString);
+               List<Resource> parentMembers = 
newParentCollection.getMembersRdf();
+               if (parentMembers.contains(newUri) || 
parentMembers.contains(alternativeUri)) {
                        HierarchyNode existingNode = null;
                        try {
                                existingNode = 
hierarchyService.getHierarchyNodeWithEncodedUri(newUri);
                        } catch (NodeDoesNotExistException ex) {
-                               throw new RuntimeException(ex);
+                               try {
+                                       existingNode = 
hierarchyService.getHierarchyNodeWithEncodedUri(alternativeUri);
+                               } catch (NodeDoesNotExistException e) {
+                                       throw new RuntimeException(ex);
+                               }
                        }
                        throw new NodeAlreadyExistsException(existingNode);
                }
@@ -135,12 +171,8 @@
         * @return the name of the hierarchy node.
         */
        public String getName() {
-               String uri = getNode().getUnicodeString();
-               if (uri.endsWith("/")) {
-                       uri = uri.substring(0, uri.length() - 1);
+               return HierarchyUtils.getName(this.getNode());
                }
-               return uri.substring(uri.lastIndexOf("/") + 1, uri.length());
-       }
 
        @Override
        public UriRef getNode() {

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyService.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyService.java?rev=918996&r1=918995&r2=918996&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyService.java
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyService.java
 Thu Mar  4 14:12:04 2010
@@ -120,8 +120,12 @@
                         UriRef nodeUri = node.getNode();
                         try {
                             parent = node.getParent();
-                        } catch(IllegalArgumentException ex){
-                            throw new NodeDoesNotExistException(nodeUri);
+                        } catch(RuntimeException ex){
+                                                       if (ex.getCause() 
instanceof NodeDoesNotExistException) {
+                                                               throw new 
NodeDoesNotExistException(nodeUri);
+                                                       } else {
+                                                               throw ex;
+                                                       }
                         }                      
                        if (!parent.getMembersRdf().contains(nodeUri)) {
                                throw new NodeDoesNotExistException(nodeUri);
@@ -149,8 +153,13 @@
 
        CollectionNode getCollectionNodeWithEncodedUri(UriRef uri)
                        throws NodeDoesNotExistException{
-               CollectionNode collectionNode =
-                               new CollectionNode(uri, 
cgProvider.getContentGraph(), this);
+               CollectionNode collectionNode;
+               try {
+                       collectionNode =
+                                       new CollectionNode(uri, 
cgProvider.getContentGraph(), this);
+               } catch (IllegalArgumentException ex) {
+                       throw new NodeDoesNotExistException(uri);
+               }
                checkExistence(collectionNode);
                return collectionNode;
        }

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyUtils.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyUtils.java?rev=918996&r1=918995&r2=918996&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyUtils.java
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyUtils.java
 Thu Mar  4 14:12:04 2010
@@ -52,4 +52,12 @@
                        throw new IllegalArgumentException(uri + " does not end 
with a slash");
                }
        }
+
+       static String getName(UriRef uri) {
+               String uriString = uri.getUnicodeString();
+               if (uriString.endsWith("/")) {
+                       uriString = uriString.substring(0, uriString.length() - 
1);
+               }
+               return uriString.substring(uriString.lastIndexOf("/") + 1, 
uriString.length());
+       }
 }

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/test/java/org/apache/clerezza/platform/content/hierarchy/HierarchyTest.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/test/java/org/apache/clerezza/platform/content/hierarchy/HierarchyTest.java?rev=918996&r1=918995&r2=918996&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/test/java/org/apache/clerezza/platform/content/hierarchy/HierarchyTest.java
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/test/java/org/apache/clerezza/platform/content/hierarchy/HierarchyTest.java
 Thu Mar  4 14:12:04 2010
@@ -245,6 +245,38 @@
        }
 
        @Test
+       public void renamingTest() throws Exception {
+               HierarchyService hierarchyService = getHierarchyService();
+               CollectionNode barNode = 
hierarchyService.createCollectionNode(bar);
+               barNode.move(barNode.getParent(), "foo", 0);
+               try {
+                       barNode = hierarchyService.getCollectionNode(bar);
+                       Assert.assertTrue(false);
+               } catch (NodeDoesNotExistException e) {}
+               try {
+                       hierarchyService.getCollectionNode(foo);
+               } catch (NodeDoesNotExistException e) {
+                       Assert.assertTrue(false);
+               }
+               HierarchyNode resource = 
hierarchyService.createNonCollectionNode(fooResource);
+               resource.move(resource.getParent(), "resource2", 0);
+               try {
+                       resource = 
hierarchyService.getHierarchyNode(fooResource);
+                       Assert.assertTrue(false);
+               } catch (NodeDoesNotExistException e) {}
+               try {
+                       hierarchyService.getHierarchyNode(fooResource2);
+               } catch (NodeDoesNotExistException e) {
+                       Assert.assertTrue(false);
+               }
+               HierarchyNode resource3 = 
hierarchyService.createNonCollectionNode(fooResource3);
+               try {
+                       resource.move(resource3.getParent(), "resource2", 0);
+                       Assert.assertTrue(false);
+               } catch (NodeAlreadyExistsException ex) {}
+       }
+
+       @Test
        public void rootAutoCreationTest() throws Exception{
                HierarchyService hierarchyService = getHierarchyService();
                hierarchyService.createCollectionNode(newRootTest);


Reply via email to