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