Example usage for com.amazonaws.services.dynamodbv2.datamodeling DynamoDBQueryExpression withHashKeyValues

List of usage examples for com.amazonaws.services.dynamodbv2.datamodeling DynamoDBQueryExpression withHashKeyValues

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.datamodeling DynamoDBQueryExpression withHashKeyValues.

Prototype

public DynamoDBQueryExpression<T> withHashKeyValues(T hashKObject) 

Source Link

Document

Sets the hash key value(s) for this query.

Usage

From source file:org.socialsignin.spring.data.dynamodb.repository.query.DynamoDBEntityWithHashAndRangeKeyCriteria.java

License:Apache License

public DynamoDBQueryExpression<T> buildQueryExpression() {
    DynamoDBQueryExpression<T> queryExpression = new DynamoDBQueryExpression<T>();
    if (isHashKeySpecified()) {
        T hashKeyPrototype = entityInformation.getHashKeyPropotypeEntityForHashKey(getHashKeyPropertyValue());
        queryExpression.withHashKeyValues(hashKeyPrototype);
        queryExpression.withRangeKeyConditions(new HashMap<String, Condition>());
    }//from   w  w w. jav  a  2s.  com

    if (isRangeKeySpecified() && !isApplicableForGlobalSecondaryIndex()) {
        Condition rangeKeyCondition = createSingleValueCondition(getRangeKeyPropertyName(),
                ComparisonOperator.EQ, getRangeKeyAttributeValue(), getRangeKeyAttributeValue().getClass(),
                true);
        queryExpression.withRangeKeyCondition(getRangeKeyAttributeName(), rangeKeyCondition);
        applySortIfSpecified(queryExpression, Arrays.asList(new String[] { getRangeKeyPropertyName() }));

    } else if (isOnlyASingleAttributeConditionAndItIsOnEitherRangeOrIndexRangeKey()
            || (isApplicableForGlobalSecondaryIndex())) {

        Entry<String, List<Condition>> singlePropertyConditions = propertyConditions.entrySet().iterator()
                .next();

        List<String> allowedSortProperties = new ArrayList<String>();
        for (Entry<String, List<Condition>> singlePropertyCondition : propertyConditions.entrySet()) {
            if (entityInformation.getGlobalSecondaryIndexNamesByPropertyName().keySet()
                    .contains(singlePropertyCondition.getKey())) {
                allowedSortProperties.add(singlePropertyCondition.getKey());
            }
        }
        if (allowedSortProperties.size() == 0) {
            allowedSortProperties.add(singlePropertyConditions.getKey());
        }

        for (Entry<String, List<Condition>> singleAttributeConditions : attributeConditions.entrySet()) {
            for (Condition condition : singleAttributeConditions.getValue()) {
                queryExpression.withRangeKeyCondition(singleAttributeConditions.getKey(), condition);
            }
        }

        applySortIfSpecified(queryExpression, allowedSortProperties);
        if (getGlobalSecondaryIndexName() != null) {
            queryExpression.setIndexName(getGlobalSecondaryIndexName());
        }
    } else {
        applySortIfSpecified(queryExpression, Arrays.asList(new String[] { getRangeKeyPropertyName() }));
    }

    return queryExpression;
}