Indexed name.
[lgpl/argeo-commons.git] / org.argeo.jcr / src / org / argeo / jcr / Jcr.java
index c447a17328ca49d39bfdbdc43a799d0b168d5695..31077737e8e7e789335e263409b2279d190b1b27 100644 (file)
@@ -203,6 +203,48 @@ public class Jcr {
                }
        }
 
+       /**
+        * Returns the node name with its current index (useful for re-ordering).
+        * 
+        * @see Node#getName()
+        * @see Node#getIndex()
+        * @throws JcrException caused by {@link RepositoryException}
+        */
+       public static String getIndexedName(Node node) {
+               try {
+                       return node.getName() + "[" + node.getIndex() + "]";
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot get name of " + node, e);
+               }
+       }
+
+       /**
+        * @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}
+        */
+       public static int getIndex(Node node) {
+               try {
+                       return node.getIndex();
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot get index of " + node, e);
+               }
+       }
+
        /**
         * If node has mixin {@link NodeType#MIX_TITLE}, return
         * {@link Property#JCR_TITLE}, otherwise return {@link #getName(Node)}.
@@ -566,26 +608,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;
                        }
@@ -594,6 +621,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;