3030import org .springframework .beans .factory .BeanClassLoaderAware ;
3131import org .springframework .beans .factory .BeanFactory ;
3232import org .springframework .beans .factory .BeanFactoryAware ;
33- import org .springframework .dao .EmptyResultDataAccessException ;
3433import org .springframework .dao .IncorrectResultSizeDataAccessException ;
3534import org .springframework .data .domain .Example ;
3635import org .springframework .data .domain .Page ;
4645import org .springframework .data .projection .SpelAwareProxyProjectionFactory ;
4746import org .springframework .data .querydsl .QuerydslPredicateExecutor ;
4847import org .springframework .data .repository .query .FluentQuery ;
48+ import org .springframework .data .support .PageableExecutionUtils ;
4949import org .springframework .lang .Nullable ;
5050import org .springframework .ldap .core .LdapOperations ;
5151import org .springframework .ldap .odm .core .ObjectDirectoryMapper ;
@@ -139,45 +139,74 @@ public List<T> findAll(Predicate predicate) {
139139 */
140140 @ Override
141141 public long count (Predicate predicate ) {
142- return findAll (predicate ). size ( );
142+ return findBy (predicate , FluentQuery . FetchableFluentQuery :: count );
143143 }
144144
145145 /* (non-Javadoc)
146146 * @see org.springframework.data.querydsl.QueryDslPredicateExecutor#exists(com.querydsl.core.types.Predicate)
147147 */
148148 public boolean exists (Predicate predicate ) {
149- return count (predicate ) > 0 ;
149+ return findBy (predicate , FluentQuery . FetchableFluentQuery :: exists ) ;
150150 }
151151
152152 /* (non-Javadoc)
153153 * @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.querydsl.core.types.Predicate, org.springframework.data.domain.Sort)
154154 */
155155 public Iterable <T > findAll (Predicate predicate , Sort sort ) {
156- throw new UnsupportedOperationException ();
157- }
158156
157+ Assert .notNull (sort , "Pageable must not be null!" );
158+
159+ if (sort .isUnsorted ()) {
160+ return findAll (predicate );
161+ }
162+
163+ throw new UnsupportedOperationException ("Sorting is not supported" );
164+ }
159165
160166 /* (non-Javadoc)
161167 * @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.querydsl.core.types.OrderSpecifier[])
162168 */
163169 public Iterable <T > findAll (OrderSpecifier <?>... orders ) {
164- throw new UnsupportedOperationException ();
170+
171+ if (orders .length == 0 ) {
172+ return findAll ();
173+ }
174+
175+ throw new UnsupportedOperationException ("Sorting is not supported" );
165176 }
166177
167178 /* (non-Javadoc)
168179 * @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.querydsl.core.types.Predicate, com.querydsl.core.types.OrderSpecifier[])
169180 */
170181 @ Override
171182 public Iterable <T > findAll (Predicate predicate , OrderSpecifier <?>... orders ) {
172- throw new UnsupportedOperationException ();
183+
184+ if (orders .length == 0 ) {
185+ return findAll (predicate );
186+ }
187+
188+ throw new UnsupportedOperationException ("Sorting is not supported" );
173189 }
174190
175191 /* (non-Javadoc)
176192 * @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.querydsl.core.types.Predicate, org.springframework.data.domain.Pageable)
177193 */
178194 @ Override
179195 public Page <T > findAll (Predicate predicate , Pageable pageable ) {
180- throw new UnsupportedOperationException ();
196+
197+ Assert .notNull (pageable , "Pageable must not be null!" );
198+
199+ if (pageable .isUnpaged ()) {
200+ return PageableExecutionUtils .getPage (findAll (predicate ), pageable , () -> count (predicate ));
201+ }
202+
203+ if (pageable .getSort ().isUnsorted () && pageable .getPageNumber () == 0 ) {
204+
205+ return PageableExecutionUtils .getPage (queryFor (predicate , q -> q .countLimit (pageable .getPageSize ())).list (),
206+ pageable , () -> count (predicate ));
207+ }
208+
209+ throw new UnsupportedOperationException ("Pagination and Sorting is not supported" );
181210 }
182211
183212 /*
@@ -189,7 +218,6 @@ public Page<T> findAll(Predicate predicate, Pageable pageable) {
189218 public <S extends T , R > R findBy (Predicate predicate ,
190219 Function <FluentQuery .FetchableFluentQuery <S >, R > queryFunction ) {
191220
192- Assert .notNull (predicate , "Predicate must not be null!" );
193221 Assert .notNull (queryFunction , "Query function must not be null!" );
194222
195223 return queryFunction .apply (new FluentQuerydsl <>(predicate , (Class <S >) entityType ));
@@ -202,6 +230,9 @@ private QuerydslLdapQuery<T> queryFor(Predicate predicate) {
202230 }
203231
204232 private QuerydslLdapQuery <T > queryFor (Predicate predicate , Consumer <LdapQueryBuilder > queryBuilderConsumer ) {
233+
234+ Assert .notNull (predicate , "Predicate must not be null!" );
235+
205236 return new QuerydslLdapQuery <>(ldapOperations , entityType , queryBuilderConsumer ).where (predicate );
206237 }
207238
@@ -281,7 +312,7 @@ public R oneValue() {
281312 }
282313
283314 T one = results .get (0 );
284- return getConversionFunction (entityType , resultType ).apply (one );
315+ return getConversionFunction ().apply (one );
285316 }
286317
287318 /*
@@ -299,9 +330,10 @@ public R firstValue() {
299330 }
300331
301332 T one = results .get (0 );
302- return getConversionFunction (entityType , resultType ).apply (one );
333+ return getConversionFunction ().apply (one );
303334 }
304335
336+
305337 /*
306338 * (non-Javadoc)
307339 * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#all()
@@ -319,7 +351,21 @@ public List<R> all() {
319351 public Page <R > page (Pageable pageable ) {
320352
321353 Assert .notNull (pageable , "Pageable must not be null!" );
322- throw new UnsupportedOperationException ();
354+
355+ if (pageable .isUnpaged ()) {
356+ return PageableExecutionUtils .getPage (all (), pageable , this ::count );
357+ }
358+
359+ if (pageable .getSort ().isUnsorted () && pageable .getPageNumber () == 0 ) {
360+
361+ Function <Object , R > conversionFunction = getConversionFunction ();
362+
363+ return PageableExecutionUtils .getPage (
364+ findTop (pageable .getPageSize ()).stream ().map (conversionFunction ).collect (Collectors .toList ()), pageable ,
365+ this ::count );
366+ }
367+
368+ throw new UnsupportedOperationException ("Pagination and Sorting is not supported" );
323369 }
324370
325371 /*
@@ -329,7 +375,7 @@ public Page<R> page(Pageable pageable) {
329375 @ Override
330376 public Stream <R > stream () {
331377
332- Function <Object , R > conversionFunction = getConversionFunction (entityType , resultType );
378+ Function <Object , R > conversionFunction = getConversionFunction ();
333379
334380 return search (null , QuerydslLdapQuery ::list ).stream ().map (conversionFunction );
335381 }
@@ -390,6 +436,10 @@ private <P> Function<Object, P> getConversionFunction(Class<?> inputType, Class<
390436 return o -> (P ) converter .convert (o );
391437 }
392438
439+ private Function <Object , R > getConversionFunction () {
440+ return getConversionFunction (entityType , resultType );
441+ }
442+
393443 private List <String > getProjection () {
394444
395445 if (projection .isEmpty ()) {
@@ -410,5 +460,7 @@ private List<String> getProjection() {
410460
411461 return projection ;
412462 }
463+
413464 }
465+
414466}
0 commit comments