From 895fca9455f28703eee56b241a1ff5b15002d0c9 Mon Sep 17 00:00:00 2001 From: Rodica Sutu Date: Mon, 18 Jun 2018 10:58:48 +0300 Subject: [PATCH] review changes and fix for converting string with time to date using a LocalDateDeserializer --- .../api/util/CustomDateTimeSerializer.java | 4 +- .../api/util/CustomLocalDateDeserializer.java | 86 +++++++++++++++++++ .../api/util/CustomLocalDateSerializer.java | 4 +- 3 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/util/CustomLocalDateDeserializer.java diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/util/CustomDateTimeSerializer.java b/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/util/CustomDateTimeSerializer.java index 0df22205ab..2d75f5ec5d 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/util/CustomDateTimeSerializer.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/util/CustomDateTimeSerializer.java @@ -30,7 +30,7 @@ import java.io.IOException; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializerProvider; -import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; @@ -42,7 +42,7 @@ import org.joda.time.format.DateTimeFormatter; * @author Rodica Sutu * @since 3.0 */ -public class CustomDateTimeSerializer extends StdScalarSerializer +public class CustomDateTimeSerializer extends StdSerializer { /** Date time format */ private final static DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/util/CustomLocalDateDeserializer.java b/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/util/CustomLocalDateDeserializer.java new file mode 100644 index 0000000000..06980287d4 --- /dev/null +++ b/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/util/CustomLocalDateDeserializer.java @@ -0,0 +1,86 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2018 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * - + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * - + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * - + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * - + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ +package org.alfresco.rm.rest.api.util; + + +import java.io.IOException; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; + +import org.joda.time.LocalDate; + +/** + * Custom Local Date deserializer for converting a string value with time into a org.joda.time.LocalDate value + * + * @author Rodica Sutu + * @since 3.0 + */ +public class CustomLocalDateDeserializer extends StdDeserializer +{ + /** Local date format */ + private final static DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); + + + public CustomLocalDateDeserializer() + { + super(LocalDate.class); + } + protected CustomLocalDateDeserializer(Class vc) + { + super(vc); + } + + /** + * Custom deserialize method to convert a string value into a org.joda.time.LocalDate value using the DATE_FORMAT + * + * @param p local date value + * @param ctx + * @throws IOException + */ + @Override + public LocalDate deserialize(JsonParser p, DeserializationContext ctx) throws IOException + { + Date date = null; + try + { + // convert the string with time into a date value using the format DATE_FORMAT + date = DATE_FORMAT.parse(p.getText()); + } + catch (ParseException e) + { + e.printStackTrace(); + } + // convert the date into a LocalDate + return LocalDate.fromDateFields(date); + } +} diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/util/CustomLocalDateSerializer.java b/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/util/CustomLocalDateSerializer.java index dfdc5f7afe..f10fd69cd4 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/util/CustomLocalDateSerializer.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/util/CustomLocalDateSerializer.java @@ -30,7 +30,7 @@ import java.io.IOException; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializerProvider; -import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; import org.joda.time.LocalDate; import org.joda.time.format.DateTimeFormat; @@ -42,7 +42,7 @@ import org.joda.time.format.DateTimeFormatter; * @author Rodica Sutu * @since 3.0 */ -public class CustomLocalDateSerializer extends StdScalarSerializer +public class CustomLocalDateSerializer extends StdSerializer { /** Local date format */ private final static DateTimeFormatter DATE_FORMAT = DateTimeFormat.forPattern("yyyy-MM-dd");