DATACMNS-1259 - Fixed support for Long values in Auditables.

Using Instant as internal data type since it's a point in time without time zone which LocalDateTime isn't. Added necessary converters. Fixed one JodaTime converter that used UTC to use SystemDefault like other similar converters.

In case of a conversion failure the error message now contains the source type.

Original pull request: #273.
This commit is contained in:
Jens Schauder
2018-02-13 14:34:58 +01:00
committed by Oliver Gierke
parent 4947ca01dc
commit 5251f72e69
5 changed files with 175 additions and 8 deletions

View File

@@ -19,6 +19,7 @@ import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.lang.reflect.Field;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
@@ -42,6 +43,7 @@ import org.springframework.util.Assert;
*
* @author Oliver Gierke
* @author Christoph Strobl
* @author Jens Schauder
* @since 1.5
*/
class DefaultAuditableBeanWrapperFactory implements AuditableBeanWrapperFactory {
@@ -207,8 +209,7 @@ class DefaultAuditableBeanWrapperFactory implements AuditableBeanWrapperFactory
return conversionService.convert(date, targetType);
}
throw new IllegalArgumentException(String.format("Invalid date type for member %s! Supported types are %s.",
source, AnnotationAuditingMetadata.SUPPORTED_DATE_TYPES));
throw new IllegalArgumentException(createUnsupportedTypeErrorMessage(source));
}
/**
@@ -228,19 +229,24 @@ class DefaultAuditableBeanWrapperFactory implements AuditableBeanWrapperFactory
return (T) it;
}
Class<?> typeToConvertTo = Stream.of(target, LocalDateTime.class)//
Class<?> typeToConvertTo = Stream.of(target, Instant.class)//
.filter(type -> target.isAssignableFrom(type))//
.filter(type -> conversionService.canConvert(it.getClass(), type))//
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(
String.format("Invalid date type for member %s! Supported types are %s.", source,
AnnotationAuditingMetadata.SUPPORTED_DATE_TYPES)));
createUnsupportedTypeErrorMessage(((Optional<Object>)source).orElseGet(() -> source))));
return (T) conversionService.convert(it, typeToConvertTo);
});
}
}
private static String createUnsupportedTypeErrorMessage(Object source) {
return String.format("Invalid date type %s for member %s! Supported types are %s.", source.getClass(), source,
AnnotationAuditingMetadata.SUPPORTED_DATE_TYPES);
}
/**
* An {@link AuditableBeanWrapper} implementation that sets values on the target object using reflection.
*

View File

@@ -15,16 +15,19 @@
*/
package org.springframework.data.convert;
import java.time.Instant;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import javax.annotation.Nonnull;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.springframework.core.convert.converter.Converter;
@@ -65,6 +68,9 @@ public abstract class JodaTimeConverters {
converters.add(LocalDateTimeToJodaLocalDateTime.INSTANCE);
converters.add(LocalDateTimeToJodaDateTime.INSTANCE);
converters.add(InstantToJodaLocalDateTime.INSTANCE);
converters.add(JodaLocalDateTimeToInstant.INSTANCE);
converters.add(LocalDateTimeToJsr310Converter.INSTANCE);
return converters;
@@ -77,7 +83,7 @@ public abstract class JodaTimeConverters {
@Nonnull
@Override
public java.time.LocalDateTime convert(LocalDateTime source) {
return java.time.LocalDateTime.ofInstant(source.toDate().toInstant(), ZoneId.of("UTC"));
return java.time.LocalDateTime.ofInstant(source.toDate().toInstant(), ZoneId.systemDefault());
}
}
@@ -158,6 +164,28 @@ public abstract class JodaTimeConverters {
}
}
public enum InstantToJodaLocalDateTime implements Converter<java.time.Instant, LocalDateTime> {
INSTANCE;
@Nonnull
@Override
public LocalDateTime convert(java.time.Instant source) {
return LocalDateTime.fromDateFields(new Date(source.toEpochMilli()));
}
}
public enum JodaLocalDateTimeToInstant implements Converter<LocalDateTime, Instant> {
INSTANCE;
@Nonnull
@Override
public Instant convert(LocalDateTime source) {
return Instant.ofEpochMilli(source.toDateTime().getMillis());
}
}
public enum LocalDateTimeToJodaDateTime implements Converter<java.time.LocalDateTime, DateTime> {
INSTANCE;

View File

@@ -36,6 +36,7 @@ import org.threeten.bp.LocalDate;
import org.threeten.bp.LocalDateTime;
import org.threeten.bp.LocalTime;
import org.threeten.bp.ZoneId;
import org.threeten.bp.ZoneOffset;
/**
* Helper class to register {@link Converter} implementations for the ThreeTen Backport project in case it's present on
@@ -43,6 +44,7 @@ import org.threeten.bp.ZoneId;
*
* @author Oliver Gierke
* @author Christoph Strobl
* @author Jens Schauder
* @see <a href="http://www.threeten.org/threetenbp">http://www.threeten.org/threetenbp</a>
* @since 1.10
*/
@@ -74,6 +76,8 @@ public abstract class ThreeTenBackPortConverters {
converters.add(ZoneIdToStringConverter.INSTANCE);
converters.add(StringToZoneIdConverter.INSTANCE);
converters.add(LocalDateTimeToJsr310LocalDateTimeConverter.INSTANCE);
converters.add(LocalDateTimeToJavaTimeInstantConverter.INSTANCE);
converters.add(JavaTimeInstantToLocalDateTimeConverter.INSTANCE);
return converters;
}
@@ -84,7 +88,7 @@ public abstract class ThreeTenBackPortConverters {
return false;
}
return Arrays.<Class<?>> asList(LocalDateTime.class, LocalDate.class, LocalTime.class, Instant.class)
return Arrays.<Class<?>> asList(LocalDateTime.class, LocalDate.class, LocalTime.class, Instant.class, java.time.Instant.class)
.contains(type);
}
@@ -195,6 +199,28 @@ public abstract class ThreeTenBackPortConverters {
}
}
public static enum LocalDateTimeToJavaTimeInstantConverter implements Converter<LocalDateTime, java.time.Instant> {
INSTANCE;
@Nonnull
@Override
public java.time.Instant convert(LocalDateTime source) {
return java.time.Instant.ofEpochMilli(source.atZone(ZoneOffset.systemDefault()).toInstant().toEpochMilli());
}
}
public static enum JavaTimeInstantToLocalDateTimeConverter implements Converter<java.time.Instant, LocalDateTime> {
INSTANCE;
@Nonnull
@Override
public LocalDateTime convert(java.time.Instant source) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(source.toEpochMilli()), ZoneOffset.systemDefault());
}
}
@WritingConverter
public static enum ZoneIdToStringConverter implements Converter<ZoneId, String> {