@Override
protected void onKeyAttributeValue(String attributeName,
AttributeValue keyAttributeValue) {
/* Treat key values as common attribute value updates. */
getAttributeValueUpdates().put(attributeName,
new AttributeValueUpdate().withValue(keyAttributeValue)
.withAction("PUT"));
}
/* Use default implementation of onNonKeyAttribute(...) */
@Override
protected void onNullNonKeyAttribute(String attributeName) {
/* When doing a force put, we can safely ignore the null-valued attributes. */
return;
}
@Override
protected void executeLowLevelRequest() {
/* Send a putItem request */
doPutItem();
}
};
} else {
saveObjectHandler = this.new SaveObjectHandler(clazz, object,
tableName, finalConfig, converter, saveExpression) {
@Override
protected void onKeyAttributeValue(String attributeName,
AttributeValue keyAttributeValue) {
/* Put it in the key collection which is later used in the updateItem request. */
getKeyAttributeValues().put(attributeName, keyAttributeValue);
}
@Override
protected void onNonKeyAttribute(String attributeName,
AttributeValue currentValue) {
/* If it's a set attribute and the mapper is configured with APPEND_SET,
* we do an "ADD" update instead of the default "PUT".
*/
if (getLocalSaveBehavior() == SaveBehavior.APPEND_SET) {
if (currentValue.getBS() != null
|| currentValue.getNS() != null
|| currentValue.getSS() != null) {
getAttributeValueUpdates().put(
attributeName,
new AttributeValueUpdate().withValue(
currentValue).withAction("ADD"));
return;
}
}
/* Otherwise, we do the default "PUT" update. */
super.onNonKeyAttribute(attributeName, currentValue);
}
@Override
protected void onNullNonKeyAttribute(String attributeName) {
/*
* If UPDATE_SKIP_NULL_ATTRIBUTES or APPEND_SET is
* configured, we don't delete null value attributes.
*/
if (getLocalSaveBehavior() == SaveBehavior.UPDATE_SKIP_NULL_ATTRIBUTES
|| getLocalSaveBehavior() == SaveBehavior.APPEND_SET) {
return;
}
else {
/* Delete attributes that are set as null in the object. */
getAttributeValueUpdates()
.put(attributeName,
new AttributeValueUpdate()
.withAction("DELETE"));
}
}
@Override
protected void executeLowLevelRequest() {
UpdateItemResult updateItemResult = doUpdateItem();
// The UpdateItem request is specified to return ALL_NEW
// attributes of the affected item. So if the returned
// UpdateItemResult does not include any ReturnedAttributes,
// it indicates the UpdateItem failed silently (e.g. the
// key-only-put nightmare -
// https://forums.aws.amazon.com/thread.jspa?threadID=86798&tstart=25),
// in which case we should re-send a PutItem
// request instead.
if (updateItemResult.getAttributes() == null
|| updateItemResult.getAttributes().isEmpty()) {
// Before we proceed with PutItem, we need to put all
// the key attributes (prepared for the
// UpdateItemRequest) into the AttributeValueUpdates
// collection.
for (String keyAttributeName : getKeyAttributeValues().keySet()) {
getAttributeValueUpdates().put(keyAttributeName,
new AttributeValueUpdate()
.withValue(getKeyAttributeValues().get(keyAttributeName))
.withAction("PUT"));
}
doPutItem();