|
| 1 | +/* |
| 2 | + * Copyright 2020 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 | + |
| 17 | +package org.springframework.integration.mongodb.dsl; |
| 18 | + |
| 19 | +import java.util.function.Supplier; |
| 20 | + |
| 21 | +import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory; |
| 22 | +import org.springframework.data.mongodb.core.ReactiveMongoOperations; |
| 23 | +import org.springframework.data.mongodb.core.convert.MongoConverter; |
| 24 | +import org.springframework.data.mongodb.core.query.Query; |
| 25 | +import org.springframework.expression.Expression; |
| 26 | +import org.springframework.expression.common.LiteralExpression; |
| 27 | +import org.springframework.integration.dsl.MessageSourceSpec; |
| 28 | +import org.springframework.integration.expression.SupplierExpression; |
| 29 | +import org.springframework.integration.mongodb.inbound.ReactiveMongoDbMessageSource; |
| 30 | + |
| 31 | +/** |
| 32 | + * A {@link MessageSourceSpec} implementation for a {@link ReactiveMongoDbMessageSource}. |
| 33 | + * |
| 34 | + * @author Artem Bilan |
| 35 | + * |
| 36 | + * @since 5.3 |
| 37 | + */ |
| 38 | +public class ReactiveMongoDbMessageSourceSpec |
| 39 | + extends MessageSourceSpec<ReactiveMongoDbMessageSourceSpec, ReactiveMongoDbMessageSource> { |
| 40 | + |
| 41 | + ReactiveMongoDbMessageSourceSpec(ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory, |
| 42 | + Expression queryExpression) { |
| 43 | + this.target = new ReactiveMongoDbMessageSource(reactiveMongoDatabaseFactory, queryExpression); |
| 44 | + } |
| 45 | + |
| 46 | + ReactiveMongoDbMessageSourceSpec(ReactiveMongoOperations reactiveMongoTemplate, Expression queryExpression) { |
| 47 | + this.target = new ReactiveMongoDbMessageSource(reactiveMongoTemplate, queryExpression); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Allow you to set the type of the entityClass that will be passed to the |
| 52 | + * {@link ReactiveMongoOperations#find(Query, Class)} or {@link ReactiveMongoOperations#findOne(Query, Class)} |
| 53 | + * method. |
| 54 | + * Default is {@link com.mongodb.DBObject}. |
| 55 | + * @param entityClass The entity class. |
| 56 | + * @return the spec |
| 57 | + * @see ReactiveMongoDbMessageSource#setEntityClass(Class) |
| 58 | + */ |
| 59 | + public ReactiveMongoDbMessageSourceSpec entityClass(Class<?> entityClass) { |
| 60 | + this.target.setEntityClass(entityClass); |
| 61 | + return this; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Allow you to manage which find* method to invoke on {@link ReactiveMongoOperations}. |
| 66 | + * @param expectSingleResult true if a single result is expected. |
| 67 | + * @return the spec |
| 68 | + * @see ReactiveMongoDbMessageSource#setExpectSingleResult(boolean) |
| 69 | + */ |
| 70 | + public ReactiveMongoDbMessageSourceSpec expectSingleResult(boolean expectSingleResult) { |
| 71 | + this.target.setExpectSingleResult(expectSingleResult); |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Configure a collection name to query against. |
| 77 | + * @param collectionName the name of the MongoDb collection |
| 78 | + * @return the spec |
| 79 | + */ |
| 80 | + public ReactiveMongoDbMessageSourceSpec collectionName(String collectionName) { |
| 81 | + return collectionNameExpression(new LiteralExpression(collectionName)); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Configure a SpEL expression to evaluation a collection name on each {@code receive()} call. |
| 86 | + * @param collectionNameExpression the SpEL expression for name of the MongoDb collection |
| 87 | + * @return the spec |
| 88 | + */ |
| 89 | + public ReactiveMongoDbMessageSourceSpec collectionNameExpression(String collectionNameExpression) { |
| 90 | + return collectionNameExpression(PARSER.parseExpression(collectionNameExpression)); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Configure a {@link Supplier} to obtain a collection name on each {@code receive()} call. |
| 95 | + * @param collectionNameSupplier the {@link Supplier} for name of the MongoDb collection |
| 96 | + * @return the spec |
| 97 | + */ |
| 98 | + public ReactiveMongoDbMessageSourceSpec collectionNameSupplier(Supplier<String> collectionNameSupplier) { |
| 99 | + return collectionNameExpression(new SupplierExpression<>(collectionNameSupplier)); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Configure a SpEL expression to evaluation a collection name on each {@code receive()} call. |
| 104 | + * @param collectionNameExpression the SpEL expression for name of the MongoDb collection |
| 105 | + * @return the spec |
| 106 | + * @see ReactiveMongoDbMessageSource#setCollectionNameExpression(Expression) |
| 107 | + */ |
| 108 | + public ReactiveMongoDbMessageSourceSpec collectionNameExpression(Expression collectionNameExpression) { |
| 109 | + this.target.setCollectionNameExpression(collectionNameExpression); |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Configure a custom {@link MongoConverter} used to assist in deserialization |
| 115 | + * data read from MongoDb. Only allowed if this instance was constructed with a |
| 116 | + * {@link ReactiveMongoDatabaseFactory}. |
| 117 | + * @param mongoConverter The mongo converter. |
| 118 | + * @return the spec |
| 119 | + * @see ReactiveMongoDbMessageSource#setMongoConverter(MongoConverter) |
| 120 | + */ |
| 121 | + public ReactiveMongoDbMessageSourceSpec mongoConverter(MongoConverter mongoConverter) { |
| 122 | + this.target.setMongoConverter(mongoConverter); |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments