use of software.amazon.awssdk.enhanced.dynamodb.model.UpdateItemEnhancedRequest in project aws-sdk-java-v2 by aws.
the class UpdateItemOperation method generateConditionExpressionIfExist.
/**
* Retrieves the ConditionExpression from extensions if existing, and retrieves the ConditionExpression from the request
* if existing. If both exist, they are merged.
*/
private Expression generateConditionExpressionIfExist(WriteModification transformation, Either<UpdateItemEnhancedRequest<T>, TransactUpdateItemEnhancedRequest<T>> request) {
Expression conditionExpression = null;
if (transformation != null && transformation.additionalConditionalExpression() != null) {
conditionExpression = transformation.additionalConditionalExpression();
}
Expression operationConditionExpression = request.map(r -> Optional.ofNullable(r.conditionExpression()), r -> Optional.ofNullable(r.conditionExpression())).orElse(null);
if (operationConditionExpression != null) {
conditionExpression = operationConditionExpression.and(conditionExpression);
}
return conditionExpression;
}
use of software.amazon.awssdk.enhanced.dynamodb.model.UpdateItemEnhancedRequest in project aws-sdk-java-v2 by aws.
the class UpdateItemOperation method generateRequest.
@Override
public UpdateItemRequest generateRequest(TableSchema<T> tableSchema, OperationContext operationContext, DynamoDbEnhancedClientExtension extension) {
if (!TableMetadata.primaryIndexName().equals(operationContext.indexName())) {
throw new IllegalArgumentException("UpdateItem cannot be executed against a secondary index.");
}
T item = request.map(UpdateItemEnhancedRequest::item, TransactUpdateItemEnhancedRequest::item);
Boolean ignoreNulls = request.map(r -> Optional.ofNullable(r.ignoreNulls()), r -> Optional.ofNullable(r.ignoreNulls())).orElse(null);
Map<String, AttributeValue> itemMap = tableSchema.itemToMap(item, Boolean.TRUE.equals(ignoreNulls));
TableMetadata tableMetadata = tableSchema.tableMetadata();
WriteModification transformation = extension != null ? extension.beforeWrite(DefaultDynamoDbExtensionContext.builder().items(itemMap).operationContext(operationContext).tableMetadata(tableMetadata).tableSchema(tableSchema).operationName(operationName()).build()) : null;
if (transformation != null && transformation.transformedItem() != null) {
itemMap = transformation.transformedItem();
}
Collection<String> primaryKeys = tableSchema.tableMetadata().primaryKeys();
Map<String, AttributeValue> keyAttributes = filterMap(itemMap, entry -> primaryKeys.contains(entry.getKey()));
Map<String, AttributeValue> nonKeyAttributes = filterMap(itemMap, entry -> !primaryKeys.contains(entry.getKey()));
Expression updateExpression = generateUpdateExpressionIfExist(tableMetadata, transformation, nonKeyAttributes);
Expression conditionExpression = generateConditionExpressionIfExist(transformation, request);
Map<String, String> expressionNames = coalesceExpressionNames(updateExpression, conditionExpression);
Map<String, AttributeValue> expressionValues = coalesceExpressionValues(updateExpression, conditionExpression);
UpdateItemRequest.Builder requestBuilder = UpdateItemRequest.builder().tableName(operationContext.tableName()).key(keyAttributes).returnValues(ReturnValue.ALL_NEW);
if (request.left().isPresent()) {
addPlainUpdateItemParameters(requestBuilder, request.left().get());
}
if (updateExpression != null) {
requestBuilder.updateExpression(updateExpression.expression());
}
if (conditionExpression != null) {
requestBuilder.conditionExpression(conditionExpression.expression());
}
if (CollectionUtils.isNotEmpty(expressionNames)) {
requestBuilder = requestBuilder.expressionAttributeNames(expressionNames);
}
if (CollectionUtils.isNotEmpty(expressionValues)) {
requestBuilder = requestBuilder.expressionAttributeValues(expressionValues);
}
return requestBuilder.build();
}
Aggregations