Example usage for com.amazonaws.services.dynamodbv2.model ConditionalCheckFailedException getMessage

List of usage examples for com.amazonaws.services.dynamodbv2.model ConditionalCheckFailedException getMessage

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.model ConditionalCheckFailedException getMessage.

Prototype

@Override
    public String getMessage() 

Source Link

Usage

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

License:Apache License

protected final <T extends Item> Collection<PropertyDescriptor> createUniqueConstraintIndexes(final T item,
        final ItemConfiguration itemConfiguration,
        final Collection<PropertyDescriptor> constraintPropertyDescriptors) {
    final Set<PropertyDescriptor> createdConstraintPropertyDescriptors = new HashSet<>();
    ItemConstraintViolationException itemConstraintViolationException = null;
    for (final UniqueConstraint uniqueConstraint : itemConfiguration.uniqueConstraints()) {
        final String uniqueConstraintPropertyName = uniqueConstraint.propertyName();
        final PropertyDescriptor uniqueConstraintPropertyDescriptor = uniqueConstraint.propertyDescriptor();
        if (constraintPropertyDescriptors.contains(uniqueConstraintPropertyDescriptor)) {
            final AttributeValue uniqueConstraintAttributeValue = DynamoDbPropertyMarshaller.getValue(item,
                    uniqueConstraintPropertyDescriptor);
            if (uniqueConstraintAttributeValue == null) {
                continue;
            }//from  w w w .  j  a va  2s.  co m
            if (uniqueConstraintAttributeValue.getS() != null) {
                uniqueConstraintAttributeValue.setS(uniqueConstraintAttributeValue.getS().toUpperCase());
            }
            final Map<String, AttributeValue> attributeMap = new HashMap<>();
            attributeMap.put("property", new AttributeValue(uniqueConstraintPropertyName));
            attributeMap.put("value", uniqueConstraintAttributeValue);
            final Map<String, ExpectedAttributeValue> expectedResults = new HashMap<>();
            expectedResults.put("value", new ExpectedAttributeValue(false));
            final String indexTableName = databaseSchemaHolder.schemaName() + "-indexes."
                    + itemConfiguration.tableName();
            final PutItemRequest itemRequest = new PutItemRequest().withTableName(indexTableName)
                    .withItem(attributeMap).withExpected(expectedResults);
            try {
                amazonDynamoDbClient.putItem(itemRequest);
                createdConstraintPropertyDescriptors.add(uniqueConstraintPropertyDescriptor);
            } catch (final ConditionalCheckFailedException e) {
                itemConstraintViolationException = new ItemConstraintViolationException(
                        uniqueConstraintPropertyName,
                        "Unique constraint violation on property '" + uniqueConstraintPropertyName + "' ('"
                                + uniqueConstraintAttributeValue + "') of item " + item.getClass());
                break;
            } catch (final AmazonServiceException e) {
                throw new PersistenceResourceFailureException(
                        "Failure while attempting DynamoDb put (creating unique constraint index entry)", e);
            }
        }
    }
    if (itemConstraintViolationException != null) {
        try {
            deleteUniqueConstraintIndexes(item, itemConfiguration, createdConstraintPropertyDescriptors);
        } catch (final Exception e) {
            logger.error(e.getMessage(), e);
        }
        throw itemConstraintViolationException;
    }
    return createdConstraintPropertyDescriptors;
}

From source file:com.exorath.service.treasurehunt.dynamodb.DynamoDBService.java

License:Apache License

@Override
public PutResult setTreasure(UUID playerId, String treasureId) {
    UpdateItemSpec spec = new UpdateItemSpec().withPrimaryKey(PRIM_KEY, playerId.toString())
            .withAttributeUpdate(new AttributeUpdate(TREASURES_FIELD).addElements(treasureId))
            .withReturnValues(ReturnValue.UPDATED_OLD);
    try {//w w w. j  a  va  2s  .co m
        UpdateItemOutcome outcome = table.updateItem(spec);
        if (!outcome.getUpdateItemResult().toString().contains(treasureId)) {
            logger.info("Successfully set treasure " + treasureId + " for player " + playerId);
            return new PutResult();
        } else {
            logger.warn(
                    "Attempted to set treasure " + treasureId + " for player " + playerId + " a second time");
            return new PutResult("Treasure " + treasureId + " already set for player " + playerId);
        }
    } catch (ConditionalCheckFailedException ex) {
        logger.warn(
                "Updated treasure " + treasureId + " for player " + playerId + " failed\n:" + ex.getMessage());
        return new PutResult(ex.getMessage());
    }
}