1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.layout.collections;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.uif.CssConstants;
20 import org.kuali.rice.krad.uif.UifConstants;
21 import org.kuali.rice.krad.uif.component.DataBinding;
22 import org.kuali.rice.krad.uif.container.CollectionGroup;
23 import org.kuali.rice.krad.uif.container.collections.LineBuilderContext;
24 import org.kuali.rice.krad.uif.element.Message;
25 import org.kuali.rice.krad.uif.field.Field;
26 import org.kuali.rice.krad.uif.field.FieldGroup;
27 import org.kuali.rice.krad.uif.field.MessageField;
28 import org.kuali.rice.krad.uif.field.SpaceField;
29 import org.kuali.rice.krad.uif.layout.CollectionLayoutUtils;
30 import org.kuali.rice.krad.uif.layout.TableLayoutManager;
31 import org.kuali.rice.krad.uif.util.ComponentFactory;
32 import org.kuali.rice.krad.uif.util.ComponentUtils;
33 import org.kuali.rice.krad.uif.util.ContextUtils;
34 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
35
36 import java.io.Serializable;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.List;
40 import java.util.Map;
41
42
43
44
45
46
47
48
49
50
51
52 public class TableRowBuilder implements Serializable {
53 private static final long serialVersionUID = 5098939594340088940L;
54
55 private CollectionGroup collectionGroup;
56 private TableLayoutManager tableLayoutManager;
57
58 private LineBuilderContext lineBuilderContext;
59
60
61
62
63 public TableRowBuilder() {
64
65 }
66
67
68
69
70
71
72
73 public TableRowBuilder(CollectionGroup collectionGroup, LineBuilderContext lineBuilderContext) {
74 this.collectionGroup = collectionGroup;
75
76 if (collectionGroup != null) {
77 this.tableLayoutManager = (TableLayoutManager) collectionGroup.getLayoutManager();
78 }
79
80 this.lineBuilderContext = lineBuilderContext;
81 }
82
83
84
85
86
87
88
89
90
91
92 public TableRow buildRow() {
93 ColumnCollector columnCollector = new ColumnCollector(tableLayoutManager.getActionColumnIndex());
94
95 if (tableLayoutManager.isRenderSequenceField()) {
96 addSequenceColumn(columnCollector);
97 }
98
99 if (collectionGroup.isIncludeLineSelectionField()) {
100 addLineSelectColumn(columnCollector);
101 }
102
103 boolean hasGrouping = (tableLayoutManager.getGroupingPropertyNames() != null) || StringUtils.isNotBlank(
104 tableLayoutManager.getGroupingTitle());
105 if (hasGrouping) {
106 addGroupingColumn(columnCollector);
107 }
108
109
110 for (Field lineField : lineBuilderContext.getLineFields()) {
111 Map<String, String> fieldDataAttributes = lineField.getDataAttributes();
112
113
114 boolean hasRoleAttribute = (fieldDataAttributes != null) && fieldDataAttributes.containsKey(
115 UifConstants.DataAttributes.ROLE);
116 if (hasRoleAttribute && UifConstants.RoleTypes.ROW_GROUPING.equals(fieldDataAttributes.get(
117 UifConstants.DataAttributes.ROLE))) {
118 continue;
119 }
120
121 columnCollector.addColumn(lineField);
122 }
123
124 columnCollector.finishRow();
125
126 return new TableRow(columnCollector.getColumns());
127 }
128
129
130
131
132
133
134
135
136
137 protected void addSequenceColumn(ColumnCollector columnCollector) {
138 Field sequenceField;
139
140 if (lineBuilderContext.isAddLine()) {
141 sequenceField = ComponentFactory.getMessageField();
142
143 Message sequenceMessage = ComponentUtils.copy(collectionGroup.getAddLineLabel(),
144 lineBuilderContext.getIdSuffix());
145 ((MessageField) sequenceField).setMessage(sequenceMessage);
146 } else {
147 sequenceField = ComponentUtils.copy(tableLayoutManager.getSequenceFieldPrototype(),
148 lineBuilderContext.getIdSuffix());
149
150
151 sequenceField.addDataAttribute(UifConstants.DataAttributes.VIGNORE, "yes");
152
153 if (tableLayoutManager.isGenerateAutoSequence() && (sequenceField instanceof MessageField)) {
154 ((MessageField) sequenceField).setMessageText(Integer.toString(lineBuilderContext.getLineIndex() + 1));
155 }
156
157 if (sequenceField instanceof DataBinding) {
158 ((DataBinding) sequenceField).getBindingInfo().setBindByNamePrefix(lineBuilderContext.getBindingPath());
159 }
160 }
161
162
163
164
165
166 ContextUtils.updateContextForLine(sequenceField, collectionGroup, lineBuilderContext.getCurrentLine(),
167 lineBuilderContext.getLineIndex(), lineBuilderContext.getIdSuffix());
168
169 columnCollector.addColumn(sequenceField);
170 }
171
172
173
174
175
176
177
178
179 protected void addLineSelectColumn(ColumnCollector columnCollector) {
180 Field selectField = ComponentUtils.copy(tableLayoutManager.getSelectFieldPrototype(),
181 lineBuilderContext.getIdSuffix());
182
183 CollectionLayoutUtils.prepareSelectFieldForLine(selectField, collectionGroup,
184 lineBuilderContext.getBindingPath(), lineBuilderContext.getCurrentLine());
185
186 ContextUtils.updateContextForLine(selectField, collectionGroup, lineBuilderContext.getCurrentLine(),
187 lineBuilderContext.getLineIndex(), lineBuilderContext.getIdSuffix());
188
189
190
191 columnCollector.addColumn(selectField);
192 }
193
194
195
196
197
198
199
200
201
202 protected void addGroupingColumn(ColumnCollector columnCollector) {
203
204 if (lineBuilderContext.isAddLine()) {
205 SpaceField spaceField = ComponentFactory.getSpaceField();
206 columnCollector.addColumn(spaceField);
207
208 return;
209 }
210
211 MessageField groupingMessageField = ComponentFactory.getColGroupingField();
212
213 StringBuilder groupingTitle = new StringBuilder();
214 if (StringUtils.isNotBlank(tableLayoutManager.getGroupingTitle())) {
215 groupingTitle.append(tableLayoutManager.getGroupingTitle());
216 } else if (tableLayoutManager.getGroupingPropertyNames() != null) {
217 for (String propertyName : tableLayoutManager.getGroupingPropertyNames()) {
218 Object propertyValue = ObjectPropertyUtils.getPropertyValue(lineBuilderContext.getCurrentLine(),
219 propertyName);
220
221 if (propertyValue == null) {
222 propertyValue = "Null";
223 }
224
225 if (groupingTitle.length() != 0) {
226 groupingTitle.append(", ");
227 }
228
229 groupingTitle.append(propertyValue);
230 }
231
232 }
233
234 groupingMessageField.setMessageText(groupingTitle.toString());
235 groupingMessageField.addDataAttribute(UifConstants.DataAttributes.ROLE, UifConstants.RoleTypes.ROW_GROUPING);
236
237 columnCollector.addColumn(groupingMessageField);
238 }
239
240
241
242
243 public class ColumnCollector implements Serializable {
244 private static final long serialVersionUID = 7129699106011942622L;
245
246 private int actionColumnIndex;
247
248 private int currentIndex = 0;
249 private List<Field> columns;
250
251
252
253
254
255
256 public ColumnCollector(int actionColumnIndex) {
257 this.actionColumnIndex = actionColumnIndex;
258 this.columns = new ArrayList<Field>();
259 }
260
261
262
263
264
265
266
267
268
269 public void addColumn(Field column) {
270 if (isRenderActions() && (actionColumnIndex == (currentIndex + 1))) {
271 Field actionColumn = buildActionColumn();
272
273 columns.add(actionColumn);
274 currentIndex++;
275 }
276
277 columns.add(column);
278 currentIndex++;
279 }
280
281
282
283
284
285 public void finishRow() {
286 if (isRenderActions() && (actionColumnIndex == (currentIndex + 1)) || (actionColumnIndex == -1)) {
287 Field actionColumn = buildActionColumn();
288
289 columns.add(actionColumn);
290 }
291 }
292
293
294
295
296
297
298
299
300
301
302 protected FieldGroup buildActionColumn() {
303 FieldGroup lineActionsField = ComponentUtils.copy(tableLayoutManager.getActionFieldPrototype(),
304 lineBuilderContext.getIdSuffix());
305
306 ContextUtils.updateContextForLine(lineActionsField, collectionGroup, lineBuilderContext.getCurrentLine(),
307 lineBuilderContext.getLineIndex(), lineBuilderContext.getIdSuffix());
308
309
310 lineActionsField.setItems(lineBuilderContext.getLineActions());
311
312 if (lineActionsField.getWrapperCssClasses() != null && !lineActionsField.getWrapperCssClasses().contains(
313 CssConstants.Classes.ACTION_COLUMN_STYLE_CLASS)) {
314 lineActionsField.getWrapperCssClasses().add(CssConstants.Classes.ACTION_COLUMN_STYLE_CLASS);
315 } else {
316 lineActionsField.setWrapperCssClasses(Arrays.asList(CssConstants.Classes.ACTION_COLUMN_STYLE_CLASS));
317 }
318
319
320
321 return lineActionsField;
322 }
323
324
325
326
327
328
329 public boolean isRenderActions() {
330 return collectionGroup.isRenderLineActions() && !Boolean.TRUE.equals(collectionGroup.getReadOnly());
331 }
332
333
334
335
336
337
338 public List<Field> getColumns() {
339 return columns;
340 }
341 }
342
343 }