Coverage Report - org.kuali.rice.kim.impl.common.attribute.AttributeTransform
 
Classes in this File Line Coverage Branch Coverage Complexity
AttributeTransform
0%
0/25
0%
0/10
2.5
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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  
  * this is an internal class used by kim service implementations that have generic lookup methods for classes
 35  
  * with "attributes".
 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  
 }