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