Example usage for com.amazonaws.services.dynamodbv2.model ComparisonOperator NOT_NULL

List of usage examples for com.amazonaws.services.dynamodbv2.model ComparisonOperator NOT_NULL

Introduction

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

Prototype

ComparisonOperator NOT_NULL

To view the source code for com.amazonaws.services.dynamodbv2.model ComparisonOperator NOT_NULL.

Click Source Link

Usage

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

License:Apache License

private <T extends Item> Collection<T> executeQuery(final AttributeQuery query, final Class<T> itemClass) {
    final ItemConfiguration itemConfiguration = getItemConfiguration(itemClass);
    final com.amazonaws.services.dynamodbv2.model.Condition condition = new com.amazonaws.services.dynamodbv2.model.Condition();

    if (query.getCondition().getComparisonOperator() == Operators.NULL) {
        condition.setComparisonOperator(ComparisonOperator.NULL);
    } else if (query.getCondition().getComparisonOperator() == Operators.NOT_NULL) {
        condition.setComparisonOperator(ComparisonOperator.NOT_NULL);
    } else {/*w w  w . java2  s  .  co m*/
        if (query.getCondition().getComparisonOperator() == Operators.EQUALS) {
            condition.setComparisonOperator(ComparisonOperator.EQ);
        } else if (query.getCondition().getComparisonOperator() == Operators.LESS_THAN_OR_EQUALS) {
            condition.setComparisonOperator(ComparisonOperator.LE);
        } else if (query.getCondition().getComparisonOperator() == Operators.GREATER_THAN_OR_EQUALS) {
            condition.setComparisonOperator(ComparisonOperator.GE);
        }

        final Collection<AttributeValue> attributeValueList = new ArrayList<>();

        for (final String value : query.getCondition().getValues()) {
            if (value != null && !value.isEmpty()) {
                attributeValueList.add(new AttributeValue(value));
            }
        }

        if (attributeValueList.size() == 0) {
            return new ArrayList<>();
        }

        condition.setAttributeValueList(attributeValueList);
    }

    final Map<String, com.amazonaws.services.dynamodbv2.model.Condition> conditions = new HashMap<>();
    conditions.put(query.getAttributeName(), condition);
    final List<T> totalItems = new ArrayList<>();
    Map<String, AttributeValue> lastEvaluatedKey = null;
    final String tableName = databaseSchemaHolder.schemaName() + "." + itemConfiguration.tableName();
    if (itemConfiguration.hasIndexOn(query.getAttributeName())) {
        do {
            final String queryAttributeName = query.getAttributeName();
            final PrimaryKeyDefinition primaryKeyDefinition = itemConfiguration.primaryKeyDefinition();
            final String primaryKeyPropertyName = primaryKeyDefinition.propertyName();
            final boolean isPrimaryKeyQuery = queryAttributeName.equals(primaryKeyPropertyName);
            final QueryRequest queryRequest = new QueryRequest().withTableName(tableName)
                    .withKeyConditions(conditions).withExclusiveStartKey(lastEvaluatedKey);
            if (!isPrimaryKeyQuery) {
                queryRequest.withIndexName(queryAttributeName + "_idx");
            }

            final QueryResult queryResult;
            try {
                queryResult = amazonDynamoDbClient.query(queryRequest);
            } catch (final AmazonServiceException e) {
                throw new PersistenceResourceFailureException("Failure while attempting DynamoDb Query", e);
            }
            totalItems.addAll(marshallIntoObjects(itemClass, queryResult.getItems()));
            lastEvaluatedKey = queryResult.getLastEvaluatedKey();
        } while (lastEvaluatedKey != null);

    } else {
        logger.debug("Performing table scan with query: " + query);
        do {
            final ScanRequest scanRequest = new ScanRequest().withTableName(tableName)
                    .withScanFilter(conditions).withExclusiveStartKey(lastEvaluatedKey);
            final ScanResult scanResult;
            try {
                scanResult = amazonDynamoDbClient.scan(scanRequest);
            } catch (final AmazonServiceException e) {
                throw new PersistenceResourceFailureException("Failure while attempting DynamoDb Scan", e);
            }
            totalItems.addAll(marshallIntoObjects(itemClass, scanResult.getItems()));
            lastEvaluatedKey = scanResult.getLastEvaluatedKey();
        } while (lastEvaluatedKey != null);
    }

    return totalItems;
}

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

License:Apache License

