|
26 | 26 | import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverted;
|
27 | 27 | import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverter;
|
28 | 28 | import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBVersionAttribute;
|
| 29 | +import org.socialsignin.spring.data.dynamodb.core.DynamoDBOperations; |
29 | 30 | import org.springframework.util.Assert;
|
30 | 31 | import org.springframework.util.ReflectionUtils;
|
31 | 32 | import org.springframework.util.StringUtils;
|
@@ -117,6 +118,65 @@ public DynamoDBEntityMetadataSupport(final Class<T> domainType) {
|
117 | 118 | Assert.notNull(hashKeyPropertyName, "Unable to find hash key field or getter method on " + domainType + "!");
|
118 | 119 | }
|
119 | 120 |
|
| 121 | + /** |
| 122 | + * Creates a new {@link DynamoDBEntityMetadataSupport} for the given domain |
| 123 | + * type and dynamoDB mapper config. |
| 124 | + * |
| 125 | + * @param domainType |
| 126 | + * must not be {@literal null}. |
| 127 | + * @param dynamoDBOperations |
| 128 | + * must not be {@literal null}. |
| 129 | + */ |
| 130 | + public DynamoDBEntityMetadataSupport(final Class<T> domainType, DynamoDBOperations dynamoDBOperations) { |
| 131 | + |
| 132 | + Assert.notNull(domainType, "Domain type must not be null!"); |
| 133 | + this.domainType = domainType; |
| 134 | + |
| 135 | + DynamoDBTable table = this.domainType.getAnnotation(DynamoDBTable.class); |
| 136 | + Assert.notNull(table, "Domain type must by annotated with DynamoDBTable!"); |
| 137 | + |
| 138 | + this.dynamoDBTableName = dynamoDBOperations.getOverriddenTableName(domainType, table.tableName()); |
| 139 | + this.hashKeyPropertyName = null; |
| 140 | + this.globalSecondaryIndexNames = new HashMap<>(); |
| 141 | + this.globalIndexHashKeyPropertyNames = new ArrayList<>(); |
| 142 | + this.globalIndexRangeKeyPropertyNames = new ArrayList<>(); |
| 143 | + ReflectionUtils.doWithMethods(domainType, method -> { |
| 144 | + if (method.getAnnotation(DynamoDBHashKey.class) != null) { |
| 145 | + hashKeyPropertyName = getPropertyNameForAccessorMethod(method); |
| 146 | + } |
| 147 | + if (method.getAnnotation(DynamoDBRangeKey.class) != null) { |
| 148 | + hasRangeKey = true; |
| 149 | + } |
| 150 | + DynamoDBIndexRangeKey dynamoDBRangeKeyAnnotation = method.getAnnotation(DynamoDBIndexRangeKey.class); |
| 151 | + DynamoDBIndexHashKey dynamoDBHashKeyAnnotation = method.getAnnotation(DynamoDBIndexHashKey.class); |
| 152 | + |
| 153 | + if (dynamoDBRangeKeyAnnotation != null) { |
| 154 | + addGlobalSecondaryIndexNames(method, dynamoDBRangeKeyAnnotation); |
| 155 | + } |
| 156 | + if (dynamoDBHashKeyAnnotation != null) { |
| 157 | + addGlobalSecondaryIndexNames(method, dynamoDBHashKeyAnnotation); |
| 158 | + } |
| 159 | + }); |
| 160 | + ReflectionUtils.doWithFields(domainType, field -> { |
| 161 | + if (field.getAnnotation(DynamoDBHashKey.class) != null) { |
| 162 | + hashKeyPropertyName = getPropertyNameForField(field); |
| 163 | + } |
| 164 | + if (field.getAnnotation(DynamoDBRangeKey.class) != null) { |
| 165 | + hasRangeKey = true; |
| 166 | + } |
| 167 | + DynamoDBIndexRangeKey dynamoDBRangeKeyAnnotation = field.getAnnotation(DynamoDBIndexRangeKey.class); |
| 168 | + DynamoDBIndexHashKey dynamoDBHashKeyAnnotation = field.getAnnotation(DynamoDBIndexHashKey.class); |
| 169 | + |
| 170 | + if (dynamoDBRangeKeyAnnotation != null) { |
| 171 | + addGlobalSecondaryIndexNames(field, dynamoDBRangeKeyAnnotation); |
| 172 | + } |
| 173 | + if (dynamoDBHashKeyAnnotation != null) { |
| 174 | + addGlobalSecondaryIndexNames(field, dynamoDBHashKeyAnnotation); |
| 175 | + } |
| 176 | + }); |
| 177 | + Assert.notNull(hashKeyPropertyName, "Unable to find hash key field or getter method on " + domainType + "!"); |
| 178 | + } |
| 179 | + |
120 | 180 | public DynamoDBEntityInformation<T, ID> getEntityInformation() {
|
121 | 181 |
|
122 | 182 | if (hasRangeKey) {
|
|
0 commit comments