Example usage for com.amazonaws.services.dynamodbv2.document PrimaryKey addComponent

List of usage examples for com.amazonaws.services.dynamodbv2.document PrimaryKey addComponent

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.document PrimaryKey addComponent.

Prototype

public PrimaryKey addComponent(String keyAttributeName, Object keyAttributeValue) 

Source Link

Document

Add a key component to this primary key.

Usage

From source file:com.clicktravel.infrastructure.persistence.aws.dynamodb.DynamoDocumentStoreTemplate.java

License:Apache License

@Override
public <T extends Item> T update(final T item,
        final PersistenceExceptionHandler<?>... persistenceExceptionHandlers) {
    final ItemConfiguration itemConfiguration = getItemConfiguration(item.getClass());
    if (item.getVersion() == null) {
        return create(item);
    }// w  w w . j ava2  s . c om

    final Expected expectedCondition = new Expected(VERSION_ATTRIBUTE).eq(item.getVersion());
    final Long newVersion = item.getVersion() + 1l;
    item.setVersion(newVersion);

    final String tableName = databaseSchemaHolder.schemaName() + "." + itemConfiguration.tableName();
    final String itemJson = itemToString(item);
    final PrimaryKey primaryKey = new PrimaryKey();
    final ItemId itemId = itemConfiguration.getItemId(item);
    final PrimaryKeyDefinition primaryKeyDefinition = itemConfiguration.primaryKeyDefinition();
    primaryKey.addComponent(primaryKeyDefinition.propertyName(), itemId.value());
    if (primaryKeyDefinition instanceof CompoundPrimaryKeyDefinition) {
        primaryKey.addComponent(((CompoundPrimaryKeyDefinition) primaryKeyDefinition).supportingPropertyName(),
                itemId.supportingValue());
    }
    final Table table = dynamoDBClient.getTable(tableName);
    final com.amazonaws.services.dynamodbv2.document.Item previousAwsItem = table.getItem(primaryKey);
    final String previousItemJson = previousAwsItem.toJSON();

    final String mergedJson = mergeJSONObjects(itemJson, previousItemJson);
    final com.amazonaws.services.dynamodbv2.document.Item awsItem = com.amazonaws.services.dynamodbv2.document.Item
            .fromJSON(mergedJson);
    final PutItemSpec putItemSpec = new PutItemSpec().withItem(awsItem).withExpected(expectedCondition);
    try {
        table.putItem(putItemSpec);
    } catch (final ConditionalCheckFailedException e) {
        throw new OptimisticLockException("Conflicting write detected while updating item");
    }
    return item;
}

From source file:com.clicktravel.infrastructure.persistence.aws.dynamodb.DynamoDocumentStoreTemplate.java

License:Apache License

private PrimaryKey getPrimaryKey(final ItemId itemId, final ItemConfiguration itemConfiguration) {
    final PrimaryKeyDefinition primaryKeyDefinition = itemConfiguration.primaryKeyDefinition();
    final PrimaryKey key = new PrimaryKey(primaryKeyDefinition.propertyName(), itemId.value());
    if (CompoundPrimaryKeyDefinition.class.isAssignableFrom(primaryKeyDefinition.getClass())) {
        final CompoundPrimaryKeyDefinition compoundPrimaryKeyDefinition = (CompoundPrimaryKeyDefinition) primaryKeyDefinition;
        key.addComponent(compoundPrimaryKeyDefinition.supportingPropertyName(), itemId.supportingValue());
    }/*w  w w  .j a v a  2 s  .  c om*/
    return key;
}

From source file:org.xmlsh.aws.util.AWSDDBCommand.java

License:BSD License

private static PrimaryKey getPrimaryKey(Options opts) throws InvalidArgumentException, XPathException,
        UnexpectedException, UnimplementedException, IOException {
    INameObjectMap keys = DDBTypes.parseKeyValueObjectOptions(opts);
    PrimaryKey key = new PrimaryKey();
    for (Entry<String, Object> e : keys.entrySet())
        key.addComponent(e.getKey(), e.getValue());
    return key;//from   www  .j  a v a 2  s .com
}