|
| 1 | +/* |
| 2 | + * Copyright 2018 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 | + * http://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.cloud.gcp.data.spanner.core.mapping; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import org.springframework.data.mapping.model.BasicPersistentEntity; |
| 26 | +import org.springframework.data.util.TypeInformation; |
| 27 | +import org.springframework.util.StringUtils; |
| 28 | + |
| 29 | +/** |
| 30 | + * Represents a Google Spanner table and its columns' mapping to fields within an entity type. |
| 31 | + * |
| 32 | + * @author Ray Tsang |
| 33 | + * @author Chengyuan Zhao |
| 34 | + */ |
| 35 | +public class SpannerPersistentEntityImpl<T> |
| 36 | + extends BasicPersistentEntity<T, SpannerPersistentProperty> |
| 37 | + implements SpannerPersistentEntity<T> { |
| 38 | + |
| 39 | + private final String tableName; |
| 40 | + |
| 41 | + private final Set<String> columnNames = new HashSet<>(); |
| 42 | + |
| 43 | + private final Map<String, String> columnNameToPropertyName = new HashMap<>(); |
| 44 | + |
| 45 | + /** |
| 46 | + * Creates a {@link SpannerPersistentEntityImpl} |
| 47 | + * @param information type information about the underlying entity type. |
| 48 | + */ |
| 49 | + public SpannerPersistentEntityImpl(TypeInformation<T> information) { |
| 50 | + super(information); |
| 51 | + |
| 52 | + Class<?> rawType = information.getType(); |
| 53 | + String fallback = StringUtils.uncapitalize(rawType.getSimpleName()); |
| 54 | + |
| 55 | + SpannerTable table = this.findAnnotation(SpannerTable.class); |
| 56 | + this.tableName = table != null && StringUtils.hasText(table.name()) ? table.name() : fallback; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void addPersistentProperty(SpannerPersistentProperty property) { |
| 61 | + addPersistentPropertyToPersistentEntity(property); |
| 62 | + this.columnNames.add(property.getColumnName()); |
| 63 | + this.columnNameToPropertyName.put(property.getColumnName(), property.getName()); |
| 64 | + } |
| 65 | + |
| 66 | + private void addPersistentPropertyToPersistentEntity(SpannerPersistentProperty property) { |
| 67 | + super.addPersistentProperty(property); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public String tableName() { |
| 72 | + return this.tableName; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public SpannerPersistentProperty getPersistentPropertyByColumnName( |
| 77 | + String columnName) { |
| 78 | + return getPersistentProperty(this.columnNameToPropertyName.get(columnName)); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public Iterable<String> columns() { |
| 83 | + return Collections.unmodifiableSet(this.columnNames); |
| 84 | + } |
| 85 | +} |
0 commit comments