X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=org.argeo.cms.jcr%2Fsrc%2Forg%2Fargeo%2Fjcr%2FJcr.java;h=b6ee85eef345f6cf8f9ce0e2f521cdbb346c0d44;hb=6b149cd77b13dafd166e134b11d5c2d5dc7bc8b1;hp=49b008d70751e24e4f362c9e5e2b9caab66f06cc;hpb=bce03099b0d2f1758e7a3d74fba339d0200924d5;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 49b008d..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;
@@ -49,11 +50,15 @@ public class Jcr {
/**
* The name of a node which will be serialized as XML text, as per section 7.3.1
* of the JCR 2.0 specifications.
+ *
+ * @see Workspace#NAME_JCR_XMLTEXT
*/
public final static String JCR_XMLTEXT = "jcr:xmltext";
/**
* The name of a property which will be serialized as XML text, as per section
* 7.3.1 of the JCR 2.0 specifications.
+ *
+ * @see Workspace#NAME_JCR_XMLCHARACTERS
*/
public final static String JCR_XMLCHARACTERS = "jcr:xmlcharacters";
/**
@@ -427,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
.
@@ -577,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)) {
@@ -599,14 +619,49 @@ 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);
+// }
+ }
+
+ /**
+ * Retrieve a {@link PropertyType#DATE} property as an {@link Instant}.
+ *
+ * @return the property value, or null
if not found.
+ */
+ public static Instant getAsInstant(Node node, String property) {
+ try {
+ if (!node.hasProperty(property))
+ return null;
+ Calendar calendar = node.getProperty(property).getDate();
+ return calendar.getTime().toInstant();
+ } catch (RepositoryException e) {
+ throw new JcrException("Cannot get property " + property + " of " + node + " as an instant.", e);
}
+
}
/**