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