From 477a9e02a112fc273660835be25b57bb552878dd Mon Sep 17 00:00:00 2001 From: Pascal Robert Date: Wed, 12 Dec 2012 04:40:22 -0500 Subject: [PATCH] Adding support for joda LocalDateTime class in ERRest. Use the er.rest.jodaTimeFormatter property to specify the format of your dateTime objects. --- .../ERRest/Sources/er/rest/ERXRestUtils.java | 49 +++++++++++++++++++ .../er/rest/format/_ERXJSONConfig.java | 18 +++++++ 2 files changed, 67 insertions(+) diff --git a/Frameworks/EOF/ERRest/Sources/er/rest/ERXRestUtils.java b/Frameworks/EOF/ERRest/Sources/er/rest/ERXRestUtils.java index 9ecbb8f4e30..067e85cdae4 100644 --- a/Frameworks/EOF/ERRest/Sources/er/rest/ERXRestUtils.java +++ b/Frameworks/EOF/ERRest/Sources/er/rest/ERXRestUtils.java @@ -8,6 +8,7 @@ import org.joda.time.DateTime; import org.joda.time.LocalDate; +import org.joda.time.LocalDateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; @@ -89,6 +90,9 @@ else if (Date.class.isAssignableFrom(valueType)) { else if (Calendar.class.isAssignableFrom(valueType)) { primitive = true; } + else if (LocalDateTime.class.isAssignableFrom(valueType)) { + primitive = true; + } else if (LocalDate.class.isAssignableFrom(valueType)) { primitive = true; } @@ -125,6 +129,9 @@ else if (value instanceof NSTimestamp) { else if (value instanceof Date) { formattedValue = ERXRestUtils.dateFormat(false, context).format(value); } + else if (value instanceof LocalDateTime) { + formattedValue = ERXRestUtils.jodaLocalDateTimeFormat(false, context).print((LocalDateTime)value); + } else if (value instanceof LocalDate) { formattedValue = ERXRestUtils.jodaLocalDateFormat(false, context).print((LocalDate)value); } @@ -199,6 +206,26 @@ protected static DateTimeFormatter jodaLocalDateFormat(boolean spaces, ERXRestCo return dateFormatter; } + protected static DateTimeFormatter jodaLocalDateTimeFormat(boolean spaces, ERXRestContext context) { + DateTimeFormatter dateFormatter = (DateTimeFormatter)context.userInfoForKey("er.rest.jodaTimeFormatter"); + if (dateFormatter == null) { + String dateFormat = (String)context.userInfoForKey("er.rest.jodaFormatTime"); + if (dateFormat == null) { + dateFormat = ERXProperties.stringForKey("er.rest.jodaFormatTime"); + if (dateFormat == null) { + if (spaces) { + dateFormat = ERXProperties.stringForKeyWithDefault("er.rest.jodaFormat.secondary", "yyyy-MM-dd HH:mm:ss z"); + } + else { + dateFormat = ERXProperties.stringForKeyWithDefault("er.rest.jodaFormat.primary", "yyyy-MM-dd'T'HH:mm:ss'Z'"); + } + } + } + dateFormatter = DateTimeFormat.forPattern(dateFormat); + } + return dateFormatter; + } + @SuppressWarnings("unchecked") public static Object coerceValueToTypeNamed(Object value, String valueTypeName, ERXRestContext context, boolean resolveEntities) { Object parsedValue; @@ -334,6 +361,28 @@ else if (valueType != null && Date.class.isAssignableFrom(valueType)) { } } } + else if (valueType != null && LocalDateTime.class.isAssignableFrom(valueType)) { + if (value instanceof NSTimestamp) { + parsedValue = value; + } + else { + String strValue = (String) value; + DateTimeFormatter formatter = null; + try { + boolean spaces = strValue.indexOf(' ') != -1; + formatter = ERXRestUtils.jodaLocalDateTimeFormat(spaces, context); + parsedValue = new LocalDateTime((DateTime)formatter.parseDateTime(strValue)); + } + catch (Throwable t) { + String msg = "Failed to parse '" + strValue + "' as a timestamp"; + if (formatter != null) { + msg += " (example: " + formatter.print(new LocalDateTime()) + ")"; + } + msg += "."; + throw new IllegalArgumentException(msg, t); + } + } + } else if (valueType != null && LocalDate.class.isAssignableFrom(valueType)) { if (value instanceof NSTimestamp) { parsedValue = value; diff --git a/Frameworks/EOF/ERRest/Sources/er/rest/format/_ERXJSONConfig.java b/Frameworks/EOF/ERRest/Sources/er/rest/format/_ERXJSONConfig.java index 9ec6e664723..69dd3d180fe 100644 --- a/Frameworks/EOF/ERRest/Sources/er/rest/format/_ERXJSONConfig.java +++ b/Frameworks/EOF/ERRest/Sources/er/rest/format/_ERXJSONConfig.java @@ -4,6 +4,7 @@ import java.util.Set; import org.joda.time.LocalDate; +import org.joda.time.LocalDateTime; import net.sf.json.JsonConfig; import net.sf.json.processors.JsonValueProcessor; @@ -76,10 +77,27 @@ public Object processObjectValue(String s, Object obj, JsonConfig jsonconfig) { } } + public static class JodaDateTimeProcessor implements JsonValueProcessor { + private ERXRestContext _context; + + public JodaDateTimeProcessor(ERXRestContext context) { + _context = context; + } + + public Object processArrayValue(Object obj, JsonConfig jsonconfig) { + return ERXRestUtils.coerceValueToString(obj, _context); + } + + public Object processObjectValue(String s, Object obj, JsonConfig jsonconfig) { + return ERXRestUtils.coerceValueToString(obj, _context); + } + } + public static JsonConfig createDefaultConfig(ERXRestContext context) { JsonConfig config = new JsonConfig(); config.registerJsonValueProcessor(NSTimestamp.class, new NSTimestampProcessor(context)); config.registerJsonValueProcessor(LocalDate.class, new JodaTimeProcessor(context)); + config.registerJsonValueProcessor(LocalDateTime.class, new JodaDateTimeProcessor(context)); config.registerJsonValueProcessor(Date.class, new NSTimestampProcessor(context)); config.registerJsonValueProcessor(NSData.class, new NSDataProcessor(context)); config.setJsonValueProcessorMatcher(new ERXRestValueProcessorMatcher());