]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java
Add create calendar utility
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jcr / src / main / java / org / argeo / jcr / JcrUtils.java
index d3174a1cace4427be061598273b2039b529968c7..b7dfdffc3d81e0e4c686f240848606b34595cf26 100644 (file)
@@ -1,6 +1,26 @@
+/*
+ * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 package org.argeo.jcr;
 
+import java.text.DateFormat;
+import java.text.ParseException;
 import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
 import java.util.StringTokenizer;
 
 import javax.jcr.NamespaceRegistry;
@@ -18,9 +38,17 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.ArgeoException;
 
+/** Utility methods to simplify common JCR operations. */
 public class JcrUtils {
        private final static Log log = LogFactory.getLog(JcrUtils.class);
 
+       /**
+        * Queries one single node.
+        * 
+        * @return one single node or null if none was found
+        * @throws ArgeoException
+        *             if more than one node was found
+        */
        public static Node querySingleNode(Query query) {
                NodeIterator nodeIterator;
                try {
@@ -40,12 +68,14 @@ public class JcrUtils {
                return node;
        }
 
+       /** Removes forbidden characters from a path, replacing them with '_' */
        public static String removeForbiddenCharacters(String str) {
                return str.replace('[', '_').replace(']', '_').replace('/', '_')
                                .replace('*', '_');
 
        }
 
+       /** Retrieves the parent path of the provided path */
        public static String parentPath(String path) {
                if (path.equals("/"))
                        throw new ArgeoException("Root path '/' has no parent path");
@@ -59,7 +89,20 @@ public class JcrUtils {
                return pathT.substring(0, index);
        }
 
+       /** The provided data as a path ('/' at the end, not the beginning) */
        public static String dateAsPath(Calendar cal) {
+               return dateAsPath(cal, false);
+       }
+
+       /**
+        * The provided data as a path ('/' at the end, not the beginning)
+        * 
+        * @param cal
+        *            the date
+        * @param addHour
+        *            whether to add hour as well
+        */
+       public static String dateAsPath(Calendar cal, Boolean addHour) {
                StringBuffer buf = new StringBuffer(14);
                buf.append('Y').append(cal.get(Calendar.YEAR));// 5
                buf.append('/');// 1
@@ -74,16 +117,39 @@ public class JcrUtils {
                        buf.append(0);
                buf.append('D').append(day);// 3
                buf.append('/');// 1
+               if (addHour) {
+                       int hour = cal.get(Calendar.HOUR_OF_DAY);
+                       if (hour < 10)
+                               buf.append(0);
+                       buf.append('H').append(hour);// 3
+                       buf.append('/');// 1
+               }
                return buf.toString();
 
        }
 
+       /** Converts in one call a string into a gregorian calendar. */
+       public static Calendar parseCalendar(DateFormat dateFormat, String value) {
+               try {
+                       Date date = dateFormat.parse(value);
+                       Calendar calendar = new GregorianCalendar();
+                       calendar.setTime(date);
+                       return calendar;
+               } catch (ParseException e) {
+                       throw new ArgeoException("Cannot parse " + value
+                                       + " with date format " + dateFormat, e);
+               }
+
+       }
+
+       /** Converts the FQDN of an host into a path (converts '.' into '/'). */
        public static String hostAsPath(String host) {
                // TODO : inverse order of the elements (to have org/argeo/test IO
                // test/argeo/org
                return host.replace('.', '/');
        }
 
+       /** The last element of a path. */
        public static String lastPathElement(String path) {
                if (path.charAt(path.length() - 1) == '/')
                        throw new ArgeoException("Path " + path + " cannot end with '/'");
@@ -94,16 +160,31 @@ public class JcrUtils {
                return path.substring(index + 1);
        }
 
+       /** Creates the nodes making path, if they don't exist. */
        public static Node mkdirs(Session session, String path) {
                return mkdirs(session, path, null, false);
        }
 
+       /** Creates the nodes making path, if they don't exist. */
        public static Node mkdirs(Session session, String path, String type,
                        Boolean versioning) {
                try {
                        if (path.equals('/'))
                                return session.getRootNode();
 
+                       if (session.itemExists(path)) {
+                               Node node = session.getNode(path);
+                               // check type
+                               if (type != null
+                                               && !type.equals(node.getPrimaryNodeType().getName()))
+                                       throw new ArgeoException("Node " + node
+                                                       + " exists but is of type "
+                                                       + node.getPrimaryNodeType().getName()
+                                                       + " not of type " + type);
+                               // TODO: check versioning
+                               return node;
+                       }
+
                        StringTokenizer st = new StringTokenizer(path, "/");
                        StringBuffer current = new StringBuffer("/");
                        Node currentNode = session.getRootNode();
@@ -130,6 +211,10 @@ public class JcrUtils {
                }
        }
 
+       /**
+        * Safe and repository implementation independent registration of a
+        * namespace.
+        */
        public static void registerNamespaceSafely(Session session, String prefix,
                        String uri) {
                try {
@@ -140,6 +225,10 @@ public class JcrUtils {
                }
        }
 
+       /**
+        * Safe and repository implementation independent registration of a
+        * namespace.
+        */
        public static void registerNamespaceSafely(NamespaceRegistry nr,
                        String prefix, String uri) {
                try {
@@ -164,37 +253,42 @@ public class JcrUtils {
        }
 
        /** Recursively outputs the contents of the given node. */
-       public static void debug(Node node) throws RepositoryException {
-               // First output the node path
-               log.debug(node.getPath());
-               // Skip the virtual (and large!) jcr:system subtree
-               if (node.getName().equals(ArgeoJcrConstants.JCR_SYSTEM)) {
-                       return;
-               }
+       public static void debug(Node node) {
+               try {
+                       // First output the node path
+                       log.debug(node.getPath());
+                       // Skip the virtual (and large!) jcr:system subtree
+                       if (node.getName().equals(ArgeoJcrConstants.JCR_SYSTEM)) {
+                               return;
+                       }
 
-               // Then the children nodes (recursive)
-               NodeIterator it = node.getNodes();
-               while (it.hasNext()) {
-                       Node childNode = it.nextNode();
-                       debug(childNode);
-               }
+                       // Then the children nodes (recursive)
+                       NodeIterator it = node.getNodes();
+                       while (it.hasNext()) {
+                               Node childNode = it.nextNode();
+                               debug(childNode);
+                       }
 
-               // Then output the properties
-               PropertyIterator properties = node.getProperties();
-               // log.debug("Property are : ");
-
-               while (properties.hasNext()) {
-                       Property property = properties.nextProperty();
-                       if (property.getDefinition().isMultiple()) {
-                               // A multi-valued property, print all values
-                               Value[] values = property.getValues();
-                               for (int i = 0; i < values.length; i++) {
-                                       log.debug(property.getPath() + "=" + values[i].getString());
+                       // Then output the properties
+                       PropertyIterator properties = node.getProperties();
+                       // log.debug("Property are : ");
+
+                       while (properties.hasNext()) {
+                               Property property = properties.nextProperty();
+                               if (property.getDefinition().isMultiple()) {
+                                       // A multi-valued property, print all values
+                                       Value[] values = property.getValues();
+                                       for (int i = 0; i < values.length; i++) {
+                                               log.debug(property.getPath() + "="
+                                                               + values[i].getString());
+                                       }
+                               } else {
+                                       // A single-valued property
+                                       log.debug(property.getPath() + "=" + property.getString());
                                }
-                       } else {
-                               // A single-valued property
-                               log.debug(property.getPath() + "=" + property.getString());
                        }
+               } catch (Exception e) {
+                       log.error("Could not debug " + node, e);
                }
 
        }