From: Mathieu Baudier Date: Mon, 12 Nov 2012 18:13:42 +0000 (+0000) Subject: Add some JCR utilities X-Git-Tag: argeo-commons-2.1.30~758 X-Git-Url: http://git.argeo.org/?a=commitdiff_plain;h=82fc1287c13e9bd42b96af762a1c0f1549c82a4f;p=lgpl%2Fargeo-commons.git Add some JCR utilities git-svn-id: https://svn.argeo.org/commons/trunk@5783 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 5afebcee4..bcb13b438 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 @@ -195,6 +195,10 @@ public class JcrUtils implements ArgeoJcrConstants { } } + /* + * PATH UTILITIES + */ + /** Make sure that: starts with '/', do not end with '/', do not have '//' */ public static String normalizePath(String path) { List tokens = tokenize(path); @@ -221,6 +225,21 @@ public class JcrUtils implements ArgeoJcrConstants { return path.toString(); } + /** + * Creates a path from a UUID (e.g. 6ebda899-217d-4bf1-abe4-2839085c8f3c => + * 6ebda899-217d/4bf1/abe4/2839085c8f3c/). '/' at the end, not the beginning + */ + public static String uuidAsPath(String uuid) { + StringBuffer path = new StringBuffer(uuid.length()); + String[] tokens = uuid.split("-"); + for (int i = 0; i < tokens.length; i++) { + path.append(tokens[i]); + if (i != 0) + path.append('/'); + } + return path.toString(); + } + /** * The provided data as a path ('/' at the end, not the beginning) * @@ -402,6 +421,48 @@ public class JcrUtils implements ArgeoJcrConstants { return mkdirs(session, path, type, null, false); } + /** + * Create sub nodes relative to a parent node + * + * @param nodeType + * the type of the leaf node + */ + public static Node mkdirs(Node parentNode, String relativePath, + String nodeType) { + return mkdirs(parentNode, relativePath, nodeType, null); + } + + /** + * Create sub nodes relative to a parent node + * + * @param nodeType + * the type of the leaf node + */ + public static Node mkdirs(Node parentNode, String relativePath, + String nodeType, String intermediaryNodeType) { + List tokens = tokenize(relativePath); + Node currParent = parentNode; + try { + for (int i = 0; i < tokens.size(); i++) { + String name = tokens.get(i); + if (parentNode.hasNode(name)) { + currParent = currParent.getNode(name); + } else { + if (i != (tokens.size() - 1)) {// intermediary + currParent = currParent.addNode(name, + intermediaryNodeType); + } else {// leaf + currParent = currParent.addNode(name, nodeType); + } + } + } + return currParent; + } catch (RepositoryException e) { + throw new ArgeoException("Cannot mkdirs relative path " + + relativePath + " from " + parentNode, e); + } + } + /** * Synchronized and save is performed, to avoid race conditions in * initializers leading to duplicate nodes.