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