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 public final class AttributeTransform implements LookupCustomizer.Transform<Predicate, Predicate> {
38
39 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 private static final Pattern ATTRIBUTES_PATTERN = Pattern.compile(ATTRIBUTES_REGEX);
44
45 private AttributeTransform() {
46
47 }
48
49 @Override
50 public Predicate apply(final Predicate input) {
51 if (input instanceof PropertyPathPredicate) {
52 String pp = ((PropertyPathPredicate) input).getPropertyPath();
53 if (isAttributesPredicate(pp)) {
54 final String attributeName = pp.substring(pp.indexOf('[') + 1, pp.indexOf(']'));
55
56 final Predicate attrValue;
57 if (input instanceof SingleValuedPredicate) {
58 final CriteriaValue<?> value = ((SingleValuedPredicate) input).getValue();
59 attrValue = dynConstruct(input.getClass().getSimpleName(), ATTRIBUTE_DETAILS_ATTRIBUTE_VALUE, value.getValue());
60 } else if (input instanceof MultiValuedPredicate) {
61 final Set<? extends CriteriaValue<?>> values = ((MultiValuedPredicate) input).getValues();
62 List<Object> l = new ArrayList<Object>();
63 for (CriteriaValue<?> v : values) {
64 l.add(v.getValue());
65 }
66
67 attrValue = dynConstruct(input.getClass().getSimpleName(), ATTRIBUTE_DETAILS_ATTRIBUTE_VALUE, l.toArray());
68 } else {
69 attrValue = dynConstruct(input.getClass().getSimpleName(), ATTRIBUTE_DETAILS_ATTRIBUTE_VALUE);
70 }
71 return and(equal(ATTRIBUTE_DETAILS_ATTRIBUTE_NAME, attributeName), attrValue);
72 }
73 }
74
75 return input;
76 }
77
78 private boolean isAttributesPredicate(String pp) {
79 Matcher matcher = ATTRIBUTES_PATTERN.matcher(pp);
80 return matcher.matches();
81 }
82
83 public static LookupCustomizer.Transform<Predicate, Predicate> getInstance() {
84 return INSTANCE;
85 }
86 }