|
| 1 | +/* |
| 2 | + * Copyright 2011-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.neo4j.repository.query; |
| 17 | + |
| 18 | +import reactor.core.publisher.Flux; |
| 19 | +import reactor.core.publisher.Mono; |
| 20 | + |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.function.Function; |
| 23 | + |
| 24 | +import org.apiguardian.api.API; |
| 25 | +import org.neo4j.cypherdsl.core.Cypher; |
| 26 | +import org.springframework.data.domain.Page; |
| 27 | +import org.springframework.data.domain.Pageable; |
| 28 | +import org.springframework.data.domain.Sort; |
| 29 | +import org.springframework.data.neo4j.core.ReactiveFluentFindOperation; |
| 30 | +import org.springframework.data.neo4j.core.mapping.Neo4jPersistentEntity; |
| 31 | +import org.springframework.data.repository.query.FluentQuery.ReactiveFluentQuery; |
| 32 | +import org.springframework.data.support.PageableExecutionUtils; |
| 33 | +import org.springframework.lang.Nullable; |
| 34 | + |
| 35 | +import com.querydsl.core.types.Predicate; |
| 36 | + |
| 37 | +/** |
| 38 | + * Immutable implementation of a {@link ReactiveFluentQuery}. All |
| 39 | + * methods that return a {@link ReactiveFluentQuery} return a new instance, the original instance won't be |
| 40 | + * modified. |
| 41 | + * |
| 42 | + * @author Michael J. Simons |
| 43 | + * @param <S> Source type |
| 44 | + * @param <R> Result type |
| 45 | + * @since 6.2 |
| 46 | + */ |
| 47 | +@API(status = API.Status.INTERNAL, since = "6.2") final class ReactiveFluentQueryByPredicate<S, R> |
| 48 | + extends FluentQuerySupport<R> implements ReactiveFluentQuery<R> { |
| 49 | + |
| 50 | + private final Predicate predicate; |
| 51 | + |
| 52 | + private final Neo4jPersistentEntity<S> metaData; |
| 53 | + |
| 54 | + private final ReactiveFluentFindOperation findOperation; |
| 55 | + |
| 56 | + private final Function<Predicate, Mono<Long>> countOperation; |
| 57 | + |
| 58 | + private final Function<Predicate, Mono<Boolean>> existsOperation; |
| 59 | + |
| 60 | + ReactiveFluentQueryByPredicate( |
| 61 | + Predicate predicate, |
| 62 | + Neo4jPersistentEntity<S> metaData, |
| 63 | + Class<R> resultType, |
| 64 | + ReactiveFluentFindOperation findOperation, |
| 65 | + Function<Predicate, Mono<Long>> countOperation, |
| 66 | + Function<Predicate, Mono<Boolean>> existsOperation |
| 67 | + ) { |
| 68 | + this(predicate, metaData, resultType, findOperation, countOperation, existsOperation, Sort.unsorted(), null); |
| 69 | + } |
| 70 | + |
| 71 | + ReactiveFluentQueryByPredicate( |
| 72 | + Predicate predicate, |
| 73 | + Neo4jPersistentEntity<S> metaData, |
| 74 | + Class<R> resultType, |
| 75 | + ReactiveFluentFindOperation findOperation, |
| 76 | + Function<Predicate, Mono<Long>> countOperation, |
| 77 | + Function<Predicate, Mono<Boolean>> existsOperation, |
| 78 | + Sort sort, |
| 79 | + @Nullable Collection<String> properties |
| 80 | + ) { |
| 81 | + super(resultType, sort, properties); |
| 82 | + this.predicate = predicate; |
| 83 | + this.metaData = metaData; |
| 84 | + this.findOperation = findOperation; |
| 85 | + this.countOperation = countOperation; |
| 86 | + this.existsOperation = existsOperation; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + @SuppressWarnings("HiddenField") |
| 91 | + public ReactiveFluentQuery<R> sortBy(Sort sort) { |
| 92 | + |
| 93 | + return new ReactiveFluentQueryByPredicate<>(this.predicate, this.metaData, this.resultType, this.findOperation, |
| 94 | + this.countOperation, this.existsOperation, this.sort.and(sort), this.properties); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + @SuppressWarnings("HiddenField") |
| 99 | + public <NR> ReactiveFluentQuery<NR> as(Class<NR> resultType) { |
| 100 | + |
| 101 | + return new ReactiveFluentQueryByPredicate<>(this.predicate, this.metaData, resultType, this.findOperation, |
| 102 | + this.countOperation, this.existsOperation); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + @SuppressWarnings("HiddenField") |
| 107 | + public ReactiveFluentQuery<R> project(Collection<String> properties) { |
| 108 | + |
| 109 | + return new ReactiveFluentQueryByPredicate<>(this.predicate, this.metaData, resultType, this.findOperation, |
| 110 | + this.countOperation, this.existsOperation, sort, mergeProperties(properties)); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public Mono<R> one() { |
| 115 | + |
| 116 | + return findOperation.find(metaData.getType()) |
| 117 | + .as(resultType) |
| 118 | + .matching( |
| 119 | + QueryFragmentsAndParameters.forCondition(metaData, |
| 120 | + Cypher.adapt(predicate).asCondition(), |
| 121 | + null, |
| 122 | + CypherAdapterUtils.toSortItems(this.metaData, sort), |
| 123 | + createIncludedFieldsPredicate())) |
| 124 | + .one(); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public Mono<R> first() { |
| 129 | + |
| 130 | + return all().take(1).singleOrEmpty(); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public Flux<R> all() { |
| 135 | + |
| 136 | + return findOperation.find(metaData.getType()) |
| 137 | + .as(resultType) |
| 138 | + .matching( |
| 139 | + QueryFragmentsAndParameters.forCondition(metaData, |
| 140 | + Cypher.adapt(predicate).asCondition(), |
| 141 | + null, |
| 142 | + CypherAdapterUtils.toSortItems(this.metaData, sort), |
| 143 | + createIncludedFieldsPredicate())) |
| 144 | + .all(); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public Mono<Page<R>> page(Pageable pageable) { |
| 149 | + |
| 150 | + Flux<R> results = findOperation.find(metaData.getType()) |
| 151 | + .as(resultType) |
| 152 | + .matching( |
| 153 | + QueryFragmentsAndParameters.forCondition(metaData, |
| 154 | + Cypher.adapt(predicate).asCondition(), |
| 155 | + pageable, null, |
| 156 | + createIncludedFieldsPredicate())) |
| 157 | + .all(); |
| 158 | + |
| 159 | + return results.collectList().zipWith(countOperation.apply(predicate)).map(tuple -> { |
| 160 | + Page<R> page = PageableExecutionUtils.getPage(tuple.getT1(), pageable, () -> tuple.getT2()); |
| 161 | + return page; |
| 162 | + }); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public Mono<Long> count() { |
| 167 | + return countOperation.apply(predicate); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public Mono<Boolean> exists() { |
| 172 | + return existsOperation.apply(predicate); |
| 173 | + } |
| 174 | +} |
0 commit comments