1
1
package org .springframework .data .mongodb .datatables ;
2
2
3
+ import lombok .Getter ;
3
4
import org .springframework .beans .BeanUtils ;
4
5
import org .springframework .data .annotation .Id ;
5
6
import org .springframework .data .domain .Sort ;
33
34
import static org .springframework .util .StringUtils .hasText ;
34
35
35
36
final class DataTablesCriteria <T > {
36
- private Map <String , String > resolvedColumn = new HashMap <>();
37
- private Aggregation aggregation ;
37
+ private final Map <String , String > resolvedColumn = new HashMap <>();
38
+ private final Aggregation aggregation ;
38
39
39
- private Aggregation filteredCountAggregation ;
40
- private int filteredCountAggregationOperationCount ;
40
+ private final Aggregation filteredCountAggregation ;
41
+ @ Getter
42
+ private final int filteredCountAggregationOperationCount ;
41
43
42
- private Fields allClassFields ;
44
+ private final Fields allClassFields ;
43
45
private String originalIdField ;
44
46
45
- private Map <String , DataTablesInput .SearchConfiguration .ColumnSearchConfiguration > columnSearchConfiguration ;
46
- private List <String > excludedColumns ;
47
- private List <String > dateProjectionColumns = new ArrayList <>();
47
+ private final Map <String , DataTablesInput .SearchConfiguration .ColumnSearchConfiguration > columnSearchConfiguration ;
48
+ private final List <String > dateProjectionColumns = new ArrayList <>();
48
49
49
50
DataTablesCriteria (DataTablesInput input , Criteria additionalCriteria , Criteria preFilteringCriteria , Class <T > classType ) {
51
+ List <String > excludedColumns ;
50
52
if (input .getSearchConfiguration () != null ) {
51
53
columnSearchConfiguration = input .getSearchConfiguration ().getColumnSearchConfiguration ();
52
54
excludedColumns = input .getSearchConfiguration ().getExcludedColumns ();
@@ -65,7 +67,7 @@ final class DataTablesCriteria<T> {
65
67
66
68
setDeclaredIdField (classType );
67
69
68
- if (! StringUtils .isEmpty (originalIdField )) {
70
+ if (StringUtils .hasText (originalIdField )) {
69
71
input .getColumn (originalIdField ).ifPresent (c -> {
70
72
71
73
DataTablesInput .SearchConfiguration .ColumnSearchConfiguration searchConfig = columnSearchConfiguration .get (c .getData ());
@@ -127,7 +129,7 @@ final class DataTablesCriteria<T> {
127
129
128
130
aggregation = Aggregation .newAggregation (aggregationOperations );
129
131
130
- if (! StringUtils .isEmpty (originalIdField )) {
132
+ if (StringUtils .hasText (originalIdField )) {
131
133
input .getColumn ("_id" ).ifPresent (c -> {
132
134
c .setData (originalIdField );
133
135
input .setColumns (input .getColumns ());
@@ -259,7 +261,7 @@ private MatchOperation addColumnCriteria(DataTablesInput.Column column) {
259
261
if (column .isSearchable () && hasText (column .getSearch ().getValue ())) {
260
262
List <Criteria > criteria = createCriteria (column , column .getSearch ());
261
263
if (criteria .size () == 1 ) {
262
- return Aggregation .match (criteria .get ( 0 ));
264
+ return Aggregation .match (criteria .getFirst ( ));
263
265
} else if (criteria .size () >= 2 ) {
264
266
return Aggregation .match (new Criteria ().orOperator (criteria .toArray (new Criteria [0 ])));
265
267
}
@@ -337,7 +339,7 @@ private List<AggregationOperation> addSort(DataTablesInput input) {
337
339
.filter (order -> isOrderable (input , order ))
338
340
.map (order -> toOrder (input , order )).collect (toList ());
339
341
340
- if (orders .size () != 0 ) {
342
+ if (! orders .isEmpty () ) {
341
343
operations .add (Aggregation .sort (by (orders )));
342
344
}
343
345
@@ -360,7 +362,7 @@ private boolean isOrderable(DataTablesInput input, DataTablesInput.Order order)
360
362
if (columnSearchConfiguration .containsKey (column .getData ())) {
361
363
DataTablesInput .SearchConfiguration .ColumnSearchConfiguration searchConfig = columnSearchConfiguration .get (column .getData ());
362
364
return isWithinBounds && column .isOrderable ()
363
- && (!searchConfig .isReference () || ! StringUtils .isEmpty (searchConfig .getReferenceOrderColumn ()));
365
+ && (!searchConfig .isReference () || StringUtils .hasText (searchConfig .getReferenceOrderColumn ()));
364
366
} else {
365
367
return isWithinBounds && column .isOrderable ();
366
368
}
@@ -382,16 +384,16 @@ private Sort.Order toOrder(DataTablesInput input, DataTablesInput.Order order) {
382
384
383
385
private String getResolvedRefColumn (DataTablesInput .Column c , List <String > columnStrings ) {
384
386
385
- String resolvedColumn = c .getData ();
387
+ StringBuilder resolvedColumn = new StringBuilder ( c .getData () );
386
388
boolean columnAlreadyExists ;
387
389
388
390
do {
389
- resolvedColumn += "_" ;
390
- String columnName = resolvedColumn ;
391
+ resolvedColumn . append ( "_" ) ;
392
+ String columnName = resolvedColumn . toString () ;
391
393
columnAlreadyExists = columnStrings .stream ().anyMatch (s -> s .startsWith (columnName ));
392
394
} while (columnAlreadyExists );
393
395
394
- return resolvedColumn ;
396
+ return resolvedColumn . toString () ;
395
397
}
396
398
397
399
public Aggregation toAggregation () {
@@ -402,10 +404,6 @@ public Aggregation toFilteredCountAggregation() {
402
404
return filteredCountAggregation ;
403
405
}
404
406
405
- public int getFilteredCountAggregationOperationCount () {
406
- return filteredCountAggregationOperationCount ;
407
- }
408
-
409
407
private AggregationOperation createFieldProjection (DataTablesInput input ) {
410
408
List <String > columnStrings = input .getColumns ().stream ()
411
409
.map (column -> column .getData ().contains ("." ) ? column .getData ().substring (0 , column .getData ().indexOf ("." )) : column .getData ())
@@ -417,7 +415,7 @@ private AggregationOperation createFieldProjection(DataTablesInput input) {
417
415
418
416
/**
419
417
* Use official getFields method to list all fields of the class.
420
- * Source: https://github.com/spring-projects/spring-data-mongodb/blob/1a5de2e1db939f7b35579f11815894fd637fc227/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationOperationContext.java#L88
418
+ * Source: <a href=" https://github.com/spring-projects/spring-data-mongodb/blob/1a5de2e1db939f7b35579f11815894fd637fc227/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationOperationContext.java#L88">...</a>
421
419
*
422
420
* @return Class fields
423
421
*/
0 commit comments