Coverage Report - org.kuali.rice.krad.uif.widget.RichTable
 
Classes in this File Line Coverage Branch Coverage Complexity
RichTable
0%
0/95
0%
0/78
4.9
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.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/ecl2.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.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.control.TextControl;
 34  
 import org.kuali.rice.krad.uif.component.Component;
 35  
 import org.kuali.rice.krad.uif.field.FieldGroup;
 36  
 import org.kuali.rice.krad.uif.layout.LayoutManager;
 37  
 import org.kuali.rice.krad.uif.layout.TableLayoutManager;
 38  
 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
 39  
 import org.kuali.rice.krad.web.form.UifFormBase;
 40  
 
 41  
 import java.sql.Timestamp;
 42  
 
 43  
 /**
 44  
  * Decorates a HTML Table client side with various tools
 45  
  *
 46  
  * <p>
 47  
  * Decorations implemented depend on widget implementation. Examples are
 48  
  * sorting, paging and skinning.
 49  
  * </p>
 50  
  *
 51  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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  0
     private boolean showSearchAndExportOptions = true;
 60  
 
 61  
     public RichTable() {
 62  0
         super();
 63  0
     }
 64  
 
 65  
     /**
 66  
      * The following initialization is performed:
 67  
      *
 68  
      * <ul>
 69  
      * <li>Initializes component options for empty table message</li>
 70  
      * </ul>
 71  
      */
 72  
     @Override
 73  
     public void performFinalize(View view, Object model, Component component) {
 74  0
         super.performFinalize(view, model, component);
 75  
 
 76  0
         UifFormBase formBase = (UifFormBase) model;
 77  
 
 78  0
         if (isRender()) {
 79  0
             if (StringUtils.isNotBlank(getEmptyTableMessage())) {
 80  0
                 getComponentOptions().put(UifConstants.TableToolsKeys.LANGUAGE,
 81  
                         "{\"" + UifConstants.TableToolsKeys.EMPTY_TABLE + "\" : \"" + getEmptyTableMessage() + "\"}");
 82  
             }
 83  
 
 84  0
             if (!isShowSearchAndExportOptions()) {
 85  0
                 Object domOption = getComponentOptions().get(UifConstants.TableToolsKeys.SDOM);
 86  0
                 if (domOption instanceof String) {
 87  0
                     String sDomOption = (String) domOption;
 88  0
                     if (StringUtils.isNotBlank(sDomOption)) {
 89  0
                         sDomOption = StringUtils.remove(sDomOption, "T"); //Removes Export option
 90  0
                         sDomOption = StringUtils.remove(sDomOption, "f"); //Removes search option
 91  0
                         getComponentOptions().put(UifConstants.TableToolsKeys.SDOM, sDomOption);
 92  
                     }
 93  
                 }
 94  
 
 95  
             }
 96  
 
 97  
             // for add events, disable initial sorting
 98  0
             if (UifConstants.ActionEvents.ADD_LINE.equals(formBase.getActionEvent())) {
 99  0
                 getComponentOptions().put(UifConstants.TableToolsKeys.AASORTING, "[]");
 100  
             }
 101  
 
 102  0
             if (component instanceof CollectionGroup) {
 103  0
                 buildTableSortOptions((CollectionGroup) component);
 104  
             }
 105  
 
 106  0
             if (isDisableTableSort()) {
 107  0
                 getComponentOptions().put(UifConstants.TableToolsKeys.TABLE_SORT, "false");
 108  
             }
 109  
         }
 110  0
     }
 111  
 
 112  
     /**
 113  
      * Builds column options for sorting
 114  
      *
 115  
      * @param collectionGroup
 116  
      */
 117  
     protected void buildTableSortOptions(CollectionGroup collectionGroup) {
 118  0
         LayoutManager layoutManager = collectionGroup.getLayoutManager();
 119  
 
 120  
         // if sub collection exists, don't allow the table sortable
 121  0
         if (!collectionGroup.getSubCollections().isEmpty()) {
 122  0
             setDisableTableSort(true);
 123  
         }
 124  
 
 125  0
        if (!isDisableTableSort()) {
 126  
             // if rendering add line, skip that row from col sorting
 127  0
             if (collectionGroup.isRenderAddLine()
 128  
                     && !collectionGroup.isReadOnly()
 129  
                     && !((layoutManager instanceof TableLayoutManager) && ((TableLayoutManager) layoutManager)
 130  
                     .isSeparateAddLine())) {
 131  0
                 getComponentOptions().put(UifConstants.TableToolsKeys.SORT_SKIP_ROWS,
 132  
                         "[" + UifConstants.TableToolsValues.ADD_ROW_DEFAULT_INDEX + "]");
 133  
             }
 134  
 
 135  0
             StringBuffer tableToolsColumnOptions = new StringBuffer("[");
 136  
 
 137  0
             if (layoutManager instanceof TableLayoutManager && ((TableLayoutManager) layoutManager)
 138  
                     .isRenderSequenceField()) {
 139  0
                 tableToolsColumnOptions.append(" null ,");
 140  
             }
 141  
 
 142  
             // skip select field if enabled
 143  0
             if (collectionGroup.isRenderSelectField()) {
 144  0
                 String colOptions = constructTableColumnOptions(false, null, null);
 145  0
                 tableToolsColumnOptions.append(colOptions + " , ");
 146  
             }
 147  
 
 148  
             // TODO: does this handle multiple rows correctly?
 149  0
             for (Component component : collectionGroup.getItems()) {
 150  
                 // For FieldGroup, get the first field from that group
 151  0
                 if (component instanceof FieldGroup) {
 152  0
                     component = ((FieldGroup) component).getItems().get(0);
 153  
                 }
 154  
 
 155  0
                 if (component instanceof DataField) {
 156  0
                     DataField field = (DataField) component;
 157  
 
 158  0
                     String sortType = null;
 159  0
                     if (!collectionGroup.isReadOnly() && (field instanceof InputField)
 160  
                             && ((InputField) field).getControl() != null) {
 161  0
                         Control control = ((InputField) field).getControl();
 162  0
                         if (control instanceof SelectControl) {
 163  0
                             sortType = UifConstants.TableToolsValues.DOM_SELECT;
 164  0
                         } else if (control instanceof CheckboxControl || control instanceof CheckboxGroupControl) {
 165  0
                             sortType = UifConstants.TableToolsValues.DOM_CHECK;
 166  0
                         } else if (control instanceof RadioGroupControl) {
 167  0
                             sortType = UifConstants.TableToolsValues.DOM_RADIO;
 168  
                         } else {
 169  0
                             sortType = UifConstants.TableToolsValues.DOM_TEXT;
 170  
                         }
 171  0
                     } else {
 172  0
                         sortType = UifConstants.TableToolsValues.DOM_TEXT;
 173  
                     }
 174  
 
 175  0
                     Class dataTypeClass = ObjectPropertyUtils.getPropertyType(
 176  
                             collectionGroup.getCollectionObjectClass(), field.getPropertyName());
 177  0
                     String colOptions = constructTableColumnOptions(true, dataTypeClass, sortType);
 178  0
                     tableToolsColumnOptions.append(colOptions + " , ");
 179  0
                 } else {
 180  0
                     String colOptions = constructTableColumnOptions(false, null, null);
 181  0
                     tableToolsColumnOptions.append(colOptions + " , ");
 182  0
                 }
 183  
             }
 184  
 
 185  0
             if (collectionGroup.isRenderLineActions() && !collectionGroup.isReadOnly()) {
 186  0
                 String colOptions = constructTableColumnOptions(false, null, null);
 187  0
                 tableToolsColumnOptions.append(colOptions);
 188  0
             } else {
 189  0
                 tableToolsColumnOptions = new StringBuffer(StringUtils.removeEnd(tableToolsColumnOptions.toString(),
 190  
                         ", "));
 191  
             }
 192  
 
 193  0
             tableToolsColumnOptions.append("]");
 194  
 
 195  0
             getComponentOptions().put(UifConstants.TableToolsKeys.AO_COLUMNS, tableToolsColumnOptions.toString());
 196  
        }
 197  0
     }
 198  
 
 199  
     /**
 200  
      * Constructs the sort data type for each datatable columns.
 201  
      */
 202  
     protected String constructTableColumnOptions(boolean isSortable, Class dataTypeClass, String sortDataType) {
 203  0
         String colOptions = "null";
 204  
 
 205  0
         String sortType = "";
 206  0
         if (!isSortable || dataTypeClass == null || sortType == null) {
 207  0
             colOptions = "\"" + UifConstants.TableToolsKeys.SORTABLE + "\" : false, \"sType\" : \"string\"";
 208  
         } else {
 209  0
             if (ClassUtils.isAssignable(dataTypeClass, KualiPercent.class)) {
 210  0
                 sortType = UifConstants.TableToolsValues.PERCENT;
 211  0
             } else if (ClassUtils.isAssignable(dataTypeClass, KualiInteger.class) || ClassUtils.isAssignable(
 212  
                     dataTypeClass, KualiDecimal.class)) {
 213  0
                 sortType = UifConstants.TableToolsValues.CURRENCY;
 214  0
             } else if (ClassUtils.isAssignable(dataTypeClass, Timestamp.class)) {
 215  0
                 sortType = "date";
 216  0
             } else if (ClassUtils.isAssignable(dataTypeClass, java.sql.Date.class) || ClassUtils.isAssignable(
 217  
                     dataTypeClass, java.util.Date.class)) {
 218  0
                 sortType = UifConstants.TableToolsValues.DATE;
 219  0
             } else if (ClassUtils.isAssignable(dataTypeClass, Number.class)) {
 220  0
                 sortType = UifConstants.TableToolsValues.NUMERIC;
 221  
             }
 222  
             else {
 223  0
                 sortType = UifConstants.TableToolsValues.STRING;
 224  
             }
 225  
 
 226  0
             colOptions = "\"" + UifConstants.TableToolsKeys.SORT_DATA_TYPE + "\" : \"" + sortDataType + "\"";
 227  0
             colOptions += " , \"" + UifConstants.TableToolsKeys.SORT_TYPE + "\" : \"" + sortType + "\"";
 228  
         }
 229  
 
 230  0
         colOptions = "{" + colOptions + "}";
 231  
 
 232  0
         return colOptions;
 233  
     }
 234  
 
 235  
     /**
 236  
      * Returns the text which is used to display text when the table is empty
 237  
      *
 238  
      * @return empty table message
 239  
      */
 240  
     public String getEmptyTableMessage() {
 241  0
         return emptyTableMessage;
 242  
     }
 243  
 
 244  
     /**
 245  
      * Setter for a text to be displayed when the table is empty
 246  
      *
 247  
      * @param emptyTableMessage
 248  
      */
 249  
     public void setEmptyTableMessage(String emptyTableMessage) {
 250  0
         this.emptyTableMessage = emptyTableMessage;
 251  0
     }
 252  
 
 253  
     /**
 254  
      * Returns true if sorting is disabled
 255  
      *
 256  
      * @return the disableTableSort
 257  
      */
 258  
     public boolean isDisableTableSort() {
 259  0
         return this.disableTableSort;
 260  
     }
 261  
 
 262  
     /**
 263  
      * Enables/disables the table sorting
 264  
      *
 265  
      * @param disableTableSort the disableTableSort to set
 266  
      */
 267  
     public void setDisableTableSort(boolean disableTableSort) {
 268  0
         this.disableTableSort = disableTableSort;
 269  0
     }
 270  
 
 271  
     /**
 272  
      * Returns true if search and export options are enabled
 273  
      *
 274  
      * @return the showSearchAndExportOptions
 275  
      */
 276  
     public boolean isShowSearchAndExportOptions() {
 277  0
         return this.showSearchAndExportOptions;
 278  
     }
 279  
 
 280  
     /**
 281  
      * Show/Hide the search and export options in tabletools
 282  
      *
 283  
      * @param showSearchAndExportOptions the showSearchAndExportOptions to set
 284  
      */
 285  
     public void setShowSearchAndExportOptions(boolean showSearchAndExportOptions) {
 286  0
         this.showSearchAndExportOptions = showSearchAndExportOptions;
 287  0
     }
 288  
 }