|
| 1 | +/* |
| 2 | + * Copyright 2022 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 | + |
| 17 | +package org.springframework.data.couchbase.domain; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | + |
| 21 | +import org.springframework.data.convert.DefaultTypeMapper; |
| 22 | +import org.springframework.data.convert.TypeAliasAccessor; |
| 23 | +import org.springframework.data.couchbase.core.convert.CouchbaseTypeMapper; |
| 24 | +import org.springframework.data.couchbase.core.mapping.CouchbaseDocument; |
| 25 | +import org.springframework.data.mapping.Alias; |
| 26 | +import org.springframework.data.mapping.context.MappingContext; |
| 27 | +import org.springframework.data.util.TypeInformation; |
| 28 | + |
| 29 | +/** |
| 30 | + * TypeMapper that leverages subtype |
| 31 | + * |
| 32 | + * @author Michael Reiche |
| 33 | + */ |
| 34 | +public class AbstractingTypeMapper extends DefaultTypeMapper<CouchbaseDocument> implements CouchbaseTypeMapper { |
| 35 | + |
| 36 | + public static final String SUBTYPE = "subtype"; |
| 37 | + private final String typeKey; |
| 38 | + |
| 39 | + public static class Type { |
| 40 | + public static final String ABSTRACTUSER = "abstractuser", USER = "user", OTHERUSER = "otheruser"; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Create a new type mapper with the type key. |
| 45 | + * |
| 46 | + * @param typeKey the typeKey to use. |
| 47 | + */ |
| 48 | + public AbstractingTypeMapper(final String typeKey) { |
| 49 | + super(new CouchbaseDocumentTypeAliasAccessor(typeKey), (MappingContext) null, Collections |
| 50 | + .singletonList(new org.springframework.data.couchbase.core.convert.TypeAwareTypeInformationMapper())); |
| 51 | + this.typeKey = typeKey; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String getTypeKey() { |
| 56 | + return this.typeKey; |
| 57 | + } |
| 58 | + |
| 59 | + public static final class CouchbaseDocumentTypeAliasAccessor implements TypeAliasAccessor<CouchbaseDocument> { |
| 60 | + |
| 61 | + private final String typeKey; |
| 62 | + |
| 63 | + public CouchbaseDocumentTypeAliasAccessor(final String typeKey) { |
| 64 | + this.typeKey = typeKey; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Alias readAliasFrom(final CouchbaseDocument source) { |
| 69 | + String alias = (String) source.get(typeKey); |
| 70 | + if (Type.ABSTRACTUSER.equals(alias)) { |
| 71 | + String subtype = (String) source.get(AbstractingTypeMapper.SUBTYPE); |
| 72 | + if (Type.OTHERUSER.equals(subtype)) { |
| 73 | + alias = OtherUser.class.getName(); |
| 74 | + } else if (Type.USER.equals(subtype)) { |
| 75 | + alias = User.class.getName(); |
| 76 | + } else { |
| 77 | + throw new RuntimeException( |
| 78 | + "no mapping for type " + SUBTYPE + "=" + subtype + " in type " + alias + " source=" + source); |
| 79 | + } |
| 80 | + } |
| 81 | + return Alias.ofNullable(alias); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void writeTypeTo(final CouchbaseDocument sink, final Object alias) { |
| 86 | + if (typeKey != null) { |
| 87 | + sink.put(typeKey, alias); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public Alias getTypeAlias(TypeInformation<?> info) { |
| 94 | + return getAliasFor(info); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments