From 8061d5a354107468885636baf07b798239b42b79 Mon Sep 17 00:00:00 2001 From: Rodica Sutu Date: Wed, 20 Jun 2018 17:52:28 +0300 Subject: [PATCH] fix the date-time string not changed to a simple date --- .../api/util/CustomDateTimeSerializer.java | 4 +- .../api/util/CustomLocalDateDeserializer.java | 74 +++++++++++++++++++ .../api/util/CustomLocalDateSerializer.java | 6 +- 3 files changed, 79 insertions(+), 5 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 2d75f5ec5d..631ccee679 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 @@ -33,8 +33,8 @@ import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.std.StdSerializer; import org.joda.time.DateTime; -import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; +import org.joda.time.format.ISODateTimeFormat; /** * Custom Date Time serializer for formatting org.joda.time.DateTime @@ -45,7 +45,7 @@ import org.joda.time.format.DateTimeFormatter; 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"); + private final static DateTimeFormatter DATE_TIME_FORMAT = ISODateTimeFormat.dateTime(); public CustomDateTimeSerializer() { 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..ef8133a114 --- /dev/null +++ b/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/util/CustomLocalDateDeserializer.java @@ -0,0 +1,74 @@ +/* + * #%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 com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; + +import org.joda.time.LocalDate; +import org.joda.time.format.DateTimeFormatter; +import org.joda.time.format.ISODateTimeFormat; + + +/** + * Custom Local Date deserializer converting a string to org.joda.time.LocalDate when the time is optional; + * + * @author Rodica Sutu + * @since 3.0 + */ +public class CustomLocalDateDeserializer extends StdDeserializer +{ + /** Date time format with time optional */ + private final static DateTimeFormatter LOCAL_DATE_OPTIONAL_TIME_PARSER = ISODateTimeFormat.localDateOptionalTimeParser(); + + public CustomLocalDateDeserializer() + { + super(LocalDate.class); + } + + /** + * Custom deserialize method to convert string to the org.joda.time.LocalDate type with LOCAL_DATE_OPTIONAL_TIME_PARSER + * + * @param jp local date value + * @param ctxt + * @throws IOException + */ + @Override + public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException + { + String str = jp.getText().trim(); + if (str.length() == 0) + { + return null; + } + return LOCAL_DATE_OPTIONAL_TIME_PARSER.parseLocalDate(str); + } +} + 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 f10fd69cd4..b3aa93c66a 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 @@ -33,8 +33,8 @@ import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.std.StdSerializer; import org.joda.time.LocalDate; -import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; +import org.joda.time.format.ISODateTimeFormat; /** * Custom Local Date serializer for formatting org.joda.time.LocalDate @@ -44,8 +44,8 @@ import org.joda.time.format.DateTimeFormatter; */ public class CustomLocalDateSerializer extends StdSerializer { - /** Local date format */ - private final static DateTimeFormatter DATE_FORMAT = DateTimeFormat.forPattern("yyyy-MM-dd"); + /** Local date format yyyy-MM-dd*/ + private final static DateTimeFormatter DATE_FORMAT = ISODateTimeFormat.date(); public CustomLocalDateSerializer() {