JCR properties.
[lgpl/argeo-commons.git] / org.argeo.jcr / src / org / argeo / jcr / Jcr.java
index 974ee02df30e338215c202d7c0b76a0c45e3f2b3..1ed7469f1d5516bb0c8cf2ad8c2ac0b086dd1a86 100644 (file)
@@ -203,6 +203,21 @@ public class Jcr {
                }
        }
 
+       /**
+        * @see Node#getProperty(String)
+        * @throws JcrException caused by {@link RepositoryException}
+        */
+       public static Property getProperty(Node node, String property) {
+               try {
+                       if (node.hasProperty(property))
+                               return node.getProperty(property);
+                       else
+                               return null;
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot get property " + property + " of " + node, e);
+               }
+       }
+
        /**
         * @see Node#getIndex()
         * @throws JcrException caused by {@link RepositoryException}
@@ -578,26 +593,11 @@ public class Jcr {
         * @throws JcrException             in case of unexpected
         *                                  {@link RepositoryException}
         */
-       @SuppressWarnings("unchecked")
        public static <T> List<T> getMultiple(Node node, String property) {
                try {
                        if (node.hasProperty(property)) {
                                Property p = node.getProperty(property);
-                               try {
-                                       List<T> res = new ArrayList<>();
-                                       if (!p.isMultiple()) {
-                                               res.add((T) get(p.getValue()));
-                                               return res;
-                                       }
-                                       Value[] values = p.getValues();
-                                       for (Value value : values) {
-                                               res.add((T) get(value));
-                                       }
-                                       return res;
-                               } catch (ClassCastException e) {
-                                       throw new IllegalArgumentException(
-                                                       "Cannot cast property of type " + PropertyType.nameFromValue(p.getType()), e);
-                               }
+                               return getMultiple(p);
                        } else {
                                return null;
                        }
@@ -606,6 +606,28 @@ public class Jcr {
                }
        }
 
+       /**
+        * Get a multiple property as a list, doing a best effort to cast it as the
+        * target list.
+        */
+       @SuppressWarnings("unchecked")
+       public static <T> List<T> getMultiple(Property p) {
+               try {
+                       List<T> res = new ArrayList<>();
+                       if (!p.isMultiple()) {
+                               res.add((T) get(p.getValue()));
+                               return res;
+                       }
+                       Value[] values = p.getValues();
+                       for (Value value : values) {
+                               res.add((T) get(value));
+                       }
+                       return res;
+               } catch (ClassCastException | RepositoryException e) {
+                       throw new IllegalArgumentException("Cannot get property " + p, e);
+               }
+       }
+
        /** Cast a {@link Value} to a standard Java object. */
        public static Object get(Value value) {
                Binary binary = null;