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