1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.uif.container; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
20 | |
import org.kuali.rice.krad.uif.UifConstants; |
21 | |
import org.kuali.rice.krad.uif.UifParameters; |
22 | |
import org.kuali.rice.krad.uif.UifPropertyPaths; |
23 | |
import org.kuali.rice.krad.uif.control.Control; |
24 | |
import org.kuali.rice.krad.uif.core.DataBinding; |
25 | |
import org.kuali.rice.krad.uif.field.ActionField; |
26 | |
import org.kuali.rice.krad.uif.field.AttributeField; |
27 | |
import org.kuali.rice.krad.uif.field.Field; |
28 | |
import org.kuali.rice.krad.uif.field.GroupField; |
29 | |
import org.kuali.rice.krad.uif.layout.CollectionLayoutManager; |
30 | |
import org.kuali.rice.krad.uif.service.ExpressionEvaluatorService; |
31 | |
import org.kuali.rice.krad.uif.util.ComponentUtils; |
32 | |
import org.kuali.rice.krad.uif.util.ObjectPropertyUtils; |
33 | |
import org.kuali.rice.krad.util.KRADUtils; |
34 | |
import org.kuali.rice.krad.util.ObjectUtils; |
35 | |
import org.kuali.rice.krad.web.form.UifFormBase; |
36 | |
|
37 | |
import java.io.Serializable; |
38 | |
import java.util.ArrayList; |
39 | |
import java.util.HashMap; |
40 | |
import java.util.List; |
41 | |
import java.util.Map; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 0 | public class CollectionGroupBuilder implements Serializable { |
52 | |
private static final long serialVersionUID = -4762031957079895244L; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public void build(View view, Object model, CollectionGroup collectionGroup) { |
75 | |
|
76 | 0 | if (collectionGroup.isRenderAddLine() && !collectionGroup.isReadOnly()) { |
77 | 0 | buildAddLine(view, model, collectionGroup); |
78 | |
} |
79 | |
|
80 | |
|
81 | 0 | List<Object> modelCollection = ObjectPropertyUtils.getPropertyValue(model, ((DataBinding) collectionGroup) |
82 | |
.getBindingInfo().getBindingPath()); |
83 | |
|
84 | |
|
85 | 0 | List<Integer> showIndexes = collectionGroup.performCollectionFiltering(view, model); |
86 | |
|
87 | |
|
88 | 0 | if (modelCollection != null) { |
89 | 0 | for (int index = 0; index < modelCollection.size(); index++) { |
90 | |
|
91 | |
|
92 | 0 | if (showIndexes == null || showIndexes.contains(index)) { |
93 | 0 | String bindingPathPrefix = collectionGroup.getBindingInfo().getBindingName() + "[" + index + "]"; |
94 | 0 | if (StringUtils.isNotBlank(collectionGroup.getBindingInfo().getBindByNamePrefix())) { |
95 | 0 | bindingPathPrefix = collectionGroup.getBindingInfo().getBindByNamePrefix() + "." |
96 | |
+ bindingPathPrefix; |
97 | |
} |
98 | |
|
99 | 0 | Object currentLine = modelCollection.get(index); |
100 | |
|
101 | 0 | List<ActionField> actions = getLineActions(view, model, collectionGroup, currentLine, index); |
102 | 0 | buildLine(view, model, collectionGroup, bindingPathPrefix, actions, false, currentLine, index); |
103 | |
} |
104 | |
} |
105 | |
} |
106 | 0 | } |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
protected void buildAddLine(View view, Object model, CollectionGroup collectionGroup) { |
121 | 0 | boolean addLineBindsToForm = false; |
122 | |
|
123 | |
|
124 | 0 | initializeNewCollectionLine(view, model, collectionGroup, false); |
125 | |
|
126 | |
|
127 | |
|
128 | 0 | if (StringUtils.isBlank(collectionGroup.getAddLinePropertyName())) { |
129 | 0 | addLineBindsToForm = true; |
130 | |
} |
131 | |
|
132 | 0 | String addLineBindingPath = collectionGroup.getAddLineBindingInfo().getBindingPath(); |
133 | 0 | List<ActionField> actions = getAddLineActions(view, model, collectionGroup); |
134 | |
|
135 | 0 | Object addLine = ObjectPropertyUtils.getPropertyValue(model, addLineBindingPath); |
136 | 0 | buildLine(view, model, collectionGroup, addLineBindingPath, actions, addLineBindsToForm, addLine, -1); |
137 | 0 | } |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
@SuppressWarnings("unchecked") |
166 | |
protected void buildLine(View view, Object model, CollectionGroup collectionGroup, String bindingPath, |
167 | |
List<ActionField> actions, boolean bindToForm, Object currentLine, int lineIndex) { |
168 | 0 | CollectionLayoutManager layoutManager = (CollectionLayoutManager) collectionGroup.getLayoutManager(); |
169 | |
|
170 | |
|
171 | 0 | List<Field> lineFields = null; |
172 | 0 | if (lineIndex == -1) { |
173 | 0 | lineFields = (List<Field>) ComponentUtils.copyFieldList(collectionGroup.getAddLineFields(), bindingPath, |
174 | |
"_add"); |
175 | |
} else { |
176 | 0 | lineFields = (List<Field>) ComponentUtils.copyFieldList(collectionGroup.getItems(), bindingPath, "_" |
177 | |
+ Integer.toString(lineIndex)); |
178 | |
} |
179 | |
|
180 | 0 | if(lineIndex == -1 && !lineFields.isEmpty()){ |
181 | 0 | for(Field f: lineFields){ |
182 | 0 | if(f instanceof AttributeField){ |
183 | |
|
184 | |
|
185 | 0 | Control control = ((AttributeField) f).getControl(); |
186 | 0 | if (control != null) { |
187 | 0 | control.addStyleClass(collectionGroup.getBaseId() + "-addField"); |
188 | 0 | control.addStyleClass("ignoreValid"); |
189 | |
} |
190 | 0 | } |
191 | |
} |
192 | 0 | for(ActionField action: actions){ |
193 | 0 | if(action.getActionParameter(UifParameters.ACTION_TYPE).equals(UifParameters.ADD_LINE)){ |
194 | 0 | action.setFocusOnAfterSubmit(lineFields.get(0).getId()); |
195 | |
} |
196 | |
} |
197 | |
} |
198 | |
|
199 | 0 | ComponentUtils.updateContextsForLine(lineFields, currentLine, lineIndex); |
200 | |
|
201 | 0 | if (bindToForm) { |
202 | 0 | ComponentUtils.setComponentsPropertyDeep(lineFields, UifPropertyPaths.BIND_TO_FORM, new Boolean(true)); |
203 | |
} |
204 | |
|
205 | |
|
206 | 0 | lineFields = removeNonRenderLineFields(view, model, collectionGroup, lineFields, currentLine, lineIndex); |
207 | |
|
208 | |
|
209 | 0 | List<GroupField> subCollectionFields = new ArrayList<GroupField>(); |
210 | 0 | if ((lineIndex != -1) && (collectionGroup.getSubCollections() != null)) { |
211 | 0 | for (int subLineIndex = 0; subLineIndex < collectionGroup.getSubCollections().size(); subLineIndex++) { |
212 | 0 | CollectionGroup subCollectionPrototype = collectionGroup.getSubCollections().get(subLineIndex); |
213 | 0 | CollectionGroup subCollectionGroup = ComponentUtils.copy(subCollectionPrototype, collectionGroup.getId() + "s" + subLineIndex); |
214 | |
|
215 | |
|
216 | 0 | boolean renderSubCollection = checkSubCollectionRender(view, model, collectionGroup, subCollectionGroup); |
217 | 0 | if (!renderSubCollection) { |
218 | 0 | continue; |
219 | |
} |
220 | |
|
221 | 0 | subCollectionGroup.getBindingInfo().setBindByNamePrefix(bindingPath); |
222 | 0 | subCollectionGroup.getAddLineBindingInfo().setBindByNamePrefix(bindingPath); |
223 | |
|
224 | 0 | GroupField groupFieldPrototype = layoutManager.getSubCollectionGroupFieldPrototype(); |
225 | 0 | GroupField subCollectionGroupField = ComponentUtils.copy(groupFieldPrototype, collectionGroup.getId() + "s" + subLineIndex); |
226 | 0 | subCollectionGroupField.setGroup(subCollectionGroup); |
227 | |
|
228 | 0 | subCollectionFields.add(subCollectionGroupField); |
229 | |
} |
230 | |
} |
231 | |
|
232 | |
|
233 | |
|
234 | 0 | layoutManager.buildLine(view, model, collectionGroup, lineFields, subCollectionFields, bindingPath, actions, |
235 | |
"_l" + lineIndex, currentLine, lineIndex); |
236 | 0 | } |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
protected List<Field> removeNonRenderLineFields(View view, Object model, CollectionGroup collectionGroup, |
262 | |
List<Field> lineFields, Object currentLine, int lineIndex) { |
263 | 0 | List<Field> fields = new ArrayList<Field>(); |
264 | |
|
265 | 0 | for (Field lineField : lineFields) { |
266 | |
|
267 | 0 | if (StringUtils.isNotBlank(lineField.getConditionalRender())) { |
268 | 0 | Map<String, Object> context = new HashMap<String, Object>(); |
269 | 0 | context.putAll(view.getContext()); |
270 | 0 | context.put(UifConstants.ContextVariableNames.PARENT, collectionGroup); |
271 | 0 | context.put(UifConstants.ContextVariableNames.COMPONENT, lineField); |
272 | 0 | context.put(UifConstants.ContextVariableNames.LINE, currentLine); |
273 | 0 | context.put(UifConstants.ContextVariableNames.INDEX, new Integer(lineIndex)); |
274 | 0 | context.put(UifConstants.ContextVariableNames.IS_ADD_LINE, new Boolean(lineIndex == -1)); |
275 | |
|
276 | 0 | Boolean render = (Boolean) getExpressionEvaluatorService().evaluateExpression(model, context, |
277 | |
lineField.getConditionalRender()); |
278 | 0 | lineField.setRender(render); |
279 | |
} |
280 | |
|
281 | |
|
282 | |
|
283 | 0 | if (lineField.isRender() |
284 | |
|| (!lineField.isRender() && !StringUtils.isEmpty(lineField.getProgressiveRender()))) { |
285 | 0 | fields.add(lineField); |
286 | |
} |
287 | |
} |
288 | |
|
289 | 0 | return fields; |
290 | |
} |
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
protected boolean checkSubCollectionRender(View view, Object model, CollectionGroup collectionGroup, |
308 | |
CollectionGroup subCollectionGroup) { |
309 | |
|
310 | 0 | if (StringUtils.isNotBlank(subCollectionGroup.getConditionalRender())) { |
311 | 0 | Map<String, Object> context = new HashMap<String, Object>(); |
312 | 0 | context.putAll(view.getContext()); |
313 | 0 | context.put(UifConstants.ContextVariableNames.PARENT, collectionGroup); |
314 | 0 | context.put(UifConstants.ContextVariableNames.COMPONENT, subCollectionGroup); |
315 | |
|
316 | 0 | Boolean render = (Boolean) getExpressionEvaluatorService().evaluateExpression(model, context, |
317 | |
subCollectionGroup.getConditionalRender()); |
318 | 0 | subCollectionGroup.setRender(render); |
319 | |
} |
320 | |
|
321 | 0 | return subCollectionGroup.isRender(); |
322 | |
} |
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
protected List<ActionField> getLineActions(View view, Object model, CollectionGroup collectionGroup, |
344 | |
Object collectionLine, int lineIndex) { |
345 | 0 | List<ActionField> lineActions = ComponentUtils.copyFieldList(collectionGroup.getActionFields(), Integer.toString(lineIndex)); |
346 | 0 | for (ActionField actionField : lineActions) { |
347 | 0 | actionField.addActionParameter(UifParameters.SELLECTED_COLLECTION_PATH, collectionGroup.getBindingInfo() |
348 | |
.getBindingPath()); |
349 | 0 | actionField.addActionParameter(UifParameters.SELECTED_LINE_INDEX, Integer.toString(lineIndex)); |
350 | 0 | actionField.setJumpToIdAfterSubmit(collectionGroup.getId() + "_div"); |
351 | 0 | actionField.setClientSideJs("performCollectionAction('"+collectionGroup.getId()+"');"); |
352 | |
} |
353 | |
|
354 | 0 | ComponentUtils.updateContextsForLine(lineActions, collectionLine, lineIndex); |
355 | |
|
356 | 0 | return lineActions; |
357 | |
} |
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
protected List<ActionField> getAddLineActions(View view, Object model, CollectionGroup collectionGroup) { |
375 | 0 | List<ActionField> lineActions = ComponentUtils.copyFieldList(collectionGroup.getAddLineActionFields(), "_add"); |
376 | 0 | for (ActionField actionField : lineActions) { |
377 | 0 | actionField.addActionParameter(UifParameters.SELLECTED_COLLECTION_PATH, collectionGroup.getBindingInfo() |
378 | |
.getBindingPath()); |
379 | |
|
380 | 0 | actionField.setJumpToIdAfterSubmit(collectionGroup.getId() + "_div"); |
381 | 0 | actionField.addActionParameter(UifParameters.ACTION_TYPE, UifParameters.ADD_LINE); |
382 | 0 | actionField.setClientSideJs("addLineToCollection('"+collectionGroup.getId()+"', '"+ collectionGroup.getBaseId() +"');"); |
383 | |
} |
384 | |
|
385 | |
|
386 | 0 | String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath(); |
387 | 0 | Object addLine = ObjectPropertyUtils.getPropertyValue(model, addLinePath); |
388 | |
|
389 | 0 | ComponentUtils.updateContextsForLine(lineActions, addLine, -1); |
390 | |
|
391 | 0 | return lineActions; |
392 | |
} |
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
public void initializeNewCollectionLine(View view, Object model, CollectionGroup collectionGroup, |
414 | |
boolean clearExistingLine) { |
415 | 0 | Object newLine = null; |
416 | |
|
417 | |
|
418 | 0 | if (StringUtils.isBlank(collectionGroup.getAddLinePropertyName())) { |
419 | |
|
420 | 0 | if (!(model instanceof UifFormBase)) { |
421 | 0 | throw new RuntimeException("Cannot create new collection line for group: " |
422 | |
+ collectionGroup.getPropertyName() + ". Model does not extend " + UifFormBase.class.getName()); |
423 | |
} |
424 | |
|
425 | |
|
426 | 0 | Map<String, Object> newCollectionLines = ObjectPropertyUtils.getPropertyValue(model, |
427 | |
UifPropertyPaths.NEW_COLLECTION_LINES); |
428 | 0 | if (newCollectionLines == null) { |
429 | 0 | newCollectionLines = new HashMap<String, Object>(); |
430 | 0 | ObjectPropertyUtils.setPropertyValue(model, UifPropertyPaths.NEW_COLLECTION_LINES, newCollectionLines); |
431 | |
} |
432 | |
|
433 | |
|
434 | 0 | String newCollectionLineKey = KRADUtils |
435 | |
.translateToMapSafeKey(collectionGroup.getBindingInfo().getBindingPath()); |
436 | 0 | String addLineBindingPath = UifPropertyPaths.NEW_COLLECTION_LINES + "['" + newCollectionLineKey + "']"; |
437 | 0 | collectionGroup.getAddLineBindingInfo().setBindingPath(addLineBindingPath); |
438 | |
|
439 | |
|
440 | |
|
441 | 0 | if (!newCollectionLines.containsKey(newCollectionLineKey) |
442 | |
|| (newCollectionLines.get(newCollectionLineKey) == null) || clearExistingLine) { |
443 | |
|
444 | 0 | newLine = ObjectUtils.newInstance(collectionGroup.getCollectionObjectClass()); |
445 | 0 | newCollectionLines.put(newCollectionLineKey, newLine); |
446 | |
} |
447 | 0 | } else { |
448 | |
|
449 | 0 | Object addLine = ObjectPropertyUtils.getPropertyValue(model, collectionGroup.getAddLineBindingInfo() |
450 | |
.getBindingPath()); |
451 | 0 | if ((addLine == null) || clearExistingLine) { |
452 | 0 | newLine = ObjectUtils.newInstance(collectionGroup.getCollectionObjectClass()); |
453 | 0 | ObjectPropertyUtils.setPropertyValue(model, collectionGroup.getAddLineBindingInfo().getBindingPath(), |
454 | |
newLine); |
455 | |
} |
456 | |
} |
457 | |
|
458 | |
|
459 | 0 | if (newLine != null) { |
460 | 0 | view.getViewHelperService().applyDefaultValuesForCollectionLine(view, model, collectionGroup, newLine); |
461 | |
} |
462 | 0 | } |
463 | |
|
464 | |
protected ExpressionEvaluatorService getExpressionEvaluatorService() { |
465 | 0 | return KRADServiceLocatorWeb.getExpressionEvaluatorService(); |
466 | |
} |
467 | |
|
468 | |
} |