protected DynamoDBQueryCriteria<T, ID> addCriteria(DynamoDBQueryCriteria<T, ID> criteria, Part part,
        Iterator<Object> iterator) {
    if (part.shouldIgnoreCase().equals(IgnoreCaseType.ALWAYS))
        throw new UnsupportedOperationException("Case insensitivity not supported");

    Class<?> leafNodePropertyType = part.getProperty().getLeafProperty().getType();

    PropertyPath leafNodePropertyPath = part.getProperty().getLeafProperty();
    String leafNodePropertyName = leafNodePropertyPath.toDotPath();
    if (leafNodePropertyName.indexOf(".") != -1) {
        int index = leafNodePropertyName.lastIndexOf(".");
        leafNodePropertyName = leafNodePropertyName.substring(index);
    }//from w w  w . ja  va 2  s  . co  m

    switch (part.getType()) {

    case IN:
        Object in = iterator.next();
        Assert.notNull(in, "Creating conditions on null parameters not supported: please specify a value for '"
                + leafNodePropertyName + "'");
        boolean isIterable = ClassUtils.isAssignable(Iterable.class, in.getClass());
        boolean isArray = ObjectUtils.isArray(in);
        Assert.isTrue(isIterable || isArray, "In criteria can only operate with Iterable or Array parameters");
        Iterable<?> iterable = isIterable ? ((Iterable<?>) in) : Arrays.asList(ObjectUtils.toObjectArray(in));
        return criteria.withPropertyIn(leafNodePropertyName, iterable, leafNodePropertyType);
    case CONTAINING:
        return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.CONTAINS,
                iterator.next(), leafNodePropertyType);
    case STARTING_WITH:
        return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.BEGINS_WITH,
                iterator.next(), leafNodePropertyType);
    case BETWEEN:
        Object first = iterator.next();
        Object second = iterator.next();
        return criteria.withPropertyBetween(leafNodePropertyName, first, second, leafNodePropertyType);
    case AFTER:
    case GREATER_THAN:
        return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.GT, iterator.next(),
                leafNodePropertyType);
    case BEFORE:
    case LESS_THAN:
        return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.LT, iterator.next(),
                leafNodePropertyType);
    case GREATER_THAN_EQUAL:
        return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.GE, iterator.next(),
                leafNodePropertyType);
    case LESS_THAN_EQUAL:
        return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.LE, iterator.next(),
                leafNodePropertyType);
    case IS_NULL:
        return criteria.withNoValuedCriteria(leafNodePropertyName, ComparisonOperator.NULL);
    case IS_NOT_NULL:
        return criteria.withNoValuedCriteria(leafNodePropertyName, ComparisonOperator.NOT_NULL);
    case TRUE:
        return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.EQ, Boolean.TRUE,
                leafNodePropertyType);
    case FALSE:
        return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.EQ, Boolean.FALSE,
                leafNodePropertyType);
    case SIMPLE_PROPERTY:
        return criteria.withPropertyEquals(leafNodePropertyName, iterator.next(), leafNodePropertyType);
    case NEGATING_SIMPLE_PROPERTY:
        return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.NE, iterator.next(),
                leafNodePropertyType);
    default:
        throw new IllegalArgumentException("Unsupported keyword " + part.getType());
    }

}

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

License:Apache License

protected DynamoDBQueryCriteria<T, ID> addCriteria(DynamoDBQueryCriteria<T, ID> criteria, Part part,
        Iterator<Object> iterator) {
    if (part.shouldIgnoreCase().equals(IgnoreCaseType.ALWAYS))
        throw new UnsupportedOperationException("Case insensitivity not supported");

    Class<?> propertyType = part.getProperty().getType();

    switch (part.getType()) {
    case IN:/*from w w  w .j ava 2  s .  co m*/
        Object in = iterator.next();
        Assert.notNull(in, "Creating conditions on null parameters not supported: please specify a value for '"
                + part.getProperty().getSegment() + "'");
        boolean isIterable = ClassUtils.isAssignable(in.getClass(), Iterable.class);
        boolean isArray = ObjectUtils.isArray(in);
        Assert.isTrue(isIterable || isArray, "In criteria can only operate with Iterable or Array parameters");
        Iterable<?> iterable = isIterable ? ((Iterable<?>) in) : Arrays.asList(ObjectUtils.toObjectArray(in));
        return criteria.withPropertyIn(part.getProperty().getSegment(), iterable, propertyType);
    case CONTAINING:
        return criteria.withSingleValueCriteria(part.getProperty().getSegment(), ComparisonOperator.CONTAINS,
                iterator.next(), propertyType);
    case STARTING_WITH:
        return criteria.withSingleValueCriteria(part.getProperty().getSegment(), ComparisonOperator.BEGINS_WITH,
                iterator.next(), propertyType);
    case BETWEEN:
        Object first = iterator.next();
        Object second = iterator.next();
        return criteria.withPropertyBetween(part.getProperty().getSegment(), first, second, propertyType);
    case AFTER:
    case GREATER_THAN:
        return criteria.withSingleValueCriteria(part.getProperty().getSegment(), ComparisonOperator.GT,
                iterator.next(), propertyType);
    case BEFORE:
    case LESS_THAN:
        return criteria.withSingleValueCriteria(part.getProperty().getSegment(), ComparisonOperator.LT,
                iterator.next(), propertyType);
    case GREATER_THAN_EQUAL:
        return criteria.withSingleValueCriteria(part.getProperty().getSegment(), ComparisonOperator.GE,
                iterator.next(), propertyType);
    case LESS_THAN_EQUAL:
        return criteria.withSingleValueCriteria(part.getProperty().getSegment(), ComparisonOperator.LE,
                iterator.next(), propertyType);
    case IS_NULL:
        return criteria.withNoValuedCriteria(part.getProperty().getSegment(), ComparisonOperator.NULL);
    case IS_NOT_NULL:
        return criteria.withNoValuedCriteria(part.getProperty().getSegment(), ComparisonOperator.NOT_NULL);
    case TRUE:
        return criteria.withSingleValueCriteria(part.getProperty().getSegment(), ComparisonOperator.EQ,
                Boolean.TRUE, propertyType);
    case FALSE:
        return criteria.withSingleValueCriteria(part.getProperty().getSegment(), ComparisonOperator.EQ,
                Boolean.FALSE, propertyType);
    case SIMPLE_PROPERTY:
        return criteria.withPropertyEquals(part.getProperty().getSegment(), iterator.next(), propertyType);
    case NEGATING_SIMPLE_PROPERTY:
        return criteria.withSingleValueCriteria(part.getProperty().getSegment(), ComparisonOperator.NE,
                iterator.next(), propertyType);
    default:
        throw new IllegalArgumentException("Unsupported keyword " + part.getType());
    }

}