Search in sources :

Example 1 with SaveBehavior

use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig.SaveBehavior in project aws-sdk-android by aws-amplify.

the class DynamoDBMapper method delete.

/**
 * Deletes the given object from its DynamoDB table using the provided
 * deleteExpression and provided configuration. Any options specified in the
 * deleteExpression parameter will be overlaid on any constraints due to
 * versioned attributes.
 *
 * @param deleteExpression The options to apply to this delete request
 * @param config Config override object. If {@link SaveBehavior#CLOBBER} is
 *            supplied, version fields will not be considered when deleting
 *            the object.
 * @param object the object to delete.
 * @param <T> the type of the object.
 */
public <T> void delete(T object, DynamoDBDeleteExpression deleteExpression, DynamoDBMapperConfig config) {
    config = mergeConfig(config);
    final ItemConverter converter = getConverter(config);
    @SuppressWarnings("unchecked") final Class<T> clazz = (Class<T>) object.getClass();
    final String tableName = getTableName(clazz, object, config);
    final Map<String, AttributeValue> key = getKey(converter, object, clazz);
    /*
         * If there is a version field, make sure we assert its value. If the
         * version field is null (only should happen in unusual circumstances),
         * pretend it doesn't have a version field after all.
         */
    final Map<String, ExpectedAttributeValue> internalAssertions = new HashMap<String, ExpectedAttributeValue>();
    if (config.getSaveBehavior() != SaveBehavior.CLOBBER) {
        for (final Method method : reflector.getRelevantGetters(clazz)) {
            if (reflector.isVersionAttributeGetter(method)) {
                final Object getterResult = ReflectionUtils.safeInvoke(method, object);
                final String attributeName = reflector.getAttributeName(method);
                final ExpectedAttributeValue expected = new ExpectedAttributeValue();
                final AttributeValue currentValue = converter.convert(method, getterResult);
                expected.setExists(currentValue != null);
                if (currentValue != null) {
                    expected.setValue(currentValue);
                }
                internalAssertions.put(attributeName, expected);
                break;
            }
        }
    }
    DeleteItemRequest req = new DeleteItemRequest().withKey(key).withTableName(tableName).withExpected(internalAssertions).withRequestMetricCollector(config.getRequestMetricCollector());
    if (deleteExpression != null) {
        final String conditionalExpression = deleteExpression.getConditionExpression();
        if (conditionalExpression != null) {
            if (internalAssertions != null && !internalAssertions.isEmpty()) {
                throw new AmazonClientException("Condition Expressions cannot be used if a versioned attribute is present");
            }
            req = req.withConditionExpression(conditionalExpression).withExpressionAttributeNames(deleteExpression.getExpressionAttributeNames()).withExpressionAttributeValues(deleteExpression.getExpressionAttributeValues());
        }
        req = req.withExpected(mergeExpectedAttributeValueConditions(internalAssertions, deleteExpression.getExpected(), deleteExpression.getConditionalOperator())).withConditionalOperator(deleteExpression.getConditionalOperator());
    }
    db.deleteItem(applyUserAgent(req));
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) ExpectedAttributeValue(com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue) HashMap(java.util.HashMap) DeleteItemRequest(com.amazonaws.services.dynamodbv2.model.DeleteItemRequest) AmazonClientException(com.amazonaws.AmazonClientException) Method(java.lang.reflect.Method) ExpectedAttributeValue(com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue) JSONObject(org.json.JSONObject)

Aggregations

AmazonClientException (com.amazonaws.AmazonClientException)1 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)1 DeleteItemRequest (com.amazonaws.services.dynamodbv2.model.DeleteItemRequest)1 ExpectedAttributeValue (com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 JSONObject (org.json.JSONObject)1