From 28b008d21ca83a0df006a9d71c0eb7ccde353917 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Wed, 18 Aug 2010 04:12:45 +0000 Subject: [PATCH] CSPACE-2653: Dates in unrecognized formats, when sent to services as values in date-type fields, now generate error messages, rather than being silently 'dropped' and resulting in null/empty values being stored in those fields. Recognized formats include Nuxeo's supported ISO 8601-based formats, as well as any additional formats configured on a per-tenant basis. --- .../common/datetime/DateTimeFormatUtils.java | 5 +- .../common/document/DocumentUtils.java | 27 +++++++-- .../client/test/MovementAuthRefsTest.java | 5 +- .../nuxeo/MovementValidatorHandler.java | 56 ------------------- 4 files changed, 25 insertions(+), 68 deletions(-) diff --git a/services/common/src/main/java/org/collectionspace/services/common/datetime/DateTimeFormatUtils.java b/services/common/src/main/java/org/collectionspace/services/common/datetime/DateTimeFormatUtils.java index 55f17f8cf..289187d30 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/datetime/DateTimeFormatUtils.java +++ b/services/common/src/main/java/org/collectionspace/services/common/datetime/DateTimeFormatUtils.java @@ -208,12 +208,11 @@ public class DateTimeFormatUtils { * * @return an ISO 8601 timestamp representation of that String. * If the String cannot be parsed by the date formatters - * for the supplied tenant, return the original string. + * for the supplied tenant, return null; */ public static String toIso8601Timestamp(String dateStr, String tenantId) { Date date = null; List formatters = getDateFormattersForTenant(tenantId); -// for (DateFormat formatter : getDateFormattersForTenant(tenantId)) { for (DateFormat formatter : formatters) { date = parseDate(dateStr, formatter); if (date != null) { @@ -221,7 +220,7 @@ public class DateTimeFormatUtils { } } if (date == null) { - return dateStr; + return null; } else { GregorianCalendar gcal = new GregorianCalendar(); gcal.setTime(date); 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 b65235f94..dbb5019f7 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 @@ -1043,7 +1043,7 @@ public class DocumentUtils { * @return the map */ public static Map parseProperties(ObjectPartType partMeta, - Document document, ServiceContext ctx) { + Document document, ServiceContext ctx) throws Exception { Map result = null; String schemaName = partMeta.getLabel(); Schema schema = getSchemaFromName(schemaName); @@ -1051,7 +1051,9 @@ public class DocumentUtils { org.dom4j.io.DOMReader xmlReader = new org.dom4j.io.DOMReader(); org.dom4j.Document dom4jDocument = xmlReader.read(document); try { - result = loadSchema(schema, dom4jDocument.getRootElement(), ctx); + result = loadSchema(schema, dom4jDocument.getRootElement(), ctx); + } catch (IllegalArgumentException iae) { + throw iae; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -1103,14 +1105,27 @@ public class DocumentUtils { */ @SuppressWarnings("unchecked") static private Object getElementData(org.dom4j.Element element, Type type, - ServiceContext ctx) { + ServiceContext ctx) throws Exception { Object result = null; + String dateStr = ""; if (type.isSimpleType()) { - // Convert incoming date values to a canonical date representation, if (isDateType(type)) { - result = DateTimeFormatUtils.toIso8601Timestamp((String) element.getText(), - ctx.getTenantId()); + // Dates or date/times in ISO 8601-based representations + // directly supported by Nuxeo will be successfully decoded. + result = type.decode(element.getText()); + // All other date or date/time values must first be converted + // to a supported ISO 8601-based representation. + if (result == null) { + dateStr = DateTimeFormatUtils.toIso8601Timestamp(element.getText(), + ctx.getTenantId()); + if (dateStr != null) { + result = type.decode(dateStr); + } else { + throw new IllegalArgumentException("Unrecognized date value '" + + element.getText() + "' in field '" + element.getName() + "'"); + } + } } else { result = type.decode(element.getText()); } diff --git a/services/movement/client/src/test/java/org/collectionspace/services/client/test/MovementAuthRefsTest.java b/services/movement/client/src/test/java/org/collectionspace/services/client/test/MovementAuthRefsTest.java index 96e9a5e50..2079f7ebf 100644 --- a/services/movement/client/src/test/java/org/collectionspace/services/client/test/MovementAuthRefsTest.java +++ b/services/movement/client/src/test/java/org/collectionspace/services/client/test/MovementAuthRefsTest.java @@ -36,10 +36,9 @@ import org.collectionspace.services.client.MovementClient; import org.collectionspace.services.client.PersonAuthorityClient; import org.collectionspace.services.client.PersonAuthorityClientUtils; import org.collectionspace.services.common.authorityref.AuthorityRefList; -//import org.collectionspace.services.common.authorityref.AuthorityRefList.AuthorityRefItem; +import org.collectionspace.services.common.datetime.GregorianCalendarDateTimeUtils; import org.collectionspace.services.jaxb.AbstractCommonList; import org.collectionspace.services.movement.MovementsCommon; -//import org.collectionspace.services.movement.MovementsCommonList; import org.jboss.resteasy.client.ClientResponse; @@ -121,7 +120,7 @@ public class MovementAuthRefsTest extends BaseServiceTest { MovementClient movementClient = new MovementClient(); MultipartOutput multipart = createMovementInstance( "movementReferenceNumber-" + identifier, - "locationDate-" + identifier, + GregorianCalendarDateTimeUtils.timestampUTC(), movementContactRefName); ClientResponse res = movementClient.create(multipart); int statusCode = res.getStatus(); diff --git a/services/movement/service/src/main/java/org/collectionspace/services/movement/nuxeo/MovementValidatorHandler.java b/services/movement/service/src/main/java/org/collectionspace/services/movement/nuxeo/MovementValidatorHandler.java index b01efb893..d374766b3 100644 --- a/services/movement/service/src/main/java/org/collectionspace/services/movement/nuxeo/MovementValidatorHandler.java +++ b/services/movement/service/src/main/java/org/collectionspace/services/movement/nuxeo/MovementValidatorHandler.java @@ -1,14 +1,9 @@ package org.collectionspace.services.movement.nuxeo; -import java.util.List; - import org.collectionspace.services.common.context.ServiceContext; -import org.collectionspace.services.common.context.MultipartServiceContext; -import org.collectionspace.services.common.datetime.DateTimeFormatUtils; import org.collectionspace.services.common.document.InvalidDocumentException; import org.collectionspace.services.common.document.ValidatorHandler; import org.collectionspace.services.common.document.DocumentHandler.Action; -import org.collectionspace.services.movement.MovementsCommon; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,57 +20,6 @@ public class MovementValidatorHandler implements ValidatorHandler { logger.debug("validate() action=" + action.name()); } - /* - try { - - MultipartServiceContext mctx = (MultipartServiceContext) ctx; - MovementsCommon mc = (MovementsCommon) mctx.getInputPart(mctx.getCommonPartLabel(), - MovementsCommon.class); - StringBuilder msgBldr = new StringBuilder("validate()"); - boolean invalid = false; - - List patterns = DateTimeFormatUtils.getDateFormatPatternsForTenant(ctx); - - // FIXME: This is an early proof-of-concept. - // - // We need a better way of determining which fields - // in the incoming payload are date fields whose values we - // might wish to validate, and of extracting their values, - // than hard-coding them here. - // - // See DocumentUtils.parseProperties() for one possible approach. - - boolean validDateFormat = false; - String locDate = mc.getLocationDate(); - for (String pattern : patterns) { - if (DateTimeFormatUtils.isParseableByDatePattern(locDate, pattern)) { - validDateFormat = true; - } - } - if (! validDateFormat) { - invalid = true; - msgBldr.append("\nlocationDate : unrecognized date format '" + locDate + "'"); - } - - if(action.equals(Action.CREATE)) { - //create specific validation here - } else if(action.equals(Action.UPDATE)) { - //update specific validation here - } - - if (invalid) { - String msg = msgBldr.toString(); - logger.error(msg); - throw new InvalidDocumentException(msg); - } - } catch (InvalidDocumentException ide) { - throw ide; - } catch (Exception e) { - throw new InvalidDocumentException(e); - } - * - */ - } } -- 2.47.3