Skip to content

Commit b77c12e

Browse files
committed
Followup: Handle new Sort.unsorted() sorting strategy
Spring-Data no longer passes in `null` if no sorting order is given by the caller but uses `Sort.unsorted()` which incorrectly causes an Exception. This should also solve #99 Followup to 8c3e139 Also getting rid of some JDK9 deprecated warnings on the JDK classes - as those methods exist in JDK8 already.
1 parent 4efd5b5 commit b77c12e

25 files changed

+328
-131
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5353
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
54-
<maven.compiler.source>1.7</maven.compiler.source>
54+
<maven.compiler.source>1.8</maven.compiler.source>
5555
<maven.compiler.target>1.7</maven.compiler.target>
5656
</properties>
5757

src/main/java/org/socialsignin/spring/data/dynamodb/mapping/event/AbstractDynamoDBEventListener.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040

4141
import java.util.List;
4242

43+
import static org.socialsignin.spring.data.dynamodb.utils.SortHandler.*;
44+
4345
/**
4446
* Base class to implement domain class specific {@link ApplicationListener}s.
4547
*
@@ -122,7 +124,7 @@ else if (domainClass.isAssignableFrom(source.getClass())) {
122124
}
123125

124126
private void publishEachElement(List<E> list, Consumer<E> publishMethod) {
125-
for(E o : list) {
127+
for (E o : list) {
126128
if (domainClass.isAssignableFrom(o.getClass())) {
127129
publishMethod.accept(o);
128130

src/main/java/org/socialsignin/spring/data/dynamodb/query/ScanExpressionCountQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public ScanExpressionCountQuery(DynamoDBOperations dynamoDBOperations, Class<T>
3737
@Override
3838
public Long getSingleResult() {
3939
assertScanCountEnabled(isScanCountEnabled());
40-
return new Long(dynamoDBOperations.count(domainClass,scanExpression));
40+
return Long.valueOf(dynamoDBOperations.count(domainClass,scanExpression));
4141
}
4242

4343
public void assertScanCountEnabled(boolean scanCountEnabled)

src/main/java/org/socialsignin/spring/data/dynamodb/repository/query/AbstractDynamoDBQueryCriteria.java

Lines changed: 37 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.socialsignin.spring.data.dynamodb.marshaller.Instant2IsoDynamoDBMarshaller;
3030
import org.socialsignin.spring.data.dynamodb.query.Query;
3131
import org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityInformation;
32+
import org.socialsignin.spring.data.dynamodb.utils.SortHandler;
3233
import org.springframework.data.domain.Sort;
3334
import org.springframework.data.domain.Sort.Direction;
3435
import org.springframework.data.domain.Sort.Order;
@@ -66,19 +67,10 @@ public abstract class AbstractDynamoDBQueryCriteria<T, ID extends Serializable>
6667
protected Object hashKeyAttributeValue;
6768
protected Object hashKeyPropertyValue;
6869
protected String globalSecondaryIndexName;
69-
protected Sort sort;
70+
protected Sort sort = null;
7071

7172
public abstract boolean isApplicableForLoad();
7273

73-
/**
74-
* @throws UnsupportedOperationException if a {@link #sort} is initalized (non-null &amp;&amp; not {@link Sort#unsorted()}
75-
*/
76-
protected void ensureNoSort() throws UnsupportedOperationException {
77-
if (sort != null) {
78-
throw new UnsupportedOperationException("Sort not supported for scan expressions");
79-
}
80-
}
81-
8274
protected QueryRequest buildQueryRequest(String tableName, String theIndexName, String hashKeyAttributeName,
8375
String rangeKeyAttributeName, String rangeKeyPropertyName, List<Condition> hashKeyConditions,
8476
List<Condition> rangeKeyConditions) {
@@ -120,14 +112,12 @@ protected QueryRequest buildQueryRequest(String tableName, String theIndexName,
120112
}
121113
}
122114

