Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CollectionGroupBuilder |
|
| 4.333333333333333;4.333 |
1 | /* | |
2 | * Copyright 2011 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 1.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.opensource.org/licenses/ecl1.php | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.kuali.rice.kns.uif.container; | |
17 | ||
18 | import java.io.Serializable; | |
19 | import java.util.ArrayList; | |
20 | import java.util.HashMap; | |
21 | import java.util.List; | |
22 | import java.util.Map; | |
23 | ||
24 | import org.apache.commons.lang.StringUtils; | |
25 | import org.kuali.rice.kns.uif.UifParameters; | |
26 | import org.kuali.rice.kns.uif.UifPropertyPaths; | |
27 | import org.kuali.rice.kns.uif.core.BindingInfo; | |
28 | import org.kuali.rice.kns.uif.core.DataBinding; | |
29 | import org.kuali.rice.kns.uif.field.ActionField; | |
30 | import org.kuali.rice.kns.uif.field.Field; | |
31 | import org.kuali.rice.kns.uif.field.GroupField; | |
32 | import org.kuali.rice.kns.uif.layout.CollectionLayoutManager; | |
33 | import org.kuali.rice.kns.uif.util.ComponentUtils; | |
34 | import org.kuali.rice.kns.uif.util.ObjectPropertyUtils; | |
35 | import org.kuali.rice.kns.util.WebUtils; | |
36 | import org.kuali.rice.kns.web.spring.form.UifFormBase; | |
37 | ||
38 | /** | |
39 | * Builds out the <code>Field</code> instances for a collection group with a | |
40 | * series of steps that interact with the configured | |
41 | * <code>CollectionLayoutManager</code> to assemble the fields as necessary for | |
42 | * the layout | |
43 | * | |
44 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
45 | */ | |
46 | 0 | public class CollectionGroupBuilder implements Serializable { |
47 | private static final long serialVersionUID = -4762031957079895244L; | |
48 | ||
49 | /** | |
50 | * Creates the <code>Field</code> instances that make up the table | |
51 | * | |
52 | * <p> | |
53 | * The corresponding collection is retrieved from the model and iterated | |
54 | * over to create the necessary fields. The binding path for fields that | |
55 | * implement <code>DataBinding</code> is adjusted to point to the collection | |
56 | * line it is apart of. For example, field 'number' of collection 'accounts' | |
57 | * for line 1 will be set to 'accounts[0].number', and for line 2 | |
58 | * 'accounts[1].number'. Finally parameters are set on the line's action | |
59 | * fields to indicate what collection and line they apply to. | |
60 | * </p> | |
61 | * | |
62 | * @param view | |
63 | * - View instance the collection belongs to | |
64 | * @param model | |
65 | * - Top level object containing the data | |
66 | * @param collectionGroup | |
67 | * - CollectionGroup component for the collection | |
68 | */ | |
69 | public void build(View view, Object model, CollectionGroup collectionGroup) { | |
70 | // create add line | |
71 | 0 | if (collectionGroup.isRenderAddLine() && !collectionGroup.isReadOnly()) { |
72 | 0 | buildAddLine(view, model, collectionGroup); |
73 | } | |
74 | ||
75 | // get the collection for this group from the model | |
76 | 0 | List<Object> modelCollection = ObjectPropertyUtils.getPropertyValue(model, ((DataBinding) collectionGroup) |
77 | .getBindingInfo().getBindingPath()); | |
78 | ||
79 | // for each collection row create a new Group | |
80 | 0 | if (modelCollection != null) { |
81 | 0 | for (int index = 0; index < modelCollection.size(); index++) { |
82 | 0 | String bindingPathPrefix = collectionGroup.getBindingInfo().getBindingName() + "[" + index + "]"; |
83 | 0 | if (StringUtils.isNotBlank(collectionGroup.getBindingInfo().getBindByNamePrefix())) { |
84 | 0 | bindingPathPrefix = collectionGroup.getBindingInfo().getBindByNamePrefix() + "." |
85 | + bindingPathPrefix; | |
86 | } | |
87 | ||
88 | 0 | Object currentLine = modelCollection.get(index); |
89 | ||
90 | 0 | List<ActionField> actions = getLineActions(view, model, collectionGroup, currentLine, index); |
91 | 0 | buildLine(view, model, collectionGroup, bindingPathPrefix, actions, false, currentLine, index); |
92 | } | |
93 | } | |
94 | 0 | } |
95 | ||
96 | /** | |
97 | * Builds the fields for holding the collection add line and if necessary | |
98 | * makes call to setup the new line instance | |
99 | * | |
100 | * @param view | |
101 | * - view instance the collection belongs to | |
102 | * @param collectionGroup | |
103 | * - collection group the layout manager applies to | |
104 | * @param model | |
105 | * - Object containing the view data, should extend UifFormBase | |
106 | * if using framework managed new lines | |
107 | */ | |
108 | protected void buildAddLine(View view, Object model, CollectionGroup collectionGroup) { | |
109 | 0 | boolean bindAddLineToForm = false; |
110 | ||
111 | // determine whether the add line binds to the generic form map or a | |
112 | // specified property | |
113 | 0 | if (StringUtils.isBlank(collectionGroup.getAddLinePropertyName())) { |
114 | 0 | initializeNewCollectionLine(view, model, collectionGroup, false); |
115 | 0 | bindAddLineToForm = true; |
116 | ||
117 | // set binding path for add line | |
118 | 0 | String collectionAddLineKey = WebUtils.translateToMapSafeKey(collectionGroup.getBindingInfo() |
119 | .getBindingPath()); | |
120 | 0 | String addLineBindingPath = UifPropertyPaths.NEW_COLLECTION_LINES + "['" + collectionAddLineKey + "']"; |
121 | 0 | collectionGroup.getAddLineBindingInfo().setBindingPath(addLineBindingPath); |
122 | } | |
123 | ||
124 | 0 | String addLineBindingPath = collectionGroup.getAddLineBindingInfo().getBindingPath(); |
125 | 0 | List<ActionField> actions = getAddLineActions(view, model, collectionGroup); |
126 | ||
127 | 0 | buildLine(view, model, collectionGroup, addLineBindingPath, actions, bindAddLineToForm, null, -1); |
128 | 0 | } |
129 | ||
130 | /** | |
131 | * Builds the field instances for the collection line. A copy of the | |
132 | * configured items on the <code>CollectionGroup</code> is made and adjusted | |
133 | * for the line (id and binding). Then a call is made to the | |
134 | * <code>CollectionLayoutManager</code> to assemble the line as necessary | |
135 | * for the layout | |
136 | * | |
137 | * @param view | |
138 | * - view instance the collection belongs to | |
139 | * @param model | |
140 | * - top level object containing the data | |
141 | * @param collectionGroup | |
142 | * - collection group component for the collection | |
143 | * @param bindingPath | |
144 | * - binding path for the line fields (if DataBinding) | |
145 | * @param actions | |
146 | * - List of actions to set in the lines action column | |
147 | * @param bindLineToForm | |
148 | * - whether the bindToForm property on the items bindingInfo | |
149 | * should be set to true (needed for add line) | |
150 | * @param currentLine | |
151 | * - object instance for the current line, or null if add line | |
152 | * @param lineIndex | |
153 | * - index of the line in the collection, or -1 if we are | |
154 | * building the add line | |
155 | */ | |
156 | @SuppressWarnings("unchecked") | |
157 | protected void buildLine(View view, Object model, CollectionGroup collectionGroup, String bindingPath, | |
158 | List<ActionField> actions, boolean bindToForm, Object currentLine, int lineIndex) { | |
159 | 0 | CollectionLayoutManager layoutManager = (CollectionLayoutManager) collectionGroup.getLayoutManager(); |
160 | ||
161 | // copy group items for new line | |
162 | 0 | List<Field> lineFields = (List<Field>) ComponentUtils.copyFieldList(collectionGroup.getItems(), bindingPath); |
163 | 0 | ComponentUtils.updateContextsForLine(lineFields, currentLine, lineIndex); |
164 | ||
165 | 0 | if (bindToForm) { |
166 | 0 | ComponentUtils.setComponentsPropertyDeep(lineFields, UifPropertyPaths.BIND_TO_FORM, new Boolean(true)); |
167 | } | |
168 | ||
169 | // add generated fields to the view's index | |
170 | 0 | view.getViewIndex().addFields(lineFields); |
171 | ||
172 | // if not add line build sub-collection field groups | |
173 | 0 | List<GroupField> subCollectionFields = new ArrayList<GroupField>(); |
174 | 0 | if ((lineIndex != -1) && (collectionGroup.getSubCollections() != null)) { |
175 | 0 | for (int subLineIndex = 0; subLineIndex < collectionGroup.getSubCollections().size(); subLineIndex++) { |
176 | 0 | CollectionGroup subCollectionPrototype = collectionGroup.getSubCollections().get(subLineIndex); |
177 | 0 | CollectionGroup subCollectionGroup = ComponentUtils.copy(subCollectionPrototype); |
178 | ||
179 | 0 | subCollectionGroup.getBindingInfo().setBindByNamePrefix(bindingPath); |
180 | 0 | subCollectionGroup.getAddLineBindingInfo().setBindByNamePrefix(bindingPath); |
181 | ||
182 | 0 | GroupField groupFieldPrototype = layoutManager.getSubCollectionGroupFieldPrototype(); |
183 | 0 | GroupField subCollectionGroupField = ComponentUtils.copy(groupFieldPrototype); |
184 | 0 | subCollectionGroupField.setGroup(subCollectionGroup); |
185 | ||
186 | 0 | view.getViewIndex().addCollection(subCollectionGroup); |
187 | ||
188 | 0 | subCollectionFields.add(subCollectionGroupField); |
189 | } | |
190 | } | |
191 | ||
192 | // invoke layout manager to build the complete line | |
193 | 0 | layoutManager.buildLine(view, model, collectionGroup, lineFields, subCollectionFields, bindingPath, actions, |
194 | "_" + lineIndex, currentLine, lineIndex); | |
195 | 0 | } |
196 | ||
197 | /** | |
198 | * Creates new <code>ActionField</code> instances for the line | |
199 | * | |
200 | * <p> | |
201 | * Adds context to the action fields for the given line so that the line the | |
202 | * action was performed on can be determined when that action is selected | |
203 | * </p> | |
204 | * | |
205 | * @param view | |
206 | * - view instance the collection belongs to | |
207 | * @param model | |
208 | * - top level object containing the data | |
209 | * @param collectionGroup | |
210 | * - collection group component for the collection | |
211 | * @param collectionLine | |
212 | * - object instance for the current line | |
213 | * @param lineIndex | |
214 | * - index of the line the actions should apply to | |
215 | */ | |
216 | protected List<ActionField> getLineActions(View view, Object model, CollectionGroup collectionGroup, | |
217 | Object collectionLine, int lineIndex) { | |
218 | 0 | List<ActionField> lineActions = ComponentUtils.copyFieldList(collectionGroup.getActionFields()); |
219 | 0 | for (ActionField actionField : lineActions) { |
220 | 0 | actionField.addActionParameter(UifParameters.SELLECTED_COLLECTION_PATH, collectionGroup.getBindingInfo() |
221 | .getBindingPath()); | |
222 | 0 | actionField.addActionParameter(UifParameters.SELECTED_LINE_INDEX, Integer.toString(lineIndex)); |
223 | } | |
224 | ||
225 | 0 | ComponentUtils.updateContextsForLine(lineActions, collectionLine, lineIndex); |
226 | ||
227 | 0 | return lineActions; |
228 | } | |
229 | ||
230 | /** | |
231 | * Creates new <code>ActionField</code> instances for the add line | |
232 | * | |
233 | * <p> | |
234 | * Adds context to the action fields for the add line so that the collection | |
235 | * the action was performed on can be determined | |
236 | * </p> | |
237 | * | |
238 | * @param view | |
239 | * - view instance the collection belongs to | |
240 | * @param model | |
241 | * - top level object containing the data | |
242 | * @param collectionGroup | |
243 | * - collection group component for the collection | |
244 | */ | |
245 | protected List<ActionField> getAddLineActions(View view, Object model, CollectionGroup collectionGroup) { | |
246 | 0 | List<ActionField> lineActions = ComponentUtils.copyFieldList(collectionGroup.getAddLineActionFields()); |
247 | 0 | for (ActionField actionField : lineActions) { |
248 | 0 | actionField.addActionParameter(UifParameters.SELLECTED_COLLECTION_PATH, collectionGroup.getBindingInfo() |
249 | .getBindingPath()); | |
250 | } | |
251 | ||
252 | // get add line for context | |
253 | 0 | String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath(); |
254 | 0 | Object addLine = ObjectPropertyUtils.getPropertyValue(model, addLinePath); |
255 | ||
256 | 0 | ComponentUtils.updateContextsForLine(lineActions, addLine, -1); |
257 | ||
258 | 0 | return lineActions; |
259 | } | |
260 | ||
261 | /** | |
262 | * Initializes a new instance of the collection class (configured on the | |
263 | * collection group) in the UifFormBase generic newCollectionLines Map | |
264 | * | |
265 | * @see org.kuali.rice.kns.uif.container.CollectionGroup. | |
266 | * initializeNewCollectionLine(View, Object, CollectionGroup, boolean) | |
267 | */ | |
268 | public void initializeNewCollectionLine(View view, Object model, CollectionGroup collectionGroup, | |
269 | boolean clearExistingLine) { | |
270 | 0 | if (!(model instanceof UifFormBase)) { |
271 | 0 | throw new RuntimeException("Cannot create new collection line for group: " |
272 | + collectionGroup.getPropertyName() + ". Model does not extend " + UifFormBase.class.getName()); | |
273 | } | |
274 | ||
275 | // get new collection line map from form | |
276 | 0 | Map<String, Object> newCollectionLines = ObjectPropertyUtils.getPropertyValue(model, |
277 | UifPropertyPaths.NEW_COLLECTION_LINES); | |
278 | 0 | if (newCollectionLines == null) { |
279 | 0 | newCollectionLines = new HashMap<String, Object>(); |
280 | 0 | ObjectPropertyUtils.setPropertyValue(model, UifPropertyPaths.NEW_COLLECTION_LINES, newCollectionLines); |
281 | } | |
282 | ||
283 | // if there is not an instance available or we need to clear create a | |
284 | // new instance | |
285 | 0 | BindingInfo bindingInfo = collectionGroup.getBindingInfo(); |
286 | 0 | String newCollectionLineKey = WebUtils.translateToMapSafeKey(bindingInfo.getBindingPath()); |
287 | 0 | if (!newCollectionLines.containsKey(newCollectionLineKey) |
288 | || (newCollectionLines.get(newCollectionLineKey) == null) || clearExistingLine) { | |
289 | // create new instance of the collection type for the add line | |
290 | try { | |
291 | 0 | Object newLineInstance = collectionGroup.getCollectionObjectClass().newInstance(); |
292 | 0 | newCollectionLines.put(newCollectionLineKey, newLineInstance); |
293 | } | |
294 | 0 | catch (Exception e) { |
295 | 0 | throw new RuntimeException("Cannot create new add line instance for group: " |
296 | + collectionGroup.getPropertyName() + " with collection class: " | |
297 | + collectionGroup.getCollectionObjectClass().getName()); | |
298 | 0 | } |
299 | } | |
300 | 0 | } |
301 | ||
302 | } |