Coverage Report - org.kuali.rice.core.api.criteria.LookupCustomizer
 
Classes in this File Line Coverage Branch Coverage Complexity
LookupCustomizer
0%
0/7
0%
0/4
1.154
LookupCustomizer$1
N/A
N/A
1.154
LookupCustomizer$Builder
0%
0/11
N/A
1.154
LookupCustomizer$IndentityTransform
0%
0/4
N/A
1.154
LookupCustomizer$Transform
N/A
N/A
1.154
 
 1  
 package org.kuali.rice.core.api.criteria;
 2  
 
 3  
 import org.kuali.rice.core.api.mo.ModelBuilder;
 4  
 
 5  
 import java.io.Serializable;
 6  
 
 7  
 /**
 8  
  * This class works withe the lookup framework to customize a query.  It currently can do the following things:
 9  
  *
 10  
  * <ul>
 11  
  *  <li>transform/remove predicates</li>
 12  
  *  <li>transform/remove query results</li>
 13  
  * </ul>
 14  
  *
 15  
  * <p>
 16  
  * The predicate transform will applied to the predicates in the incoming query yielding a
 17  
  * a predicate.  If the predicate does not need to be transformed then the function can return the
 18  
  * incoming argument.  If the predicate should be removed the transform should return null.
 19  
  * This is a way to remove or change a predicate before the query is executed.
 20  
  * This is a good way to add new predicates to a query or to limit the result of the query before
 21  
  * it is executed. Transforms also allow a predicate referencing a property path that does not exist
 22  
  * on database mapped object to be changed to something that is valid.
 23  
  * </p>
 24  
  *
 25  
  * <p>
 26  
  * The result transform will be applied to the results of the query after the query is executed.
 27  
  * If the result does not need to be transformed then the function can return the
 28  
  * incoming argument. This is a way to remove or change a result after the query is executed.
 29  
  * </p>
 30  
  *
 31  
  * <p>transformers should not have to deal with null items</p>
 32  
  */
 33  0
 public class LookupCustomizer<T> {
 34  
 
 35  
     //FIXME: add wilcards to make predicate transform more flexible ie. EqualsPredicate to AndPredicate
 36  
     private final Transform<Predicate,Predicate> predicateTransform;
 37  
     private final Transform<T, T> resultTransform;
 38  
 
 39  0
     private LookupCustomizer(Builder<T> builder) {
 40  0
         this.predicateTransform = builder.getPredicateTransform() != null ? builder.getPredicateTransform() : IndentityTransform.<Predicate, Predicate>getInstance();
 41  0
         this.resultTransform = builder.getResultTransform() != null ? builder.getResultTransform() : IndentityTransform.<T, T>getInstance();
 42  0
     }
 43  
 
 44  
     public Transform<Predicate, Predicate> getPredicateTransform() {
 45  0
         return predicateTransform;
 46  
     }
 47  
 
 48  
     public Transform<T, T> getResultTransform() {
 49  0
         return resultTransform;
 50  
     }
 51  
 
 52  0
     public static final class Builder<T> implements ModelBuilder, Serializable {
 53  
 
 54  
         private Transform<Predicate, Predicate> predicateTransform;
 55  
         private Transform<T, T> resultTransform;
 56  
 
 57  0
         private Builder() {
 58  
 
 59  0
         }
 60  
 
 61  
         public static <T> Builder<T> create() {
 62  0
             return new Builder<T>();
 63  
         }
 64  
 
 65  
         public Transform<Predicate, Predicate> getPredicateTransform() {
 66  0
             return predicateTransform;
 67  
         }
 68  
 
 69  
         public void setPredicateTransform(final Transform<Predicate, Predicate> predicateTransform) {
 70  0
             this.predicateTransform = predicateTransform;
 71  0
         }
 72  
 
 73  
         public Transform<T, T> getResultTransform() {
 74  0
             return resultTransform;
 75  
         }
 76  
 
 77  
         public void setResultTransform(final Transform<T, T> resultTransform) {
 78  0
             this.resultTransform = resultTransform;
 79  0
         }
 80  
 
 81  
         @Override
 82  
         public LookupCustomizer<T> build() {
 83  0
             return new LookupCustomizer<T>(this);
 84  
         }
 85  
     }
 86  
     public interface Transform<P, R> {
 87  
         R apply(P input);
 88  
     }
 89  
 
 90  
     /**
 91  
      * f: x -> x.  This function just returns the passed in parameter.
 92  
      *
 93  
      * @param <I> the type the function acts on.
 94  
      */
 95  0
     private static final class IndentityTransform<I> implements Transform<I, I> {
 96  
 
 97  
         @SuppressWarnings("unchecked")
 98  0
         private static final Transform INSTANCE = new IndentityTransform();
 99  
 
 100  
         @SuppressWarnings("unchecked")
 101  
         public static <P, R> Transform<P, R> getInstance() {
 102  0
             return INSTANCE;
 103  
         }
 104  
 
 105  
         @Override
 106  
         public I apply(final I input) {
 107  0
             return input;
 108  
         }
 109  
     }
 110  
 }