1 | |
package org.kuali.student.r2.common.criteria.transform; |
2 | |
|
3 | |
import static org.kuali.rice.core.api.criteria.PredicateFactory.and; |
4 | |
import static org.kuali.rice.core.api.criteria.PredicateFactory.equal; |
5 | |
|
6 | |
import java.util.regex.Matcher; |
7 | |
import java.util.regex.Pattern; |
8 | |
|
9 | |
import org.kuali.rice.core.api.criteria.Predicate; |
10 | |
import org.kuali.rice.core.api.criteria.PropertyPathPredicate; |
11 | |
import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria; |
12 | |
|
13 | |
public class AttributeTransform extends BaseTransform { |
14 | |
|
15 | |
private static final String ATTRIBUTE_PROPERTY = "attributes"; |
16 | |
private static final String ATTRIBUTE_ALIAS = "attr"; |
17 | |
private static final String ATTRIBUTE_VALUE = "value"; |
18 | |
private static final String ATTRIBUTE_KEY = "key"; |
19 | |
private static final String ATTRIBUTES_REGEX = "^attributes\\[\\w*\\]$"; |
20 | 0 | private static final Pattern ATTRIBUTES_PATTERN = Pattern.compile(ATTRIBUTES_REGEX); |
21 | |
|
22 | 0 | private AttributeTransform() { |
23 | |
|
24 | 0 | } |
25 | |
|
26 | |
@Override |
27 | |
public Predicate apply(final Predicate input, Criteria criteria) { |
28 | 0 | if (input instanceof PropertyPathPredicate) { |
29 | 0 | String pp = ((PropertyPathPredicate) input).getPropertyPath(); |
30 | 0 | if (isAttributesPredicate(pp)) { |
31 | 0 | criteria.join(ATTRIBUTE_PROPERTY, ATTRIBUTE_ALIAS, false, true); |
32 | |
|
33 | 0 | final String attributeName = pp.substring(pp.indexOf('[') + 1, pp.indexOf(']')); |
34 | 0 | final Predicate attrValue = this.createPredicate(input, getPropertyDesc() + ATTRIBUTE_VALUE); |
35 | |
|
36 | 0 | return and(equal(getPropertyDesc() + ATTRIBUTE_KEY, attributeName), attrValue); |
37 | |
} |
38 | |
} |
39 | |
|
40 | 0 | return input; |
41 | |
} |
42 | |
|
43 | |
private String getPropertyDesc(){ |
44 | 0 | return Criteria.JPA_ALIAS_PREFIX + "'" + ATTRIBUTE_ALIAS + "'" + Criteria.JPA_ALIAS_SUFFIX + "."; |
45 | |
} |
46 | |
|
47 | |
private boolean isAttributesPredicate(String pp) { |
48 | 0 | Matcher matcher = ATTRIBUTES_PATTERN.matcher(pp); |
49 | 0 | return matcher.matches(); |
50 | |
} |
51 | |
} |