30
30
import org .springframework .beans .factory .BeanClassLoaderAware ;
31
31
import org .springframework .beans .factory .BeanFactory ;
32
32
import org .springframework .beans .factory .BeanFactoryAware ;
33
- import org .springframework .dao .EmptyResultDataAccessException ;
34
33
import org .springframework .dao .IncorrectResultSizeDataAccessException ;
35
34
import org .springframework .data .domain .Example ;
36
35
import org .springframework .data .domain .Page ;
46
45
import org .springframework .data .projection .SpelAwareProxyProjectionFactory ;
47
46
import org .springframework .data .querydsl .QuerydslPredicateExecutor ;
48
47
import org .springframework .data .repository .query .FluentQuery ;
48
+ import org .springframework .data .support .PageableExecutionUtils ;
49
49
import org .springframework .lang .Nullable ;
50
50
import org .springframework .ldap .core .LdapOperations ;
51
51
import org .springframework .ldap .odm .core .ObjectDirectoryMapper ;
@@ -139,45 +139,74 @@ public List<T> findAll(Predicate predicate) {
139
139
*/
140
140
@ Override
141
141
public long count (Predicate predicate ) {
142
- return findAll (predicate ). size ( );
142
+ return findBy (predicate , FluentQuery . FetchableFluentQuery :: count );
143
143
}
144
144
145
145
/* (non-Javadoc)
146
146
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#exists(com.querydsl.core.types.Predicate)
147
147
*/
148
148
public boolean exists (Predicate predicate ) {
149
- return count (predicate ) > 0 ;
149
+ return findBy (predicate , FluentQuery . FetchableFluentQuery :: exists ) ;
150
150
}
151
151
152
152
/* (non-Javadoc)
153
153
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.querydsl.core.types.Predicate, org.springframework.data.domain.Sort)
154
154
*/
155
155
public Iterable <T > findAll (Predicate predicate , Sort sort ) {
156
- throw new UnsupportedOperationException ();
157
- }
158
156
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
+ }
159
165
160
166
/* (non-Javadoc)
161
167
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.querydsl.core.types.OrderSpecifier[])
162
168
*/
163
169
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" );
165
176
}
166
177
167
178
/* (non-Javadoc)
168
179
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.querydsl.core.types.Predicate, com.querydsl.core.types.OrderSpecifier[])
169
180
*/
170
181
@ Override
171
182
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" );
173
189
}
174
190
175
191
/* (non-Javadoc)
176
192
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.querydsl.core.types.Predicate, org.springframework.data.domain.Pageable)
177
193
*/
178
194
@ Override
179
195
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" );
181
210
}
182
211
183
212
/*
@@ -189,7 +218,6 @@ public Page<T> findAll(Predicate predicate, Pageable pageable) {
189
218
public <S extends T , R > R findBy (Predicate predicate ,
190
219
Function <FluentQuery .FetchableFluentQuery <S >, R > queryFunction ) {
191
220
192
- Assert .notNull (predicate , "Predicate must not be null!" );
193
221
Assert .notNull (queryFunction , "Query function must not be null!" );
194
222
195
223
return queryFunction .apply (new FluentQuerydsl <>(predicate , (Class <S >) entityType ));
@@ -202,6 +230,9 @@ private QuerydslLdapQuery<T> queryFor(Predicate predicate) {
202
230
}
203
231
204
232
private QuerydslLdapQuery <T > queryFor (Predicate predicate , Consumer <LdapQueryBuilder > queryBuilderConsumer ) {
233
+
234
+ Assert .notNull (predicate , "Predicate must not be null!" );
235
+
205
236
return new QuerydslLdapQuery <>(ldapOperations , entityType , queryBuilderConsumer ).where (predicate );
206
237
}
207
238
@@ -281,7 +312,7 @@ public R oneValue() {
281
312
}
282
313
283
314
T one = results .get (0 );
284
- return getConversionFunction (entityType , resultType ).apply (one );
315
+ return getConversionFunction ().apply (one );
285
316
}
286
317
287
318
/*
@@ -299,9 +330,10 @@ public R firstValue() {
299
330
}
300
331
301
332
T one = results .get (0 );
302
- return getConversionFunction (entityType , resultType ).apply (one );
333
+ return getConversionFunction ().apply (one );
303
334
}
304
335
336
+
305
337
/*
306
338
* (non-Javadoc)
307
339
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#all()
@@ -319,7 +351,21 @@ public List<R> all() {
319
351
public Page <R > page (Pageable pageable ) {
320
352
321
353
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" );
323
369
}
324
370
325
371
/*
@@ -329,7 +375,7 @@ public Page<R> page(Pageable pageable) {
329
375
@ Override
330
376
public Stream <R > stream () {
331
377
332
- Function <Object , R > conversionFunction = getConversionFunction (entityType , resultType );
378
+ Function <Object , R > conversionFunction = getConversionFunction ();
333
379
334
380
return search (null , QuerydslLdapQuery ::list ).stream ().map (conversionFunction );
335
381
}
@@ -390,6 +436,10 @@ private <P> Function<Object, P> getConversionFunction(Class<?> inputType, Class<
390
436
return o -> (P ) converter .convert (o );
391
437
}
392
438
439
+ private Function <Object , R > getConversionFunction () {
440
+ return getConversionFunction (entityType , resultType );
441
+ }
442
+
393
443
private List <String > getProjection () {
394
444
395
445
if (projection .isEmpty ()) {
@@ -410,5 +460,7 @@ private List<String> getProjection() {
410
460
411
461
return projection ;
412
462
}
463
+
413
464
}
465
+
414
466
}
0 commit comments