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

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

Introduction

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

Prototype

public PrimaryKey() 

Source Link

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 ww  . j  a  va 2  s. com*/

    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:jp.classmethod.aws.dynamodb.DynamoDbRepository.java

License:Open Source License

private static <K extends Serializable> PrimaryKey createKeys(String hashKeyName,
        ScalarAttributeType hashKeyType, K key) {
    Preconditions.checkNotNull(key);//w w  w. j  av  a2 s . co m
    Preconditions.checkArgument(hashKeyType == scalarAttributeType(key));

    //hash key only
    return new PrimaryKey().addComponent(hashKeyName, 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 w w  w  . j av a 2s .  c o m*/
}