You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2016/02/18 11:50:51 UTC

svn commit: r1731043 - in /sling/trunk/contrib/extensions/sling-pipes/src: main/java/org/apache/sling/pipes/WritePipe.java test/java/org/apache/sling/pipes/WritePipeTest.java test/resources/write.json

Author: rombert
Date: Thu Feb 18 10:50:51 2016
New Revision: 1731043

URL: http://svn.apache.org/viewvc?rev=1731043&view=rev
Log:
SLING-5434 - WritePipe shoud remove properties at the very end

Submitted-By: Nicolas Peltier

Modified:
    sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/WritePipe.java
    sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/WritePipeTest.java
    sling/trunk/contrib/extensions/sling-pipes/src/test/resources/write.json

Modified: sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/WritePipe.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/WritePipe.java?rev=1731043&r1=1731042&r2=1731043&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/WritePipe.java (original)
+++ sling/trunk/contrib/extensions/sling-pipes/src/main/java/org/apache/sling/pipes/WritePipe.java Thu Feb 18 10:50:51 2016
@@ -43,6 +43,7 @@ public class WritePipe extends BasePipe
     public static final String RESOURCE_TYPE = "slingPipes/write";
     Node confTree;
     String resourceExpression;
+    private List<Resource> propertiesToRemove;
     Pattern addPatch = Pattern.compile("\\+\\[(.*)\\]");
     Pattern multi = Pattern.compile("\\[(.*)\\]");
 
@@ -112,16 +113,7 @@ public class WritePipe extends BasePipe
                     if (value == null) {
                         //null value are not handled by modifiable value maps,
                         //removing the property if it exists
-                        Resource propertyResource = target.getChild(key);
-                        if (propertyResource != null) {
-                            logger.info("removing {}", propertyResource.getPath());
-                            if (!isDryRun()){
-                                Property property = propertyResource.adaptTo(Property.class);
-                                if (property != null) {
-                                    property.remove();
-                                }
-                            }
-                        }
+                        addPropertyToRemove(target.getChild(key));
                     } else {
                         logger.info("writing {}={}",target.getPath() + "@" + key, value);
                         if (!isDryRun()){
@@ -134,6 +126,19 @@ public class WritePipe extends BasePipe
     }
 
     /**
+     * we store all property to remove for very last moment (in order to potentially reuse their value)
+     * @param property
+     */
+    private void addPropertyToRemove(Resource property){
+        if (property != null) {
+            if (propertiesToRemove == null) {
+                propertiesToRemove = new ArrayList<>();
+            }
+            propertiesToRemove.add(property);
+        }
+    }
+
+    /**
      * write the configured tree at the target resource, creating each node if needed, copying values.
      * @param conf
      * @return
@@ -162,10 +167,25 @@ public class WritePipe extends BasePipe
             Resource resource = getInput();
             if (resource != null) {
                 writeTree(confTree, resource);
+                if (propertiesToRemove != null && !propertiesToRemove.isEmpty()){
+                    for (Resource propertyResource : propertiesToRemove) {
+                        logger.info("removing {}", propertyResource.getPath());
+                        if (!isDryRun()){
+                            Property property = propertyResource.adaptTo(Property.class);
+                            if (property != null) {
+                                property.remove();
+                            }
+                        }
+                    }
+                }
                 return super.getOutput();
             }
         } catch (Exception e) {
             logger.error("unable to write values, cutting pipe", e);
+        } finally {
+            if (propertiesToRemove != null){
+                propertiesToRemove.clear();
+            }
         }
         return EMPTY_ITERATOR;
     }

Modified: sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/WritePipeTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/WritePipeTest.java?rev=1731043&r1=1731042&r2=1731043&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/WritePipeTest.java (original)
+++ sling/trunk/contrib/extensions/sling-pipes/src/test/java/org/apache/sling/pipes/WritePipeTest.java Thu Feb 18 10:50:51 2016
@@ -98,7 +98,12 @@ public class WritePipeTest extends Abstr
         Resource resource = it.next();
         assertEquals("path should be the one configured in first pipe", pipePath + "/conf/fruit/conf/apple", resource.getPath());
         context.resourceResolver().commit();
-        assertEquals("Configured value should be written", "apple is a fruit and its color is green", resource.adaptTo(ValueMap.class).get("jcr:description", ""));
+        ValueMap properties = resource.adaptTo(ValueMap.class);
+        assertEquals("Configured value should be written", "apple is a fruit and its color is green", properties.get("jcr:description", ""));
+        assertEquals("Worm has been removed", "", properties.get("worm", ""));
+        Resource archive = resource.getChild("archive/wasthereworm");
+        assertNotNull("there is an archive of the worm value", archive);
+        assertEquals("Worm value has been written at the same time", "true", archive.adaptTo(String.class));
     }
 
     @Test

Modified: sling/trunk/contrib/extensions/sling-pipes/src/test/resources/write.json
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/sling-pipes/src/test/resources/write.json?rev=1731043&r1=1731042&r2=1731043&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/sling-pipes/src/test/resources/write.json (original)
+++ sling/trunk/contrib/extensions/sling-pipes/src/test/resources/write.json Thu Feb 18 10:50:51 2016
@@ -45,7 +45,7 @@
         "sling:resourceType":"slingPipes/dummySearch",
         "conf":{
           "jcr:primaryType":"nt:unstructured",
-            "apple":{"jcr:primaryType":"nt:unstructured","color":"green", "name":"apple"},
+            "apple":{"jcr:primaryType":"nt:unstructured","worm":"true","color":"green", "name":"apple"},
             "banana":{"jcr:primaryType":"nt:unstructured", "color":"yellow", "name":"banana"}
           }
         },
@@ -55,7 +55,12 @@
         "jcr:description":"written resource is here is coming from previous pipe",
         "conf":{
           "jcr:primaryType":"nt:unstructured",
-          "jcr:description":"${fruit.name} is a fruit and its color is ${fruit.color}"
+          "jcr:description":"${fruit.name} is a fruit and its color is ${fruit.color}",
+          "worm":"${null}",
+          "archive":{
+            "jcr:primaryType":"nt:unstructured",
+            "wasthereworm":"${fruit.worm}"
+          }
         }
       }
     }