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