From d4465e805a815f35b954c19ef0228567c3e3d928 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Wed, 19 Jan 2011 19:39:37 +0000 Subject: [PATCH] Introduce node copy git-svn-id: https://svn.argeo.org/commons/trunk@4055 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../src/main/java/org/argeo/jcr/JcrUtils.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java index b7dfdffc3..4bc30534a 100644 --- a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java +++ b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java @@ -292,4 +292,42 @@ public class JcrUtils { } } + + /** + * Copies recursively the content of a node to another one. Mixin are NOT + * copied. + */ + public static void copy(Node fromNode, Node toNode) { + try { + PropertyIterator pit = fromNode.getProperties(); + properties: while (pit.hasNext()) { + Property fromProperty = pit.nextProperty(); + String propertyName = fromProperty.getName(); + if (toNode.hasProperty(propertyName) + && toNode.getProperty(propertyName).getDefinition() + .isProtected()) + continue properties; + + toNode.setProperty(fromProperty.getName(), + fromProperty.getValue()); + } + + NodeIterator nit = fromNode.getNodes(); + while (nit.hasNext()) { + Node fromChild = nit.nextNode(); + Integer index = fromChild.getIndex(); + String nodeRelPath = fromChild.getName() + "[" + index + "]"; + Node toChild; + if (toNode.hasNode(nodeRelPath)) + toChild = toNode.getNode(nodeRelPath); + else + toChild = toNode.addNode(fromChild.getName(), fromChild + .getPrimaryNodeType().getName()); + copy(fromChild, toChild); + } + } catch (RepositoryException e) { + throw new ArgeoException("Cannot copy " + fromNode + " to " + + toNode, e); + } + } } -- 2.30.2