View Javadoc

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