|
| 1 | +/* |
| 2 | + * Copyright 2021 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 | + * https://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; |
| 17 | + |
| 18 | +import org.bson.Document; |
| 19 | +import org.bson.codecs.DocumentCodec; |
| 20 | +import org.bson.codecs.configuration.CodecRegistry; |
| 21 | +import org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec; |
| 22 | +import org.springframework.data.util.Lazy; |
| 23 | +import org.springframework.lang.Nullable; |
| 24 | +import org.springframework.util.ObjectUtils; |
| 25 | +import org.springframework.util.StringUtils; |
| 26 | + |
| 27 | +/** |
| 28 | + * A {@link MongoExpression} using the {@link ParameterBindingDocumentCodec} for parsing a raw ({@literal json}) |
| 29 | + * expression. The expression will be wrapped within <code>{ ... }</code> if necessary. The actual parsing and parameter |
| 30 | + * binding of placeholders like {@code ?0} is delayed upon first call on the the target {@link Document} via |
| 31 | + * {@link #toDocument()}. |
| 32 | + * <p /> |
| 33 | + * |
| 34 | + * <pre class="code"> |
| 35 | + * $toUpper : $name -> { '$toUpper' : '$name' } |
| 36 | + * |
| 37 | + * { '$toUpper' : '$name' } -> { '$toUpper' : '$name' } |
| 38 | + * |
| 39 | + * { '$toUpper' : '?0' }, "$name" -> { '$toUpper' : '$name' } |
| 40 | + * </pre> |
| 41 | + * |
| 42 | + * Some types might require a special {@link org.bson.codecs.Codec}. If so, make sure to provide a {@link CodecRegistry} |
| 43 | + * containing the required {@link org.bson.codecs.Codec codec} via {@link #withCodecRegistry(CodecRegistry)}. |
| 44 | + * |
| 45 | + * @author Christoph Strobl |
| 46 | + * @since 3.2 |
| 47 | + */ |
| 48 | +public class BindableMongoExpression implements MongoExpression { |
| 49 | + |
| 50 | + private final String expressionString; |
| 51 | + |
| 52 | + @Nullable // |
| 53 | + private final CodecRegistryProvider codecRegistryProvider; |
| 54 | + |
| 55 | + @Nullable // |
| 56 | + private final Object[] args; |
| 57 | + |
| 58 | + private final Lazy<Document> target; |
| 59 | + |
| 60 | + /** |
| 61 | + * Create a new instance of {@link BindableMongoExpression}. |
| 62 | + * |
| 63 | + * @param expression must not be {@literal null}. |
| 64 | + * @param args can be {@literal null}. |
| 65 | + */ |
| 66 | + public BindableMongoExpression(String expression, @Nullable Object[] args) { |
| 67 | + this(expression, null, args); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Create a new instance of {@link BindableMongoExpression}. |
| 72 | + * |
| 73 | + * @param expression must not be {@literal null}. |
| 74 | + * @param codecRegistryProvider can be {@literal null}. |
| 75 | + * @param args can be {@literal null}. |
| 76 | + */ |
| 77 | + public BindableMongoExpression(String expression, @Nullable CodecRegistryProvider codecRegistryProvider, |
| 78 | + @Nullable Object[] args) { |
| 79 | + |
| 80 | + this.expressionString = expression; |
| 81 | + this.codecRegistryProvider = codecRegistryProvider; |
| 82 | + this.args = args; |
| 83 | + this.target = Lazy.of(this::parse); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Provide the {@link CodecRegistry} used to convert expressions. |
| 88 | + * |
| 89 | + * @param codecRegistry must not be {@literal null}. |
| 90 | + * @return new instance of {@link BindableMongoExpression}. |
| 91 | + */ |
| 92 | + public BindableMongoExpression withCodecRegistry(CodecRegistry codecRegistry) { |
| 93 | + return new BindableMongoExpression(expressionString, () -> codecRegistry, args); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Provide the arguments to bind to the placeholders via their index. |
| 98 | + * |
| 99 | + * @param args must not be {@literal null}. |
| 100 | + * @return new instance of {@link BindableMongoExpression}. |
| 101 | + */ |
| 102 | + public BindableMongoExpression bind(Object... args) { |
| 103 | + return new BindableMongoExpression(expressionString, codecRegistryProvider, args); |
| 104 | + } |
| 105 | + |
| 106 | + /* |
| 107 | + * (non-Javadoc) |
| 108 | + * @see org.springframework.data.mongodb.MongoExpression#toDocument() |
| 109 | + */ |
| 110 | + @Override |
| 111 | + public Document toDocument() { |
| 112 | + return target.get(); |
| 113 | + } |
| 114 | + |
| 115 | + /* |
| 116 | + * (non-Javadoc) |
| 117 | + * @see java.lang.Object#toString() |
| 118 | + */ |
| 119 | + @Override |
| 120 | + public String toString() { |
| 121 | + return "BindableMongoExpression{" + "expressionString='" + expressionString + '\'' + ", args=" + args + '}'; |
| 122 | + } |
| 123 | + |
| 124 | + private String wrapJsonIfNecessary(String json) { |
| 125 | + |
| 126 | + if (StringUtils.hasText(json) && (json.startsWith("{") && json.endsWith("}"))) { |
| 127 | + return json; |
| 128 | + } |
| 129 | + |
| 130 | + return "{" + json + "}"; |
| 131 | + } |
| 132 | + |
| 133 | + private Document parse() { |
| 134 | + |
| 135 | + String expression = wrapJsonIfNecessary(expressionString); |
| 136 | + |
| 137 | + if (ObjectUtils.isEmpty(args)) { |
| 138 | + |
| 139 | + if (codecRegistryProvider == null) { |
| 140 | + return Document.parse(expression); |
| 141 | + } |
| 142 | + |
| 143 | + return Document.parse(expression, codecRegistryProvider.getCodecFor(Document.class) |
| 144 | + .orElseGet(() -> new DocumentCodec(codecRegistryProvider.getCodecRegistry()))); |
| 145 | + } |
| 146 | + |
| 147 | + ParameterBindingDocumentCodec codec = codecRegistryProvider == null ? new ParameterBindingDocumentCodec() |
| 148 | + : new ParameterBindingDocumentCodec(codecRegistryProvider.getCodecRegistry()); |
| 149 | + return codec.decode(expression, args); |
| 150 | + } |
| 151 | +} |
0 commit comments