From: Mathieu Baudier Date: Tue, 2 Feb 2021 11:30:09 +0000 (+0100) Subject: JCR properties. X-Git-Tag: argeo-commons-2.1.91~6 X-Git-Url: https://git.argeo.org/?p=lgpl%2Fargeo-commons.git;a=commitdiff_plain;h=563e3bf057cbb917096f34c9d365127660558588 JCR properties. --- diff --git a/org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/JcrComposite.java b/org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/JcrComposite.java index eb43438a0..2d394c6f8 100644 --- a/org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/JcrComposite.java +++ b/org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/JcrComposite.java @@ -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()) diff --git a/org.argeo.jcr/src/org/argeo/jcr/Jcr.java b/org.argeo.jcr/src/org/argeo/jcr/Jcr.java index 974ee02df..1ed7469f1 100644 --- a/org.argeo.jcr/src/org/argeo/jcr/Jcr.java +++ b/org.argeo.jcr/src/org/argeo/jcr/Jcr.java @@ -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 List getMultiple(Node node, String property) { try { if (node.hasProperty(property)) { Property p = node.getProperty(property); - try { - List 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 List getMultiple(Property p) { + try { + List 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;