Example usage for com.amazonaws.services.dynamodbv2.document Table getItem

List of usage examples for com.amazonaws.services.dynamodbv2.document Table getItem

Introduction

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

Prototype

@Override
    public Item getItem(GetItemSpec spec) 

Source Link

Usage

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

License:Apache License

@Override
public <T extends Item> T read(final ItemId itemId, final Class<T> itemClass) throws NonExistentItemException {
    final ItemConfiguration itemConfiguration = getItemConfiguration(itemClass);
    final String tableName = databaseSchemaHolder.schemaName() + "." + itemConfiguration.tableName();

    final GetItemSpec itemSpec = new GetItemSpec().withPrimaryKey(getPrimaryKey(itemId, itemConfiguration));

    T item = null;/*from   w w w. j a v  a  2 s. c  o  m*/

    final Table table = dynamoDBClient.getTable(tableName);

    final com.amazonaws.services.dynamodbv2.document.Item tableItem = table.getItem(itemSpec);
    if (tableItem != null) {
        final String tableText = tableItem.toJSON();
        if (tableText.isEmpty()) {
            throw new NonExistentItemException(String.format(
                    "The document of type [%s] with id [%s] does not exist", itemClass.getName(), itemId));
        }
        item = stringToItem(tableText, itemClass);
    } else {
        throw new NonExistentItemException(String
                .format("The document of type [%s] with id [%s] does not exist", itemClass.getName(), itemId));
    }
    return item;
}

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  a  v a2  s  . c  o  m*/

    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;
}