From: Mathieu Baudier Date: Wed, 24 Oct 2012 14:33:10 +0000 (+0000) Subject: Introduce copyFiles X-Git-Tag: argeo-commons-2.1.30~806 X-Git-Url: https://git.argeo.org/?a=commitdiff_plain;h=8152c4123b304cc9bd6b6c2b5d3401100511437f;p=lgpl%2Fargeo-commons.git Introduce copyFiles git-svn-id: https://svn.argeo.org/commons/trunk@5639 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- 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 91eaceeb2..b0d87b197 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 @@ -64,6 +64,7 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.ArgeoException; +import org.argeo.ArgeoMonitor; import org.argeo.util.security.DigestUtils; import org.argeo.util.security.SimplePrincipal; @@ -643,6 +644,9 @@ public class JcrUtils implements ArgeoJcrConstants { */ public static void copy(Node fromNode, Node toNode) { try { + if (toNode.getDefinition().isProtected()) + return; + // process properties PropertyIterator pit = fromNode.getProperties(); properties: while (pit.hasNext()) { @@ -698,6 +702,79 @@ public class JcrUtils implements ArgeoJcrConstants { } } + /** + * Copy only nt:folder and nt:file, without their additional types and + * properties. + * + * @param recursive + * if true copies folders as well, otherwise only first level + * files + * @return how many files were copied + */ + public static Long copyFiles(Node fromNode, Node toNode, Boolean recursive, + ArgeoMonitor monitor) { + long count = 0l; + + Binary binary = null; + InputStream in = null; + try { + NodeIterator fromChildren = fromNode.getNodes(); + while (fromChildren.hasNext()) { + if (monitor != null && monitor.isCanceled()) + throw new ArgeoException( + "Copy cancelled before it was completed"); + + Node fromChild = fromChildren.nextNode(); + String fileName = fromChild.getName(); + if (fromChild.isNodeType(NodeType.NT_FILE)) { + if (monitor != null) + monitor.subTask("Copy " + fileName); + binary = fromChild.getNode(Node.JCR_CONTENT) + .getProperty(Property.JCR_DATA).getBinary(); + in = binary.getStream(); + copyStreamAsFile(toNode, fileName, in); + IOUtils.closeQuietly(in); + JcrUtils.closeQuietly(binary); + + // save session + toNode.getSession().save(); + count++; + + if (log.isDebugEnabled()) + log.debug("Copied file " + fromChild.getPath()); + if (monitor != null) + monitor.worked(1); + } else if (fromChild.isNodeType(NodeType.NT_FOLDER) + && recursive) { + Node toChildFolder; + if (toNode.hasNode(fileName)) { + toChildFolder = toNode.getNode(fileName); + if (!toNode.isNodeType(NodeType.NT_FOLDER)) + throw new ArgeoException(toChildFolder + + " is not of type nt:folder"); + } else { + toChildFolder = toNode.addNode(fileName, + NodeType.NT_FOLDER); + + // save session + toNode.getSession().save(); + } + count = count + + copyFiles(fromChild, toChildFolder, recursive, + monitor); + } + } + return count; + } catch (RepositoryException e) { + throw new ArgeoException("Cannot copy files between " + fromNode + + " and " + toNode); + } finally { + // in case there was an exception + IOUtils.closeQuietly(in); + JcrUtils.closeQuietly(binary); + } + } + /** * Check whether all first-level properties (except jcr:* properties) are * equal. Skip jcr:* properties @@ -1002,6 +1079,9 @@ public class JcrUtils implements ArgeoJcrConstants { Node contentNode; if (folderNode.hasNode(fileName)) { fileNode = folderNode.getNode(fileName); + if (!fileNode.isNodeType(NodeType.NT_FILE)) + throw new ArgeoException(fileNode + + " is not of type nt:file"); // we assume that the content node is already there contentNode = fileNode.getNode(Node.JCR_CONTENT); } else {