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