1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kim.impl.common.attribute; |
17 | |
|
18 | |
import org.kuali.rice.core.api.criteria.CriteriaValue; |
19 | |
import org.kuali.rice.core.api.criteria.LookupCustomizer; |
20 | |
import org.kuali.rice.core.api.criteria.MultiValuedPredicate; |
21 | |
import org.kuali.rice.core.api.criteria.Predicate; |
22 | |
import org.kuali.rice.core.api.criteria.PropertyPathPredicate; |
23 | |
import org.kuali.rice.core.api.criteria.SingleValuedPredicate; |
24 | |
|
25 | |
import java.util.ArrayList; |
26 | |
import java.util.List; |
27 | |
import java.util.Set; |
28 | |
import java.util.regex.Matcher; |
29 | |
import java.util.regex.Pattern; |
30 | |
|
31 | |
import static org.kuali.rice.core.api.criteria.PredicateFactory.*; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 0 | public final class AttributeTransform implements LookupCustomizer.Transform<Predicate, Predicate> { |
38 | |
|
39 | 0 | private static final LookupCustomizer.Transform<Predicate, Predicate> INSTANCE = new AttributeTransform(); |
40 | |
private static final String ATTRIBUTE_DETAILS_ATTRIBUTE_VALUE = "attributeDetails.attributeValue"; |
41 | |
private static final String ATTRIBUTE_DETAILS_ATTRIBUTE_NAME = "attributeDetails.kimAttribute.attributeName"; |
42 | |
private static final String ATTRIBUTES_REGEX = "^attributes\\[\\w*\\]$"; |
43 | 0 | private static final Pattern ATTRIBUTES_PATTERN = Pattern.compile(ATTRIBUTES_REGEX); |
44 | |
|
45 | 0 | private AttributeTransform() { |
46 | |
|
47 | 0 | } |
48 | |
|
49 | |
@Override |
50 | |
public Predicate apply(final Predicate input) { |
51 | 0 | if (input instanceof PropertyPathPredicate) { |
52 | 0 | String pp = ((PropertyPathPredicate) input).getPropertyPath(); |
53 | 0 | if (isAttributesPredicate(pp)) { |
54 | 0 | final String attributeName = pp.substring(pp.indexOf('[') + 1, pp.indexOf(']')); |
55 | |
|
56 | |
final Predicate attrValue; |
57 | 0 | if (input instanceof SingleValuedPredicate) { |
58 | 0 | final CriteriaValue<?> value = ((SingleValuedPredicate) input).getValue(); |
59 | 0 | attrValue = dynConstruct(input.getClass().getSimpleName(), ATTRIBUTE_DETAILS_ATTRIBUTE_VALUE, value.getValue()); |
60 | 0 | } else if (input instanceof MultiValuedPredicate) { |
61 | 0 | final Set<? extends CriteriaValue<?>> values = ((MultiValuedPredicate) input).getValues(); |
62 | 0 | List<Object> l = new ArrayList<Object>(); |
63 | 0 | for (CriteriaValue<?> v : values) { |
64 | 0 | l.add(v.getValue()); |
65 | |
} |
66 | |
|
67 | 0 | attrValue = dynConstruct(input.getClass().getSimpleName(), ATTRIBUTE_DETAILS_ATTRIBUTE_VALUE, l.toArray()); |
68 | 0 | } else { |
69 | 0 | attrValue = dynConstruct(input.getClass().getSimpleName(), ATTRIBUTE_DETAILS_ATTRIBUTE_VALUE); |
70 | |
} |
71 | 0 | return and(equal(ATTRIBUTE_DETAILS_ATTRIBUTE_NAME, attributeName), attrValue); |
72 | |
} |
73 | |
} |
74 | |
|
75 | 0 | return input; |
76 | |
} |
77 | |
|
78 | |
private boolean isAttributesPredicate(String pp) { |
79 | 0 | Matcher matcher = ATTRIBUTES_PATTERN.matcher(pp); |
80 | 0 | return matcher.matches(); |
81 | |
} |
82 | |
|
83 | |
public static LookupCustomizer.Transform<Predicate, Predicate> getInstance() { |
84 | 0 | return INSTANCE; |
85 | |
} |
86 | |
} |