You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by np...@apache.org on 2019/08/23 10:03:45 UTC

[sling-org-apache-sling-pipes] branch master updated: SLING-8648 Move pipe to incorporate ordering of the nodes . (#11)

This is an automated email from the ASF dual-hosted git repository.

npeltier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-pipes.git


The following commit(s) were added to refs/heads/master by this push:
     new c9ec60e  SLING-8648 Move pipe to incorporate ordering of the nodes . (#11)
c9ec60e is described below

commit c9ec60e843c8fb93c29143932cdbf29a31b7cbeb
Author: Dinesh Chaudhary <di...@adobe.com>
AuthorDate: Fri Aug 23 15:33:40 2019 +0530

    SLING-8648 Move pipe to incorporate ordering of the nodes . (#11)
---
 .../org/apache/sling/pipes/internal/MovePipe.java  | 69 ++++++++++++++++------
 .../apache/sling/pipes/internal/MovePipeTest.java  | 52 ++++++++++++----
 src/test/resources/move.json                       |  8 ++-
 3 files changed, 97 insertions(+), 32 deletions(-)

diff --git a/src/main/java/org/apache/sling/pipes/internal/MovePipe.java b/src/main/java/org/apache/sling/pipes/internal/MovePipe.java
index 8fb226e..7c5eb4a 100644
--- a/src/main/java/org/apache/sling/pipes/internal/MovePipe.java
+++ b/src/main/java/org/apache/sling/pipes/internal/MovePipe.java
@@ -18,6 +18,7 @@ package org.apache.sling.pipes.internal;
 
 import org.apache.jackrabbit.api.JackrabbitNode;
 import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.pipes.BasePipe;
 import org.apache.sling.pipes.PipeBindings;
 import org.apache.sling.pipes.Plumber;
@@ -41,12 +42,16 @@ public class MovePipe extends BasePipe {
 
     public static final String RESOURCE_TYPE = RT_PREFIX + "mv";
     public static final String PN_OVERWRITE_TARGET = "overwriteTarget";
+    public static final String PN_ORDERBEFORE = "orderBeforeTarget";
 
     private boolean overwriteTarget;
+    private boolean orderBefore;
+
 
     public MovePipe(Plumber plumber, Resource resource, PipeBindings upperBindings) throws Exception {
         super(plumber, resource, upperBindings);
         overwriteTarget = properties.get(PN_OVERWRITE_TARGET, false);
+        orderBefore = properties.get(PN_ORDERBEFORE, false);
     }
 
     @Override
@@ -62,24 +67,31 @@ public class MovePipe extends BasePipe {
             String targetPath = getExpr();
             try {
                 Session session = resolver.adaptTo(Session.class);
-                if (session.itemExists(targetPath)){
-                    if (overwriteTarget && !isDryRun()) {
-                        logger.debug("overwriting {}", targetPath);
-                        Node targetParent = session.getItem(targetPath).getParent();
-                        if (targetParent.getPrimaryNodeType().hasOrderableChildNodes()) {
-                            String targetPathNewNode = targetPath + UUID.randomUUID() ;
-                            session.move(resource.getPath(), targetPathNewNode);
-                            String newNodeName = targetPathNewNode.substring(targetPathNewNode.lastIndexOf("/") + 1);
-                            String oldNodeName = targetPath.substring(targetPath.lastIndexOf("/") + 1);
-                            targetParent.orderBefore(newNodeName, oldNodeName);
-                            session.removeItem(targetPath);
-                            // Need to use JackrabbitNode.rename() here, since session.move(targetPathNewNode, targetPath)
-                            // would move the new node back to the end of its siblings list
-                            JackrabbitNode newNode = (JackrabbitNode) session.getNode(targetPathNewNode);
-                            newNode.rename(oldNodeName);
-                        } else {
-                            session.removeItem(targetPath);
-                            session.move(resource.getPath(), targetPath);
+                if (orderBefore) {
+                    output = reorder(resource, targetPath, session);
+                } else if (session.itemExists(targetPath)) {
+                    if (overwriteTarget) {
+                        logger.info("overwriting {}", targetPath);
+                        if (!isDryRun()) {
+                            Resource parent = resolver.getResource(targetPath).getParent();
+                            Node targetParent = session.getItem(targetPath).getParent();
+                            if (targetParent.getPrimaryNodeType().hasOrderableChildNodes()) {
+                                String oldNodeName = targetPath.substring(targetPath.lastIndexOf("/") + 1);
+                                String targetPathNewNode = targetPath + UUID.randomUUID();
+                                String newNodeName = targetPathNewNode.substring(targetPathNewNode.lastIndexOf("/") + 1);
+                                session.move(resource.getPath(), targetPathNewNode);
+                                targetParent.orderBefore(newNodeName, oldNodeName);
+                                session.removeItem(targetPath);
+                                // Need to use JackrabbitNode.rename() here, since session.move(targetPathNewNode, targetPath)
+                                // would move the new node back to the end of its siblings list
+                                JackrabbitNode newNode = (JackrabbitNode) session.getNode(targetPathNewNode);
+                                newNode.rename(oldNodeName);
+                                return Collections.singleton(parent.getChild(oldNodeName)).iterator();
+                            } else {
+                                session.removeItem(targetPath);
+                                session.move(resource.getPath(), targetPath);
+                                return Collections.singleton(parent.getChild(resource.getName())).iterator();
+                            }
                         }
                     } else {
                         logger.warn("{} already exists, nothing will be done here, nothing outputed");
@@ -116,4 +128,25 @@ public class MovePipe extends BasePipe {
         }
         return output;
     }
+
+    private Iterator<Resource> reorder(Resource resource, String targetPath, Session session) throws Exception {
+        logger.info("ordering {} before {}", resource.getPath(), targetPath);
+        if (session.itemExists(targetPath) && !isDryRun()) {
+            Resource parent = resolver.getResource(targetPath).getParent();
+            Node targetParent = session.getItem(targetPath).getParent();
+            String oldNodeName = targetPath.substring(targetPath.lastIndexOf("/") + 1);
+            if (targetParent.getPrimaryNodeType().hasOrderableChildNodes()) {
+                String targetNodeName = ResourceUtil.createUniqueChildName(parent, resource.getName());
+                String targetNodePath = targetParent.getPath() + SLASH + targetNodeName;
+                session.move(resource.getPath(), targetNodePath);
+                targetParent.orderBefore(targetNodeName, oldNodeName);
+                return Collections.singleton(parent.getChild(targetNodeName)).iterator();
+            } else {
+                logger.warn("parent resource {} doesn't support ordering", parent.getPath());
+            }
+        } else {
+            logger.warn("target resource {} doesn't exist ordering not possible", targetPath);
+        }
+        return EMPTY_ITERATOR;
+    }
 }
diff --git a/src/test/java/org/apache/sling/pipes/internal/MovePipeTest.java b/src/test/java/org/apache/sling/pipes/internal/MovePipeTest.java
index d26c621..bc3f559 100644
--- a/src/test/java/org/apache/sling/pipes/internal/MovePipeTest.java
+++ b/src/test/java/org/apache/sling/pipes/internal/MovePipeTest.java
@@ -16,16 +16,21 @@
  */
 package org.apache.sling.pipes.internal;
 
+import org.apache.commons.collections.IteratorUtils;
 import org.apache.sling.api.resource.PersistenceException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.pipes.AbstractPipeTest;
+import org.apache.sling.pipes.Pipe;
+import org.apache.sling.testing.mock.sling.ResourceResolverType;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
 import org.junit.Assert;
 import org.junit.Before;
-import org.junit.Ignore;
+import org.junit.Rule;
 import org.junit.Test;
 
 import javax.jcr.Session;
 import java.util.Iterator;
+import java.util.List;
 
 /**
  * testing moving nodes & properties
@@ -34,50 +39,71 @@ public class MovePipeTest extends AbstractPipeTest {
 
     static final String MOVENODE_PIPE = "/moveNode";
     static final String MOVENODEOVERWRITE_PIPE = "/moveNodeOverwrite";
+    static final String MOVENODEORDER_PIPE = "/moveNodeOrder";
     static final String MOVEPROPERTY_PIPE = "/moveProperty";
     static final String APPLE_NODE_PATH = "/apple";
     static final String BANANA_NODE_PATH = "/banana";
     static final String MOVED_NODE_PATH = "/granny";
-    static final String MOVED_PROPERTY_PATH = "/indexFruits";
+    static final String MOVED_PROPERTY_PATH = "/fruitsIndex";
+
+    @Rule
+    public SlingContext oak = new SlingContext(ResourceResolverType.JCR_OAK);
 
     @Before
     public void setup() throws PersistenceException {
         super.setup();
-        context.load().json("/move.json", PATH_PIPE);
+        oak.load().json("/move.json", PATH_PIPE);
+        oak.load().json("/SLING-INF/jcr_root/content/fruits.json", PATH_FRUITS);
     }
 
-    @Ignore //move operation is not supported yet by MockSession
     @Test
     public void testMoveNode() throws Exception {
-        Iterator<Resource> output = getOutput(PATH_PIPE + MOVENODE_PIPE);
+        Pipe pipe = plumber.getPipe(oak.resourceResolver().getResource(PATH_PIPE + MOVENODE_PIPE));
+        Iterator<Resource> output = pipe.getOutput();
         Assert.assertTrue(output.hasNext());
         output.next();
-        Session session = context.resourceResolver().adaptTo(Session.class);
+        Session session = oak.resourceResolver().adaptTo(Session.class);
         session.save();
         Assert.assertTrue("new node path should exists", session.nodeExists(PATH_FRUITS + MOVED_NODE_PATH));
     }
 
-    @Ignore //move operation is not supported yet by MockSession
     @Test
     public void testMoveNodeWithOverwrite() throws Exception {
-        Iterator<Resource> output = getOutput(PATH_PIPE + MOVENODEOVERWRITE_PIPE);
+        Pipe pipe = plumber.getPipe(oak.resourceResolver().getResource(PATH_PIPE + MOVENODEOVERWRITE_PIPE));
+        Iterator<Resource> output = pipe.getOutput();
         Assert.assertTrue(output.hasNext());
         output.next();
-        Session session = context.resourceResolver().adaptTo(Session.class);
+        Session session = oak.resourceResolver().adaptTo(Session.class);
         session.save();
         Assert.assertTrue("target node path should exist", session.nodeExists(PATH_FRUITS + BANANA_NODE_PATH));
         Assert.assertFalse("source node path should have gone", session.nodeExists(PATH_FRUITS + APPLE_NODE_PATH));
     }
 
-    @Ignore //move operation is not supported yet by MockSession
+    @Test
+    public void testMoveNodeWithOrdering() throws Exception {
+        Pipe pipe = plumber.getPipe(oak.resourceResolver().getResource(PATH_PIPE + MOVENODEORDER_PIPE));
+        Iterator<Resource> output = pipe.getOutput();
+        Assert.assertTrue(output.hasNext());
+        Resource resource = output.next();
+        Resource parent = resource.getParent();
+        List<Resource> allFruits = IteratorUtils.toList(parent.listChildren());
+        Session session = oak.resourceResolver().adaptTo(Session.class);
+        session.save();
+        Assert.assertTrue("target node path should exist", session.nodeExists(PATH_FRUITS + APPLE_NODE_PATH));
+        Assert.assertTrue("source node path also should exist", session.nodeExists(resource.getPath()));
+        Assert.assertEquals("banana should be at first position", allFruits.get(0).getName(), resource.getName());
+        Assert.assertEquals("apple should be at first position", allFruits.get(1).getName(), "apple");
+    }
+
     @Test
     public void testMoveProperty() throws Exception {
-        Iterator<Resource> output = getOutput(PATH_PIPE + MOVEPROPERTY_PIPE);
+        Pipe pipe = plumber.getPipe(oak.resourceResolver().getResource(PATH_PIPE + MOVEPROPERTY_PIPE));
+        Iterator<Resource> output = pipe.getOutput();
         Assert.assertTrue(output.hasNext());
         output.next();
-        Session session = context.resourceResolver().adaptTo(Session.class);
+        Session session = oak.resourceResolver().adaptTo(Session.class);
         session.save();
-        Assert.assertTrue("new property path should exists", session.propertyExists(PATH_FRUITS + MOVED_NODE_PATH));
+        Assert.assertTrue("new property path should exists", session.propertyExists(PATH_FRUITS + MOVED_PROPERTY_PATH));
         Assert.assertFalse("old property path should not", session.propertyExists(PATH_FRUITS + PN_INDEX));
     }
 }
diff --git a/src/test/resources/move.json b/src/test/resources/move.json
index dc0c5ec..36b777f 100644
--- a/src/test/resources/move.json
+++ b/src/test/resources/move.json
@@ -14,6 +14,12 @@
     "sling:resourceType":"slingPipes/mv",
     "path": "/content/fruits/apple",
     "expr": "/content/fruits/banana",
-    "overwrite": "true"
+    "overwriteTarget": true
+  },
+  "moveNodeOrder": {
+    "sling:resourceType":"slingPipes/mv",
+    "path": "/content/fruits/banana",
+    "expr": "/content/fruits/apple",
+    "orderBeforeTarget": true
   }
 }
\ No newline at end of file