]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5763: Allow date values to be used in generic name and number properties in...
authorAron Roberts <aron@socrates.berkeley.edu>
Fri, 26 Apr 2013 02:39:38 +0000 (19:39 -0700)
committerAron Roberts <aron@socrates.berkeley.edu>
Fri, 26 Apr 2013 20:16:35 +0000 (13:16 -0700)
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.

services/common/src/main/java/org/collectionspace/services/common/context/ServiceBindingUtils.java
services/common/src/main/java/org/collectionspace/services/common/document/DocumentUtils.java
services/common/src/main/java/org/collectionspace/services/nuxeo/util/NuxeoUtils.java

index 9f1bbc29e64f44c21f3e27ef40671d2c869ac0f6..0031dbdeb830dbc87c9621b423b980db2e11da48 100644 (file)
@@ -17,6 +17,10 @@ import org.collectionspace.services.nuxeo.util.NuxeoUtils;
 import org.nuxeo.ecm.core.api.ClientException;\r
 import org.nuxeo.ecm.core.api.DocumentModel;\r
 import java.lang.IndexOutOfBoundsException;\r
+import java.util.GregorianCalendar;\r
+import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;\r
+import org.collectionspace.services.common.api.Tools;\r
+import org.collectionspace.services.common.document.DocumentUtils;\r
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
 \r
@@ -176,10 +180,15 @@ public class ServiceBindingUtils {
        }\r
        */\r
        String propName = getPropertyValue(sb, logicalFieldName);\r
-       if(propName==null||propName.isEmpty())\r
-               return null;\r
+       if(Tools.isBlank(propName)) {\r
+                logger.warn("Property name is empty for property " + logicalFieldName + " in service " + sb.getName());\r
+                logger.warn("This may be due to an improperly configured or missing "\r
+                        + "generic property (objectNameProperty, objectNumberProperty ...) in tenant bindings configuration");\r
+               return "";\r
+        }\r
        try {\r
-               return (String)docModel.getPropertyValue(propName);\r
+            Object obj = docModel.getPropertyValue(propName);\r
+            return DocumentUtils.propertyValueAsString(obj, docModel, propName);\r
        } catch(IndexOutOfBoundsException ioobe) {\r
                                // Should not happen, but may with certain array forms\r
                                if(logger.isTraceEnabled()) {\r
index 77a132e407f95bc9bc7f9c415c1f49f2bf641c6c..b310d1d3324f0b11279b101dd09d56e7956cee76 100644 (file)
@@ -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.
          * 
index f6ed2623ebd27b4b9c2fcca8f8ad5c552fe79962..4ef19c0ace83f76ed1a485a8f6318073a0a639f5 100644 (file)
@@ -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;
         }
     }
+
     
 }