View Javadoc

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