Coverage Report - org.kuali.student.common.ui.client.widgets.search.SearchResultsTable
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldAscendingRowComparator
0%
0/11
0%
0/2
1.8
FieldDescendingRowComparator
0%
0/11
0%
0/2
1.8
SearchResultsRow
0%
0/8
N/A
1.8
SearchResultsTable
0%
0/77
0%
0/20
1.8
SearchResultsTable$1
0%
0/3
N/A
1.8
SearchResultsTable$2
0%
0/27
0%
0/16
1.8
 
 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.search;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import org.kuali.student.common.assembly.data.LookupResultMetadata;
 22  
 import org.kuali.student.common.assembly.data.Data.DataType;
 23  
 import org.kuali.student.common.search.dto.SearchRequest;
 24  
 import org.kuali.student.common.search.dto.SearchResult;
 25  
 import org.kuali.student.common.search.dto.SearchResultCell;
 26  
 import org.kuali.student.common.search.dto.SearchResultRow;
 27  
 import org.kuali.student.common.ui.client.application.Application;
 28  
 import org.kuali.student.common.ui.client.application.KSAsyncCallback;
 29  
 import org.kuali.student.common.ui.client.service.CachingSearchService;
 30  
 import org.kuali.student.common.ui.client.service.SearchRpcServiceAsync;
 31  
 import org.kuali.student.common.ui.client.service.SearchServiceFactory;
 32  
 import org.kuali.student.common.ui.client.widgets.KSButton;
 33  
 import org.kuali.student.common.ui.client.widgets.KSLabel;
 34  
 import org.kuali.student.common.ui.client.widgets.KSButtonAbstract.ButtonStyle;
 35  
 import org.kuali.student.common.ui.client.widgets.layout.VerticalFlowPanel;
 36  
 import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow;
 37  
 import org.kuali.student.common.ui.client.widgets.table.scroll.Column;
 38  
 import org.kuali.student.common.ui.client.widgets.table.scroll.DefaultTableModel;
 39  
 import org.kuali.student.common.ui.client.widgets.table.scroll.RetrieveAdditionalDataHandler;
 40  
 import org.kuali.student.common.ui.client.widgets.table.scroll.Row;
 41  
 import org.kuali.student.common.ui.client.widgets.table.scroll.RowComparator;
 42  
 import org.kuali.student.common.ui.client.widgets.table.scroll.Table;
 43  
 
 44  
 import com.google.gwt.core.client.GWT;
 45  
 import com.google.gwt.user.client.Window;
 46  
 import com.google.gwt.user.client.ui.Composite;
 47  
 import com.google.gwt.user.client.ui.VerticalPanel;
 48  
 
 49  0
 public class SearchResultsTable extends Composite{
 50  
 
 51  0
     private final int PAGE_SIZE = 10;
 52  
     
 53  0
     private SearchRpcServiceAsync searchRpcServiceAsync = SearchServiceFactory.getSearchService();
 54  
     
 55  0
     private VerticalPanel layout = new VerticalPanel();
 56  
     
 57  
     private DefaultTableModel tableModel;
 58  
     private String resultIdColumnKey;
 59  
     private SearchRequest searchRequest;
 60  0
     private Table table = new Table();
 61  0
         private boolean isMultiSelect = true;
 62  0
         private boolean withMslable = true;
 63  0
     private KSButton mslabel = new KSButton("Modify your search?", ButtonStyle.DEFAULT_ANCHOR);
 64  
     
 65  
         
 66  
     public KSButton getMslabel() {
 67  0
                 return mslabel;
 68  
         }
 69  
 
 70  
         public void setMslabel(KSButton mslabel) {
 71  0
                 this.mslabel = mslabel;
 72  0
         }
 73  
 
 74  
         public Table getContentTable(){
 75  0
                 return table;
 76  
         }
 77  
         public SearchResultsTable(){
 78  0
         super();
 79  0
         redraw();
 80  0
         layout.setWidth("100%");
 81  0
         initWidget(layout);
 82  0
     }
 83  
     
 84  
     public void redraw(){
 85  0
         layout.clear();      
 86  0
     }
 87  
     
 88  
     public void setMutipleSelect(boolean isMultiSelect){
 89  0
             this.isMultiSelect = isMultiSelect;
 90  0
     }
 91  
  
 92  
         public void setWithMslable(boolean withMslable) {
 93  0
                 this.withMslable = withMslable;
 94  0
         }
 95  
 
 96  
         //FIXME do we really need to recreate the table for every refresh?
 97  
     public void initializeTable(List<LookupResultMetadata> listResultMetadata, String resultIdKey){ 
 98  
             
 99  0
             if(table == null){
 100  0
                     table = new Table();
 101  
             }
 102  0
             table.removeAllRows();
 103  0
         this.resultIdColumnKey = resultIdKey;
 104  
         
 105  0
         tableModel = new DefaultTableModel();
 106  0
         tableModel.setMultipleSelectable(isMultiSelect);
 107  
 
 108  
         //create table heading
 109  0
         for (LookupResultMetadata r: listResultMetadata){
 110  0
             if(!r.isHidden()){
 111  0
                 Column col1 = new Column();
 112  0
                 col1.setId(r.getKey());
 113  0
                 String header = Application.getApplicationContext().getUILabel("", null, null, r.getName());
 114  0
                 col1.setName(header);
 115  0
                 col1.setId(r.getKey());
 116  0
                 col1.setWidth("100px");                    
 117  0
                 col1.setAscendingRowComparator(new FieldAscendingRowComparator(r.getKey(), r.getDataType()));
 118  0
                 col1.setDescendingRowComparator(new FieldDescendingRowComparator(r.getKey(), r.getDataType()));                
 119  
                 
 120  0
                 tableModel.addColumn(col1);
 121  0
             }
 122  
         }      
 123  
                      
 124  
      // TODO - there's a better way to do this
 125  0
         if (this.searchRequest.getSearchKey().toLowerCase().contains("cross")) {
 126  0
                 tableModel.setMoreData(false);
 127  
         }
 128  0
         if(isMultiSelect){
 129  0
                 tableModel.installCheckBoxRowHeaderColumn();
 130  
         }
 131  
         
 132  0
         table.getScrollPanel().setHeight("300px");
 133  0
         table.setTableModel(tableModel);
 134  
         
 135  0
         table.addRetrieveAdditionalDataHandler(new RetrieveAdditionalDataHandler(){
 136  
                         @Override
 137  
                         public void onAdditionalDataRequest() {
 138  0
                                  performOnDemandSearch(tableModel.getRowCount(), PAGE_SIZE);
 139  
                  //tableModel.fireTableDataChanged();
 140  0
                         }
 141  
                 });
 142  
 
 143  0
         redraw();
 144  0
         layout.add(table);
 145  0
   }   
 146  
     
 147  
     public void performSearch(SearchRequest searchRequest, List<LookupResultMetadata> listResultMetadata, String resultIdKey, boolean pagedResults){
 148  0
         this.searchRequest = searchRequest;
 149  0
         initializeTable(listResultMetadata, resultIdKey);
 150  0
         if (this.searchRequest.getSearchKey().toLowerCase().contains("cross")) {
 151  
             //FIXME Do we still need this if condition?
 152  
             // Added an else to the if(pagedResults) line to prevent searches being executed
 153  
             // twice if the search name includes cross
 154  0
             performOnDemandSearch(0, 0);
 155  
         }
 156  0
         else if(pagedResults){
 157  0
                 performOnDemandSearch(0, PAGE_SIZE);
 158  
         }
 159  
         else{
 160  0
                 performOnDemandSearch(0, 0);
 161  
         }
 162  0
     }    
 163  
     
 164  
     public void performSearch(SearchRequest searchRequest, List<LookupResultMetadata> listResultMetadata, String resultIdKey){
 165  0
         this.performSearch(searchRequest, listResultMetadata, resultIdKey, true);
 166  0
     }    
 167  
     
 168  
     private void performOnDemandSearch(int startAt, int size) {
 169  
                 
 170  0
             table.displayLoading(true);
 171  0
         searchRequest.setStartAt(startAt);
 172  0
         if (size != 0) {
 173  0
                 searchRequest.setNeededTotalResults(false);
 174  0
                 searchRequest.setMaxResults(size);
 175  
         } else {
 176  0
                 searchRequest.setNeededTotalResults(true);
 177  
         }
 178  
 
 179  0
         searchRpcServiceAsync.search(searchRequest, new KSAsyncCallback<SearchResult>(){
 180  
 
 181  
             @Override
 182  
             public void handleFailure(Throwable cause) {
 183  0
                 GWT.log("Failed to perform search", cause); //FIXME more detail info here
 184  0
                 Window.alert("Failed to perform search");
 185  0
                 table.displayLoading(false);
 186  0
             }
 187  
 
 188  
             @Override
 189  
             public void onSuccess(SearchResult results) {
 190  0
                     table.addContent();
 191  
                     
 192  0
                 if(results != null && results.getRows() != null && results.getRows().size() != 0){
 193  0
                     for (SearchResultRow r: results.getRows()){
 194  0
                         ResultRow theRow = new ResultRow();
 195  0
                         for(SearchResultCell c: r.getCells()){
 196  0
                             if(c.getKey().equals(resultIdColumnKey)){
 197  0
                                 theRow.setId(c.getValue());
 198  
                             }
 199  0
                             theRow.setValue(c.getKey(), c.getValue());
 200  
                         }
 201  0
                        tableModel.addRow(new SearchResultsRow(theRow));
 202  0
                     }
 203  
                 } else {
 204  0
                         tableModel.setMoreData(false);
 205  
                         
 206  
                         //add no matches found if no search results
 207  0
                         if(searchRequest.getStartAt() == 0){
 208  0
                                 table.removeContent();
 209  0
                                 VerticalFlowPanel noResultsPanel = new VerticalFlowPanel();
 210  0
                                 noResultsPanel.add(new KSLabel("No matches found"));
 211  0
                                 if(withMslable) noResultsPanel.add(mslabel);
 212  0
                                 noResultsPanel.addStyleName("ks-no-results-message");
 213  0
                                 table.getScrollPanel().add(noResultsPanel);
 214  
                         }
 215  
                 }
 216  0
                 tableModel.selectFirstRow();
 217  0
                 tableModel.fireTableDataChanged();
 218  0
                 table.displayLoading(false);
 219  0
             }
 220  
         });
 221  0
     }
 222  
     
 223  
     public List<ResultRow> getSelectedRows(){
 224  0
         List<ResultRow> rows = new ArrayList<ResultRow>();
 225  0
         for(Row row : tableModel.getSelectedRows()){
 226  0
             rows.add(((SearchResultsRow)row).getResultRow());
 227  
         }
 228  0
         return rows;
 229  
     }
 230  
 
 231  
     public List<String> getSelectedIds(){
 232  0
         List<String> ids = new ArrayList<String>();
 233  0
         for(Row row : tableModel.getSelectedRows()){
 234  0
             ids.add(((SearchResultsRow)row).getResultRow().getId());
 235  
         }                
 236  0
         return ids;
 237  
     }        
 238  
 }
 239  
 
 240  
 class SearchResultsRow extends Row {
 241  
     
 242  
     ResultRow row;
 243  
     
 244  0
     public SearchResultsRow(ResultRow row){
 245  0
        this.row = row;
 246  0
     }
 247  
     
 248  
     @Override
 249  
     public Object getCellData(String columnId) {
 250  0
         return row.getValue(columnId);        
 251  
     }
 252  
     
 253  
     @Override
 254  
     public void setCellData(String columnId, Object newValue) {
 255  0
         row.setValue(columnId, newValue.toString());
 256  0
     }
 257  
     
 258  
     @Override
 259  
     public String toString(){
 260  0
         return row.toString();
 261  
     }
 262  
     
 263  
     public ResultRow getResultRow() {
 264  0
         return row;
 265  
     }
 266  
 }   
 267  
 
 268  0
 class FieldAscendingRowComparator extends RowComparator{
 269  
     
 270  
     String columnId;
 271  
     DataType type;
 272  
     
 273  0
     FieldAscendingRowComparator(String columnId, DataType type) {
 274  0
         this.columnId = columnId;
 275  0
         this.type = type;
 276  0
     }
 277  
     
 278  
     @Override
 279  
     public int compare(Row row0, Row row1) {
 280  
         String id0, id1;
 281  
         
 282  0
         if (type.equals(DataType.STRING)) {
 283  0
             id0 = (String)row0.getCellData(columnId);
 284  0
             id1 = (String)row1.getCellData(columnId);
 285  
         } else {
 286  0
             id0 = (String)row0.getCellData(columnId);
 287  0
             id1 = (String)row1.getCellData(columnId);            
 288  
         }
 289  0
         return id0.compareTo(id1);
 290  
     }    
 291  
 }
 292  
 
 293  0
 class FieldDescendingRowComparator extends RowComparator{
 294  
     
 295  
     String columnId;
 296  
     DataType type;    
 297  
     
 298  0
     FieldDescendingRowComparator(String columnId, DataType type) {
 299  0
         this.columnId = columnId;
 300  0
         this.type = type;        
 301  0
     }    
 302  
     
 303  
     @Override
 304  
     public int compare(Row row0, Row row1) {
 305  
         String id0, id1;
 306  
         
 307  0
         if (type.equals(DataType.STRING)) {
 308  0
             id0 = (String)row0.getCellData(columnId);
 309  0
             id1 = (String)row1.getCellData(columnId);
 310  
         } else {
 311  0
             id0 = (String)row0.getCellData(columnId);
 312  0
             id1 = (String)row1.getCellData(columnId);            
 313  
         }
 314  0
         return id1.compareTo(id0);
 315  
     }    
 316  
 }