This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.pipes-0.0.10 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-pipes.git
commit dd440e9e5eb8df18bdce01eb804539719b492aac Author: Robert Munteanu <[email protected]> AuthorDate: Thu Feb 18 10:50:51 2016 +0000 SLING-5434 - WritePipe shoud remove properties at the very end Submitted-By: Nicolas Peltier git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/sling-pipes@1731043 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/sling/pipes/WritePipe.java | 40 ++++++++++++++++------ .../java/org/apache/sling/pipes/WritePipeTest.java | 7 +++- src/test/resources/write.json | 9 +++-- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/apache/sling/pipes/WritePipe.java b/src/main/java/org/apache/sling/pipes/WritePipe.java index 16f0f8f..d12c2c0 100644 --- a/src/main/java/org/apache/sling/pipes/WritePipe.java +++ b/src/main/java/org/apache/sling/pipes/WritePipe.java @@ -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; } diff --git a/src/test/java/org/apache/sling/pipes/WritePipeTest.java b/src/test/java/org/apache/sling/pipes/WritePipeTest.java index 315cf37..75b4371 100644 --- a/src/test/java/org/apache/sling/pipes/WritePipeTest.java +++ b/src/test/java/org/apache/sling/pipes/WritePipeTest.java @@ -98,7 +98,12 @@ public class WritePipeTest extends AbstractPipeTest { 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 diff --git a/src/test/resources/write.json b/src/test/resources/write.json index 3d3dda7..4af4e1c 100644 --- a/src/test/resources/write.json +++ b/src/test/resources/write.json @@ -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}" + } } } } -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
