View Javadoc

1   /**
2    * Copyright © 2018 spring-data-dynamodb (https://github.com/derjust/spring-data-dynamodb)
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  package org.socialsignin.spring.data.dynamodb.mapping;
17  
18  import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
19  import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
20  import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
21  import org.springframework.data.mapping.context.AbstractMappingContext;
22  import org.springframework.data.mapping.model.Property;
23  import org.springframework.data.mapping.model.SimpleTypeHolder;
24  import org.springframework.data.util.TypeInformation;
25  
26  import java.lang.reflect.Field;
27  import java.lang.reflect.Method;
28  
29  /**
30   * Default implementation of a
31   * {@link org.springframework.data.mapping.context.MappingContext} for DynamoDB
32   * using {@link DynamoDBPersistentEntityImpl} and
33   * {@link DynamoDBPersistentProperty} as primary abstractions.
34   *
35   * @author Michael Lavelle
36   * @author Sebastian Just
37   */
38  public class DynamoDBMappingContext
39  		extends
40  			AbstractMappingContext<DynamoDBPersistentEntityImpl<?>, DynamoDBPersistentProperty> {
41  	/*
42  	 * (non-Javadoc)
43  	 *
44  	 * @see org.springframework.data.mapping.context.AbstractMappingContext#
45  	 * shouldCreatePersistentEntityFor
46  	 * (org.springframework.data.util.TypeInformation)
47  	 */
48  	@Override
49  	protected <T> DynamoDBPersistentEntityImpl<?> createPersistentEntity(TypeInformation<T> typeInformation) {
50  		return new DynamoDBPersistentEntityImpl<>(typeInformation, null);
51  
52  	}
53  
54  	/*
55  	 * (non-Javadoc)
56  	 *
57  	 * @see org.springframework.data.mapping.AbstractMappingContext#
58  	 * createPersistentProperty(java.lang.reflect.Field,
59  	 * java.beans.PropertyDescriptor,
60  	 * org.springframework.data.mapping.MutablePersistentEntity,
61  	 * org.springframework.data.mapping.SimpleTypeHolder)
62  	 */
63  	@Override
64  	protected DynamoDBPersistentProperty createPersistentProperty(Property property,
65  			DynamoDBPersistentEntityImpl<?> owner, SimpleTypeHolder simpleTypeHolder) {
66  		return new DynamoDBPersistentPropertyImpl(property, owner, simpleTypeHolder);
67  	}
68  
69  	/*
70  	 * (non-Javadoc)
71  	 *
72  	 * @see org.springframework.data.mapping.context.AbstractMappingContext#
73  	 * shouldCreatePersistentEntityFor
74  	 * (org.springframework.data.util.TypeInformation)
75  	 */
76  	@Override
77  	protected boolean shouldCreatePersistentEntityFor(TypeInformation<?> type) {
78  
79  		boolean hasHashKey = false;
80  		boolean hasRangeKey = false;
81  		for (Method method : type.getType().getMethods()) {
82  			if (method.isAnnotationPresent(DynamoDBHashKey.class)) {
83  				hasHashKey = true;
84  			}
85  			if (method.isAnnotationPresent(DynamoDBRangeKey.class)) {
86  				hasRangeKey = true;
87  			}
88  
89  		}
90  		for (Field field : type.getType().getFields()) {
91  			if (field.isAnnotationPresent(DynamoDBHashKey.class)) {
92  				hasHashKey = true;
93  			}
94  			if (field.isAnnotationPresent(DynamoDBRangeKey.class)) {
95  				hasRangeKey = true;
96  			}
97  
98  		}
99  		return type.getType().isAnnotationPresent(DynamoDBTable.class) || (hasHashKey && hasRangeKey);
100 	}
101 
102 }