Skip to content

Commit

Permalink
Adding support for joda LocalDateTime class in ERRest. Use the er.res…
Browse files Browse the repository at this point in the history
…t.jodaTimeFormatter property to specify the format of your dateTime objects.
  • Loading branch information
Pascal Robert committed Dec 12, 2012
1 parent 2fd7dc7 commit 477a9e0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Frameworks/EOF/ERRest/Sources/er/rest/ERXRestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions Frameworks/EOF/ERRest/Sources/er/rest/format/_ERXJSONConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 477a9e0

Please sign in to comment.