Introduce node copy
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 19 Jan 2011 19:39:37 +0000 (19:39 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 19 Jan 2011 19:39:37 +0000 (19:39 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@4055 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java

index b7dfdffc3d81e0e4c686f240848606b34595cf26..4bc30534af14b4aa112c4f49e2180c0833388665 100644 (file)
@@ -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);
+               }
+       }
 }