1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.modifier;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
21 import org.kuali.rice.krad.uif.UifConstants;
22 import org.kuali.rice.krad.uif.UifPropertyPaths;
23 import org.kuali.rice.krad.uif.container.Group;
24 import org.kuali.rice.krad.uif.field.DataField;
25 import org.kuali.rice.krad.uif.view.View;
26 import org.kuali.rice.krad.uif.component.Component;
27 import org.kuali.rice.krad.uif.field.HeaderField;
28 import org.kuali.rice.krad.uif.util.ComponentUtils;
29 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
30
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Set;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class CompareFieldCreateModifier extends ComponentModifierBase {
54 private static final Logger LOG = Logger.getLogger(CompareFieldCreateModifier.class);
55
56 private static final long serialVersionUID = -6285531580512330188L;
57
58 private int defaultOrderSequence;
59 private boolean generateCompareHeaders;
60
61 private HeaderField headerFieldPrototype;
62 private List<ComparableInfo> comparables;
63
64 public CompareFieldCreateModifier() {
65 defaultOrderSequence = 1;
66 generateCompareHeaders = true;
67
68 comparables = new ArrayList<ComparableInfo>();
69 }
70
71
72
73
74
75
76
77 @Override
78 public void performInitialization(View view, Object model, Component component) {
79 super.performInitialization(view, model, component);
80
81 if (headerFieldPrototype != null) {
82 view.getViewHelperService().performComponentInitialization(view, model, headerFieldPrototype);
83 }
84 }
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 @SuppressWarnings("unchecked")
104 @Override
105 public void performModification(View view, Object model, Component component) {
106 if ((component != null) && !(component instanceof Group)) {
107 throw new IllegalArgumentException(
108 "Compare field initializer only support Group components, found type: " + component.getClass());
109 }
110
111 if (component == null) {
112 return;
113 }
114
115
116 List<Component> comparisonItems = new ArrayList<Component>();
117
118
119 List<ComparableInfo> groupComparables = (List<ComparableInfo>) ComponentUtils.sort(comparables,
120 defaultOrderSequence);
121
122
123 Map<String, Object> context = new HashMap<String, Object>();
124 context.putAll(view.getContext());
125 context.put(UifConstants.ContextVariableNames.COMPONENT, component);
126
127 for (ComparableInfo comparable : groupComparables) {
128 KRADServiceLocatorWeb.getExpressionEvaluatorService().evaluateObjectExpressions(comparable, model,
129 context);
130 }
131
132
133 if (isGenerateCompareHeaders()) {
134 for (ComparableInfo comparable : groupComparables) {
135 HeaderField compareHeaderField = ComponentUtils.copy(headerFieldPrototype, comparable.getIdSuffix());
136 compareHeaderField.setHeaderText(comparable.getHeaderText());
137
138 comparisonItems.add(compareHeaderField);
139 }
140 }
141
142
143
144 boolean performValueChangeComparison = false;
145 String compareValueObjectBindingPath = null;
146 for (ComparableInfo comparable : groupComparables) {
147 if (comparable.isCompareToForValueChange()) {
148 performValueChangeComparison = true;
149 compareValueObjectBindingPath = comparable.getBindingObjectPath();
150 }
151 }
152
153
154 Group group = (Group) component;
155 for (Component item : group.getItems()) {
156 int defaultSuffix = 0;
157 for (ComparableInfo comparable : groupComparables) {
158 String idSuffix = comparable.getIdSuffix();
159 if (StringUtils.isBlank(idSuffix)) {
160 idSuffix = UifConstants.IdSuffixes.COMPARE + defaultSuffix;
161 }
162
163 Component compareItem = ComponentUtils.copy(item, idSuffix);
164
165 ComponentUtils.setComponentPropertyDeep(compareItem, UifPropertyPaths.BIND_OBJECT_PATH,
166 comparable.getBindingObjectPath());
167 if (comparable.isReadOnly()) {
168 compareItem.setReadOnly(true);
169 if (compareItem.getPropertyExpressions().containsKey("readOnly")) {
170 compareItem.getPropertyExpressions().remove("readOnly");
171 }
172 }
173
174
175 if (performValueChangeComparison && comparable.isHighlightValueChange() && !comparable
176 .isCompareToForValueChange()) {
177 performValueComparison(group, compareItem, model, compareValueObjectBindingPath);
178 }
179
180 comparisonItems.add(compareItem);
181 defaultSuffix++;
182 }
183 }
184
185
186 group.setItems(comparisonItems);
187 }
188
189
190
191
192
193
194
195
196
197
198
199 protected void performValueComparison(Group group, Component compareItem, Object model,
200 String compareValueObjectBindingPath) {
201
202 List<DataField> itemFields = ComponentUtils.getComponentsOfTypeDeep(compareItem, DataField.class);
203 for (DataField field : itemFields) {
204 String fieldBindingPath = field.getBindingInfo().getBindingPath();
205 Object fieldValue = ObjectPropertyUtils.getPropertyValue(model, fieldBindingPath);
206
207 String compareBindingPath = StringUtils.replaceOnce(fieldBindingPath,
208 field.getBindingInfo().getBindingObjectPath(), compareValueObjectBindingPath);
209 Object compareValue = ObjectPropertyUtils.getPropertyValue(model, compareBindingPath);
210
211 boolean valueChanged = false;
212 if (!((fieldValue == null) && (compareValue == null))) {
213
214 if ((fieldValue == null) || (compareValue == null)) {
215 valueChanged = true;
216 } else {
217
218 valueChanged = !fieldValue.equals(compareValue);
219 }
220 }
221
222
223 if (valueChanged) {
224 String onReadyScript = "showChangeIcon('" + field.getId() + "');";
225
226
227 Component headerField = group.getHeader();
228 onReadyScript += "showChangeIconOnHeader('" + headerField.getId() + "');";
229
230 field.setOnDocumentReadyScript(onReadyScript);
231 }
232
233
234 }
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251 protected String getIdSuffix(ComparableInfo comparable, int index) {
252 String idSuffix = comparable.getIdSuffix();
253 if (StringUtils.isBlank(idSuffix)) {
254 idSuffix = "_" + index;
255 }
256
257 return idSuffix;
258 }
259
260
261
262
263 @Override
264 public Set<Class<? extends Component>> getSupportedComponents() {
265 Set<Class<? extends Component>> components = new HashSet<Class<? extends Component>>();
266 components.add(Group.class);
267
268 return components;
269 }
270
271
272
273
274 public List<Component> getComponentPrototypes() {
275 List<Component> components = new ArrayList<Component>();
276
277 components.add(headerFieldPrototype);
278
279 return components;
280 }
281
282
283
284
285
286
287
288
289 public int getDefaultOrderSequence() {
290 return this.defaultOrderSequence;
291 }
292
293
294
295
296
297
298 public void setDefaultOrderSequence(int defaultOrderSequence) {
299 this.defaultOrderSequence = defaultOrderSequence;
300 }
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315 public boolean isGenerateCompareHeaders() {
316 return this.generateCompareHeaders;
317 }
318
319
320
321
322
323
324 public void setGenerateCompareHeaders(boolean generateCompareHeaders) {
325 this.generateCompareHeaders = generateCompareHeaders;
326 }
327
328
329
330
331
332
333
334 public HeaderField getHeaderFieldPrototype() {
335 return this.headerFieldPrototype;
336 }
337
338
339
340
341
342
343 public void setHeaderFieldPrototype(HeaderField headerFieldPrototype) {
344 this.headerFieldPrototype = headerFieldPrototype;
345 }
346
347
348
349
350
351
352
353
354
355
356
357
358 public List<ComparableInfo> getComparables() {
359 return this.comparables;
360 }
361
362
363
364
365
366
367 public void setComparables(List<ComparableInfo> comparables) {
368 this.comparables = comparables;
369 }
370
371 }