1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.widget;
17
18 import org.apache.commons.lang.ClassUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.kuali.rice.core.api.util.type.KualiDecimal;
21 import org.kuali.rice.core.api.util.type.KualiInteger;
22 import org.kuali.rice.core.api.util.type.KualiPercent;
23 import org.kuali.rice.krad.uif.UifConstants;
24 import org.kuali.rice.krad.uif.container.CollectionGroup;
25 import org.kuali.rice.krad.uif.control.Control;
26 import org.kuali.rice.krad.uif.field.DataField;
27 import org.kuali.rice.krad.uif.field.InputField;
28 import org.kuali.rice.krad.uif.view.View;
29 import org.kuali.rice.krad.uif.control.CheckboxControl;
30 import org.kuali.rice.krad.uif.control.CheckboxGroupControl;
31 import org.kuali.rice.krad.uif.control.RadioGroupControl;
32 import org.kuali.rice.krad.uif.control.SelectControl;
33 import org.kuali.rice.krad.uif.component.Component;
34 import org.kuali.rice.krad.uif.field.FieldGroup;
35 import org.kuali.rice.krad.uif.layout.LayoutManager;
36 import org.kuali.rice.krad.uif.layout.TableLayoutManager;
37 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
38 import org.kuali.rice.krad.web.form.UifFormBase;
39
40 import java.sql.Timestamp;
41 import java.util.Set;
42
43
44
45
46
47
48
49
50
51
52
53 public class RichTable extends WidgetBase {
54 private static final long serialVersionUID = 4671589690877390070L;
55
56 private String emptyTableMessage;
57 private boolean disableTableSort;
58
59 private Set<String> hiddenColumns;
60
61 private Set<String> sortableColumns;
62
63
64 private boolean showSearchAndExportOptions = true;
65
66 public RichTable() {
67 super();
68 }
69
70
71
72
73
74
75
76
77 @Override
78 public void performFinalize(View view, Object model, Component component) {
79 super.performFinalize(view, model, component);
80
81 UifFormBase formBase = (UifFormBase) model;
82
83 if (isRender()) {
84 if (StringUtils.isNotBlank(getEmptyTableMessage())) {
85 getComponentOptions().put(UifConstants.TableToolsKeys.LANGUAGE,
86 "{\"" + UifConstants.TableToolsKeys.EMPTY_TABLE + "\" : \"" + getEmptyTableMessage() + "\"}");
87 }
88
89 if (!isShowSearchAndExportOptions()) {
90 Object domOption = getComponentOptions().get(UifConstants.TableToolsKeys.SDOM);
91 if (domOption instanceof String) {
92 String sDomOption = (String) domOption;
93 if (StringUtils.isNotBlank(sDomOption)) {
94 sDomOption = StringUtils.remove(sDomOption, "T");
95 sDomOption = StringUtils.remove(sDomOption, "f");
96 getComponentOptions().put(UifConstants.TableToolsKeys.SDOM, sDomOption);
97 }
98 }
99
100 }
101
102
103 if (UifConstants.ActionEvents.ADD_LINE.equals(formBase.getActionEvent())) {
104 getComponentOptions().put(UifConstants.TableToolsKeys.AASORTING, "[]");
105 }
106
107 if (component instanceof CollectionGroup) {
108 buildTableOptions((CollectionGroup) component);
109 }
110
111 if (isDisableTableSort()) {
112 getComponentOptions().put(UifConstants.TableToolsKeys.TABLE_SORT, "false");
113 }
114 }
115 }
116
117
118
119
120
121
122 protected void buildTableOptions(CollectionGroup collectionGroup) {
123 LayoutManager layoutManager = collectionGroup.getLayoutManager();
124
125
126 if (!collectionGroup.getSubCollections().isEmpty()) {
127 setDisableTableSort(true);
128 }
129
130 if (!isDisableTableSort()) {
131
132 if (collectionGroup.isRenderAddLine()
133 && !collectionGroup.isReadOnly()
134 && !((layoutManager instanceof TableLayoutManager) && ((TableLayoutManager) layoutManager)
135 .isSeparateAddLine())) {
136 getComponentOptions().put(UifConstants.TableToolsKeys.SORT_SKIP_ROWS,
137 "[" + UifConstants.TableToolsValues.ADD_ROW_DEFAULT_INDEX + "]");
138 }
139
140 StringBuffer tableToolsColumnOptions = new StringBuffer("[");
141
142 if (layoutManager instanceof TableLayoutManager && ((TableLayoutManager) layoutManager)
143 .isRenderSequenceField()) {
144 tableToolsColumnOptions.append(" null ,");
145 }
146
147
148 if (collectionGroup.isRenderSelectField()) {
149 String colOptions = constructTableColumnOptions(false, null, null);
150 tableToolsColumnOptions.append(colOptions + " , ");
151 }
152
153
154 if (!StringUtils.isEmpty(getComponentOptions().get(UifConstants.TableToolsKeys.AO_COLUMNS))) {
155
156 String jsArray = getComponentOptions().get(UifConstants.TableToolsKeys.AO_COLUMNS);
157 int startBrace = StringUtils.indexOf(jsArray,"[");
158 int endBrace = StringUtils.lastIndexOf(jsArray, "]");
159 tableToolsColumnOptions.append(StringUtils.substring(jsArray, startBrace + 1, endBrace) + " , ");
160 } else {
161
162 Set<String> currentSortableColumns = getSortableColumns();
163 Set<String> currentHiddenColumns = getHiddenColumns();
164 if (layoutManager instanceof TableLayoutManager) {
165 TableLayoutManager tableLayoutMgr = (TableLayoutManager) layoutManager;
166 if (tableLayoutMgr.getSortableColumns() != null && !tableLayoutMgr.getSortableColumns().isEmpty()) {
167 currentSortableColumns = tableLayoutMgr.getSortableColumns();
168 }
169 if (tableLayoutMgr.getHiddenColumns() != null && !tableLayoutMgr.getHiddenColumns().isEmpty()) {
170 currentHiddenColumns = tableLayoutMgr.getHiddenColumns();
171 }
172 }
173
174 for (Component component : collectionGroup.getItems()) {
175
176 if (component instanceof FieldGroup) {
177 component = ((FieldGroup) component).getItems().get(0);
178 }
179
180 if (component instanceof DataField) {
181 DataField field = (DataField) component;
182
183 if (currentHiddenColumns != null && currentHiddenColumns.contains(field.getPropertyName())) {
184 tableToolsColumnOptions.append("{" + UifConstants.TableToolsKeys.VISIBLE + ": " + UifConstants.TableToolsValues.FALSE + "}, ");
185
186 } else if (currentSortableColumns != null && !currentSortableColumns.isEmpty()) {
187 if (currentSortableColumns.contains(field.getPropertyName())) {
188 tableToolsColumnOptions.append(getDataFieldColumnOptions(collectionGroup, field) + ", ");
189 } else {
190 tableToolsColumnOptions.append("{'" + UifConstants.TableToolsKeys.SORTABLE + "': " + UifConstants.TableToolsValues.FALSE + "}, ");
191 }
192 } else {
193 String colOptions = getDataFieldColumnOptions(collectionGroup, field);
194 tableToolsColumnOptions.append(colOptions + " , ");
195 }
196 } else {
197 String colOptions = constructTableColumnOptions(false, null, null);
198 tableToolsColumnOptions.append(colOptions + " , ");
199 }
200 }
201 }
202
203 if (collectionGroup.isRenderLineActions() && !collectionGroup.isReadOnly()) {
204 String colOptions = constructTableColumnOptions(false, null, null);
205 tableToolsColumnOptions.append(colOptions);
206 } else {
207 tableToolsColumnOptions = new StringBuffer(StringUtils.removeEnd(tableToolsColumnOptions.toString(),
208 ", "));
209 }
210
211 tableToolsColumnOptions.append("]");
212
213 getComponentOptions().put(UifConstants.TableToolsKeys.AO_COLUMNS, tableToolsColumnOptions.toString());
214 }
215 }
216
217
218
219
220
221
222
223
224 private String getDataFieldColumnOptions(CollectionGroup collectionGroup, DataField field) {
225 String sortType = null;
226 if (!collectionGroup.isReadOnly() && (field instanceof InputField)
227 && ((InputField) field).getControl() != null) {
228 Control control = ((InputField) field).getControl();
229 if (control instanceof SelectControl) {
230 sortType = UifConstants.TableToolsValues.DOM_SELECT;
231 } else if (control instanceof CheckboxControl || control instanceof CheckboxGroupControl) {
232 sortType = UifConstants.TableToolsValues.DOM_CHECK;
233 } else if (control instanceof RadioGroupControl) {
234 sortType = UifConstants.TableToolsValues.DOM_RADIO;
235 } else {
236 sortType = UifConstants.TableToolsValues.DOM_TEXT;
237 }
238 } else {
239 sortType = UifConstants.TableToolsValues.DOM_TEXT;
240 }
241
242 Class dataTypeClass = ObjectPropertyUtils.getPropertyType(collectionGroup.getCollectionObjectClass(),
243 field.getPropertyName());
244 return constructTableColumnOptions(true, dataTypeClass, sortType);
245 }
246
247
248
249
250 protected String constructTableColumnOptions(boolean isSortable, Class dataTypeClass, String sortDataType) {
251 String colOptions = "null";
252
253 String sortType = "";
254 if (!isSortable || dataTypeClass == null || sortType == null) {
255 colOptions = "\"" + UifConstants.TableToolsKeys.SORTABLE + "\" : false, \"sType\" : \"string\"";
256 } else {
257 if (ClassUtils.isAssignable(dataTypeClass, KualiPercent.class)) {
258 sortType = UifConstants.TableToolsValues.PERCENT;
259 } else if (ClassUtils.isAssignable(dataTypeClass, KualiInteger.class) || ClassUtils.isAssignable(
260 dataTypeClass, KualiDecimal.class)) {
261 sortType = UifConstants.TableToolsValues.CURRENCY;
262 } else if (ClassUtils.isAssignable(dataTypeClass, Timestamp.class)) {
263 sortType = "date";
264 } else if (ClassUtils.isAssignable(dataTypeClass, java.sql.Date.class) || ClassUtils.isAssignable(
265 dataTypeClass, java.util.Date.class)) {
266 sortType = UifConstants.TableToolsValues.DATE;
267 } else if (ClassUtils.isAssignable(dataTypeClass, Number.class)) {
268 sortType = UifConstants.TableToolsValues.NUMERIC;
269 }
270 else {
271 sortType = UifConstants.TableToolsValues.STRING;
272 }
273
274 colOptions = "\"" + UifConstants.TableToolsKeys.SORT_DATA_TYPE + "\" : \"" + sortDataType + "\"";
275 colOptions += " , \"" + UifConstants.TableToolsKeys.SORT_TYPE + "\" : \"" + sortType + "\"";
276 }
277
278 colOptions = "{" + colOptions + "}";
279
280 return colOptions;
281 }
282
283
284
285
286
287
288 public String getEmptyTableMessage() {
289 return emptyTableMessage;
290 }
291
292
293
294
295
296
297 public void setEmptyTableMessage(String emptyTableMessage) {
298 this.emptyTableMessage = emptyTableMessage;
299 }
300
301
302
303
304
305
306 public boolean isDisableTableSort() {
307 return this.disableTableSort;
308 }
309
310
311
312
313
314
315 public void setDisableTableSort(boolean disableTableSort) {
316 this.disableTableSort = disableTableSort;
317 }
318
319
320
321
322
323
324 public boolean isShowSearchAndExportOptions() {
325 return this.showSearchAndExportOptions;
326 }
327
328
329
330
331
332
333 public void setShowSearchAndExportOptions(boolean showSearchAndExportOptions) {
334 this.showSearchAndExportOptions = showSearchAndExportOptions;
335 }
336
337 public Set<String> getHiddenColumns() {
338 return hiddenColumns;
339 }
340
341 public void setHiddenColumns(Set<String> hiddenColumns) {
342 this.hiddenColumns = hiddenColumns;
343 }
344
345 public Set<String> getSortableColumns() {
346 return sortableColumns;
347 }
348
349 public void setSortableColumns(Set<String> sortableColumns) {
350 this.sortableColumns = sortableColumns;
351 }
352 }