Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PredicateFactoryLookup |
|
| 5.25;5.25 |
1 | package org.kuali.rice.core.api.criteria; | |
2 | ||
3 | import org.kuali.rice.core.api.search.SearchOperator; | |
4 | ||
5 | import java.util.ArrayList; | |
6 | import java.util.Collection; | |
7 | import java.util.List; | |
8 | import java.util.Map; | |
9 | ||
10 | import static org.kuali.rice.core.api.criteria.PredicateFactory.*; | |
11 | ||
12 | /** | |
13 | * Contains methods used in the predicate factory related to the lookup framework. | |
14 | * *************************************************************************************************************** | |
15 | * FIXME: issues to talk to the group about. | |
16 | * http://kuali.org/rice/documentation/1.0.3/UG_Global/Documents/lookupwildcards.htm | |
17 | * | |
18 | * 1) Should we support isNotNull, isNull, as wildcards? Then do we still translate | |
19 | * null values into isNull predicates. I believe the lookup framework right now | |
20 | * barfs on null values but that is a guess. | |
21 | * | |
22 | * 2) We need to support case insensitivity in the old lookup framework. Right now the lookup | |
23 | * framework looks at Data dictionary entries. This can still be configured in the DD | |
24 | * but should be placed in the lookup criteria. We could have a "flag" section on a | |
25 | * lookup sequence like foo.bar=(?i)ba*|bing | |
26 | * | |
27 | * This would translate to | |
28 | * | |
29 | * or(like("foo.bar", "ba*"), equalsIgnoreCase("foo.bar", "bing")) | |
30 | * | |
31 | * Btw. My flag format was stolen from regex but we could use anything really. | |
32 | * http://download.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#CASE_INSENSITIVE | |
33 | * | |
34 | * 3) In the above example, I used a case insensitive flag but Like doesn't support case | |
35 | * insensitive. Should it? | |
36 | * | |
37 | * 4) Do we need to support escaping in the lookup framework & criteria api. Right now the | |
38 | * lookup framework looks at Data dictionary entries. This can still be configured in the DD | |
39 | * but should be placed in the lookup criteria. Escaping is tricky and I worry if we support | |
40 | * it in the criteria api then it will make the criteria service much harder to make custom | |
41 | * implementations. To me it seems it's better to make escaping behavior undefined. | |
42 | * | |
43 | * If we do support an escape character then we should probably also support a flag to treat | |
44 | * escape chars as literal like (?i) - is that right? need to confirm what flag get append | |
45 | * to the regex | |
46 | * | |
47 | * http://download.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#LITERAL | |
48 | * | |
49 | * 5) Maybe we should just support what is in the org.kuali.rice.core.framework.logic.LogicalOperator class | |
50 | * which btw contains more than just logical operators - This should be split into multiple enums. | |
51 | * ? | |
52 | * 6) Maybe the predicate class could have a toLookupString, toLookupMap() methods on them to translate | |
53 | * to various formats of criteria? Or maybe this factory method & related methods should get placed into | |
54 | * krad or somewhere else? | |
55 | * *************************************************************************************************************** | |
56 | */ | |
57 | class PredicateFactoryLookup { | |
58 | ||
59 | 0 | private PredicateFactoryLookup() { |
60 | 0 | throw new IllegalArgumentException("do not call"); |
61 | } | |
62 | ||
63 | static Predicate fromCriteriaMap(Map<String, ?> criteria) { | |
64 | 0 | if (criteria == null || criteria.isEmpty()) { |
65 | 0 | throw new IllegalArgumentException("criteria is null or empty"); |
66 | } | |
67 | ||
68 | 0 | final List<Predicate> toAnd = new ArrayList<Predicate>(); |
69 | 0 | for (Map.Entry<String, ?> entry : criteria.entrySet()) { |
70 | 0 | final String key = entry.getKey(); |
71 | 0 | if (key == null) { |
72 | 0 | throw new IllegalArgumentException("criteria contains a null key"); |
73 | } | |
74 | 0 | toAnd.add(createPredicate(key, entry.getValue())); |
75 | 0 | } |
76 | ||
77 | 0 | return and(toAnd.toArray(new Predicate[] {})); |
78 | } | |
79 | ||
80 | private static Predicate createPredicate(String key, Object value) { | |
81 | //null case | |
82 | 0 | if (value == null) { |
83 | 0 | return isNull(key); |
84 | 0 | } else if (value instanceof String) { |
85 | 0 | if (containsOperator((String) value)) { |
86 | //handle operator parsing | |
87 | } else { | |
88 | 0 | return equal(key, value); |
89 | } | |
90 | 0 | } else if (value instanceof Collection) { |
91 | //recurs | |
92 | } else { | |
93 | 0 | throw new IllegalArgumentException("criteria map contained a value that was non supported :" + value.getClass().getName()); |
94 | } | |
95 | 0 | return null; |
96 | } | |
97 | ||
98 | //does not handle escaping, assumes non-null | |
99 | private static boolean containsOperator(String value) { | |
100 | 0 | for (SearchOperator o : SearchOperator.values()) { |
101 | 0 | if (value.contains(o.op())) { |
102 | 0 | return true; |
103 | } | |
104 | } | |
105 | 0 | return false; |
106 | } | |
107 | } |