Search in sources :

Example 1 with ParameterBindingContext

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);
}
Also used : SpELExpressionEvaluator(org.springframework.data.mapping.model.SpELExpressionEvaluator) ExpressionDependencies(org.springframework.data.spel.ExpressionDependencies) ParameterBindingContext(org.springframework.data.mongodb.util.json.ParameterBindingContext)

Example 2 with ParameterBindingContext

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()));
}
Also used : Locale(java.util.Locale) Matcher(java.util.regex.Matcher) ParameterBindingContext(org.springframework.data.mongodb.util.json.ParameterBindingContext) Document(org.bson.Document) Nullable(org.springframework.lang.Nullable)

Example 3 with ParameterBindingContext

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);
}
Also used : ParameterBindingContext(org.springframework.data.mongodb.util.json.ParameterBindingContext) DBRef(com.mongodb.DBRef) Document(org.bson.Document) ReferenceCollection(org.springframework.data.mongodb.core.convert.ReferenceResolver.ReferenceCollection) DocumentReference(org.springframework.data.mongodb.core.mapping.DocumentReference)

Aggregations

ParameterBindingContext (org.springframework.data.mongodb.util.json.ParameterBindingContext)3 Document (org.bson.Document)2 DBRef (com.mongodb.DBRef)1 Locale (java.util.Locale)1 Matcher (java.util.regex.Matcher)1 SpELExpressionEvaluator (org.springframework.data.mapping.model.SpELExpressionEvaluator)1 ReferenceCollection (org.springframework.data.mongodb.core.convert.ReferenceResolver.ReferenceCollection)1 DocumentReference (org.springframework.data.mongodb.core.mapping.DocumentReference)1 ExpressionDependencies (org.springframework.data.spel.ExpressionDependencies)1 Nullable (org.springframework.lang.Nullable)1