123-
if (sort != null) {
124-
for (Order order : sort) {
125-
final String sortProperty = order.getProperty();
126-
if (entityInformation.isGlobalIndexRangeKeyProperty(sortProperty)) {
127-
allowedSortProperties.add(sortProperty);
128-
}
129-
}
130-
}
115+
for (Order order : sort) {
116+
final String sortProperty = order.getProperty();
117+
if (entityInformation.isGlobalIndexRangeKeyProperty(sortProperty)) {
118+
allowedSortProperties.add(sortProperty);
119+
}
120+
}
131121

132122
queryRequest.setKeyConditions(keyConditions);
133123
queryRequest.setSelect(Select.ALL_PROJECTED_ATTRIBUTES);
@@ -141,20 +131,19 @@ protected void applySortIfSpecified(DynamoDBQueryExpression<T> queryExpression,
141131
throw new UnsupportedOperationException("Can only sort by at most a single range or index range key");
142132

143133
}
144-
if (sort != null) {
145-
boolean sortAlreadySet = false;
146-
for (Order order : sort) {
147-
if (permittedPropertyNames.contains(order.getProperty())) {
148-
if (sortAlreadySet) {
149-
throw new UnsupportedOperationException("Sorting by multiple attributes not possible");
150134

151-
}
152-
queryExpression.setScanIndexForward(order.getDirection().equals(Direction.ASC));
153-
sortAlreadySet = true;
154-
} else {
155-
throw new UnsupportedOperationException("Sorting only possible by " + permittedPropertyNames
156-
+ " for the criteria specified");
135+
boolean sortAlreadySet = false;
136+
for (Order order : sort) {
137+
if (permittedPropertyNames.contains(order.getProperty())) {
138+
if (sortAlreadySet) {
139+
throw new UnsupportedOperationException("Sorting by multiple attributes not possible");
140+
157141
}
142+
queryExpression.setScanIndexForward(order.getDirection().equals(Direction.ASC));
143+
sortAlreadySet = true;
144+
} else {
145+
throw new UnsupportedOperationException("Sorting only possible by " + permittedPropertyNames
146+
+ " for the criteria specified");
158147
}
159148
}
160149
}
@@ -164,25 +153,23 @@ protected void applySortIfSpecified(QueryRequest queryRequest, List<String> perm
164153
throw new UnsupportedOperationException("Can only sort by at most a single global hash and range key");
165154
}
166155

167-
if (sort != null) {
168-
boolean sortAlreadySet = false;
169-
for (Order order : sort) {
170-
if (permittedPropertyNames.contains(order.getProperty())) {
171-
if (sortAlreadySet) {
172-
throw new UnsupportedOperationException("Sorting by multiple attributes not possible");
156+
boolean sortAlreadySet = false;
157+
for (Order order : sort) {
158+
if (permittedPropertyNames.contains(order.getProperty())) {
159+
if (sortAlreadySet) {
160+
throw new UnsupportedOperationException("Sorting by multiple attributes not possible");
173161

174-
}
175-
if (queryRequest.getKeyConditions().size() > 1 && !hasIndexHashKeyEqualCondition()) {
176-
throw new UnsupportedOperationException(
177-
"Sorting for global index queries with criteria on both hash and range not possible");
162+
}
163+
if (queryRequest.getKeyConditions().size() > 1 && !hasIndexHashKeyEqualCondition()) {
164+
throw new UnsupportedOperationException(
165+
"Sorting for global index queries with criteria on both hash and range not possible");
178166

179-
}
180-
queryRequest.setScanIndexForward(order.getDirection().equals(Direction.ASC));
181-
sortAlreadySet = true;
182-
} else {
183-
throw new UnsupportedOperationException("Sorting only possible by " + permittedPropertyNames
184-
+ " for the criteria specified");
185167
}
168+
queryRequest.setScanIndexForward(order.getDirection().equals(Direction.ASC));
169+
sortAlreadySet = true;
170+
} else {
171+
throw new UnsupportedOperationException("Sorting only possible by " + permittedPropertyNames
172+
+ " for the criteria specified");
186173
}
187174
}
188175
}
@@ -413,7 +400,10 @@ public Object getHashKeyPropertyValue() {
413400
protected String getAttributeName(String propertyName) {
414401
String attributeName = attributeNamesByPropertyName.get(propertyName);
415402
if (attributeName == null) {
416-
attributeName = entityInformation.getOverriddenAttributeName(propertyName).orElse(propertyName);
403+
attributeName = entityInformation.getOverriddenAttributeName(propertyName);
404+
if (attributeName == null) {
405+
attributeName = propertyName;
406+
}
417407
attributeNamesByPropertyName.put(propertyName, attributeName);
418408
}
419409
return attributeName;
@@ -500,7 +490,6 @@ protected <V extends Object> Object getPropertyAttributeValue(final String prope
500490
} else if (tableModel != null) { // purely here for testing as DynamoDBMapperTableModel cannot be mocked using Mockito
501491

502492
String attributeName = getAttributeName(propertyName);
503-
entityInformation.getOverriddenAttributeName(propertyName).orElse(propertyName);
504493

505494
DynamoDBMapperFieldModel<T,Object> fieldModel = tableModel.field(attributeName);
506495
if (fieldModel != null) {
@@ -621,16 +610,6 @@ protected <P> List<AttributeValue> addAttributeValue(List<AttributeValue> attrib
621610
String marshalledDate = new Date2IsoDynamoDBMarshaller().marshall(date);
622611
attributeValueObject.withS(marshalledDate);
623612
}
624-
} else if (ClassUtils.isAssignable(Instant.class, propertyType)) {
625-
List<Instant> attributeValueAsList = getAttributeValueAsList(attributeValue);
626-
if (expandCollectionValues && attributeValueAsList != null) {
627-
List<String> attributeValueAsStringList = getInstantListAsStringList(attributeValueAsList);
628-
attributeValueObject.withSS(attributeValueAsStringList);
629-
} else {
630-
Instant date = (Instant) attributeValue;
631-
String marshalledDate = new Instant2IsoDynamoDBMarshaller().marshall(date);
632-
attributeValueObject.withS(marshalledDate);
633-
}
634613
} else {
635614
throw new RuntimeException("Cannot create condition for type:" + attributeValue.getClass()
636615
+ " property conditions must be String,Number or Boolean, or have a DynamoDBMarshaller configured");

src/main/java/org/socialsignin/spring/data/dynamodb/repository/query/DynamoDBEntityWithHashAndRangeKeyCriteria.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import java.util.Map.Entry;
4545
import java.util.Set;
4646

47+
import static org.socialsignin.spring.data.dynamodb.utils.SortHandler.*;
48+
4749
/**
4850
* @author Michael Lavelle
4951
*/
@@ -331,7 +333,7 @@ public boolean isApplicableForQuery() {
331333

332334
public DynamoDBScanExpression buildScanExpression() {
333335

334-
ensureNoSort();
336+
ensureNoSort(sort);
335337

336338
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
337339
if (isHashKeySpecified()) {

src/main/java/org/socialsignin/spring/data/dynamodb/repository/query/DynamoDBEntityWithHashKeyOnlyCriteria.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import java.util.List;
3535
import java.util.Map;
3636

37+
import static org.socialsignin.spring.data.dynamodb.utils.SortHandler.*;
38+
3739
/**
3840
* @author Michael Lavelle
3941
*/
@@ -91,7 +93,7 @@ public boolean isApplicableForLoad() {
9193

9294
public DynamoDBScanExpression buildScanExpression() {
9395

94-
ensureNoSort();
96+
ensureNoSort(sort);
9597

9698
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
9799
if (isHashKeySpecified()) {

src/main/java/org/socialsignin/spring/data/dynamodb/repository/support/DynamoDBEntityMetadataSupport.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
import java.io.Serializable;
3434
import java.lang.annotation.Annotation;
3535
import java.lang.reflect.Field;
36+
import java.lang.reflect.InvocationTargetException;
3637
import java.lang.reflect.Method;
3738
import java.util.ArrayList;
3839
import java.util.HashMap;
3940
import java.util.List;
4041
import java.util.Map;
41-
import java.util.Optional;
4242

4343
/**
4444
* @author Michael Lavelle
@@ -223,64 +223,64 @@ public String getOverriddenAttributeName(Method method) {
223223
}
224224

225225
@Override
226-
public Optional<String> getOverriddenAttributeName(final String propertyName) {
226+
public String getOverriddenAttributeName(final String propertyName) {
227227

228228
Method method = findMethod(propertyName);
229229
if (method != null) {
230230
if (method.getAnnotation(DynamoDBAttribute.class) != null
231231
&& !StringUtils.isEmpty(method.getAnnotation(DynamoDBAttribute.class).attributeName())) {
232-
return Optional.of(method.getAnnotation(DynamoDBAttribute.class).attributeName());
232+
return method.getAnnotation(DynamoDBAttribute.class).attributeName();
233233
}
234234
if (method.getAnnotation(DynamoDBHashKey.class) != null
235235
&& !StringUtils.isEmpty(method.getAnnotation(DynamoDBHashKey.class).attributeName())) {
236-
return Optional.of(method.getAnnotation(DynamoDBHashKey.class).attributeName());
236+
return method.getAnnotation(DynamoDBHashKey.class).attributeName();
237237
}
238238
if (method.getAnnotation(DynamoDBRangeKey.class) != null
239239
&& !StringUtils.isEmpty(method.getAnnotation(DynamoDBRangeKey.class).attributeName())) {
240-
return Optional.of(method.getAnnotation(DynamoDBRangeKey.class).attributeName());
240+
return method.getAnnotation(DynamoDBRangeKey.class).attributeName();
241241
}
242242
if (method.getAnnotation(DynamoDBIndexRangeKey.class) != null
243243
&& !StringUtils.isEmpty(method.getAnnotation(DynamoDBIndexRangeKey.class).attributeName())) {
244-
return Optional.of(method.getAnnotation(DynamoDBIndexRangeKey.class).attributeName());
244+
return method.getAnnotation(DynamoDBIndexRangeKey.class).attributeName();
245245
}
246246
if (method.getAnnotation(DynamoDBIndexHashKey.class) != null
247247
&& !StringUtils.isEmpty(method.getAnnotation(DynamoDBIndexHashKey.class).attributeName())) {
248-
return Optional.of(method.getAnnotation(DynamoDBIndexHashKey.class).attributeName());
248+
return method.getAnnotation(DynamoDBIndexHashKey.class).attributeName();
249249
}
250250
if (method.getAnnotation(DynamoDBVersionAttribute.class) != null
251251
&& !StringUtils.isEmpty(method.getAnnotation(DynamoDBVersionAttribute.class).attributeName())) {
252-
return Optional.of(method.getAnnotation(DynamoDBVersionAttribute.class).attributeName());
252+
return method.getAnnotation(DynamoDBVersionAttribute.class).attributeName();
253253
}
254254
}
255255

256256
Field field = findField(propertyName);
257257
if (field != null) {
258258
if (field.getAnnotation(DynamoDBAttribute.class) != null
259259
&& !StringUtils.isEmpty(field.getAnnotation(DynamoDBAttribute.class).attributeName())) {
260-
return Optional.of(field.getAnnotation(DynamoDBAttribute.class).attributeName());
260+
return field.getAnnotation(DynamoDBAttribute.class).attributeName();
261261
}
262262
if (field.getAnnotation(DynamoDBHashKey.class) != null
263263
&& !StringUtils.isEmpty(field.getAnnotation(DynamoDBHashKey.class).attributeName())) {
264-
return Optional.of(field.getAnnotation(DynamoDBHashKey.class).attributeName());
264+
return field.getAnnotation(DynamoDBHashKey.class).attributeName();
265265
}
266266
if (field.getAnnotation(DynamoDBRangeKey.class) != null
267267
&& !StringUtils.isEmpty(field.getAnnotation(DynamoDBRangeKey.class).attributeName())) {
268-
return Optional.of(field.getAnnotation(DynamoDBRangeKey.class).attributeName());
268+
return field.getAnnotation(DynamoDBRangeKey.class).attributeName();
269269
}
270270
if (field.getAnnotation(DynamoDBIndexRangeKey.class) != null
271271
&& !StringUtils.isEmpty(field.getAnnotation(DynamoDBIndexRangeKey.class).attributeName())) {
272-
return Optional.of(field.getAnnotation(DynamoDBIndexRangeKey.class).attributeName());
272+
return field.getAnnotation(DynamoDBIndexRangeKey.class).attributeName();
273273
}
274274
if (field.getAnnotation(DynamoDBIndexHashKey.class) != null
275275
&& !StringUtils.isEmpty(field.getAnnotation(DynamoDBIndexHashKey.class).attributeName())) {
276-
return Optional.of(field.getAnnotation(DynamoDBIndexHashKey.class).attributeName());
276+
return field.getAnnotation(DynamoDBIndexHashKey.class).attributeName();
277277
}
278278
if (field.getAnnotation(DynamoDBVersionAttribute.class) != null
279279
&& !StringUtils.isEmpty(field.getAnnotation(DynamoDBVersionAttribute.class).attributeName())) {
280-
return Optional.of(field.getAnnotation(DynamoDBVersionAttribute.class).attributeName());
280+
return field.getAnnotation(DynamoDBVersionAttribute.class).attributeName();
281281
}
282282
}
283-
return Optional.empty();
283+
return null;
284284

285285
}
286286

@@ -302,12 +302,9 @@ public DynamoDBMarshaller<?> getMarshallerForProperty(final String propertyName)
302302

303303
if(annotation != null) {
304304
try {
305-
return annotation.marshallerClass().newInstance();
306-
} catch (InstantiationException e) {
305+
return annotation.marshallerClass().getDeclaredConstructor().newInstance();
306+
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
307307
throw new RuntimeException(e);
308-
} catch (IllegalAccessException e) {
309-
throw new RuntimeException(e);
310-
311308
}
312309
}
313310

src/main/java/org/socialsignin/spring/data/dynamodb/repository/support/DynamoDBHashAndRangeKeyExtractingEntityMetadataImpl.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.io.Serializable;
2727
import java.lang.reflect.Field;
28+
import java.lang.reflect.InvocationTargetException;
2829
import java.lang.reflect.Method;
2930
import java.util.HashSet;
3031
import java.util.Set;
@@ -110,7 +111,7 @@ public void doWith(Field field) {
110111
public T getHashKeyPropotypeEntityForHashKey(Object hashKey) {
111112

112113
try {
113-
T entity = getJavaType().newInstance();
114+
T entity = getJavaType().getDeclaredConstructor().newInstance();
114115
if (hashKeySetterMethod != null)
115116
{
116117
ReflectionUtils.invokeMethod(hashKeySetterMethod, entity, hashKey);
@@ -121,9 +122,7 @@ public T getHashKeyPropotypeEntityForHashKey(Object hashKey) {
121122
}
122123

123124
return entity;
124-
} catch (InstantiationException e) {
125-
throw new RuntimeException(e);
126-
} catch (IllegalAccessException e) {
125+
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
127126
throw new RuntimeException(e);
128127
}
129128
}

src/main/java/org/socialsignin/spring/data/dynamodb/repository/support/DynamoDBHashKeyExtractingEntityMetadata.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.springframework.data.repository.core.EntityMetadata;
2020

2121
import java.util.Map;
22-
import java.util.Optional;
2322

2423
/**
2524
* Obtains basic hash key-related metadata about a DynamoDBEntity, such as
@@ -31,7 +30,7 @@
3130
*/
3231
public interface DynamoDBHashKeyExtractingEntityMetadata<T> extends EntityMetadata<T> {
3332

34-
Optional<String> getOverriddenAttributeName(String propertyName);
33+
String getOverriddenAttributeName(String propertyName);
3534

3635
DynamoDBMarshaller<?> getMarshallerForProperty(String propertyName);
3736

src/main/java/org/socialsignin/spring/data/dynamodb/repository/support/DynamoDBIdIsHashAndRangeKeyEntityInformationImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public Object getRangeKey(final ID id) {
6262
}
6363

6464
@Override
65-
public Optional<String> getOverriddenAttributeName(String attributeName) {
65+
public String getOverriddenAttributeName(String attributeName) {
6666
return metadata.getOverriddenAttributeName(attributeName);
6767
}
6868

0 commit comments

Comments
 (0)