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