1 /**
2 * Copyright 2005-2014 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 * @deprecated Use {@link org.kuali.rice.core.api.criteria.Transform} directly
49 */
50 @Deprecated
51 public class LookupCustomizer<T> {
52
53 //FIXME: add wilcards to make predicate transform more flexible ie. EqualsPredicate to AndPredicate
54 private final Transform<Predicate,Predicate> predicateTransform;
55 private final Transform<T, T> resultTransform;
56
57 private LookupCustomizer(Builder<T> builder) {
58 this.predicateTransform = builder.getPredicateTransform() != null ? builder.getPredicateTransform() : IndentityTransform.<Predicate, Predicate>getInstance();
59 this.resultTransform = builder.getResultTransform() != null ? builder.getResultTransform() : IndentityTransform.<T, T>getInstance();
60 }
61
62 public Transform<Predicate, Predicate> getPredicateTransform() {
63 return predicateTransform;
64 }
65
66 public Transform<T, T> getResultTransform() {
67 return resultTransform;
68 }
69
70 @Deprecated
71 public static final class Builder<T> implements ModelBuilder, Serializable {
72
73 private Transform<Predicate, Predicate> predicateTransform;
74 private Transform<T, T> resultTransform;
75
76 private Builder() {
77
78 }
79
80 public static <T> Builder<T> create() {
81 return new Builder<T>();
82 }
83
84 public Transform<Predicate, Predicate> getPredicateTransform() {
85 return predicateTransform;
86 }
87
88 public void setPredicateTransform(final Transform<Predicate, Predicate> predicateTransform) {
89 this.predicateTransform = predicateTransform;
90 }
91
92 public Transform<T, T> getResultTransform() {
93 return resultTransform;
94 }
95
96 public void setResultTransform(final Transform<T, T> resultTransform) {
97 this.resultTransform = resultTransform;
98 }
99
100 @Override
101 public LookupCustomizer<T> build() {
102 return new LookupCustomizer<T>(this);
103 }
104 }
105
106 @Deprecated
107 public interface Transform<P, R> {
108 R apply(P input);
109 }
110
111 /**
112 * f: x -> x. This function just returns the passed in parameter.
113 *
114 * @param <I> the type the function acts on.
115 */
116 @Deprecated
117 private static final class IndentityTransform<I> implements Transform<I, I> {
118
119 @SuppressWarnings("unchecked")
120 private static final Transform INSTANCE = new IndentityTransform();
121
122 @SuppressWarnings("unchecked")
123 public static <P, R> Transform<P, R> getInstance() {
124 return INSTANCE;
125 }
126
127 @Override
128 public I apply(final I input) {
129 return input;
130 }
131 }
132 }