X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.jcr%2Fsrc%2Forg%2Fargeo%2Fjcr%2FJcr.java;h=31077737e8e7e789335e263409b2279d190b1b27;hb=e7c40442d7e33e619f427e0d71084196e2efeb6a;hp=974ee02df30e338215c202d7c0b76a0c45e3f2b3;hpb=623a35d1a39522cf8b6a1d6c860ac60a2646f03d;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.jcr/src/org/argeo/jcr/Jcr.java b/org.argeo.jcr/src/org/argeo/jcr/Jcr.java index 974ee02df..31077737e 100644 --- a/org.argeo.jcr/src/org/argeo/jcr/Jcr.java +++ b/org.argeo.jcr/src/org/argeo/jcr/Jcr.java @@ -203,6 +203,36 @@ 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} @@ -578,26 +608,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 +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 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;