|
| 1 | +/* |
| 2 | + * Copyright 2016. the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.mongodb.core.aggregation; |
| 17 | + |
| 18 | +import org.bson.Document; |
| 19 | +import org.springframework.util.Assert; |
| 20 | + |
| 21 | +/** |
| 22 | + * An {@link AggregationExpression} that renders a MongoDB Aggregation Framework expression from the AST of a |
| 23 | + * <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html">SpEL |
| 24 | + * expression</a>. <br /> |
| 25 | + * <br /> |
| 26 | + * <strong>Samples:</strong> <br /> |
| 27 | + * <code> |
| 28 | + * <pre> |
| 29 | + * // { $and: [ { $gt: [ "$qty", 100 ] }, { $lt: [ "$qty", 250 ] } ] } |
| 30 | + * expressionOf("qty > 100 && qty < 250); |
| 31 | + * |
| 32 | + * // { $cond : { if : { $gte : [ "$a", 42 ]}, then : "answer", else : "no-answer" } } |
| 33 | + * expressionOf("cond(a >= 42, 'answer', 'no-answer')"); |
| 34 | + * </pre> |
| 35 | + * </code> |
| 36 | + * |
| 37 | + * @author Christoph Strobl |
| 38 | + * @see SpelExpressionTransformer |
| 39 | + * @since 1.10 |
| 40 | + */ |
| 41 | +public class AggregationSpELExpression implements AggregationExpression { |
| 42 | + |
| 43 | + private static final SpelExpressionTransformer TRANSFORMER = new SpelExpressionTransformer(); |
| 44 | + private final String rawExpression; |
| 45 | + private final Object[] parameters; |
| 46 | + |
| 47 | + private AggregationSpELExpression(String rawExpression, Object[] parameters) { |
| 48 | + |
| 49 | + this.rawExpression = rawExpression; |
| 50 | + this.parameters = parameters; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Creates new {@link AggregationSpELExpression} for the given {@literal expressionString} and {@literal parameters}. |
| 55 | + * |
| 56 | + * @param expression must not be {@literal null}. |
| 57 | + * @param parameters can be empty. |
| 58 | + * @return |
| 59 | + */ |
| 60 | + public static AggregationSpELExpression expressionOf(String expressionString, Object... parameters) { |
| 61 | + |
| 62 | + Assert.notNull(expressionString, "ExpressionString must not be null!"); |
| 63 | + return new AggregationSpELExpression(expressionString, parameters); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Document toDocument(AggregationOperationContext context) { |
| 68 | + return (Document) TRANSFORMER.transform(rawExpression, context, parameters); |
| 69 | + } |
| 70 | +} |
0 commit comments