|
| 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.RequiredArgsConstructor; |
| 19 | + |
| 20 | +import org.springframework.data.mongodb.core.aggregation.Aggregation; |
| 21 | +import org.springframework.data.mongodb.core.aggregation.AggregationResults; |
| 22 | +import org.springframework.data.mongodb.core.aggregation.TypedAggregation; |
| 23 | +import org.springframework.data.util.CloseableIterator; |
| 24 | +import org.springframework.util.Assert; |
| 25 | +import org.springframework.util.StringUtils; |
| 26 | + |
| 27 | +/** |
| 28 | + * Implementation of {@link ExecutableAggregationOperation} operating directly on {@link MongoTemplate}. |
| 29 | + * |
| 30 | + * @author Christoph Strobl |
| 31 | + * @since 2.0 |
| 32 | + */ |
| 33 | +class ExecutableAggregationOperationSupport implements ExecutableAggregationOperation { |
| 34 | + |
| 35 | + private final MongoTemplate template; |
| 36 | + |
| 37 | + /** |
| 38 | + * Create new instance of ExecutableAggregationOperationSupport. |
| 39 | + * |
| 40 | + * @param template must not be {@literal null}. |
| 41 | + * @throws IllegalArgumentException if template is {@literal null}. |
| 42 | + */ |
| 43 | + ExecutableAggregationOperationSupport(MongoTemplate template) { |
| 44 | + |
| 45 | + Assert.notNull(template, "Template must not be null!"); |
| 46 | + this.template = template; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public <T> AggregationOperation<T> aggregateAndReturn(Class<T> domainType) { |
| 51 | + |
| 52 | + Assert.notNull(domainType, "DomainType must not be null!"); |
| 53 | + return new AggregationOperationSupport<T>(template, null, domainType, null); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param <T> |
| 58 | + * @author Christoph Strobl |
| 59 | + * @since 2.0 |
| 60 | + */ |
| 61 | + @RequiredArgsConstructor |
| 62 | + static class AggregationOperationSupport<T> |
| 63 | + implements AggregationOperationWithAggregation<T>, AggregationOperation<T>, TerminatingAggregationOperation<T> { |
| 64 | + |
| 65 | + private final MongoTemplate template; |
| 66 | + private final Aggregation aggregation; |
| 67 | + private final Class<T> domainType; |
| 68 | + private final 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 | + return new AggregationOperationSupport<T>(template, aggregation, domainType, collection); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public TerminatingAggregationOperation<T> by(Aggregation aggregation) { |
| 79 | + |
| 80 | + Assert.notNull(aggregation, "Aggregation must not be null!"); |
| 81 | + return new AggregationOperationSupport<T>(template, aggregation, domainType, collection); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public AggregationResults<T> get() { |
| 86 | + return template.aggregate(aggregation, getCollectionName(aggregation), domainType); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public CloseableIterator<T> stream() { |
| 91 | + return template.aggregateStream(aggregation, getCollectionName(aggregation), domainType); |
| 92 | + } |
| 93 | + |
| 94 | + private String getCollectionName(Aggregation aggregation) { |
| 95 | + |
| 96 | + if (StringUtils.hasText(collection)) { |
| 97 | + return collection; |
| 98 | + } |
| 99 | + |
| 100 | + if (aggregation instanceof TypedAggregation) { |
| 101 | + |
| 102 | + if (((TypedAggregation<?>) aggregation).getInputType() != null) { |
| 103 | + return template.determineCollectionName(((TypedAggregation<?>) aggregation).getInputType()); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return template.determineCollectionName(domainType); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments