forked from michaellavelle/spring-data-dynamodb
-
Notifications
You must be signed in to change notification settings - Fork 141
/
AbstractDynamoDBQueryCreator.java
162 lines (146 loc) · 7.25 KB
/
AbstractDynamoDBQueryCreator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* Copyright © 2018 spring-data-dynamodb (https://github.com/derjust/spring-data-dynamodb)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.socialsignin.spring.data.dynamodb.repository.query;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperTableModel;
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
import org.socialsignin.spring.data.dynamodb.core.DynamoDBOperations;
import org.socialsignin.spring.data.dynamodb.query.Query;
import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityInformation;
import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBIdIsHashAndRangeKeyEntityInformation;
import org.springframework.data.mapping.PropertyPath;
import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.data.repository.query.parser.AbstractQueryCreator;
import org.springframework.data.repository.query.parser.Part;
import org.springframework.data.repository.query.parser.Part.IgnoreCaseType;
import org.springframework.data.repository.query.parser.PartTree;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Optional;
/**
* @author Michael Lavelle
* @author Sebastian Just
*/
public abstract class AbstractDynamoDBQueryCreator<T, ID, R>
extends
AbstractQueryCreator<Query<R>, DynamoDBQueryCriteria<T, ID>> {
protected final DynamoDBEntityInformation<T, ID> entityMetadata;
protected final DynamoDBOperations dynamoDBOperations;
protected final Optional<String> projection;
protected final Optional<Integer> limit;
public AbstractDynamoDBQueryCreator(PartTree tree, DynamoDBEntityInformation<T, ID> entityMetadata,
Optional<String> projection, Optional<Integer> limitResults, DynamoDBOperations dynamoDBOperations) {
super(tree);
this.entityMetadata = entityMetadata;
this.projection = projection;
this.limit = limitResults;
this.dynamoDBOperations = dynamoDBOperations;
}
public AbstractDynamoDBQueryCreator(PartTree tree, ParameterAccessor parameterAccessor,
DynamoDBEntityInformation<T, ID> entityMetadata, Optional<String> projection,
Optional<Integer> limitResults, DynamoDBOperations dynamoDBOperations) {
super(tree, parameterAccessor);
this.entityMetadata = entityMetadata;
this.projection = projection;
this.limit = limitResults;
this.dynamoDBOperations = dynamoDBOperations;
}
@Override
protected DynamoDBQueryCriteria<T, ID> create(Part part, Iterator<Object> iterator) {
final DynamoDBMapperTableModel<T> tableModel = dynamoDBOperations.getTableModel(entityMetadata.getJavaType());
DynamoDBQueryCriteria<T, ID> criteria = entityMetadata.isRangeKeyAware()
? new DynamoDBEntityWithHashAndRangeKeyCriteria<T, ID>(
(DynamoDBIdIsHashAndRangeKeyEntityInformation<T, ID>) entityMetadata, tableModel)
: new DynamoDBEntityWithHashKeyOnlyCriteria<>(entityMetadata, tableModel);
return addCriteria(criteria, part, iterator);
}
protected DynamoDBQueryCriteria<T, ID> addCriteria(DynamoDBQueryCriteria<T, ID> criteria, Part part,
Iterator<Object> iterator) {
if (part.shouldIgnoreCase().equals(IgnoreCaseType.ALWAYS))
throw new UnsupportedOperationException("Case insensitivity not supported");
Class<?> leafNodePropertyType = part.getProperty().getLeafProperty().getType();
PropertyPath leafNodePropertyPath = part.getProperty().getLeafProperty();
String leafNodePropertyName = leafNodePropertyPath.toDotPath();
if (leafNodePropertyName.indexOf(".") != -1) {
int index = leafNodePropertyName.lastIndexOf(".");
leafNodePropertyName = leafNodePropertyName.substring(index);
}
switch (part.getType()) {
case IN :
Object in = iterator.next();
Assert.notNull(in, "Creating conditions on null parameters not supported: please specify a value for '"
+ leafNodePropertyName + "'");
boolean isIterable = ClassUtils.isAssignable(Iterable.class, in.getClass());
boolean isArray = ObjectUtils.isArray(in);
Assert.isTrue(isIterable || isArray, "In criteria can only operate with Iterable or Array parameters");
Iterable<?> iterable = isIterable ? ((Iterable<?>) in) : Arrays.asList(ObjectUtils.toObjectArray(in));
return criteria.withPropertyIn(leafNodePropertyName, iterable, leafNodePropertyType);
case CONTAINING :
return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.CONTAINS,
iterator.next(), leafNodePropertyType);
case STARTING_WITH :
return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.BEGINS_WITH,
iterator.next(), leafNodePropertyType);
case BETWEEN :
Object first = iterator.next();
Object second = iterator.next();
return criteria.withPropertyBetween(leafNodePropertyName, first, second, leafNodePropertyType);
case AFTER :
case GREATER_THAN :
return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.GT, iterator.next(),
leafNodePropertyType);
case BEFORE :
case LESS_THAN :
return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.LT, iterator.next(),
leafNodePropertyType);
case GREATER_THAN_EQUAL :
return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.GE, iterator.next(),
leafNodePropertyType);
case LESS_THAN_EQUAL :
return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.LE, iterator.next(),
leafNodePropertyType);
case IS_NULL :
return criteria.withNoValuedCriteria(leafNodePropertyName, ComparisonOperator.NULL);
case IS_NOT_NULL :
return criteria.withNoValuedCriteria(leafNodePropertyName, ComparisonOperator.NOT_NULL);
case TRUE :
return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.EQ, Boolean.TRUE,
leafNodePropertyType);
case FALSE :
return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.EQ, Boolean.FALSE,
leafNodePropertyType);
case SIMPLE_PROPERTY :
return criteria.withPropertyEquals(leafNodePropertyName, iterator.next(), leafNodePropertyType);
case NEGATING_SIMPLE_PROPERTY :
return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.NE, iterator.next(),
leafNodePropertyType);
default :
throw new IllegalArgumentException("Unsupported keyword " + part.getType());
}
}
@Override
protected DynamoDBQueryCriteria<T, ID> and(Part part, DynamoDBQueryCriteria<T, ID> base,
Iterator<Object> iterator) {
return addCriteria(base, part, iterator);
}
@Override
protected DynamoDBQueryCriteria<T, ID> or(DynamoDBQueryCriteria<T, ID> base,
DynamoDBQueryCriteria<T, ID> criteria) {
throw new UnsupportedOperationException("Or queries not supported");
}
}