Example usage for com.amazonaws.services.dynamodbv2.document.spec PutItemSpec PutItemSpec

List of usage examples for com.amazonaws.services.dynamodbv2.document.spec PutItemSpec PutItemSpec

Introduction

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

Prototype

public PutItemSpec() 

Source Link

Usage

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

License:Apache License

@Override
public <T extends Item> T create(final T item,
        final PersistenceExceptionHandler<?>... persistenceExceptionHandlers) {
    item.setVersion(1l);// ww w  .j  a  va2s .c  o m
    final ItemConfiguration itemConfiguration = getItemConfiguration(item.getClass());
    final Collection<PropertyDescriptor> createdConstraintPropertyDescriptors = createUniqueConstraintIndexes(
            item, itemConfiguration);
    final String tableName = databaseSchemaHolder.schemaName() + "." + itemConfiguration.tableName();
    final com.amazonaws.services.dynamodbv2.document.Item awsItem = com.amazonaws.services.dynamodbv2.document.Item
            .fromJSON(itemToString(item));
    final PutItemSpec putItemSpec = new PutItemSpec().withItem(awsItem);

    final Table table = dynamoDBClient.getTable(tableName);
    boolean itemRequestSucceeded = false;
    try {
        table.putItem(putItemSpec);
        itemRequestSucceeded = true;
    } finally {
        if (!itemRequestSucceeded) {
            try {
                deleteUniqueConstraintIndexes(item, itemConfiguration, createdConstraintPropertyDescriptors);
            } catch (final Exception deleteUniqueConstraintIndexesException) {
                logger.error(deleteUniqueConstraintIndexesException.getMessage(),
                        deleteUniqueConstraintIndexesException);
            }
        }
    }
    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);
    }//from  w  ww.  j av  a2s .  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.eho.dynamodb.DynamoDBConnection.java

public static PutItemOutcome upload_resource_old(String resource) throws Exception {
    String id;//from www . j  a va2  s .co m
    JSONObject json_resource = new JSONObject(resource);
    //does the resource have a primary key?
    if (json_resource.has(PRIMARY_KEY))//if it does not have a primary key, create one using uuid
        id = json_resource.getString(PRIMARY_KEY);
    else
        id = UUID.randomUUID().toString();

    DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);
    Table table = dynamoDB.getTable(PATIENT_TABLE);

    //lets retreive based on the key. if key invalid (not assigned yet) nullis returned.
    Item retreived_item = table.getItem(PRIMARY_KEY, id);
    if (retreived_item == null)//if null instantiate it
    {
        retreived_item = new Item();
        retreived_item.withPrimaryKey(PRIMARY_KEY, id);
    }

    Integer new_version = retreived_item.getInt("version") + 1;
    retreived_item.withInt("version", new_version);

    Item item_to_upload = Item.fromJSON(retreived_item.toJSONPretty()).withJSON("Document", resource);
    PutItemSpec putItemSpec = new PutItemSpec().withItem(item_to_upload).withReturnValues(ReturnValue.NONE);
    return table.putItem(putItemSpec);
}

From source file:com.eho.dynamodb.DynamoDBConnection.java

public static String upload_resource(BaseResource resource,
        String primary_key /* if no primary key in case of post, send null*/ ) throws Exception {
    String id = add_primary_as_extension(resource, primary_key);
    String resource_string = DynamoDBConnection.fCtx.newJsonParser().setPrettyPrint(true)
            .encodeResourceToString(resource);
    ;/*from  w ww. j a  v a2  s .  c o  m*/
    DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);
    Table table = dynamoDB.getTable(PATIENT_TABLE);

    //lets retreive based on the key. if key invalid (not assigned yet) nullis returned.
    Item retreived_item = table.getItem(PRIMARY_KEY, id);
    if (retreived_item == null)//if null instantiate it
    {
        retreived_item = new Item();
        retreived_item.withPrimaryKey(PRIMARY_KEY, id);
        retreived_item.withInt("version", -1);
    }

    Integer new_version = retreived_item.getInt("version") + 1;
    retreived_item.withInt("version", new_version);

    Item item_to_upload = retreived_item//Item.fromJSON(retreived_item.toJSONPretty())
            .withString("text" + new_version.toString(), resource_string)
            .withMap("json-document", new ObjectMapper().readValue(resource_string, LinkedHashMap.class));
    PutItemSpec putItemSpec = new PutItemSpec().withItem(item_to_upload);
    table.putItem(putItemSpec);
    return id;
}

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

License:Open Source License

PutItemSpec putItemSpec(Item domainItem) {
    return new PutItemSpec().withItem(domainItem).withConditionExpression(conditionalCreateCondition);
}

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

License:Open Source License

@Override
public <S extends E> S update(S domain, VersionCondition condition) {
    Preconditions.checkNotNull(domain, "domain must not be null");
    final Item domainItem = convertDomainToItem(domain);
    Preconditions.checkArgument(domainItem.hasAttribute(hashKeyName),
            "hash key must be set in domain object when updating: " + hashKeyName);

    ExpressionSpecBuilder builder = new ExpressionSpecBuilder();
    builder.withCondition(ExpressionSpecBuilder.S(hashKeyName).exists());
    if (condition != null) {
        Preconditions.checkState(versionProperty != null);
        builder.withCondition(ExpressionSpecBuilder.N(versionProperty).eq(condition.getVersion()));
    }//from   w w w.  j  a v  a 2  s. co m
    PutItemExpressionSpec xSpec = builder.buildForPut();
    PutItemSpec spec = new PutItemSpec().withItem(domainItem).withExpressionSpec(xSpec);
    try {
        table.putItem(spec);
    } catch (AmazonClientException e) {
        throw processUpdateItemException(getId(domain), e);
    }
    // PutItem does not accept ReturnValue.ALL_NEW
    return domain;
}