View Javadoc

1   /**
2    * Copyright 2013 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   * Created by venkat on 8/8/13
16   */
17  package org.kuali.student.common.uif.widget;
18  
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.krad.uif.UifConstants;
21  import org.kuali.rice.krad.uif.component.Component;
22  import org.kuali.rice.krad.uif.container.CollectionGroup;
23  import org.kuali.rice.krad.uif.field.DataField;
24  import org.kuali.rice.krad.uif.field.Field;
25  import org.kuali.rice.krad.uif.field.FieldGroup;
26  import org.kuali.rice.krad.uif.field.LinkField;
27  import org.kuali.rice.krad.uif.field.MessageField;
28  import org.kuali.rice.krad.uif.layout.LayoutManager;
29  import org.kuali.rice.krad.uif.layout.TableLayoutManager;
30  import org.kuali.rice.krad.uif.widget.RichTable;
31  
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  
36  /**
37   *
38   * @author Kuali Student Team
39   */
40  
41  public class KSRichTable extends RichTable {
42      /**
43       * Builds column options for sorting
44       *
45       * @param collectionGroup
46       */
47      @Override
48      protected void buildTableOptions(CollectionGroup collectionGroup) {
49          LayoutManager layoutManager = collectionGroup.getLayoutManager();
50          final boolean isUseServerPaging = collectionGroup.isUseServerPaging();
51  
52          // if sub collection exists, don't allow the table sortable
53          if (!collectionGroup.getSubCollections().isEmpty()) {
54              setDisableTableSort(true);
55          }
56  
57          if (!isDisableTableSort()) {
58              // if rendering add line, skip that row from col sorting
59              if (collectionGroup.isRenderAddLine()
60                      && !collectionGroup.isReadOnly()
61                      && !((layoutManager instanceof TableLayoutManager) && ((TableLayoutManager) layoutManager)
62                              .isSeparateAddLine())) {
63                  Map<String, String> oTemplateOptions = this.getTemplateOptions();
64  
65                  if (oTemplateOptions == null) {
66                      setTemplateOptions(oTemplateOptions = new HashMap<String, String>());
67                  }
68  
69                  oTemplateOptions.put(UifConstants.TableToolsKeys.SORT_SKIP_ROWS,
70                          "[" + UifConstants.TableToolsValues.ADD_ROW_DEFAULT_INDEX + "]");
71              }
72  
73              StringBuilder tableToolsColumnOptions = new StringBuilder("[");
74  
75              int columnIndex = 0;
76              int actionIndex = UifConstants.TableLayoutValues.ACTIONS_COLUMN_RIGHT_INDEX;
77              boolean actionFieldVisible = collectionGroup.isRenderLineActions() && !collectionGroup.isReadOnly();
78  
79              if (layoutManager instanceof TableLayoutManager) {
80                  actionIndex = ((TableLayoutManager) layoutManager).getActionColumnIndex();
81              }
82  
83              if (actionIndex == UifConstants.TableLayoutValues.ACTIONS_COLUMN_LEFT_INDEX && actionFieldVisible) {
84                  String actionColOptions = constructTableColumnOptions(columnIndex, false, isUseServerPaging, null,
85                          null);
86                  tableToolsColumnOptions.append(actionColOptions + " , ");
87                  columnIndex++;
88              }
89  
90              if (layoutManager instanceof TableLayoutManager && ((TableLayoutManager) layoutManager)
91                      .isRenderSequenceField()) {
92  
93                  //add mData handling if using a json data source
94                  String mDataOption = "";
95                  if (collectionGroup.isUseServerPaging() || isForceLocalJsonData()) {
96                      mDataOption = "\""
97                              + UifConstants.TableToolsKeys.MDATA
98                              +
99                              "\" : function(row,type,newVal){ return _handleColData(row,type,'c"
100                             + columnIndex
101                             + "',newVal);}, ";
102                 }
103 
104                 tableToolsColumnOptions.append("{\""
105                         + UifConstants.TableToolsKeys.SORTABLE
106                         + "\" : "
107                         + false
108                         // auto sequence column is never sortable
109                         + ", \""
110                         + UifConstants.TableToolsKeys.SORT_TYPE
111                         + "\" : \""
112                         + UifConstants.TableToolsValues.NUMERIC
113                         + "\", "
114                         + mDataOption
115                         + "\""
116                         + UifConstants.TableToolsKeys.TARGETS
117                         + "\": ["
118                         + columnIndex
119                         + "]}, ");
120                 columnIndex++;
121                 if (actionIndex == 2 && actionFieldVisible) {
122                     String actionColOptions = constructTableColumnOptions(columnIndex, false, isUseServerPaging, null,
123                             null);
124                     tableToolsColumnOptions.append(actionColOptions + " , ");
125                     columnIndex++;
126                 }
127             }
128 
129             // skip select field if enabled
130             if (collectionGroup.isIncludeLineSelectionField()) {
131                 String colOptions = constructTableColumnOptions(columnIndex, false, isUseServerPaging, null, null);
132                 tableToolsColumnOptions.append(colOptions + " , ");
133                 columnIndex++;
134             }
135 
136             // if data dictionary defines aoColumns, copy here and skip default sorting/visibility behaviour
137             if (!StringUtils.isEmpty(getTemplateOptions().get(UifConstants.TableToolsKeys.AO_COLUMNS))) {
138                 // get the contents of the JS array string
139                 String jsArray = getTemplateOptions().get(UifConstants.TableToolsKeys.AO_COLUMNS);
140                 int startBrace = StringUtils.indexOf(jsArray, "[");
141                 int endBrace = StringUtils.lastIndexOf(jsArray, "]");
142                 tableToolsColumnOptions.append(StringUtils.substring(jsArray, startBrace + 1, endBrace) + ", ");
143 
144                 if (actionFieldVisible && (actionIndex == -1 || actionIndex >= columnIndex)) {
145                     String actionColOptions = constructTableColumnOptions(columnIndex, false, isUseServerPaging, null,
146                             null);
147                     tableToolsColumnOptions.append(actionColOptions);
148                 } else {
149                     tableToolsColumnOptions = new StringBuilder(StringUtils.removeEnd(
150                             tableToolsColumnOptions.toString(),
151                             ", "));
152                 }
153 
154                 tableToolsColumnOptions.append("]");
155                 getTemplateOptions().put(UifConstants.TableToolsKeys.AO_COLUMNS, tableToolsColumnOptions.toString());
156             } else if (!StringUtils.isEmpty(getTemplateOptions().get(UifConstants.TableToolsKeys.AO_COLUMN_DEFS))
157                     && isForceAoColumnDefsOverride()) {
158                 String jsArray = getTemplateOptions().get(UifConstants.TableToolsKeys.AO_COLUMN_DEFS);
159                 int startBrace = StringUtils.indexOf(jsArray, "[");
160                 int endBrace = StringUtils.lastIndexOf(jsArray, "]");
161                 tableToolsColumnOptions.append(StringUtils.substring(jsArray, startBrace + 1, endBrace) + ", ");
162 
163                 if (actionFieldVisible && (actionIndex == -1 || actionIndex >= columnIndex)) {
164                     String actionColOptions = constructTableColumnOptions(columnIndex, false, isUseServerPaging, null,
165                             null);
166                     tableToolsColumnOptions.append(actionColOptions);
167                 } else {
168                     tableToolsColumnOptions = new StringBuilder(StringUtils.removeEnd(
169                             tableToolsColumnOptions.toString(),
170                             ", "));
171                 }
172 
173                 tableToolsColumnOptions.append("]");
174                 getTemplateOptions().put(UifConstants.TableToolsKeys.AO_COLUMN_DEFS,
175                         tableToolsColumnOptions.toString());
176             } else if (layoutManager instanceof TableLayoutManager) {
177                 List<Field> rowFields = ((TableLayoutManager) layoutManager).getFirstRowFields();
178 
179                 // build column defs from the the first row of the table
180                 for (Component component : rowFields) {
181                     if (actionFieldVisible && columnIndex + 1 == actionIndex) {
182                         String actionColOptions = constructTableColumnOptions(columnIndex, false, isUseServerPaging,
183                                 null, null);
184                         tableToolsColumnOptions.append(actionColOptions + " , ");
185                         columnIndex++;
186                     }
187 
188                     //add mData handling if using a json data source
189                     String mDataOption = "";
190                     if (collectionGroup.isUseServerPaging() || isForceLocalJsonData()) {
191                         mDataOption = "\""
192                                 + UifConstants.TableToolsKeys.MDATA
193                                 +
194                                 "\" : function(row,type,newVal){ return _handleColData(row,type,'c"
195                                 + columnIndex
196                                 + "',newVal);}, ";
197                     }
198 
199                     // for FieldGroup, get the first field from that group
200                     if (component instanceof FieldGroup) {
201                         component = ((FieldGroup) component).getItems().get(0);
202                     }
203 
204                     Map<String, String> componentDataAttributes;
205                     if (component instanceof DataField) {
206                         DataField field = (DataField) component;
207 
208                         // if a field is marked as invisible in hiddenColumns, append options and skip sorting
209                         if (getHiddenColumns() != null && getHiddenColumns().contains(field.getPropertyName())) {
210                             tableToolsColumnOptions.append("{"
211                                     + UifConstants.TableToolsKeys.VISIBLE
212                                     + ": "
213                                     + UifConstants.TableToolsValues.FALSE
214                                     + ", "
215                                     + mDataOption
216                                     + "\""
217                                     + UifConstants.TableToolsKeys.TARGETS
218                                     + "\": ["
219                                     + columnIndex
220                                     + "]"
221                                     + "}, ");
222                             // if sortableColumns is present and a field is marked as sortable or unspecified
223                         } else if (getSortableColumns() != null && !getSortableColumns().isEmpty()) {
224                             if (getSortableColumns().contains(field.getPropertyName())) {
225                                 tableToolsColumnOptions.append(getDataFieldColumnOptions(columnIndex, collectionGroup,
226                                         field) + ", ");
227                             } else {
228                                 tableToolsColumnOptions.append("{'"
229                                         + UifConstants.TableToolsKeys.SORTABLE
230                                         + "': "
231                                         + UifConstants.TableToolsValues.FALSE
232                                         + ", "
233                                         + mDataOption
234                                         + "\""
235                                         + UifConstants.TableToolsKeys.TARGETS
236                                         + "\": ["
237                                         + columnIndex
238                                         + "]"
239                                         + "}, ");
240                             }
241                         } else {// sortable columns not defined
242                             String colOptions = getDataFieldColumnOptions(columnIndex, collectionGroup, field);
243                             tableToolsColumnOptions.append(colOptions + " , ");
244                         }
245                         columnIndex++;
246                     } else if ((component instanceof MessageField)
247                             && (componentDataAttributes = component.getDataAttributes()) != null
248                             && UifConstants.RoleTypes.ROW_GROUPING.equals(componentDataAttributes.get(
249                                     UifConstants.DataAttributes.ROLE))) {
250                         //Grouping column is never shown, so skip
251                         tableToolsColumnOptions.append("{"
252                                 + UifConstants.TableToolsKeys.VISIBLE
253                                 + ": "
254                                 + UifConstants.TableToolsValues.FALSE
255                                 + ", "
256                                 + mDataOption
257                                 + "\""
258                                 + UifConstants.TableToolsKeys.TARGETS
259                                 + "\": ["
260                                 + columnIndex
261                                 + "]"
262                                 + "}, ");
263                         columnIndex++;
264                     // start KS customization (to allow message field sorting) - KSENROLL-4999
265                     } else if (component instanceof MessageField) {
266                         componentDataAttributes = component.getDataAttributes();
267                         //For message field, sort as a regular String
268                         String sortType = "";
269                         if (componentDataAttributes != null) {
270                             sortType = componentDataAttributes.get("sortType");
271                         }
272                         if (StringUtils.isBlank(sortType)){
273                             sortType = UifConstants.TableToolsValues.DOM_TEXT;
274                         }
275                         String colOptions = constructTableColumnOptions(columnIndex, true, isUseServerPaging,
276                                 String.class, sortType);
277                         tableToolsColumnOptions.append(colOptions + " , ");
278                         columnIndex++;
279                     // end KS customization (to allow message field sorting) - KSENROLL-4999
280                     } else if (component instanceof LinkField) {
281                         String colOptions = constructTableColumnOptions(columnIndex, true, isUseServerPaging,
282                                 String.class, UifConstants.TableToolsValues.DOM_TEXT);
283                         tableToolsColumnOptions.append(colOptions + " , ");
284                         columnIndex++;
285                     } else {
286                         String colOptions = constructTableColumnOptions(columnIndex, false, isUseServerPaging, null,
287                                 null);
288                         tableToolsColumnOptions.append(colOptions + " , ");
289                         columnIndex++;
290                     }
291 
292                 }
293 
294                 if (actionFieldVisible && (actionIndex == -1 || actionIndex >= columnIndex)) {
295                     String actionColOptions = constructTableColumnOptions(columnIndex, false, isUseServerPaging, null,
296                             null);
297                     tableToolsColumnOptions.append(actionColOptions);
298                 } else {
299                     tableToolsColumnOptions = new StringBuilder(StringUtils.removeEnd(
300                             tableToolsColumnOptions.toString(),
301                             ", "));
302                 }
303                 //merge the aoColumnDefs passed in
304                 if (!StringUtils.isEmpty(getTemplateOptions().get(UifConstants.TableToolsKeys.AO_COLUMN_DEFS))) {
305                     String origAoOptions = getTemplateOptions().get(UifConstants.TableToolsKeys.AO_COLUMN_DEFS).trim();
306                     origAoOptions = StringUtils.removeStart(origAoOptions, "[");
307                     origAoOptions = StringUtils.removeEnd(origAoOptions, "]");
308                     tableToolsColumnOptions.append("," + origAoOptions);
309                 }
310 
311                 tableToolsColumnOptions.append("]");
312                 getTemplateOptions().put(UifConstants.TableToolsKeys.AO_COLUMN_DEFS,
313                         tableToolsColumnOptions.toString());
314             }
315         }
316     }
317 
318 }