|
| 1 | +/* |
| 2 | + * Copyright © 2024 XDEV Software (https://xdev.software) |
| 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 software.xdev.spring.data.eclipse.store.repository.query.by.example; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.List; |
| 20 | +import java.util.stream.Stream; |
| 21 | + |
| 22 | +import org.springframework.data.domain.Example; |
| 23 | +import org.springframework.data.domain.Page; |
| 24 | +import org.springframework.data.domain.Pageable; |
| 25 | +import org.springframework.data.domain.Sort; |
| 26 | +import org.springframework.data.repository.query.FluentQuery; |
| 27 | + |
| 28 | +import software.xdev.spring.data.eclipse.store.repository.EclipseStoreStorage; |
| 29 | +import software.xdev.spring.data.eclipse.store.repository.query.criteria.CriteriaByExample; |
| 30 | +import software.xdev.spring.data.eclipse.store.repository.query.executors.CountQueryExecutor; |
| 31 | +import software.xdev.spring.data.eclipse.store.repository.query.executors.ExistsQueryExecutor; |
| 32 | +import software.xdev.spring.data.eclipse.store.repository.query.executors.ListQueryExecutor; |
| 33 | +import software.xdev.spring.data.eclipse.store.repository.query.executors.PageableQueryExecutor; |
| 34 | +import software.xdev.spring.data.eclipse.store.repository.query.executors.SingleQueryExecutor; |
| 35 | +import software.xdev.spring.data.eclipse.store.repository.support.copier.working.WorkingCopier; |
| 36 | + |
| 37 | + |
| 38 | +/** |
| 39 | + * Needed to support {@link QueryByExampleExecutor}. |
| 40 | + */ |
| 41 | +public class EclipseStoreFetchableFluentQuery<T, S extends T> implements FluentQuery.FetchableFluentQuery<S> |
| 42 | +{ |
| 43 | + private final WorkingCopier<T> copier; |
| 44 | + private final Example<S> example; |
| 45 | + private final Class<T> domainClass; |
| 46 | + private final EclipseStoreStorage storage; |
| 47 | + private final Sort sort; |
| 48 | + |
| 49 | + public EclipseStoreFetchableFluentQuery( |
| 50 | + final WorkingCopier<T> copier, |
| 51 | + final Example<S> example, |
| 52 | + final Class<T> domainClass, |
| 53 | + final EclipseStoreStorage storage, |
| 54 | + final Sort sort |
| 55 | + ) |
| 56 | + { |
| 57 | + this.copier = copier; |
| 58 | + this.example = example; |
| 59 | + this.domainClass = domainClass; |
| 60 | + this.storage = storage; |
| 61 | + this.sort = sort; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public FetchableFluentQuery<S> sortBy(final Sort sort) |
| 66 | + { |
| 67 | + return new EclipseStoreFetchableFluentQuery( |
| 68 | + this.copier, |
| 69 | + this.example, |
| 70 | + this.domainClass, |
| 71 | + this.storage, |
| 72 | + sort |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public <R> FetchableFluentQuery<R> as(final Class<R> resultType) |
| 78 | + { |
| 79 | + throw new UnsupportedOperationException("The method as() is not yet supported"); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public FetchableFluentQuery<S> project(final Collection<String> properties) |
| 84 | + { |
| 85 | + throw new UnsupportedOperationException("The method project() is not yet supported"); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public S oneValue() |
| 90 | + { |
| 91 | + return this.firstValue(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public S firstValue() |
| 96 | + { |
| 97 | + final SingleQueryExecutor<T> query = |
| 98 | + new SingleQueryExecutor<>(this.copier, new CriteriaByExample<>((Example<T>)this.example), this.sort); |
| 99 | + return this.storage.getReadWriteLock().read( |
| 100 | + () -> |
| 101 | + (S)query.execute( |
| 102 | + this.domainClass, |
| 103 | + this.storage.getEntityList(this.domainClass), |
| 104 | + new Object[]{this.sort}) |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public List<S> all() |
| 110 | + { |
| 111 | + final ListQueryExecutor<T> query = |
| 112 | + new ListQueryExecutor<>(this.copier, new CriteriaByExample<>(this.example)); |
| 113 | + return this.storage.getReadWriteLock().read( |
| 114 | + () -> (List<S>)query.execute(this.domainClass, this.storage.getEntityList(this.domainClass), new Object[]{ |
| 115 | + this.sort}) |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public Page<S> page(final Pageable pageable) |
| 121 | + { |
| 122 | + final PageableQueryExecutor<T> pageableQuery = |
| 123 | + new PageableQueryExecutor<>(this.copier, new CriteriaByExample<>(this.example), this.sort); |
| 124 | + return this.storage.getReadWriteLock().read( |
| 125 | + () -> |
| 126 | + (Page<S>)pageableQuery.execute( |
| 127 | + this.domainClass, |
| 128 | + this.storage.getEntityList(this.domainClass), |
| 129 | + new Object[]{pageable, this.sort}) |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public Stream<S> stream() |
| 135 | + { |
| 136 | + return this.all().stream(); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public long count() |
| 141 | + { |
| 142 | + final CountQueryExecutor<T> query = new CountQueryExecutor<>(new CriteriaByExample<>(this.example)); |
| 143 | + return this.storage.getReadWriteLock().read( |
| 144 | + () -> query.execute(this.domainClass, this.storage.getEntityList(this.domainClass), null) |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public boolean exists() |
| 150 | + { |
| 151 | + final ExistsQueryExecutor<T> query = new ExistsQueryExecutor<>(new CriteriaByExample<>(this.example)); |
| 152 | + return this.storage.getReadWriteLock().read( |
| 153 | + () -> query.execute(this.domainClass, this.storage.getEntityList(this.domainClass), null) |
| 154 | + ); |
| 155 | + } |
| 156 | +} |
0 commit comments