in src/main/java/com/amazonaws/dao/OrderDao.java [148:198]
public Order updateOrder(final Order order) {
if (order == null) {
throw new IllegalArgumentException("Order to update was null");
}
String orderId = order.getOrderId();
if (isNullOrEmpty(orderId)) {
throw new IllegalArgumentException("orderId was null or empty");
}
Map<String, AttributeValue> expressionAttributeValues = new HashMap<>();
expressionAttributeValues.put(":cid",
AttributeValue.builder().s(validateCustomerId(order.getCustomerId())).build());
try {
expressionAttributeValues.put(":pre",
AttributeValue.builder().n(order.getPreTaxAmount().toString()).build());
} catch (NullPointerException e) {
throw new IllegalArgumentException(PRE_TAX_AMOUNT_WAS_NULL);
}
try {
expressionAttributeValues.put(":post",
AttributeValue.builder().n(order.getPostTaxAmount().toString()).build());
} catch (NullPointerException e) {
throw new IllegalArgumentException(POST_TAX_AMOUNT_WAS_NULL);
}
expressionAttributeValues.put(":o", AttributeValue.builder().n("1").build());
try {
expressionAttributeValues.put(":v",
AttributeValue.builder().n(order.getVersion().toString()).build());
} catch (NullPointerException e) {
throw new IllegalArgumentException(VERSION_WAS_NULL);
}
final UpdateItemResponse result;
try {
result = dynamoDb.updateItem(UpdateItemRequest.builder()
.tableName(tableName)
.key(Collections.singletonMap(ORDER_ID,
AttributeValue.builder().s(order.getOrderId()).build()))
.returnValues(ReturnValue.ALL_NEW)
.updateExpression(UPDATE_EXPRESSION)
.conditionExpression("attribute_exists(orderId) AND version = :v")
.expressionAttributeValues(expressionAttributeValues)
.build());
} catch (ConditionalCheckFailedException e) {
throw new UnableToUpdateException(
"Either the order did not exist or the provided version was not current");
} catch (ResourceNotFoundException e) {
throw new TableDoesNotExistException("Order table " + tableName
+ " does not exist and was deleted after reading the order");
}
return convert(result.attributes());
}