Author: mduerig
Date: Thu Jan 16 17:31:14 2014
New Revision: 1558859

URL: http://svn.apache.org/r1558859
Log:
OAK-770 : NodeImpl should implement JackrabbitNode
- Wrap Node.rename into a perform closure
- Only reestablish initial order if parent is actually orderable
- Correctly build destination path for move
- Additional test case

Modified:
    
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/NodeImpl.java
    
jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java

Modified: 
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/NodeImpl.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/NodeImpl.java?rev=1558859&r1=1558858&r2=1558859&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/NodeImpl.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/NodeImpl.java
 Thu Jan 16 17:31:14 2014
@@ -16,12 +16,23 @@
  */
 package org.apache.jackrabbit.oak.jcr.session;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.collect.Sets.newLinkedHashSet;
+import static java.util.Arrays.asList;
+import static java.util.Collections.singleton;
+import static org.apache.jackrabbit.JcrConstants.JCR_MIXINTYPES;
+import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
+import static org.apache.jackrabbit.oak.api.Type.NAME;
+import static org.apache.jackrabbit.oak.api.Type.NAMES;
+import static org.apache.jackrabbit.oak.util.TreeUtil.getNames;
+
 import java.io.InputStream;
 import java.math.BigDecimal;
 import java.util.Calendar;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
+
 import javax.annotation.Nonnull;
 import javax.jcr.AccessDeniedException;
 import javax.jcr.Binary;
@@ -80,16 +91,6 @@ import org.apache.jackrabbit.value.Value
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static com.google.common.base.Preconditions.checkNotNull;
-import static com.google.common.collect.Sets.newLinkedHashSet;
-import static java.util.Arrays.asList;
-import static java.util.Collections.singleton;
-import static org.apache.jackrabbit.JcrConstants.JCR_MIXINTYPES;
-import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
-import static org.apache.jackrabbit.oak.api.Type.NAME;
-import static org.apache.jackrabbit.oak.api.Type.NAMES;
-import static org.apache.jackrabbit.oak.util.TreeUtil.getNames;
-
 /**
  * TODO document
  *
@@ -1441,37 +1442,66 @@ public class NodeImpl<T extends NodeDele
      * @throws RepositoryException If an error occurs.
      */
     @Override
-    public void rename(String newName) throws RepositoryException {
+    public void rename(final String newName) throws RepositoryException {
         if (dlg.isRoot()) {
             throw new RepositoryException("Cannot rename the root node");
         }
 
-        String name = getName();
+        final String name = getName();
         if (newName.equals(name)) {
             // nothing to do
             return;
         }
 
-        String beforeName = null;
-        Node parent = getParent();
-        NodeIterator nit = parent.getNodes();
-        while (nit.hasNext()) {
-            Node child = nit.nextNode();
-            if (name.equals(child.getName())) {
-                if (nit.hasNext()) {
-                    beforeName = nit.nextNode().getName();
+        perform(new ItemWriteOperation<Void>() {
+            @Override
+            public Void perform() throws RepositoryException {
+                Node parent = getParent();
+                String beforeName = null;
+
+                if (isOrderable(parent)) {
+                    // remember position amongst siblings
+                    NodeIterator nit = parent.getNodes();
+                    while (nit.hasNext()) {
+                        Node child = nit.nextNode();
+                        if (name.equals(child.getName())) {
+                            if (nit.hasNext()) {
+                                beforeName = nit.nextNode().getName();
+                            }
+                            break;
+                        }
+                    }
+                }
+
+                String srcPath = getPath();
+                String destPath = '/' + newName;
+                String parentPath = parent.getPath();
+                if (!"/".equals(parentPath)) {
+                    destPath = parentPath + destPath;
+                }
+                sessionContext.getSession().move(srcPath, destPath);
+
+                if (beforeName != null) {
+                    // restore position within siblings
+                    parent.orderBefore(newName, beforeName);
                 }
-                break;
+                return null;
             }
-        }
+        });
+    }
 
-        String srcPath = getPath();
-        String destPath = PathUtils.getAncestorPath(srcPath, 1) + '/' + 
newName;
-        sessionContext.getSession().move(srcPath, destPath);
+    private static boolean isOrderable(Node node) throws RepositoryException {
+        if (node.getPrimaryNodeType().hasOrderableChildNodes()) {
+            return true;
+        }
 
-        if (beforeName != null) {
-            parent.orderBefore(newName, beforeName);
+        NodeType[] types = node.getMixinNodeTypes();
+        for (NodeType type : types) {
+            if (type.hasOrderableChildNodes()) {
+                return true;
+            }
         }
+        return false;
     }
 
     /**

Modified: 
jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java?rev=1558859&r1=1558858&r2=1558859&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java
 Thu Jan 16 17:31:14 2014
@@ -18,6 +18,15 @@
  */
 package org.apache.jackrabbit.oak.jcr;
 
+import static java.util.Arrays.asList;
+import static org.apache.jackrabbit.commons.JcrUtils.getChildNodes;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
@@ -29,6 +38,7 @@ import java.util.Calendar;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
+
 import javax.jcr.Binary;
 import javax.jcr.GuestCredentials;
 import javax.jcr.ImportUUIDBehavior;
@@ -56,6 +66,7 @@ import javax.jcr.nodetype.NodeTypeManage
 import javax.jcr.nodetype.NodeTypeTemplate;
 
 import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.api.JackrabbitNode;
 import org.apache.jackrabbit.api.JackrabbitRepository;
 import org.apache.jackrabbit.commons.cnd.CndImporter;
 import org.apache.jackrabbit.commons.cnd.ParseException;
@@ -64,15 +75,6 @@ import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
 
-import static java.util.Arrays.asList;
-import static org.apache.jackrabbit.commons.JcrUtils.getChildNodes;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-
 public class RepositoryTest extends AbstractRepositoryTest {
     private static final String TEST_NODE = "test_node";
     private static final String TEST_PATH = '/' + TEST_NODE;
@@ -1698,6 +1700,21 @@ public class RepositoryTest extends Abst
         assertTrue(node.hasNode("target/moved"));
     }
 
+    @Test
+    public void renameNonOrderable() throws RepositoryException {
+        Session session = getAdminSession();
+        Node root = session.getRootNode();
+        Node parent = root.addNode("parent", "oak:Unstructured");
+        parent.addNode("fo");
+        Node foo = parent.addNode("foo");
+        session.save();
+
+        ((JackrabbitNode) foo).rename("renamed");
+        assertEquals("renamed", foo.getName());
+        assertFalse(session.nodeExists("/parent/foo"));
+        assertTrue(session.nodeExists("/parent/renamed"));
+    }
+
     @Test(expected = RepositoryException.class)
     public void moveToDescendant() throws RepositoryException {
         Session session = getAdminSession();


Reply via email to