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/01/18 08:16:04 UTC

[sling-org-apache-sling-pipes] branch master updated: SLING-8220 Add 'overwrite' option to Move Pipe

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 ac820c7  SLING-8220 Add 'overwrite' option to Move Pipe
     new 7a1a058  Merge pull request #9 from spopescu/SLING-8220
ac820c7 is described below

commit ac820c73812b6708838d2a55524a00ce83d9c268
Author: Stefan Popescu <sp...@adobe.com>
AuthorDate: Thu Jan 17 19:02:32 2019 +0200

    SLING-8220 Add 'overwrite' option to Move Pipe
---
 .../org/apache/sling/pipes/internal/MovePipe.java  | 28 +++++++++++++++++++++-
 .../apache/sling/pipes/internal/MovePipeTest.java  | 15 ++++++++++++
 src/test/resources/move.json                       |  6 +++++
 3 files changed, 48 insertions(+), 1 deletion(-)

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 f131998..8fb226e 100644
--- a/src/main/java/org/apache/sling/pipes/internal/MovePipe.java
+++ b/src/main/java/org/apache/sling/pipes/internal/MovePipe.java
@@ -16,6 +16,7 @@
  */
 package org.apache.sling.pipes.internal;
 
+import org.apache.jackrabbit.api.JackrabbitNode;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.pipes.BasePipe;
 import org.apache.sling.pipes.PipeBindings;
@@ -30,6 +31,7 @@ import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import java.util.Collections;
 import java.util.Iterator;
+import java.util.UUID;
 
 /**
  * Does a JCR Move of a node, returns the resource corresponding to the moved node
@@ -38,9 +40,13 @@ public class MovePipe extends BasePipe {
     Logger logger = LoggerFactory.getLogger(MovePipe.class);
 
     public static final String RESOURCE_TYPE = RT_PREFIX + "mv";
+    public static final String PN_OVERWRITE_TARGET = "overwriteTarget";
+
+    private boolean overwriteTarget;
 
     public MovePipe(Plumber plumber, Resource resource, PipeBindings upperBindings) throws Exception {
         super(plumber, resource, upperBindings);
+        overwriteTarget = properties.get(PN_OVERWRITE_TARGET, false);
     }
 
     @Override
@@ -57,7 +63,27 @@ public class MovePipe extends BasePipe {
             try {
                 Session session = resolver.adaptTo(Session.class);
                 if (session.itemExists(targetPath)){
-                    logger.warn("{} already exists, nothing will be done here, nothing outputed");
+                    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);
+                        }
+                    } else {
+                        logger.warn("{} already exists, nothing will be done here, nothing outputed");
+                    }
                 } else {
                     logger.info("moving resource {} to {}", resource.getPath(), targetPath);
                     if (!isDryRun()) {
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 a6cba2f..d26c621 100644
--- a/src/test/java/org/apache/sling/pipes/internal/MovePipeTest.java
+++ b/src/test/java/org/apache/sling/pipes/internal/MovePipeTest.java
@@ -33,7 +33,10 @@ import java.util.Iterator;
 public class MovePipeTest extends AbstractPipeTest {
 
     static final String MOVENODE_PIPE = "/moveNode";
+    static final String MOVENODEOVERWRITE_PIPE = "/moveNodeOverwrite";
     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";
 
@@ -56,6 +59,18 @@ public class MovePipeTest extends AbstractPipeTest {
 
     @Ignore //move operation is not supported yet by MockSession
     @Test
+    public void testMoveNodeWithOverwrite() throws Exception {
+        Iterator<Resource> output = getOutput(PATH_PIPE + MOVENODEOVERWRITE_PIPE);
+        Assert.assertTrue(output.hasNext());
+        output.next();
+        Session session = context.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 testMoveProperty() throws Exception {
         Iterator<Resource> output = getOutput(PATH_PIPE + MOVEPROPERTY_PIPE);
         Assert.assertTrue(output.hasNext());
diff --git a/src/test/resources/move.json b/src/test/resources/move.json
index 638fb31..dc0c5ec 100644
--- a/src/test/resources/move.json
+++ b/src/test/resources/move.json
@@ -9,5 +9,11 @@
     "sling:resourceType":"slingPipes/mv",
     "path": "/content/fruits/apple",
     "expr": "/content/fruits/granny"
+  },
+  "moveNodeOverwrite": {
+    "sling:resourceType":"slingPipes/mv",
+    "path": "/content/fruits/apple",
+    "expr": "/content/fruits/banana",
+    "overwrite": "true"
   }
 }
\ No newline at end of file