From bae4fd16caca2902f6d9ab64a2146d65a674a889 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Thu, 25 Apr 2013 19:39:38 -0700 Subject: [PATCH] CSPACE-5763: Allow date values to be used in generic name and number properties in summary lists. CSPACE-5763: Consolidate handling of Nuxeo property values that aren't directly capable of being cast to a String. Handle decimal values as well as date values, in that category. --- .../common/context/ServiceBindingUtils.java | 15 +++++-- .../common/document/DocumentUtils.java | 42 +++++++++++++++++++ .../services/nuxeo/util/NuxeoUtils.java | 7 ++-- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/services/common/src/main/java/org/collectionspace/services/common/context/ServiceBindingUtils.java b/services/common/src/main/java/org/collectionspace/services/common/context/ServiceBindingUtils.java index 9f1bbc29e..0031dbdeb 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/context/ServiceBindingUtils.java +++ b/services/common/src/main/java/org/collectionspace/services/common/context/ServiceBindingUtils.java @@ -17,6 +17,10 @@ import org.collectionspace.services.nuxeo.util.NuxeoUtils; import org.nuxeo.ecm.core.api.ClientException; import org.nuxeo.ecm.core.api.DocumentModel; import java.lang.IndexOutOfBoundsException; +import java.util.GregorianCalendar; +import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils; +import org.collectionspace.services.common.api.Tools; +import org.collectionspace.services.common.document.DocumentUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -176,10 +180,15 @@ public class ServiceBindingUtils { } */ String propName = getPropertyValue(sb, logicalFieldName); - if(propName==null||propName.isEmpty()) - return null; + if(Tools.isBlank(propName)) { + logger.warn("Property name is empty for property " + logicalFieldName + " in service " + sb.getName()); + logger.warn("This may be due to an improperly configured or missing " + + "generic property (objectNameProperty, objectNumberProperty ...) in tenant bindings configuration"); + return ""; + } try { - return (String)docModel.getPropertyValue(propName); + Object obj = docModel.getPropertyValue(propName); + return DocumentUtils.propertyValueAsString(obj, docModel, propName); } catch(IndexOutOfBoundsException ioobe) { // Should not happen, but may with certain array forms if(logger.isTraceEnabled()) { diff --git a/services/common/src/main/java/org/collectionspace/services/common/document/DocumentUtils.java b/services/common/src/main/java/org/collectionspace/services/common/document/DocumentUtils.java index 77a132e40..b310d1d33 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/document/DocumentUtils.java +++ b/services/common/src/main/java/org/collectionspace/services/common/document/DocumentUtils.java @@ -36,6 +36,7 @@ import java.text.FieldPosition; import java.text.NumberFormat; import java.util.ArrayList; +import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -55,6 +56,7 @@ import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import org.collectionspace.services.common.ServiceMain; +import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.datetime.DateTimeFormatUtils; import org.collectionspace.services.config.service.ObjectPartContentType; @@ -68,6 +70,7 @@ import org.jboss.resteasy.plugins.providers.multipart.InputPart; import org.nuxeo.ecm.core.io.ExportConstants; import org.nuxeo.common.collections.PrimitiveArrays; +import org.nuxeo.ecm.core.api.DocumentModel; import org.nuxeo.ecm.core.api.model.Property; import org.nuxeo.ecm.core.schema.SchemaManager; import org.nuxeo.ecm.core.schema.TypeConstants; @@ -593,6 +596,8 @@ public class DocumentUtils { parent.appendChild(element); // extract the element content if (type.isSimpleType()) { + // Avoid returning scientific notation representations of + // very large or very small decimal values. See CSPACE-4691. if (isNuxeoDecimalType(type) && valueMatchesNuxeoType(type, value)) { element.setTextContent(nuxeoDecimalValueToDecimalString(value)); /* @@ -887,6 +892,43 @@ public class DocumentUtils { return ((SimpleType)type).getPrimitiveType() instanceof DoubleType; } + /** + * Obtains a String representation of a Nuxeo property value, where + * the latter is an opaque Object that may or may not be directly + * convertible to a string. + * + * @param obj an Object containing a property value + * @param docModel the document model associated with this property. + * @param propertyPath a path to the property, such as a property name, XPath, etc. + * @return a String representation of the Nuxeo property value. + */ + static public String propertyValueAsString(Object obj, DocumentModel docModel, String propertyPath) { + if (obj == null) { + return ""; + } + if (String.class.isAssignableFrom(obj.getClass())) { + return (String)obj; + } else { + // Handle cases where a property value returned from the repository + // can't be directly cast to a String. + // + // FIXME: This method provides specific, hard-coded formatting + // for String representations of property values. We might want + // to add the ability to specify these formats via configuration. + // - ADR 2013-04-26 + if (obj instanceof GregorianCalendar) { + return GregorianCalendarDateTimeUtils.formatAsISO8601Date((GregorianCalendar)obj); + } else if (obj instanceof Double) { + return nuxeoDecimalValueToDecimalString(obj); + } else { + logger.warn("Could not convert value of property " + propertyPath + + " in document " + docModel.getPathAsString() + " to a String."); + logger.warn("This may be due to a new, as-yet-unhandled datatype returned from the repository"); + return ""; + } + } + } + /* * Returns a string representation of the value of a Nuxeo decimal type. * diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/util/NuxeoUtils.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/util/NuxeoUtils.java index f6ed2623e..4ef19c0ac 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/util/NuxeoUtils.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/util/NuxeoUtils.java @@ -765,11 +765,9 @@ public class NuxeoUtils { Object value = docModel.getPropertyValue(xpath); String returnVal = null; if (value == null) { - // Nothing to do - leave returnVal null - } else if (value instanceof GregorianCalendar) { - returnVal = GregorianCalendarDateTimeUtils.formatAsISO8601Timestamp((GregorianCalendar) value); + // Nothing to do - leave returnVal null } else { - returnVal = value.toString(); + returnVal = DocumentUtils.propertyValueAsString(value, docModel, xpath); } result = returnVal; } catch (PropertyException pe) { @@ -828,5 +826,6 @@ public class NuxeoUtils { return schema + ":" + complexPropertyName + "/*/" + fieldName; } } + } -- 2.47.3