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