use of org.springframework.data.mongodb.util.json.ParameterBindingContext in project spring-data-mongodb by spring-projects.
the class AbstractMongoQuery method prepareBindingContext.
/**
* Create the {@link ParameterBindingContext binding context} used for SpEL evaluation.
*
* @param source the JSON source.
* @param accessor value provider for parameter binding.
* @return never {@literal null}.
* @since 3.4
*/
protected ParameterBindingContext prepareBindingContext(String source, ConvertingParameterAccessor accessor) {
ExpressionDependencies dependencies = getParameterBindingCodec().captureExpressionDependencies(source, accessor::getBindableValue, expressionParser);
SpELExpressionEvaluator evaluator = getSpELExpressionEvaluatorFor(dependencies, accessor);
return new ParameterBindingContext(accessor::getBindableValue, evaluator);
}
use of org.springframework.data.mongodb.util.json.ParameterBindingContext in project spring-data-mongodb by spring-projects.
the class CollationUtils method computeCollation.
/**
* Compute the {@link Collation} by inspecting the {@link ConvertingParameterAccessor#getCollation() parameter
* accessor} or parsing a potentially given {@literal collationExpression}.
*
* @param collationExpression
* @param accessor
* @param parameters
* @param expressionParser
* @param evaluationContextProvider
* @return can be {@literal null} if neither {@link ConvertingParameterAccessor#getCollation()} nor
* {@literal collationExpression} are present.
*/
@Nullable
static Collation computeCollation(@Nullable String collationExpression, ConvertingParameterAccessor accessor, MongoParameters parameters, ExpressionParser expressionParser, QueryMethodEvaluationContextProvider evaluationContextProvider) {
if (accessor.getCollation() != null) {
return accessor.getCollation();
}
if (!StringUtils.hasText(collationExpression)) {
return null;
}
if (StringUtils.trimLeadingWhitespace(collationExpression).startsWith("{")) {
ParameterBindingContext bindingContext = ParameterBindingContext.forExpressions(accessor::getBindableValue, expressionParser, dependencies -> evaluationContextProvider.getEvaluationContext(parameters, accessor.getValues(), dependencies));
return Collation.from(CODEC.decode(collationExpression, bindingContext));
}
Matcher matcher = PARAMETER_BINDING_PATTERN.matcher(collationExpression);
if (!matcher.find()) {
return Collation.parse(collationExpression);
}
String placeholder = matcher.group();
Object placeholderValue = accessor.getBindableValue(computeParameterIndex(placeholder));
if (collationExpression.startsWith("?")) {
if (placeholderValue instanceof String) {
return Collation.parse(placeholderValue.toString());
}
if (placeholderValue instanceof Locale) {
return Collation.of((Locale) placeholderValue);
}
if (placeholderValue instanceof Document) {
return Collation.from((Document) placeholderValue);
}
throw new IllegalArgumentException(String.format("Collation must be a String, Locale or Document but was %s", ObjectUtils.nullSafeClassName(placeholderValue)));
}
return Collation.parse(collationExpression.replace(placeholder, placeholderValue.toString()));
}
use of org.springframework.data.mongodb.util.json.ParameterBindingContext in project spring-data-mongodb by spring-projects.
the class ReferenceLookupDelegate method computeReferenceContext.
private ReferenceCollection computeReferenceContext(MongoPersistentProperty property, Object value, SpELContext spELContext) {
// Use the first value as a reference for others in case of collection like
if (value instanceof Iterable) {
Iterator<?> iterator = ((Iterable<?>) value).iterator();
value = iterator.hasNext() ? iterator.next() : new Document();
}
// handle DBRef value
if (value instanceof DBRef) {
return ReferenceCollection.fromDBRef((DBRef) value);
}
String collection = mappingContext.getRequiredPersistentEntity(property.getAssociationTargetType()).getCollection();
if (value instanceof Document) {
Document documentPointer = (Document) value;
if (property.isDocumentReference()) {
ParameterBindingContext bindingContext = bindingContext(property, value, spELContext);
DocumentReference documentReference = property.getDocumentReference();
String targetDatabase = parseValueOrGet(documentReference.db(), bindingContext, () -> documentPointer.get("db", String.class));
String targetCollection = parseValueOrGet(documentReference.collection(), bindingContext, () -> documentPointer.get("collection", collection));
return new ReferenceCollection(targetDatabase, targetCollection);
}
return new ReferenceCollection(documentPointer.getString("db"), documentPointer.get("collection", collection));
}
if (property.isDocumentReference()) {
ParameterBindingContext bindingContext = bindingContext(property, value, spELContext);
DocumentReference documentReference = property.getDocumentReference();
String targetDatabase = parseValueOrGet(documentReference.db(), bindingContext, () -> null);
String targetCollection = parseValueOrGet(documentReference.collection(), bindingContext, () -> collection);
return new ReferenceCollection(targetDatabase, targetCollection);
}
return new ReferenceCollection(null, collection);
}
Aggregations