Coverage Report - org.kuali.student.common.ui.client.widgets.pagetable.GenericTableModel
 
Classes in this File Line Coverage Branch Coverage Complexity
GenericTableModel
0%
0/40
0%
0/4
1.438
GenericTableModel$1
0%
0/19
0%
0/10
1.438
GenericTableModel$1$1
0%
0/2
N/A
1.438
 
 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.common.ui.client.widgets.pagetable;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Collections;
 20  
 import java.util.Comparator;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 
 24  
 import org.kuali.student.common.dto.Idable;
 25  
 import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow;
 26  
 import org.kuali.student.common.ui.client.widgets.searchtable.SearchColumnDefinition;
 27  
 
 28  
 import com.google.gwt.gen2.table.client.AbstractColumnDefinition;
 29  
 import com.google.gwt.gen2.table.client.CachedTableModel;
 30  
 import com.google.gwt.gen2.table.client.MutableTableModel;
 31  
 import com.google.gwt.gen2.table.client.TableModelHelper.Request;
 32  
 import com.google.gwt.gen2.table.client.TableModelHelper.Response;
 33  
 import com.google.gwt.gen2.table.client.TableModelHelper.SerializableResponse;
 34  
 
 35  
 
 36  
 /**
 37  
  * This is a description of what this class does - Gary Struthers don't forget to fill this in.
 38  
  *
 39  
  * @author Kuali Student Team (gstruthers@berkeley.edu)
 40  
  * @param <IsSerializiable>
 41  
  *
 42  
  */
 43  0
 public class GenericTableModel<RowType> extends MutableTableModel<RowType> {
 44  0
     private List<RowType> rowDTOs = null;
 45  0
     private int fromIndex = 0;
 46  0
     private int lastSortedColumn = -1;
 47  0
     private int lastSortDirection = 1;
 48  
 
 49  
 
 50  
     private List<AbstractColumnDefinition<ResultRow, ?>> columnDefs;
 51  
 
 52  
     /**
 53  
      * A boolean indicating that we should throw an error.
 54  
      */
 55  0
     private boolean errorMode = false;
 56  
     /**
 57  
      * A boolean indicating that we should return 0 rowDTOs in the response.
 58  
      */
 59  0
     private boolean zeroMode = false;
 60  
     /**
 61  
      * This constructs an empty table model for the RowType
 62  
      *
 63  
      */
 64  
     public GenericTableModel() {
 65  0
         super();
 66  0
     }
 67  
     /**
 68  
      * This constructs a populated table model for the RowType
 69  
      *
 70  
      */
 71  
     public GenericTableModel(List<RowType>rows) {
 72  0
         super();
 73  0
         setRows(rows);
 74  0
     }
 75  
     /**
 76  
      * This returns cached table model for the RowType
 77  
      *
 78  
      */
 79  
     public CachedTableModel<RowType>createCachedTableModel(int pageSize,int pages) {
 80  0
         CachedTableModel<RowType>cachedTableModel = new CachedTableModel<RowType>(this);
 81  0
         cachedTableModel.setPreCachedRowCount(pageSize);
 82  0
         cachedTableModel.setPostCachedRowCount(pageSize);
 83  0
         cachedTableModel.setRowCount(pageSize * pages);
 84  0
         return cachedTableModel;
 85  
     }
 86  
     /**
 87  
      * This overridden method ...
 88  
      *
 89  
      * @see com.google.gwt.gen2.table.client.MutableTableModel#onRowInserted(int)
 90  
      */
 91  
     @Override
 92  
     protected boolean onRowInserted(int beforeRow) {
 93  0
         return true;
 94  
     }
 95  
 
 96  
     /**
 97  
      * This overridden method ...
 98  
      *
 99  
      * @see com.google.gwt.gen2.table.client.MutableTableModel#onRowRemoved(int)
 100  
      */
 101  
     @Override
 102  
     protected boolean onRowRemoved(int row) {
 103  0
         return true;
 104  
     }
 105  
 
 106  
     /**
 107  
      * This overridden method ...
 108  
      *
 109  
      * @see com.google.gwt.gen2.table.client.MutableTableModel#onSetRowValue(int, java.lang.Object)
 110  
      */
 111  
     @Override
 112  
     protected boolean onSetRowValue(int row, RowType rowValue) {
 113  0
         return true;
 114  
     }
 115  
 
 116  
     /**
 117  
      * This overridden method handles only errorMode, zeroMode, and local dtos
 118  
      * Production code will need rpc to load server side dto's, that feature can be
 119  
      * added in a subclass and perhaps here if it can be done generically
 120  
      *
 121  
      * @see com.google.gwt.gen2.table.client.TableModel#requestRows(com.google.gwt.gen2.table.client.TableModelHelper.Request, com.google.gwt.gen2.table.client.TableModel.Callback)
 122  
      */
 123  
     @Override
 124  
     public void requestRows(final Request request, final Callback<RowType> callback) {
 125  0
         if (errorMode) {
 126  
             // Return an error
 127  0
             callback.onFailure(new Exception("An error has occured."));
 128  0
         } else if (zeroMode) {
 129  
             // Return an empty result
 130  0
             List<RowType> selectedRows = new ArrayList<RowType>(0);
 131  0
             callback.onRowsReady(request, new SerializableResponse(selectedRows));//unchecked warning
 132  0
         } else {
 133  0
             callback.onRowsReady(request, new Response<RowType>() {
 134  
 
 135  
                 @SuppressWarnings("unchecked")
 136  
                 @Override
 137  
                 public Iterator<RowType> getRowValues() {
 138  
                     // Generate data locally
 139  
 
 140  0
                     final int sortColumn = request.getColumnSortList().getPrimaryColumn();
 141  0
                     final int sortDirection = (request.getColumnSortList().isPrimaryAscending() ? 1 : -1);
 142  0
                     if (sortColumn != lastSortedColumn || sortDirection != lastSortDirection) {
 143  0
                         SearchColumnDefinition rowDef = (SearchColumnDefinition) columnDefs.get(sortColumn);
 144  0
                         final String sortColumKey = rowDef.getColumnKey();
 145  0
                         Collections.sort((List<ResultRow>)rowDTOs, new Comparator<ResultRow>() {
 146  
 
 147  
                             @Override
 148  
                             public int compare(ResultRow o1, ResultRow o2) {
 149  0
                                 return o1.getValue(sortColumKey).compareToIgnoreCase(o2.getValue(sortColumKey)) * sortDirection;
 150  
                             }});
 151  0
                         lastSortedColumn = sortColumn;
 152  0
                         lastSortDirection = sortDirection;
 153  
                     }
 154  0
                     int numRows = request.getNumRows();
 155  
                     //fromIndex = request.getStartRow(); This disables column sort, bug?
 156  0
                     if((fromIndex + numRows) >= rowDTOs.size()) {
 157  0
                         fromIndex = 0;
 158  
                     }
 159  0
                     int toIndex = fromIndex + numRows;
 160  0
                     List<RowType> selectedRows = new ArrayList<RowType>(numRows);
 161  0
                     for(int i = fromIndex; i < toIndex; i++) {
 162  0
                         RowType e = rowDTOs.get(i);
 163  0
                         selectedRows.add(e);
 164  
                     }
 165  
 
 166  0
                     fromIndex = toIndex;
 167  0
                     return new SerializableResponse(selectedRows).getRowValues();
 168  
                 }
 169  
             });
 170  
         }
 171  0
     }
 172  
 
 173  
     /**
 174  
      * @param rowDTOs the rowDTOs to set
 175  
      */
 176  
     public void setRows(List<RowType> rows) {
 177  
 
 178  0
             List<Idable> ids = (List<Idable>)rows;//Force ClassCast exception if list items aren't Idable
 179  0
             this.rowDTOs = rows;
 180  0
     }
 181  
     /**
 182  
      * This overridden method returns the static size the table
 183  
      *
 184  
      * @see com.google.gwt.gen2.table.client.TableModel#getRowCount()
 185  
      */
 186  
     @Override
 187  
     public int getRowCount() {
 188  
         // TODO return dynamic size when loading with RPC
 189  0
         return rowDTOs.size();
 190  
     }
 191  
     /**
 192  
      * @return the errorMode
 193  
      */
 194  
     public boolean isErrorMode() {
 195  0
         return errorMode;
 196  
     }
 197  
     /**
 198  
      * @param errorMode the errorMode to set
 199  
      */
 200  
     public void setErrorMode(boolean errorMode) {
 201  0
         this.errorMode = errorMode;
 202  0
     }
 203  
     /**
 204  
      * @return the zeroMode
 205  
      */
 206  
     public boolean isZeroMode() {
 207  0
         return zeroMode;
 208  
     }
 209  
     /**
 210  
      * @param zeroMode the zeroMode to set
 211  
      */
 212  
     public void setZeroMode(boolean zeroMode) {
 213  0
         this.zeroMode = zeroMode;
 214  0
     }
 215  
 
 216  
     public void setColumnDefs(List<AbstractColumnDefinition<ResultRow, ?>> columnDefs) {
 217  0
         this.columnDefs = columnDefs;
 218  0
     }
 219  
 }