DATACMNS-1109 - Reinstantiate support for all date/time APIs in auditing.
Re-enabled test cases for handling of different date/time APIs in auditing. Tweaked the lookup of the last modified value to try to convert to LocalDateTime as well in case it's a subtype of the type requested. In combination with two newly introduced converters for local date type times in JodaTime and ThreeTenBP, this allows us to still convert legacy JodaTime and ThreeTenBP types to be used as well.
This commit is contained in:
@@ -19,10 +19,12 @@ import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
@@ -216,7 +218,23 @@ class DefaultAuditableBeanWrapperFactory implements AuditableBeanWrapperFactory
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <T extends TemporalAccessor> Optional<T> getAsTemporalAccessor(Optional<? extends Object> source,
|
||||
Class<? extends T> target) {
|
||||
return source.map(it -> target.isInstance(it) ? (T) it : conversionService.convert(it, target));
|
||||
|
||||
return source.map(it -> {
|
||||
|
||||
if (target.isInstance(it)) {
|
||||
return (T) it;
|
||||
}
|
||||
|
||||
Class<?> typeToConvertTo = Stream.of(target, LocalDateTime.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)));
|
||||
|
||||
return (T) conversionService.convert(it, typeToConvertTo);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.springframework.data.auditing;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -191,7 +190,8 @@ public class MappingAuditableBeanWrapperFactory extends DefaultAuditableBeanWrap
|
||||
*/
|
||||
@Override
|
||||
public Optional<TemporalAccessor> getLastModifiedDate() {
|
||||
return getAsTemporalAccessor(metadata.lastModifiedDateProperty.map(accessor::getProperty), LocalDateTime.class);
|
||||
return getAsTemporalAccessor(metadata.lastModifiedDateProperty.map(accessor::getProperty),
|
||||
TemporalAccessor.class);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user