]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java
Improve GIS
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jcr / src / main / java / org / argeo / jcr / JcrUtils.java
index a579392dd50e97c7d13d06382f0c3904daf36978..6871e60c76a57a1f1479aff03003f246596315b9 100644 (file)
@@ -16,6 +16,8 @@
 
 package org.argeo.jcr;
 
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.util.Calendar;
@@ -27,6 +29,7 @@ import java.util.Map;
 import java.util.StringTokenizer;
 import java.util.TreeMap;
 
+import javax.jcr.Binary;
 import javax.jcr.NamespaceRegistry;
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
@@ -35,6 +38,7 @@ import javax.jcr.PropertyIterator;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.Value;
+import javax.jcr.nodetype.NodeType;
 import javax.jcr.query.Query;
 import javax.jcr.query.QueryResult;
 
@@ -98,6 +102,27 @@ public class JcrUtils {
                return dateAsPath(cal, false);
        }
 
+       /**
+        * Creates a deep path based on a URL:
+        * http://subdomain.example.com/to/content?args =>
+        * com/example/subdomain/to/content
+        */
+       public static String urlAsPath(String url) {
+               try {
+                       URL u = new URL(url);
+                       StringBuffer path = new StringBuffer(url.length());
+                       // invert host
+                       String[] hostTokens = u.getHost().split("\\.");
+                       for (int i = hostTokens.length - 1; i >= 0; i--)
+                               path.append(hostTokens[i]).append('/');
+                       // we don't put port since it may not always be there and may change
+                       path.append(u.getPath());
+                       return path.toString();
+               } catch (MalformedURLException e) {
+                       throw new ArgeoException("Cannot generate URL path for " + url, e);
+               }
+       }
+
        /**
         * The provided data as a path ('/' at the end, not the beginning)
         * 
@@ -166,12 +191,30 @@ public class JcrUtils {
 
        /** Creates the nodes making path, if they don't exist. */
        public static Node mkdirs(Session session, String path) {
-               return mkdirs(session, path, null, false);
+               return mkdirs(session, path, null, null, false);
        }
 
-       /** Creates the nodes making path, if they don't exist. */
+       /**
+        * @deprecated use {@link #mkdirs(Session, String, String, String, Boolean)}
+        *             instead.
+        */
+       @Deprecated
        public static Node mkdirs(Session session, String path, String type,
                        Boolean versioning) {
+               return mkdirs(session, path, type, type, false);
+       }
+
+       /**
+        * @param type
+        *            the type of the leaf node
+        */
+       public static Node mkdirs(Session session, String path, String type) {
+               return mkdirs(session, path, type, null, false);
+       }
+
+       /** Creates the nodes making path, if they don't exist. */
+       public static Node mkdirs(Session session, String path, String type,
+                       String intermediaryNodeType, Boolean versioning) {
                try {
                        if (path.equals('/'))
                                return session.getRootNode();
@@ -196,12 +239,15 @@ public class JcrUtils {
                                String part = st.nextToken();
                                current.append(part).append('/');
                                if (!session.itemExists(current.toString())) {
-                                       if (type != null)
+                                       if (!st.hasMoreTokens() && type != null)
                                                currentNode = currentNode.addNode(part, type);
+                                       else if (st.hasMoreTokens() && intermediaryNodeType != null)
+                                               currentNode = currentNode.addNode(part,
+                                                               intermediaryNodeType);
                                        else
                                                currentNode = currentNode.addNode(part);
                                        if (versioning)
-                                               currentNode.addMixin(ArgeoJcrConstants.MIX_VERSIONABLE);
+                                               currentNode.addMixin(NodeType.MIX_VERSIONABLE);
                                        if (log.isTraceEnabled())
                                                log.debug("Added folder " + part + " as " + current);
                                } else {
@@ -262,7 +308,7 @@ public class JcrUtils {
                        // First output the node path
                        log.debug(node.getPath());
                        // Skip the virtual (and large!) jcr:system subtree
-                       if (node.getName().equals(ArgeoJcrConstants.JCR_SYSTEM)) {
+                       if (node.getName().equals("jcr:system")) {
                                return;
                        }
 
@@ -438,16 +484,26 @@ public class JcrUtils {
                try {
                        Iterator<String> pit = properties.iterator();
 
-                       while (pit.hasNext()) {
+                       props: while (pit.hasNext()) {
                                String name = pit.next();
-                               if (!observed.hasProperty(name)) {
+                               if (!reference.hasProperty(name)) {
+                                       if (!observed.hasProperty(name))
+                                               continue props;
+                                       Value val = observed.getProperty(name).getValue();
+                                       try {
+                                               // empty String but not null
+                                               if ("".equals(val.getString()))
+                                                       continue props;
+                                       } catch (Exception e) {
+                                               // not parseable as String, silent
+                                       }
+                                       PropertyDiff pDiff = new PropertyDiff(PropertyDiff.ADDED,
+                                                       name, null, val);
+                                       diffs.put(name, pDiff);
+                               } else if (!observed.hasProperty(name)) {
                                        PropertyDiff pDiff = new PropertyDiff(PropertyDiff.REMOVED,
                                                        name, reference.getProperty(name).getValue(), null);
                                        diffs.put(name, pDiff);
-                               } else if (!reference.hasProperty(name)) {
-                                       PropertyDiff pDiff = new PropertyDiff(PropertyDiff.ADDED,
-                                                       name, null, observed.getProperty(name).getValue());
-                                       diffs.put(name, pDiff);
                                } else {
                                        Value referenceValue = reference.getProperty(name)
                                                        .getValue();
@@ -475,4 +531,18 @@ public class JcrUtils {
                else
                        return baseRelPath + '/' + propertyName;
        }
+
+       /**
+        * Normalize a name so taht it can be stores in contexts not supporting
+        * names with ':' (typically databases). Replaces ':' by '_'.
+        */
+       public static String normalize(String name) {
+               return name.replace(':', '_');
+       }
+
+       public static void closeQuietly(Binary binary) {
+               if (binary == null)
+                       return;
+               binary.dispose();
+       }
 }