1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
31
32
33
34
35
36
37
38 public class DynamoDBMappingContext
39 extends
40 AbstractMappingContext<DynamoDBPersistentEntityImpl<?>, DynamoDBPersistentProperty> {
41
42
43
44
45
46
47
48 @Override
49 protected <T> DynamoDBPersistentEntityImpl<?> createPersistentEntity(TypeInformation<T> typeInformation) {
50 return new DynamoDBPersistentEntityImpl<>(typeInformation, null);
51
52 }
53
54
55
56
57
58
59
60
61
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
71
72
73
74
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 }