Example usage for com.amazonaws.services.simpledb.model DeleteAttributesRequest setExpected

List of usage examples for com.amazonaws.services.simpledb.model DeleteAttributesRequest setExpected

Introduction

In this page you can find the example usage for com.amazonaws.services.simpledb.model DeleteAttributesRequest setExpected.

Prototype


public void setExpected(UpdateCondition expected) 

Source Link

Document

The update condition which, if specified, determines whether the specified attributes will be deleted or not.

Usage

From source file:com.dateofrock.simpledbmapper.SimpleDBMapper.java

License:Apache License

/**
 * SimpleDB????//from   w  w  w.j  av a  2s .c  o m
 * 
 * @param object
 *            {@link SimpleDBDomain}????POJO
 *            {@link SimpleDBVersionAttribute}
 *            ??????????????<a href=
 *            "http://docs.amazonwebservices.com/AmazonSimpleDB/latest/DeveloperGuide/ConditionalDelete.html"
 *            >Conditional Delete</a>????
 */
public void delete(Object object) {
    String domainName = this.reflector.getDomainName(object.getClass());
    Field itemNameField = this.reflector.findItemNameField(object.getClass());
    String itemName = this.reflector.encodeItemNameAsSimpleDBFormat(object, itemNameField);

    // S3 Blob
    GetAttributesResult results = this.sdb.getAttributes(new GetAttributesRequest(domainName, itemName));
    List<Attribute> sdbAllAttrs = results.getAttributes();
    Set<Field> blobFields = this.reflector.findBlobFields(object.getClass());
    List<S3TaskResult> s3TaskResults = new ArrayList<S3TaskResult>();
    for (Field field : blobFields) {
        SimpleDBBlob blobAnnon = field.getAnnotation(SimpleDBBlob.class);
        String attributeName = blobAnnon.attributeName();
        for (Attribute attr : sdbAllAttrs) {
            if (attr.getName().equals(attributeName)) {
                S3TaskResult taskResult = new S3TaskResult(Operation.DELETE, attributeName, null, null);
                taskResult.setSimpleDBAttributeValue(attr.getValue());
                s3TaskResults.add(taskResult);
            }
        }
    }

    DeleteAttributesRequest req = new DeleteAttributesRequest(domainName, itemName);
    // version?????Conditional Delete
    Field versionField = this.reflector.findVersionAttributeField(object.getClass());
    if (versionField != null) {
        try {
            Object versionObject = versionField.get(object);
            String versionAttributeName = versionField.getAnnotation(SimpleDBVersionAttribute.class)
                    .attributeName();
            if (versionObject != null) {
                if (versionObject instanceof Long) {
                    Long currentVersion = (Long) versionObject;
                    UpdateCondition expected = new UpdateCondition();
                    expected.setName(versionAttributeName);
                    expected.setValue(currentVersion.toString());
                    req.setExpected(expected);
                } else {
                    throw new SimpleDBMapperException(
                            "version?Long???????" + versionField);
                }
            }
        } catch (Exception e) {
            throw new SimpleDBMapperException("object?version??: " + object, e);
        }
    }
    this.sdb.deleteAttributes(req);

    // S3
    for (S3TaskResult s3TaskResult : s3TaskResults) {
        this.s3.deleteObject(s3TaskResult.getBucketName(), s3TaskResult.getKey());
    }
}