JCR properties.
authorMathieu Baudier <mbaudier@argeo.org>
Tue, 2 Feb 2021 11:30:09 +0000 (12:30 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Tue, 2 Feb 2021 11:30:09 +0000 (12:30 +0100)
org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/JcrComposite.java
org.argeo.jcr/src/org/argeo/jcr/Jcr.java

index eb43438a0f90c76a2442f84f423352f04137c738..2d394c6f89ece6564efc347e177c93d63699ffc1 100644 (file)
@@ -85,6 +85,22 @@ public class JcrComposite extends Composite {
                        return null;
        }
 
+       public synchronized String getPropertyName() {
+               try {
+                       return getProperty().getName();
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot get property name", e);
+               }
+       }
+
+       public synchronized Node getPropertyNode() {
+               try {
+                       return getProperty().getNode();
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot get property name", e);
+               }
+       }
+
        public synchronized Property getProperty() {
                try {
                        if (itemIsNode())
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;