|
| 1 | +/* |
| 2 | + * Copyright 2017 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; |
| 17 | + |
| 18 | +import lombok.AccessLevel; |
| 19 | +import lombok.NonNull; |
| 20 | +import lombok.RequiredArgsConstructor; |
| 21 | +import lombok.experimental.FieldDefaults; |
| 22 | +import reactor.core.publisher.Flux; |
| 23 | + |
| 24 | +import org.springframework.data.mongodb.core.aggregation.Aggregation; |
| 25 | +import org.springframework.data.mongodb.core.aggregation.TypedAggregation; |
| 26 | +import org.springframework.util.Assert; |
| 27 | +import org.springframework.util.StringUtils; |
| 28 | + |
| 29 | +/** |
| 30 | + * Implementation of {@link ExecutableAggregationOperation} operating directly on {@link ReactiveMongoTemplate}. |
| 31 | + * |
| 32 | + * @author Mark Paluch |
| 33 | + * @since 2.0 |
| 34 | + */ |
| 35 | +class ReactiveAggregationOperationSupport implements ReactiveAggregationOperation { |
| 36 | + |
| 37 | + private final ReactiveMongoTemplate template; |
| 38 | + |
| 39 | + /** |
| 40 | + * Create new instance of {@link ReactiveAggregationOperationSupport}. |
| 41 | + * |
| 42 | + * @param template must not be {@literal null}. |
| 43 | + * @throws IllegalArgumentException if template is {@literal null}. |
| 44 | + */ |
| 45 | + ReactiveAggregationOperationSupport(ReactiveMongoTemplate template) { |
| 46 | + |
| 47 | + Assert.notNull(template, "Template must not be null!"); |
| 48 | + |
| 49 | + this.template = template; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public <T> ReactiveAggregation<T> aggregateAndReturn(Class<T> domainType) { |
| 54 | + |
| 55 | + Assert.notNull(domainType, "DomainType must not be null!"); |
| 56 | + |
| 57 | + return new ReactiveAggregationSupport<>(template, domainType, null, null); |
| 58 | + } |
| 59 | + |
| 60 | + @RequiredArgsConstructor |
| 61 | + @FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) |
| 62 | + static class ReactiveAggregationSupport<T> |
| 63 | + implements AggregationOperationWithAggregation<T>, ReactiveAggregation<T>, TerminatingAggregationOperation<T> { |
| 64 | + |
| 65 | + @NonNull ReactiveMongoTemplate template; |
| 66 | + @NonNull Class<T> domainType; |
| 67 | + Aggregation aggregation; |
| 68 | + String collection; |
| 69 | + |
| 70 | + @Override |
| 71 | + public AggregationOperationWithAggregation<T> inCollection(String collection) { |
| 72 | + |
| 73 | + Assert.hasText(collection, "Collection must not be null nor empty!"); |
| 74 | + |
| 75 | + return new ReactiveAggregationSupport<>(template, domainType, aggregation, collection); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public TerminatingAggregationOperation<T> by(Aggregation aggregation) { |
| 80 | + |
| 81 | + Assert.notNull(aggregation, "Aggregation must not be null!"); |
| 82 | + |
| 83 | + return new ReactiveAggregationSupport<>(template, domainType, aggregation, collection); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Flux<T> all() { |
| 88 | + return template.aggregate(aggregation, getCollectionName(aggregation), domainType); |
| 89 | + } |
| 90 | + |
| 91 | + private String getCollectionName(Aggregation aggregation) { |
| 92 | + |
| 93 | + if (StringUtils.hasText(collection)) { |
| 94 | + return collection; |
| 95 | + } |
| 96 | + |
| 97 | + if (aggregation instanceof TypedAggregation) { |
| 98 | + |
| 99 | + TypedAggregation<?> typedAggregation = (TypedAggregation<?>) aggregation; |
| 100 | + |
| 101 | + if (typedAggregation.getInputType() != null) { |
| 102 | + return template.determineCollectionName(typedAggregation.getInputType()); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return template.determineCollectionName(domainType); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments