View Javadoc

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.ui.client.widgets.searchtable.ResultRow;
25  import org.kuali.student.common.ui.client.widgets.searchtable.SearchColumnDefinition;
26  import org.kuali.student.core.dto.Idable;
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  public class GenericTableModel<RowType> extends MutableTableModel<RowType> {
44      private List<RowType> rowDTOs = null;
45      private int fromIndex = 0;
46      private int lastSortedColumn = -1;
47      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      private boolean errorMode = false;
56      /**
57       * A boolean indicating that we should return 0 rowDTOs in the response.
58       */
59      private boolean zeroMode = false;
60      /**
61       * This constructs an empty table model for the RowType
62       *
63       */
64      public GenericTableModel() {
65          super();
66      }
67      /**
68       * This constructs a populated table model for the RowType
69       *
70       */
71      public GenericTableModel(List<RowType>rows) {
72          super();
73          setRows(rows);
74      }
75      /**
76       * This returns cached table model for the RowType
77       *
78       */
79      public CachedTableModel<RowType>createCachedTableModel(int pageSize,int pages) {
80          CachedTableModel<RowType>cachedTableModel = new CachedTableModel<RowType>(this);
81          cachedTableModel.setPreCachedRowCount(pageSize);
82          cachedTableModel.setPostCachedRowCount(pageSize);
83          cachedTableModel.setRowCount(pageSize * pages);
84          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          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         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         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         if (errorMode) {
126             // Return an error
127             callback.onFailure(new Exception("An error has occured."));
128         } else if (zeroMode) {
129             // Return an empty result
130             List<RowType> selectedRows = new ArrayList<RowType>(0);
131             callback.onRowsReady(request, new SerializableResponse(selectedRows));//unchecked warning
132         } else {
133             callback.onRowsReady(request, new Response<RowType>() {
134 
135                 @SuppressWarnings("unchecked")
136                 @Override
137                 public Iterator<RowType> getRowValues() {
138                     // Generate data locally
139 
140                     final int sortColumn = request.getColumnSortList().getPrimaryColumn();
141                     final int sortDirection = (request.getColumnSortList().isPrimaryAscending() ? 1 : -1);
142                     if (sortColumn != lastSortedColumn || sortDirection != lastSortDirection) {
143                         SearchColumnDefinition rowDef = (SearchColumnDefinition) columnDefs.get(sortColumn);
144                         final String sortColumKey = rowDef.getColumnKey();
145                         Collections.sort((List<ResultRow>)rowDTOs, new Comparator<ResultRow>() {
146 
147                             @Override
148                             public int compare(ResultRow o1, ResultRow o2) {
149                                 return o1.getValue(sortColumKey).compareToIgnoreCase(o2.getValue(sortColumKey)) * sortDirection;
150                             }});
151                         lastSortedColumn = sortColumn;
152                         lastSortDirection = sortDirection;
153                     }
154                     int numRows = request.getNumRows();
155                     //fromIndex = request.getStartRow(); This disables column sort, bug?
156                     if((fromIndex + numRows) >= rowDTOs.size()) {
157                         fromIndex = 0;
158                     }
159                     int toIndex = fromIndex + numRows;
160                     List<RowType> selectedRows = new ArrayList<RowType>(numRows);
161                     for(int i = fromIndex; i < toIndex; i++) {
162                         RowType e = rowDTOs.get(i);
163                         selectedRows.add(e);
164                     }
165 
166                     fromIndex = toIndex;
167                     return new SerializableResponse(selectedRows).getRowValues();
168                 }
169             });
170         }
171     }
172 
173     /**
174      * @param rowDTOs the rowDTOs to set
175      */
176     public void setRows(List<RowType> rows) {
177 
178             List<Idable> ids = (List<Idable>)rows;//Force ClassCast exception if list items aren't Idable
179             this.rowDTOs = rows;
180     }
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         return rowDTOs.size();
190     }
191     /**
192      * @return the errorMode
193      */
194     public boolean isErrorMode() {
195         return errorMode;
196     }
197     /**
198      * @param errorMode the errorMode to set
199      */
200     public void setErrorMode(boolean errorMode) {
201         this.errorMode = errorMode;
202     }
203     /**
204      * @return the zeroMode
205      */
206     public boolean isZeroMode() {
207         return zeroMode;
208     }
209     /**
210      * @param zeroMode the zeroMode to set
211      */
212     public void setZeroMode(boolean zeroMode) {
213         this.zeroMode = zeroMode;
214     }
215 
216     public void setColumnDefs(List<AbstractColumnDefinition<ResultRow, ?>> columnDefs) {
217         this.columnDefs = columnDefs;
218     }
219 }