1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.core.api.criteria; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.kuali.rice.core.api.search.SearchOperator; |
20 | |
|
21 | |
import java.util.ArrayList; |
22 | |
import java.util.Arrays; |
23 | |
import java.util.List; |
24 | |
import java.util.Map; |
25 | |
|
26 | |
import static org.kuali.rice.core.api.criteria.PredicateFactory.*; |
27 | |
|
28 | |
public final class PredicateUtils { |
29 | |
|
30 | 0 | private PredicateUtils() { |
31 | 0 | throw new UnsupportedOperationException("do not call"); |
32 | |
} |
33 | |
|
34 | |
public static Predicate convertMapToPredicate(Map<String, String> criteria) { |
35 | 0 | List<Predicate> p = new ArrayList<Predicate>(); |
36 | 0 | for (Map.Entry<String, String> entry : criteria.entrySet()) { |
37 | 0 | if (StringUtils.isNotBlank(entry.getValue())) { |
38 | 0 | List<String> values = new ArrayList<String>(); |
39 | 0 | getValueRecursive(entry.getValue(), values); |
40 | 0 | List<Predicate> tempPredicates = new ArrayList<Predicate>(); |
41 | 0 | p.addAll(tempPredicates); |
42 | |
|
43 | |
|
44 | 0 | for (String value : values) { |
45 | 0 | if (value.contains(SearchOperator.NULL.op())) { |
46 | 0 | if (isNot(value)) { |
47 | 0 | tempPredicates.add(isNotNull(entry.getKey())); |
48 | |
} else { |
49 | 0 | tempPredicates.add(isNull(entry.getKey())); |
50 | |
} |
51 | 0 | } else if (value.contains(SearchOperator.BETWEEN_EXCLUSIVE_UPPER.op())) { |
52 | 0 | String[] betweenVals = StringUtils.split(value, SearchOperator.BETWEEN_EXCLUSIVE_UPPER.op()); |
53 | 0 | if (betweenVals.length == 2) { |
54 | 0 | tempPredicates.add(and(greaterThanOrEqual(entry.getKey(), betweenVals[0]), |
55 | |
lessThan(entry.getKey(), betweenVals[1]))); |
56 | |
} |
57 | 0 | } else if (value.contains(SearchOperator.BETWEEN.op())) { |
58 | 0 | String[] betweenVals = StringUtils.split(value, SearchOperator.BETWEEN.op()); |
59 | 0 | if (betweenVals.length == 2) { |
60 | 0 | tempPredicates.add(and(greaterThanOrEqual(entry.getKey(), betweenVals[0]), |
61 | |
lessThanOrEqual(entry.getKey(), betweenVals[1]))); |
62 | |
} |
63 | 0 | } else if (value.contains(SearchOperator.GREATER_THAN_EQUAL.op())) { |
64 | 0 | tempPredicates.add(greaterThanOrEqual(entry.getKey(), StringUtils.replace(value, SearchOperator.GREATER_THAN_EQUAL.op(), ""))); |
65 | 0 | } else if (value.contains(SearchOperator.LESS_THAN_EQUAL.op())) { |
66 | 0 | tempPredicates.add(lessThanOrEqual(entry.getKey(), StringUtils.replace(value, SearchOperator.LESS_THAN_EQUAL.op(), ""))); |
67 | 0 | } else if (value.contains(SearchOperator.GREATER_THAN.op())) { |
68 | 0 | tempPredicates.add(greaterThan(entry.getKey(), StringUtils.replace(value, SearchOperator.GREATER_THAN.op(), ""))); |
69 | 0 | } else if (value.contains(SearchOperator.LESS_THAN.op())) { |
70 | 0 | tempPredicates.add(lessThan(entry.getKey(), StringUtils.replace(value, SearchOperator.LESS_THAN.op(), ""))); |
71 | |
|
72 | 0 | } else if (value.contains(SearchOperator.NOT.op())) { |
73 | 0 | String[] notValues = StringUtils.split(value, SearchOperator.NOT.op()); |
74 | 0 | List<Predicate> notPreds = new ArrayList<Predicate>(notValues.length); |
75 | 0 | for (String notValue : notValues) { |
76 | 0 | notPreds.add(notEqual(entry.getKey(), StringUtils.replace(notValue, SearchOperator.NOT.op(), ""))); |
77 | |
} |
78 | 0 | tempPredicates.add(and(notPreds.toArray(new Predicate[notPreds.size()]))); |
79 | 0 | } else if (value.contains(SearchOperator.LIKE_MANY.op()) |
80 | |
|| (value.contains(SearchOperator.LIKE_ONE.op()))) { |
81 | 0 | if (isNot(value)) { |
82 | 0 | tempPredicates.add(notLike(entry.getKey(), value )); |
83 | |
} else { |
84 | 0 | tempPredicates.add(like(entry.getKey(), value )); |
85 | |
} |
86 | |
} else { |
87 | 0 | if (isNot(value)) { |
88 | 0 | tempPredicates.add(notEqual(entry.getKey(), value)); |
89 | |
} else { |
90 | 0 | tempPredicates.add(equal(entry.getKey(), value)); |
91 | |
} |
92 | |
} |
93 | |
} |
94 | 0 | if (entry.getValue().contains(SearchOperator.AND.op())) { |
95 | 0 | p.add(and(tempPredicates.toArray(new Predicate[tempPredicates.size()]))); |
96 | 0 | } else if (entry.getValue().contains(SearchOperator.OR.op())) { |
97 | 0 | p.add(or(tempPredicates.toArray(new Predicate[tempPredicates.size()]))); |
98 | |
} else { |
99 | 0 | p.addAll(tempPredicates); |
100 | |
} |
101 | 0 | } |
102 | |
} |
103 | |
|
104 | 0 | return and(p.toArray(new Predicate[p.size()])); |
105 | |
} |
106 | |
|
107 | |
private static void getValueRecursive(String valueEntered, List<String> lRet) { |
108 | 0 | if(valueEntered == null) { |
109 | 0 | return; |
110 | |
} |
111 | |
|
112 | 0 | valueEntered = valueEntered.trim(); |
113 | |
|
114 | 0 | if(lRet == null){ |
115 | 0 | throw new NullPointerException("The list passed in is by reference and should never be null."); |
116 | |
} |
117 | |
|
118 | 0 | if (StringUtils.contains(valueEntered, SearchOperator.OR.op())) { |
119 | 0 | List<String> l = Arrays.asList(StringUtils.split(valueEntered, SearchOperator.OR.op())); |
120 | 0 | for(String value : l){ |
121 | 0 | getValueRecursive(value, lRet); |
122 | |
} |
123 | 0 | return; |
124 | |
} |
125 | 0 | if (StringUtils.contains(valueEntered, SearchOperator.AND.op())) { |
126 | |
|
127 | 0 | List<String> l = Arrays.asList(StringUtils.split(valueEntered, SearchOperator.AND.op())); |
128 | 0 | for(String value : l){ |
129 | 0 | getValueRecursive(value, lRet); |
130 | |
} |
131 | 0 | return; |
132 | |
} |
133 | |
|
134 | |
|
135 | 0 | lRet.add(valueEntered); |
136 | 0 | } |
137 | |
|
138 | |
private static boolean isNot(String value) { |
139 | 0 | if (value == null) { |
140 | 0 | return false; |
141 | |
} |
142 | 0 | return value.contains(SearchOperator.NOT.op()); |
143 | |
} |
144 | |
} |
145 | |
|
146 | |
|