1 | |
package org.kuali.student.r2.common.criteria; |
2 | |
|
3 | |
import java.io.Serializable; |
4 | |
import java.util.ArrayList; |
5 | |
import java.util.List; |
6 | |
|
7 | |
import org.kuali.rice.core.api.criteria.Predicate; |
8 | |
import org.kuali.rice.core.api.mo.ModelBuilder; |
9 | |
import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria; |
10 | |
|
11 | 0 | public class LookupCustomizer<T> { |
12 | |
|
13 | |
private List<PredicateTransform> additionalTransforms; |
14 | |
private List<PredicateTransform> predicateTransforms; |
15 | |
private final Transform<T, T> resultTransform; |
16 | |
|
17 | 0 | private LookupCustomizer(Builder<T> builder) { |
18 | 0 | this.additionalTransforms = builder.getAdditionalTransforms(); |
19 | 0 | this.predicateTransforms = builder.getPredicateTransforms(); |
20 | 0 | this.resultTransform = builder.getResultTransform() != null ? builder.getResultTransform() : IndentityTransform.<T, T>getInstance(); |
21 | 0 | } |
22 | |
|
23 | |
public Predicate applyAdditionalTransforms(Predicate predicate, Criteria criteria) { |
24 | 0 | for (PredicateTransform transform : additionalTransforms){ |
25 | 0 | predicate = transform.apply(predicate, criteria); |
26 | |
} |
27 | 0 | return predicate; |
28 | |
} |
29 | |
|
30 | |
public Predicate applyPredicateTransforms(Predicate predicate, Criteria criteria) { |
31 | 0 | for (PredicateTransform transform : predicateTransforms){ |
32 | 0 | predicate = transform.apply(predicate, criteria); |
33 | |
} |
34 | 0 | return predicate; |
35 | |
} |
36 | |
|
37 | |
public T applyResultTransforms(Object o) { |
38 | 0 | return null; |
39 | |
} |
40 | |
|
41 | |
public List<PredicateTransform> getAdditionalTransforms() { |
42 | 0 | return additionalTransforms; |
43 | |
} |
44 | |
|
45 | |
public void setAdditionalTransforms(List<PredicateTransform> additionalTransforms) { |
46 | 0 | this.additionalTransforms = additionalTransforms; |
47 | 0 | } |
48 | |
|
49 | |
public List<PredicateTransform> getPredicateTransforms() { |
50 | 0 | return predicateTransforms; |
51 | |
} |
52 | |
|
53 | |
public void setPredicateTransforms(List<PredicateTransform> predicateTransforms) { |
54 | 0 | this.predicateTransforms = predicateTransforms; |
55 | 0 | } |
56 | |
|
57 | |
public Transform<T, T> getResultTransform() { |
58 | 0 | return resultTransform; |
59 | |
} |
60 | |
|
61 | 0 | public static final class Builder<T> implements ModelBuilder, Serializable { |
62 | |
|
63 | |
private List<PredicateTransform> additionalTransforms; |
64 | |
private List<PredicateTransform> predicateTransforms; |
65 | |
private Transform<T, T> resultTransform; |
66 | |
|
67 | 0 | private Builder() { |
68 | |
|
69 | 0 | } |
70 | |
|
71 | |
public static <T> Builder<T> create() { |
72 | 0 | return new Builder<T>(); |
73 | |
} |
74 | |
|
75 | |
public List<PredicateTransform> getAdditionalTransforms() { |
76 | 0 | return additionalTransforms; |
77 | |
} |
78 | |
|
79 | |
public void setAdditionalTransforms(List<PredicateTransform> additionalTransforms) { |
80 | 0 | this.additionalTransforms = additionalTransforms; |
81 | 0 | } |
82 | |
|
83 | |
public List<PredicateTransform> getPredicateTransforms() { |
84 | 0 | return predicateTransforms; |
85 | |
} |
86 | |
|
87 | |
public void setPredicateTransforms(List<PredicateTransform> predicateTransforms) { |
88 | 0 | this.predicateTransforms = predicateTransforms; |
89 | 0 | } |
90 | |
|
91 | |
public Transform<T, T> getResultTransform() { |
92 | 0 | return resultTransform; |
93 | |
} |
94 | |
|
95 | |
public void setResultTransform(final Transform<T, T> resultTransform) { |
96 | 0 | this.resultTransform = resultTransform; |
97 | 0 | } |
98 | |
|
99 | |
@Override |
100 | |
public LookupCustomizer<T> build() { |
101 | 0 | if (additionalTransforms == null){ |
102 | 0 | additionalTransforms = new ArrayList<LookupCustomizer.PredicateTransform>(); |
103 | |
} |
104 | 0 | if (predicateTransforms == null){ |
105 | 0 | predicateTransforms = new ArrayList<LookupCustomizer.PredicateTransform>(); |
106 | |
} |
107 | 0 | return new LookupCustomizer<T>(this); |
108 | |
} |
109 | |
} |
110 | |
|
111 | |
public interface PredicateTransform { |
112 | |
Predicate apply(Predicate input, Criteria criteria); |
113 | |
} |
114 | |
|
115 | |
public interface Transform<P, R> { |
116 | |
R apply(P input); |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | 0 | private static final class IndentityTransform<I> implements Transform<I, I> { |
125 | |
|
126 | |
@SuppressWarnings("unchecked") |
127 | 0 | private static final Transform INSTANCE = new IndentityTransform(); |
128 | |
|
129 | |
@SuppressWarnings("unchecked") |
130 | |
public static <P, R> Transform<P, R> getInstance() { |
131 | 0 | return INSTANCE; |
132 | |
} |
133 | |
|
134 | |
@Override |
135 | |
public I apply(final I input) { |
136 | 0 | return input; |
137 | |
} |
138 | |
} |
139 | |
|
140 | |
} |