Coverage Report - org.kuali.student.lum.common.client.lo.CategoryManagementTable
 
Classes in this File Line Coverage Branch Coverage Complexity
CategoryManagementTable
0%
0/152
0%
0/48
2.25
CategoryManagementTable$1
0%
0/7
0%
0/2
2.25
CategoryManagementTable$2
0%
0/7
N/A
2.25
 
 1  
 /**
 2  
  * Copyright 2010 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  
 
 16  
 package org.kuali.student.lum.common.client.lo;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.HashSet;
 20  
 import java.util.List;
 21  
 import java.util.Set;
 22  
 
 23  
 import org.kuali.student.common.ui.client.application.KSAsyncCallback;
 24  
 import org.kuali.student.common.ui.client.service.ServerPropertiesRpcService;
 25  
 import org.kuali.student.common.ui.client.service.ServerPropertiesRpcServiceAsync;
 26  
 import org.kuali.student.common.ui.client.widgets.pagetable.GenericTableModel;
 27  
 import org.kuali.student.common.ui.client.widgets.pagetable.PagingScrollTableBuilder;
 28  
 import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow;
 29  
 import org.kuali.student.common.ui.client.widgets.searchtable.SearchColumnDefinition;
 30  
 import org.kuali.student.lum.common.client.lo.rpc.LoCategoryRpcService;
 31  
 import org.kuali.student.lum.common.client.lo.rpc.LoCategoryRpcServiceAsync;
 32  
 import org.kuali.student.lum.lo.dto.LoCategoryInfo;
 33  
 
 34  
 import com.google.gwt.core.client.GWT;
 35  
 import com.google.gwt.gen2.table.client.AbstractColumnDefinition;
 36  
 import com.google.gwt.gen2.table.client.PagingScrollTable;
 37  
 import com.google.gwt.gen2.table.client.AbstractScrollTable.ResizePolicy;
 38  
 import com.google.gwt.gen2.table.client.SelectionGrid.SelectionPolicy;
 39  
 import com.google.gwt.user.client.Window;
 40  
 import com.google.gwt.user.client.ui.Composite;
 41  
 import com.google.gwt.user.client.ui.VerticalPanel;
 42  
 
 43  
 /**
 44  
  * This is a description of what this class does - Gary Struthers don't forget to fill this in. 
 45  
  * 
 46  
  * @author Kuali Student Team (gstruthers@berkeley.edu)
 47  
  *
 48  
  */
 49  0
 public class CategoryManagementTable extends Composite {
 50  0
     static String NAME_COLUMN_HEADER = "Name";
 51  0
     static String TYPE_COLUMN_HEADER = "Type";
 52  0
     static String STATE_COLUMN_HEADER = "State";
 53  0
     static String ID_COLUMN_KEY = "id";
 54  0
     static String NAME_COLUMN_KEY = "name";
 55  0
     static String TYPE_COLUMN_KEY = "type";
 56  0
     static String STATE_COLUMN_KEY = "state";
 57  0
     private List<ResultRow> resultRows = new ArrayList<ResultRow>();
 58  0
     private GenericTableModel<ResultRow> tableModel = new GenericTableModel<ResultRow>(resultRows);
 59  0
     private PagingScrollTableBuilder<ResultRow> builder = new PagingScrollTableBuilder<ResultRow>();
 60  
     protected PagingScrollTable<ResultRow> pagingScrollTable;
 61  0
     private VerticalPanel layout = new VerticalPanel();
 62  
     private static Boolean displayOnlyActiveCategories; // static global
 63  0
     private boolean hideInactiveCategories = false;
 64  
 
 65  0
     private LoCategoryRpcServiceAsync loCatRpcServiceAsync = GWT.create(LoCategoryRpcService.class);
 66  0
     private static ServerPropertiesRpcServiceAsync serverProperties = GWT.create(ServerPropertiesRpcService.class);
 67  
 
 68  
     /**
 69  
      * This method should be called before constructor so config flag is pre-set
 70  
      * only needs to be called once. It's a static flag that only changes when the 
 71  
      * server is started
 72  
      * 
 73  
      */
 74  
     public static void setDisplayOnlyActiveCategories() {
 75  0
         if (null == displayOnlyActiveCategories) {
 76  0
             serverProperties.get("ks.lum.ui.displayOnlyActiveLoCategories", new KSAsyncCallback<String>() {
 77  
                 @Override
 78  
                 public void handleFailure(Throwable caught) {
 79  0
                     GWT.log("get displayOnlyActiveLoCategories failed", caught);
 80  0
                     Window.alert("Failed to get displayOnlyActiveLoCategories setting");
 81  0
                 }
 82  
     
 83  
                 @Override
 84  
                 public void onSuccess(String result) {
 85  0
                     if (result != null) {
 86  0
                         displayOnlyActiveCategories = Boolean.parseBoolean(result);
 87  
                     }
 88  0
                 }
 89  
             });
 90  
         }  
 91  0
     }
 92  
     
 93  
     private void initCategoryManagementTable(SelectionPolicy selectionPolicy){
 94  0
         layout.setWidth("100%");
 95  0
         initWidget(layout);
 96  0
         builder = new PagingScrollTableBuilder<ResultRow>();
 97  0
         createColumnDefs();
 98  0
         builder.tablePixelSize(400, 300);
 99  0
         builder.setSelectionPolicy(selectionPolicy);
 100  0
     }
 101  
     public CategoryManagementTable() {
 102  0
         super();
 103  0
         initCategoryManagementTable(SelectionPolicy.ONE_ROW);
 104  0
     }
 105  
     /**
 106  
      * This constructs a CategoryManagementTable with an instance option
 107  
      * 
 108  
      * @param hideInactiveCategories
 109  
      */
 110  
     public CategoryManagementTable(boolean hideInactiveCategories, SelectionPolicy selectionPolicy) {
 111  0
         super();
 112  0
         this.hideInactiveCategories = hideInactiveCategories;
 113  0
         initCategoryManagementTable(selectionPolicy);
 114  0
     }
 115  
     /**
 116  
      * Two flags control whether to show rows with inactive categories and the state column.
 117  
      * hideInactiveCategories can be set per table instance
 118  
      * displayOnlyActiveCategories is set at Lum startup
 119  
      * hideInactiveCategories overrides displayOnlyActiveCategories
 120  
      * @return true to show all rows and State column
 121  
      */
 122  
     public boolean isHideInactiveCategories() {
 123  0
         if(hideInactiveCategories){
 124  0
             return true;
 125  
         } 
 126  0
         if((displayOnlyActiveCategories == null)||( displayOnlyActiveCategories.booleanValue() == false)){
 127  0
             return false;
 128  
         }else {
 129  0
             return true;
 130  
         }
 131  
     }
 132  
     /**
 133  
      * @param show or hide inactive rows and State column 
 134  
      */
 135  
     public void setHideInactiveCategories(boolean show) {
 136  0
         this.hideInactiveCategories = show;
 137  0
     }
 138  
     public void redraw(){
 139  0
         tableModel.setRows(resultRows);
 140  0
         pagingScrollTable = builder.build(tableModel);
 141  0
         pagingScrollTable.setResizePolicy(ResizePolicy.FILL_WIDTH);
 142  0
         layout.clear();
 143  0
         layout.add(pagingScrollTable);
 144  0
         pagingScrollTable.fillWidth();
 145  0
         pagingScrollTable.reloadPage();//FIXME Undesirable solution to work with GWT 2.0
 146  0
     }
 147  
     
 148  
     public void redraw(List<ResultRow> filteredRows){
 149  0
         tableModel.setRows(filteredRows);
 150  0
         pagingScrollTable = builder.build(tableModel);
 151  0
         pagingScrollTable.setResizePolicy(ResizePolicy.FILL_WIDTH);
 152  0
         layout.clear();
 153  0
         layout.add(pagingScrollTable);
 154  0
         pagingScrollTable.fillWidth();
 155  0
         pagingScrollTable.reloadPage();//FIXME Undesirable solution to work with GWT 2.0
 156  0
     }
 157  
 
 158  
     public void clearTable(){
 159  0
         resultRows.clear();
 160  0
         redraw();        
 161  0
     }
 162  
     
 163  
     public void removeSelected(){
 164  0
         for(ResultRow r: getSelectedRows()){
 165  0
             resultRows.remove(r);
 166  
         }
 167  0
         this.redraw();
 168  0
     }
 169  
     public List<ResultRow> getAllRows(){
 170  0
         List<ResultRow> rows = new ArrayList<ResultRow>();
 171  0
         for(ResultRow r: resultRows){
 172  0
             rows.add(r);
 173  
         }
 174  0
         return rows;
 175  
     }    
 176  
     public List<ResultRow> getSelectedRows(){
 177  0
         List<ResultRow> rows = new ArrayList<ResultRow>();
 178  0
         Set<Integer> selectedRows = pagingScrollTable.getDataTable().getSelectedRows();
 179  0
         for(Integer i: selectedRows){
 180  0
             rows.add(pagingScrollTable.getRowValue(i));
 181  
         }
 182  0
         return rows;
 183  
     }
 184  
     public List<LoCategoryInfo> getSelectedLoCategoryInfos(){
 185  0
         ResultRow resultRow = null;
 186  0
         List<LoCategoryInfo> loCategoryInfos = new ArrayList<LoCategoryInfo>();
 187  0
         Set<Integer> selectedRows = pagingScrollTable.getDataTable().getSelectedRows();
 188  0
         if(selectedRows.isEmpty()) {
 189  0
             return loCategoryInfos;
 190  
         }
 191  0
         for(Integer i: selectedRows){
 192  0
             resultRow = pagingScrollTable.getRowValue(i);
 193  0
             LoCategoryInfo loCategoryInfo = new LoCategoryInfo();
 194  0
             loCategoryInfo.setId(resultRow.getValue(ID_COLUMN_KEY));
 195  0
             loCategoryInfo.setName(resultRow.getValue(NAME_COLUMN_KEY));
 196  0
             loCategoryInfo.setType(resultRow.getValue(TYPE_COLUMN_KEY));
 197  0
             loCategoryInfo.setState(resultRow.getValue(STATE_COLUMN_KEY));
 198  0
             loCategoryInfos.add(loCategoryInfo);
 199  0
         }
 200  0
         return loCategoryInfos;
 201  
     }    
 202  
 
 203  
     public String getSelectedLoCategoryInfoId(){
 204  0
         ResultRow resultRow = null;
 205  
         
 206  0
         Set<Integer> selectedRows = pagingScrollTable.getDataTable().getSelectedRows();
 207  0
         if(selectedRows.isEmpty()) {
 208  0
             return null;
 209  
         }
 210  0
         String id = null;
 211  0
         for(Integer i: selectedRows){
 212  0
             resultRow = pagingScrollTable.getRowValue(i);
 213  0
             id = resultRow.getValue(ID_COLUMN_KEY);
 214  
 /*            loCategoryInfo.setId(resultRow.getValue(ID_COLUMN_KEY));
 215  
             loCategoryInfo.setName(resultRow.getValue(NAME_COLUMN_KEY));
 216  
             loCategoryInfo.setType(resultRow.getValue(TYPE_COLUMN_KEY));
 217  
             loCategoryInfo.setState(resultRow.getValue(STATE_COLUMN_KEY));
 218  
 */
 219  0
             break; // just get first one
 220  
         }
 221  0
         return id;
 222  
 
 223  
     }   
 224  
     
 225  
     private void createColumnDefs() {
 226  0
         List<AbstractColumnDefinition<ResultRow, ?>> columnDefs=new ArrayList<AbstractColumnDefinition<ResultRow, ?>>();
 227  0
         SearchColumnDefinition columnDef = new SearchColumnDefinition(NAME_COLUMN_HEADER, NAME_COLUMN_KEY);
 228  0
         columnDef.setColumnSortable(false);
 229  0
         columnDefs.add(columnDef);
 230  0
         columnDef = new SearchColumnDefinition(TYPE_COLUMN_HEADER, TYPE_COLUMN_KEY);
 231  0
         columnDef.setColumnSortable(false);
 232  0
         columnDefs.add(columnDef);            
 233  0
         if (!isHideInactiveCategories()) {
 234  0
             columnDef = new SearchColumnDefinition(STATE_COLUMN_HEADER, STATE_COLUMN_KEY);
 235  0
             columnDef.setColumnSortable(false);
 236  0
             columnDefs.add(columnDef);            
 237  
         }
 238  0
         if(columnDefs.size() == 1){
 239  0
             columnDefs.get(0).setMinimumColumnWidth(370);
 240  
         }
 241  0
         builder.columnDefinitions(columnDefs);
 242  0
     }
 243  
     
 244  
 
 245  
     
 246  
     private List<LoCategoryInfo> filterResults(List<LoCategoryInfo> result) {
 247  
 
 248  0
        if(isHideInactiveCategories()) {
 249  0
             List<LoCategoryInfo> filteredResult = new ArrayList<LoCategoryInfo>();
 250  0
             for(LoCategoryInfo info : result) {
 251  0
                 if (info.getState().equals("active") ) {
 252  0
                     filteredResult.add(info);
 253  
                 }
 254  
             }
 255  0
             return filteredResult;
 256  
         } 
 257  0
         return result;   
 258  
     }
 259  
     
 260  
     public void loadTable() {
 261  0
         loCatRpcServiceAsync.getLoCategories("kuali.loRepository.key.singleUse", new KSAsyncCallback<List<LoCategoryInfo>>() {
 262  
             @Override
 263  
             public void handleFailure(Throwable caught) {
 264  0
                 GWT.log("getLoCategories failed", caught);
 265  0
                 Window.alert("Get LoCategories failed");
 266  0
             }
 267  
 
 268  
             @Override
 269  
             public void onSuccess(List<LoCategoryInfo> results) {
 270  
 
 271  0
                 List<LoCategoryInfo> filteredResults = filterResults(results);
 272  0
                 loadTable(filteredResults);
 273  
                 /*
 274  
                 List<LoCategoryInfo> filteredResults = filterResults(results);
 275  
                 loadTable(filteredResults);
 276  
                 */
 277  0
             }
 278  
         }); 
 279  0
     }
 280  
     
 281  
     private void loadTable(List<LoCategoryInfo> loCategoryInfos) {
 282  0
         resultRows.clear();
 283  0
         HashSet<String> hashSet = new HashSet<String>();
 284  0
         String id = null;
 285  0
         for(LoCategoryInfo info : loCategoryInfos) {
 286  0
             ResultRow resultRow = new ResultRow();
 287  0
             id = info.getId();
 288  0
             if(!hashSet.contains(id)) {
 289  0
                 hashSet.add(id);
 290  0
                 resultRow.setValue(ID_COLUMN_KEY, id);
 291  0
                 resultRow.setValue(NAME_COLUMN_KEY, info.getName());
 292  0
                 resultRow.setValue(TYPE_COLUMN_KEY, info.getType());
 293  0
                 resultRow.setValue(STATE_COLUMN_KEY, info.getState());
 294  0
                 resultRows.add(resultRow);                
 295  
             }
 296  0
         }
 297  0
         redraw();
 298  0
     }    
 299  
 
 300  
     public List<ResultRow> getRowsByType(String type){
 301  0
         List<ResultRow> bufferList = new ArrayList<ResultRow>();
 302  0
         for(ResultRow row : resultRows) {
 303  0
             if(row.getValue(TYPE_COLUMN_KEY).contains(type)){
 304  0
                 bufferList.add(row);
 305  
             }
 306  
         }
 307  0
         return bufferList;
 308  
     }
 309  
     
 310  
     public List<ResultRow> getRowsLikeName(String name){
 311  0
         List<ResultRow> bufferList = new ArrayList<ResultRow>();
 312  0
         for(ResultRow row : resultRows) {
 313  0
             String nameValue = row.getValue(NAME_COLUMN_KEY);
 314  0
             if(nameValue != null) {
 315  0
                 String[] words = nameValue.split("\\W");
 316  0
                 for(String word : words){
 317  0
                     if(word.toUpperCase().startsWith(name.toUpperCase())){
 318  0
                         bufferList.add(row);
 319  0
                         break;
 320  
                     }                    
 321  
                 }
 322  
             }
 323  0
         }
 324  0
         return bufferList;
 325  
     }
 326  
 }