Add some JCR utilities
authorMathieu Baudier <mbaudier@argeo.org>
Mon, 12 Nov 2012 18:13:42 +0000 (18:13 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Mon, 12 Nov 2012 18:13:42 +0000 (18:13 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@5783 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

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

index 5afebcee4811073514c498b064e525b0450ace5b..bcb13b4381e37d67cd536b74d3a24739eaef5be1 100644 (file)
@@ -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<String> 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<String> 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.