|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.jdbc.core.convert; |
| 17 | + |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.Set; |
| 20 | + |
| 21 | +import org.springframework.core.ResolvableType; |
| 22 | +import org.springframework.core.convert.ConversionService; |
| 23 | +import org.springframework.core.convert.TypeDescriptor; |
| 24 | +import org.springframework.core.convert.converter.GenericConverter; |
| 25 | +import org.springframework.data.convert.ReadingConverter; |
| 26 | +import org.springframework.data.convert.WritingConverter; |
| 27 | +import org.springframework.data.jdbc.core.mapping.AggregateReference; |
| 28 | +import org.springframework.lang.Nullable; |
| 29 | + |
| 30 | +/** |
| 31 | + * Converters for aggregate references. They need a {@link ConversionService} in order to delegate the conversion of the |
| 32 | + * content of the {@link AggregateReference}. |
| 33 | + * |
| 34 | + * @author Jens Schauder |
| 35 | + * @since 2.6 |
| 36 | + */ |
| 37 | +final class AggregateReferenceConverters { |
| 38 | + /** |
| 39 | + * Prevent instantiation. |
| 40 | + */ |
| 41 | + private AggregateReferenceConverters() {} |
| 42 | + |
| 43 | + /** |
| 44 | + * Converts from an AggregateReference to its id, leaving the conversion of the id to the ultimate target type to the |
| 45 | + * delegate {@link ConversionService}. |
| 46 | + */ |
| 47 | + @WritingConverter |
| 48 | + public static class AggregateReferenceToSimpleTypeConverter implements GenericConverter { |
| 49 | + |
| 50 | + private final ConversionService delegate; |
| 51 | + |
| 52 | + public AggregateReferenceToSimpleTypeConverter(ConversionService delegate) { |
| 53 | + this.delegate = delegate; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Set<ConvertiblePair> getConvertibleTypes() { |
| 58 | + return Collections.singleton(new ConvertiblePair(AggregateReference.class, Object.class)); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Object convert(@Nullable Object source, TypeDescriptor sourceDescriptor, TypeDescriptor targetDescriptor) { |
| 63 | + |
| 64 | + if (source == null) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + // if the target type is an AggregateReference we just going to assume it is of the correct type, |
| 69 | + // because it was already converted. |
| 70 | + Class<?> objectType = targetDescriptor.getObjectType(); |
| 71 | + if (objectType.isAssignableFrom(AggregateReference.class)) { |
| 72 | + return source; |
| 73 | + } |
| 74 | + |
| 75 | + Object id = ((AggregateReference<?, ?>) source).getId(); |
| 76 | + |
| 77 | + if (id == null) { |
| 78 | + throw new IllegalStateException( |
| 79 | + String.format("Aggregate references id must not be null when converting to %s from %s to %s", source, |
| 80 | + sourceDescriptor, targetDescriptor)); |
| 81 | + } |
| 82 | + |
| 83 | + return delegate.convert(id, TypeDescriptor.valueOf(id.getClass()), targetDescriptor); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Convert any simple type to an {@link AggregateReference}. If the {@literal targetDescriptor} contains information |
| 89 | + * about the generic type id will properly get converted to the desired type by the delegate |
| 90 | + * {@link ConversionService}. |
| 91 | + */ |
| 92 | + @ReadingConverter |
| 93 | + public static class SimpleTypeToAggregateReferenceConverter implements GenericConverter { |
| 94 | + |
| 95 | + private final ConversionService delegate; |
| 96 | + |
| 97 | + public SimpleTypeToAggregateReferenceConverter(ConversionService delegate) { |
| 98 | + this.delegate = delegate; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public Set<ConvertiblePair> getConvertibleTypes() { |
| 103 | + return Collections.singleton(new ConvertiblePair(Object.class, AggregateReference.class)); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public Object convert(@Nullable Object source, TypeDescriptor sourceDescriptor, TypeDescriptor targetDescriptor) { |
| 108 | + |
| 109 | + if (source == null) { |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + // TODO check |
| 114 | + ResolvableType componentType = targetDescriptor.getResolvableType().getGenerics()[1]; |
| 115 | + TypeDescriptor targetType = TypeDescriptor.valueOf(componentType.resolve()); |
| 116 | + Object convertedId = delegate.convert(source, TypeDescriptor.valueOf(source.getClass()), targetType); |
| 117 | + |
| 118 | + return AggregateReference.to(convertedId); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments