@@ -20,39 +20,35 @@ public Specification<T> build() {
20
20
}
21
21
22
22
private class DataTablesSpecification <S > implements Specification <S > {
23
- protected List <Predicate > columnPredicates = new ArrayList <>();
24
- protected List <Predicate > globalPredicates = new ArrayList <>();
25
-
26
23
@ Override
27
24
public Predicate toPredicate (@ NonNull Root <S > root , @ NonNull CriteriaQuery <?> query , @ NonNull CriteriaBuilder criteriaBuilder ) {
28
- initPredicatesRecursively (query , tree , root , root , criteriaBuilder );
25
+ Predicates predicates = new Predicates ();
26
+ initPredicatesRecursively (predicates , query , tree , root , root , criteriaBuilder );
29
27
30
28
if (input .getSearchPanes () != null ) {
31
29
input .getSearchPanes ().forEach ((attribute , values ) -> {
32
30
if (!values .isEmpty ()) {
33
- columnPredicates .add (root .get (attribute ).in (values ));
31
+ predicates . columns .add (root .get (attribute ).in (values ));
34
32
}
35
33
});
36
34
}
37
35
38
- final Predicate predicate = createFinalPredicate (criteriaBuilder );
39
- columnPredicates .clear ();
40
- return predicate ;
36
+ return predicates .toPredicate (criteriaBuilder );
41
37
}
42
38
43
39
private boolean isCountQuery (CriteriaQuery <?> query ) {
44
40
return query .getResultType () == Long .class ;
45
41
}
46
42
47
- protected void initPredicatesRecursively (CriteriaQuery <?> query , Node <Filter > node , From <S , S > from , FetchParent <S , S > fetch , CriteriaBuilder criteriaBuilder ) {
43
+ protected void initPredicatesRecursively (Predicates predicates , CriteriaQuery <?> query , Node <Filter > node , From <S , S > from , FetchParent <S , S > fetch , CriteriaBuilder criteriaBuilder ) {
48
44
if (node .isLeaf ()) {
49
45
boolean hasColumnFilter = node .getData () != null ;
50
46
if (hasColumnFilter ) {
51
47
Filter columnFilter = node .getData ();
52
- columnPredicates .add (columnFilter .createPredicate (from , criteriaBuilder , node .getName ()));
48
+ predicates . columns .add (columnFilter .createPredicate (from , criteriaBuilder , node .getName ()));
53
49
} else if (hasGlobalFilter ) {
54
50
Filter globalFilter = tree .getData ();
55
- globalPredicates .add (globalFilter .createPredicate (from , criteriaBuilder , node .getName ()));
51
+ predicates . global .add (globalFilter .createPredicate (from , criteriaBuilder , node .getName ()));
56
52
}
57
53
}
58
54
for (Node <Filter > child : node .getChildren ()) {
@@ -62,44 +58,34 @@ protected void initPredicatesRecursively(CriteriaQuery<?> query, Node<Filter> no
62
58
continue ;
63
59
}
64
60
if (child .isLeaf ()) {
65
- initPredicatesRecursively (query , child , from , fetch , criteriaBuilder );
61
+ initPredicatesRecursively (predicates , query , child , from , fetch , criteriaBuilder );
66
62
} else {
67
63
Join <S , S > join = from .join (child .getName (), JoinType .LEFT );
68
64
69
65
if (isCountQuery (query )) {
70
- initPredicatesRecursively (query , child , join , join , criteriaBuilder );
66
+ initPredicatesRecursively (predicates , query , child , join , join , criteriaBuilder );
71
67
} else {
72
68
Fetch <S , S > childFetch = fetch .fetch (child .getName (), JoinType .LEFT );
73
- initPredicatesRecursively (query , child , join , childFetch , criteriaBuilder );
69
+ initPredicatesRecursively (predicates , query , child , join , childFetch , criteriaBuilder );
74
70
}
75
71
}
76
72
}
77
73
}
78
-
79
- protected Predicate createFinalPredicate (CriteriaBuilder criteriaBuilder ) {
80
- List <Predicate > allPredicates = new ArrayList <>(columnPredicates );
81
-
82
- if (!globalPredicates .isEmpty ()) {
83
- allPredicates .add (criteriaBuilder .or (globalPredicates .toArray (new Predicate [0 ])));
84
- }
85
-
86
- return allPredicates .isEmpty () ? criteriaBuilder .conjunction () : criteriaBuilder .and (allPredicates .toArray (new Predicate [0 ]));
87
- }
88
74
}
89
75
90
76
private class DataTablesSearchPaneSpecification <S > extends DataTablesSpecification <S > {
91
77
92
78
@ Override
93
- protected void initPredicatesRecursively (CriteriaQuery <?> query , Node <Filter > node , From <S , S > from ,
79
+ protected void initPredicatesRecursively (Predicates predicates , CriteriaQuery <?> query , Node <Filter > node , From <S , S > from ,
94
80
FetchParent <S , S > fetch , CriteriaBuilder criteriaBuilder ) {
95
81
if (node .isLeaf ()) {
96
82
boolean hasColumnFilter = node .getData () != null ;
97
83
if (hasColumnFilter ) {
98
84
Filter columnFilter = node .getData ();
99
- columnPredicates .add (columnFilter .createPredicate (from , criteriaBuilder , node .getName ()));
85
+ predicates . columns .add (columnFilter .createPredicate (from , criteriaBuilder , node .getName ()));
100
86
} else if (hasGlobalFilter ) {
101
87
Filter globalFilter = tree .getData ();
102
- globalPredicates .add (globalFilter .createPredicate (from , criteriaBuilder , node .getName ()));
88
+ predicates . global .add (globalFilter .createPredicate (from , criteriaBuilder , node .getName ()));
103
89
}
104
90
}
105
91
for (Node <Filter > child : node .getChildren ()) {
@@ -109,10 +95,10 @@ protected void initPredicatesRecursively(CriteriaQuery<?> query, Node<Filter> no
109
95
continue ;
110
96
}
111
97
if (child .isLeaf ()) {
112
- initPredicatesRecursively (query , child , from , fetch , criteriaBuilder );
98
+ initPredicatesRecursively (predicates , query , child , from , fetch , criteriaBuilder );
113
99
} else {
114
100
Join <S , S > join = from .join (child .getName (), JoinType .LEFT );
115
- initPredicatesRecursively (query , child , join , fetch , criteriaBuilder );
101
+ initPredicatesRecursively (predicates , query , child , join , fetch , criteriaBuilder );
116
102
}
117
103
}
118
104
}
@@ -121,4 +107,17 @@ protected void initPredicatesRecursively(CriteriaQuery<?> query, Node<Filter> no
121
107
public Specification <T > buildSearchPane () {
122
108
return new DataTablesSearchPaneSpecification <>();
123
109
}
110
+
111
+ private static class Predicates {
112
+ public List <Predicate > columns = new ArrayList <>();
113
+ public List <Predicate > global = new ArrayList <>();
114
+
115
+ Predicate toPredicate (CriteriaBuilder criteriaBuilder ) {
116
+ if (!global .isEmpty ()) {
117
+ columns .add (criteriaBuilder .or (global .toArray (new Predicate [0 ])));
118
+ }
119
+
120
+ return columns .isEmpty () ? criteriaBuilder .conjunction () : criteriaBuilder .and (columns .toArray (new Predicate [0 ]));
121
+ }
122
+ }
124
123
}
0 commit comments