|
15 | 15 | */
|
16 | 16 | package org.springframework.data.mongodb.core.convert;
|
17 | 17 |
|
| 18 | +import java.time.Instant; |
| 19 | +import java.time.LocalDate; |
| 20 | +import java.time.LocalDateTime; |
| 21 | +import java.time.LocalTime; |
| 22 | +import java.time.ZoneId; |
| 23 | +import java.time.ZoneOffset; |
18 | 24 | import java.util.ArrayList;
|
19 | 25 | import java.util.Arrays;
|
| 26 | +import java.util.Collection; |
20 | 27 | import java.util.Collections;
|
| 28 | +import java.util.Date; |
21 | 29 | import java.util.HashSet;
|
22 | 30 | import java.util.List;
|
23 | 31 | import java.util.Locale;
|
24 | 32 | import java.util.Set;
|
| 33 | +import java.util.function.Consumer; |
| 34 | +import java.util.stream.Collectors; |
25 | 35 |
|
26 | 36 | import org.springframework.core.convert.TypeDescriptor;
|
| 37 | +import org.springframework.core.convert.converter.Converter; |
| 38 | +import org.springframework.core.convert.converter.ConverterFactory; |
27 | 39 | import org.springframework.core.convert.converter.GenericConverter;
|
| 40 | +import org.springframework.core.convert.converter.GenericConverter.ConvertiblePair; |
28 | 41 | import org.springframework.data.convert.JodaTimeConverters;
|
29 | 42 | import org.springframework.data.convert.WritingConverter;
|
| 43 | +import org.springframework.data.mapping.model.SimpleTypeHolder; |
30 | 44 | import org.springframework.data.mongodb.core.mapping.MongoSimpleTypes;
|
31 | 45 | import org.springframework.lang.Nullable;
|
| 46 | +import org.springframework.util.Assert; |
32 | 47 |
|
33 | 48 | /**
|
34 | 49 | * Value object to capture custom conversion. {@link MongoCustomConversions} also act as factory for
|
35 | 50 | * {@link org.springframework.data.mapping.model.SimpleTypeHolder}
|
36 | 51 | *
|
37 | 52 | * @author Mark Paluch
|
| 53 | + * @author Christoph Strobl |
38 | 54 | * @since 2.0
|
39 | 55 | * @see org.springframework.data.convert.CustomConversions
|
40 | 56 | * @see org.springframework.data.mapping.model.SimpleTypeHolder
|
@@ -71,7 +87,30 @@ public class MongoCustomConversions extends org.springframework.data.convert.Cus
|
71 | 87 | * @param converters must not be {@literal null}.
|
72 | 88 | */
|
73 | 89 | public MongoCustomConversions(List<?> converters) {
|
74 |
| - super(STORE_CONVERSIONS, converters); |
| 90 | + |
| 91 | + this(converterConfigurationAdapter -> { |
| 92 | + |
| 93 | + converterConfigurationAdapter.useSpringDataJavaTimeCodecs(); |
| 94 | + converterConfigurationAdapter.registerConverters(converters); |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Functional style {@link org.springframework.data.convert.CustomConversions} creation giving users a convenient way |
| 100 | + * of configuring store specific capabilities by providing deferred hooks to what will be configured when creating the |
| 101 | + * {@link org.springframework.data.convert.CustomConversions#CustomConversions(ConverterConfiguration) instance}. |
| 102 | + * |
| 103 | + * @param conversionConfiguration must not be {@literal null}. |
| 104 | + * @since 2.3 |
| 105 | + */ |
| 106 | + public MongoCustomConversions(Consumer<MongoConverterConfigurationAdapter> conversionConfiguration) { |
| 107 | + |
| 108 | + super(() -> { |
| 109 | + |
| 110 | + MongoConverterConfigurationAdapter adapter = new MongoConverterConfigurationAdapter(); |
| 111 | + conversionConfiguration.accept(adapter); |
| 112 | + return adapter.createConverterConfiguration(); |
| 113 | + }); |
75 | 114 | }
|
76 | 115 |
|
77 | 116 | @WritingConverter
|
@@ -99,4 +138,158 @@ public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDe
|
99 | 138 | return source != null ? source.toString() : null;
|
100 | 139 | }
|
101 | 140 | }
|
| 141 | + |
| 142 | + /** |
| 143 | + * {@link MongoConverterConfigurationAdapter} encapsulates creation of |
| 144 | + * {@link org.springframework.data.convert.CustomConversions.ConverterConfiguration} with MongoDB specifics. |
| 145 | + * |
| 146 | + * @author Christoph Strobl |
| 147 | + * @since 2.3 |
| 148 | + */ |
| 149 | + public static class MongoConverterConfigurationAdapter { |
| 150 | + |
| 151 | + /** |
| 152 | + * List of {@literal java.time} types having different representation when rendered via the native |
| 153 | + * {@link org.bson.codecs.Codec} than the Spring Data {@link Converter}. |
| 154 | + */ |
| 155 | + private static final List<Class<?>> JAVA_DRIVER_TIME_SIMPLE_TYPES = Arrays.asList(LocalDate.class, LocalTime.class, |
| 156 | + LocalDateTime.class); |
| 157 | + |
| 158 | + private boolean useNativeDriverJavaTimeCodecs = false; |
| 159 | + private List<Object> customConverters = new ArrayList<>(); |
| 160 | + |
| 161 | + /** |
| 162 | + * Set wether or not to use the native MongoDB Java Driver {@link org.bson.codecs.Codec codes} for |
| 163 | + * {@link org.bson.codecs.jsr310.LocalDateCodec LocalDate}, {@link org.bson.codecs.jsr310.LocalTimeCodec LocalTime} |
| 164 | + * and {@link org.bson.codecs.jsr310.LocalDateTimeCodec LocalDateTime} using a {@link ZoneOffset#UTC}. |
| 165 | + * |
| 166 | + * @param useNativeDriverJavaTimeCodecs |
| 167 | + * @return this. |
| 168 | + */ |
| 169 | + public MongoConverterConfigurationAdapter useNativeDriverJavaTimeCodecs(boolean useNativeDriverJavaTimeCodecs) { |
| 170 | + |
| 171 | + this.useNativeDriverJavaTimeCodecs = useNativeDriverJavaTimeCodecs; |
| 172 | + return this; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Use the native MongoDB Java Driver {@link org.bson.codecs.Codec codes} for |
| 177 | + * {@link org.bson.codecs.jsr310.LocalDateCodec LocalDate}, {@link org.bson.codecs.jsr310.LocalTimeCodec LocalTime} |
| 178 | + * and {@link org.bson.codecs.jsr310.LocalDateTimeCodec LocalDateTime} using a {@link ZoneOffset#UTC}. |
| 179 | + * |
| 180 | + * @return this. |
| 181 | + * @see #useNativeDriverJavaTimeCodecs(boolean) |
| 182 | + */ |
| 183 | + public MongoConverterConfigurationAdapter useNativeDriverJavaTimeCodecs() { |
| 184 | + return useNativeDriverJavaTimeCodecs(true); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Use SpringData {@link Converter Jsr310 converters} for |
| 189 | + * {@link org.springframework.data.convert.Jsr310Converters.LocalDateToDateConverter LocalDate}, |
| 190 | + * {@link org.springframework.data.convert.Jsr310Converters.LocalTimeToDateConverter LocalTime} and |
| 191 | + * {@link org.springframework.data.convert.Jsr310Converters.LocalDateTimeToDateConverter LocalDateTime} using the |
| 192 | + * {@link ZoneId#systemDefault()}. |
| 193 | + * |
| 194 | + * @return this. |
| 195 | + * @see #useNativeDriverJavaTimeCodecs(boolean) |
| 196 | + */ |
| 197 | + public MongoConverterConfigurationAdapter useSpringDataJavaTimeCodecs() { |
| 198 | + return useNativeDriverJavaTimeCodecs(false); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Add a custom {@link Converter} implementation. |
| 203 | + * |
| 204 | + * @param converter must not be {@literal null}. |
| 205 | + * @return this. |
| 206 | + */ |
| 207 | + public MongoConverterConfigurationAdapter registerConverter(Converter<?, ?> converter) { |
| 208 | + |
| 209 | + Assert.notNull(converter, "Converter must not be null!"); |
| 210 | + customConverters.add(converter); |
| 211 | + return this; |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Add a custom {@link ConverterFactory} implementation. |
| 216 | + * |
| 217 | + * @param converterFactory must not be {@literal null}. |
| 218 | + * @return this. |
| 219 | + */ |
| 220 | + public MongoConverterConfigurationAdapter registerConverterFactory(ConverterFactory<?, ?> converterFactory) { |
| 221 | + |
| 222 | + Assert.notNull(converterFactory, "ConverterFactory must not be null!"); |
| 223 | + customConverters.add(converterFactory); |
| 224 | + return this; |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Add {@link Converter converters}, {@link ConverterFactory factories}, ... |
| 229 | + * |
| 230 | + * @param converters must not be {@literal null} nor contain {@literal null} values. |
| 231 | + * @return this. |
| 232 | + */ |
| 233 | + public MongoConverterConfigurationAdapter registerConverters(Collection<?> converters) { |
| 234 | + |
| 235 | + Assert.noNullElements(converters, "Converters must not be null nor contain null values!"); |
| 236 | + customConverters.addAll(converters); |
| 237 | + return this; |
| 238 | + } |
| 239 | + |
| 240 | + ConverterConfiguration createConverterConfiguration() { |
| 241 | + |
| 242 | + if (!useNativeDriverJavaTimeCodecs) { |
| 243 | + return new ConverterConfiguration(STORE_CONVERSIONS, this.customConverters); |
| 244 | + } |
| 245 | + |
| 246 | + /* |
| 247 | + * We need to have those converters using UTC as the default ones would go on with the systemDefault. |
| 248 | + */ |
| 249 | + List<Object> converters = new ArrayList<>(); |
| 250 | + converters.add(DateToUtcLocalDateConverter.INSTANCE); |
| 251 | + converters.add(DateToUtcLocalTimeConverter.INSTANCE); |
| 252 | + converters.add(DateToUtcLocalDateTimeConverter.INSTANCE); |
| 253 | + converters.addAll(STORE_CONVERTERS); |
| 254 | + |
| 255 | + /* |
| 256 | + * Right, good catch! We also need to make sure to remove default writing converters for java.time types. |
| 257 | + */ |
| 258 | + List<ConvertiblePair> skipConverterRegistrationFor = JAVA_DRIVER_TIME_SIMPLE_TYPES.stream() // |
| 259 | + .map(it -> new ConvertiblePair(it, Date.class)) // |
| 260 | + .collect(Collectors.toList()); // |
| 261 | + |
| 262 | + StoreConversions storeConversions = StoreConversions |
| 263 | + .of(new SimpleTypeHolder(new HashSet<>(JAVA_DRIVER_TIME_SIMPLE_TYPES), MongoSimpleTypes.HOLDER), converters); |
| 264 | + |
| 265 | + return new ConverterConfiguration(storeConversions, this.customConverters, skipConverterRegistrationFor); |
| 266 | + } |
| 267 | + |
| 268 | + private enum DateToUtcLocalDateTimeConverter implements Converter<Date, LocalDateTime> { |
| 269 | + INSTANCE; |
| 270 | + |
| 271 | + @Override |
| 272 | + public LocalDateTime convert(Date source) { |
| 273 | + return LocalDateTime.ofInstant(Instant.ofEpochMilli(source.getTime()), ZoneId.of("UTC")); |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + private enum DateToUtcLocalTimeConverter implements Converter<Date, LocalTime> { |
| 278 | + INSTANCE; |
| 279 | + |
| 280 | + @Override |
| 281 | + public LocalTime convert(Date source) { |
| 282 | + return DateToUtcLocalDateTimeConverter.INSTANCE.convert(source).toLocalTime(); |
| 283 | + } |
| 284 | + } |
| 285 | + |
| 286 | + private enum DateToUtcLocalDateConverter implements Converter<Date, LocalDate> { |
| 287 | + INSTANCE; |
| 288 | + |
| 289 | + @Override |
| 290 | + public LocalDate convert(Date source) { |
| 291 | + return DateToUtcLocalDateTimeConverter.INSTANCE.convert(source).toLocalDate(); |
| 292 | + } |
| 293 | + } |
| 294 | + } |
102 | 295 | }
|
0 commit comments