Example usage for com.amazonaws.services.dynamodbv2.xspec UpdateItemExpressionSpec getUpdateExpression

List of usage examples for com.amazonaws.services.dynamodbv2.xspec UpdateItemExpressionSpec getUpdateExpression

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.xspec UpdateItemExpressionSpec getUpdateExpression.

Prototype

public String getUpdateExpression() 

Source Link

Document

Returns the update expression; or null if there is none.

Usage

From source file:jp.classmethod.aws.dynamodb.DynamoDbRepository.java

License:Open Source License

@Override
public E update(K key, JsonPatch patch, boolean increment, long version) {
    final PrimaryKey pk = createKeys(key);
    Preconditions.checkNotNull(patch, "patch must not be null");
    Preconditions.checkArgument(version >= -1);

    ExpressionSpecBuilder builder = patch.get();

    //add a condition on item existence
    builder.withCondition(ExpressionSpecBuilder.attribute_exists(hashKeyName));
    //add update expression for incrementing the version
    if (increment && versionProperty != null) {
        builder.addUpdate(ExpressionSpecBuilder.N(versionProperty)
                .set(ExpressionSpecBuilder.N(versionProperty).plus(1L)));
    }/*  www .java2 s  . co m*/
    //add version condition
    if (version >= 0) {
        Preconditions.checkState(versionProperty != null);
        builder.withCondition(ExpressionSpecBuilder.N(versionProperty).eq(version));
    }

    UpdateItemExpressionSpec spec = builder.buildForUpdate();
    Preconditions.checkArgument(false == Strings.isNullOrEmpty(spec.getUpdateExpression()),
            "patch may not be empty"); // TODO add mechanism to JSON patch to allow iterating over list of ops
    try {
        UpdateItemOutcome updateItemOutcome = table.updateItem(new UpdateItemSpec().withExpressionSpec(spec)
                .withPrimaryKey(pk).withReturnValues(ReturnValue.ALL_NEW));
        return convertItemToDomain(updateItemOutcome.getItem());
    } catch (AmazonClientException e) {
        throw processUpdateItemException(key, e);
    }
}