throw new AmazonServiceException(errors.toString());
}
// Check existence of table
Table table = this.tables.get(request.getTableName());
if (table == null) {
throw new ResourceNotFoundException("The table '" + request.getTableName() + "' doesn't exist.");
}
// Make sure that item specifies hash key and range key (if in schema)
KeySchemaElement hashKey = table.getKeySchema().getHashKeyElement();
KeySchemaElement rangeKey = table.getKeySchema().getRangeKeyElement();
AttributeValue hashItem = request.getItem().get(hashKey.getAttributeName());
AttributeValueType hashItemType = getAttributeValueType(hashItem);
if (hashItem == null || hashItemType != AttributeValueType.fromString(hashKey.getAttributeType())) {
throw new InternalServerErrorException("Missing hash key (" + hashKey.getAttributeName() + ") from item: " + request.getItem());
}
if (rangeKey != null) {
AttributeValue rangeItem = request.getItem().get(rangeKey.getAttributeName());
AttributeValueType rangeItemType = getAttributeValueType(rangeItem);
if (rangeItem == null || rangeItemType != AttributeValueType.fromString(rangeKey.getAttributeType())) {
throw new InternalServerErrorException("Missing range key (" + rangeKey.getAttributeName() + ") from item: " + request.getItem());
}
}
// Get current item if it exists
// Map<String, AttributeValue> item = table.getItem(getKeyValue(request.getItem().get(table.getHashKeyName())));
Map<String, AttributeValue> requestItem = request.getItem();
String hashKeyValue = getKeyValue(requestItem.get(table.getHashKeyName()));
String rangeKeyValue = getKeyValue(requestItem.get(table.getRangeKeyName()));
Map<String, AttributeValue> item = table.getItem(hashKeyValue, rangeKeyValue);
// Check conditional put
validateExpected(request.getExpected(), item);
PutItemResult result = new PutItemResult().withConsumedCapacityUnits(1D);
if (item != null && request.getReturnValues() != null && ReturnValue.fromValue(request.getReturnValues()) == ReturnValue.ALL_OLD) {
result.setAttributes(item);
}
// put the item in the table
table.putItem(request.getItem());
return result;
}