X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms.jcr%2Fsrc%2Forg%2Fargeo%2Fjcr%2FJcr.java;h=b6ee85eef345f6cf8f9ce0e2f521cdbb346c0d44;hb=HEAD;hp=88f534503d5a142e5a4d87d4deec57a0e5c24979;hpb=40e89f491fb2aad2e1219b28106588ea365198a0;p=gpl%2Fargeo-jcr.git diff --git a/org.argeo.cms.jcr/src/org/argeo/jcr/Jcr.java b/org.argeo.cms.jcr/src/org/argeo/jcr/Jcr.java index 88f5345..b6ee85e 100644 --- a/org.argeo.cms.jcr/src/org/argeo/jcr/Jcr.java +++ b/org.argeo.cms.jcr/src/org/argeo/jcr/Jcr.java @@ -14,6 +14,7 @@ import java.util.Date; import java.util.GregorianCalendar; import java.util.Iterator; import java.util.List; +import java.util.Objects; import javax.jcr.Binary; import javax.jcr.ItemNotFoundException; @@ -431,6 +432,20 @@ public class Jcr { } } + /** + * Whether this node has this property. + * + * @see Node#hasProperty(String) + * @throws JcrException caused by {@link RepositoryException} + */ + public static boolean hasProperty(Node node, String property) { + try { + return node.hasProperty(property); + } catch (RepositoryException e) { + throw new JcrException("Cannot check whether " + node + " has property " + property, e); + } + } + /** * Set a property to the given value, or remove it if the value is * null. @@ -581,6 +596,7 @@ public class Jcr { */ @SuppressWarnings("unchecked") public static T getAs(Node node, String property, T defaultValue) { + Objects.requireNonNull(defaultValue); try { // TODO deal with multiple if (node.hasProperty(property)) { @@ -603,14 +619,32 @@ public class Jcr { } } + @SuppressWarnings("unchecked") public static T getAs(Node node, String property, Class clss) { - if (String.class.isAssignableFrom(clss)) { - return (T) get(node, property); - } else if (Long.class.isAssignableFrom(clss)) { - return (T) get(node, property); - } else { - throw new IllegalArgumentException("Unsupported format " + clss); + try { + Property p = node.getProperty(property); + try { + if (p.isMultiple()) { + throw new UnsupportedOperationException("Multiple values properties are not supported"); + } + Value value = p.getValue(); + return (T) get(value); + } catch (ClassCastException e) { + throw new IllegalArgumentException( + "Cannot cast property of type " + PropertyType.nameFromValue(p.getType()), e); + } + } catch (RepositoryException e) { + throw new JcrException("Cannot retrieve property " + property + " from " + node, e); } +// if (String.class.isAssignableFrom(clss)) { +// return (T) get(node, property); +// } else if (Long.class.isAssignableFrom(clss)) { +// return (T) get(node, property); +// } else if (Boolean.class.isAssignableFrom(clss)) { +// return (T) get(node, property); +// } else { +// throw new IllegalArgumentException("Unsupported format " + clss); +// } } /**