Coverage Report - org.kuali.student.lum.lu.ui.tools.client.widgets.SearchBackedTable
 
Classes in this File Line Coverage Branch Coverage Complexity
SearchBackedTable
0%
0/88
0%
0/20
1.778
SearchBackedTable$1
0%
0/15
0%
0/8
1.778
 
 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.lu.ui.tools.client.widgets;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 import java.util.Set;
 21  
 
 22  
 import org.kuali.student.common.assembly.data.LookupResultMetadata;
 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.KSAsyncCallback;
 28  
 import org.kuali.student.common.ui.client.mvc.Callback;
 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.pagetable.GenericTableModel;
 33  
 import org.kuali.student.common.ui.client.widgets.pagetable.PagingScrollTableBuilder;
 34  
 import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow;
 35  
 import org.kuali.student.common.ui.client.widgets.searchtable.SearchColumnDefinition;
 36  
 
 37  
 import com.google.gwt.gen2.table.client.AbstractColumnDefinition;
 38  
 import com.google.gwt.gen2.table.client.AbstractScrollTable.ResizePolicy;
 39  
 import com.google.gwt.gen2.table.client.SelectionGrid.SelectionPolicy;
 40  
 import com.google.gwt.gen2.table.client.PagingScrollTable;
 41  
 import com.google.gwt.gen2.table.event.client.RowSelectionHandler;
 42  
 import com.google.gwt.user.client.ui.Composite;
 43  
 import com.google.gwt.user.client.ui.Label;
 44  
 import com.google.gwt.user.client.ui.VerticalPanel;
 45  
 
 46  0
 public class SearchBackedTable extends Composite {
 47  
 
 48  0
         private final List<ResultRow> resultRows = new ArrayList<ResultRow>();
 49  0
         private final List<ResultRow> allResults = new ArrayList<ResultRow>();
 50  0
         private List<AbstractColumnDefinition<ResultRow, ?>> columnDefs = new ArrayList<AbstractColumnDefinition<ResultRow, ?>>();
 51  0
         private GenericTableModel<ResultRow> tableModel = new GenericTableModel<ResultRow>(
 52  
                         resultRows);
 53  0
         private PagingScrollTableBuilder<ResultRow> builder = new PagingScrollTableBuilder<ResultRow>();
 54  
         private String resultIdColumnKey;
 55  
         protected PagingScrollTable<ResultRow> pagingScrollTable;
 56  0
         private VerticalPanel layout = new VerticalPanel();
 57  
 
 58  0
         private SearchRpcServiceAsync searchRpcServiceAsync = SearchServiceFactory.getSearchService();
 59  
         
 60  0
         private int defaultHeight = 200;
 61  
 
 62  0
         private SelectionPolicy selectionPolicy = SelectionPolicy.MULTI_ROW;
 63  0
         private String tableStyleName = "";
 64  
 
 65  
         public SearchBackedTable() {
 66  0
                 super();
 67  0
                 redraw();
 68  0
                 layout.setWidth("100%");
 69  0
                 initWidget(layout);
 70  0
         }
 71  
 
 72  
         public SearchBackedTable(int tableHeight) {
 73  0
                 this();
 74  0
                 this.defaultHeight = tableHeight;
 75  0
         }
 76  
 
 77  
         public void clearTable() {
 78  0
                 resultRows.clear();
 79  0
                 this.redraw();
 80  0
         }
 81  
 
 82  
         public void removeSelected() {
 83  0
                 for (ResultRow r : getSelectedRows()) {
 84  0
                         resultRows.remove(r);
 85  
                 }
 86  0
                 this.redraw();
 87  0
         }
 88  
         
 89  
         public void performSearch(SearchRequest searchRequest,
 90  
                         List<LookupResultMetadata> listResultMetadata, String resultIdKey,
 91  
                         final Callback<Boolean> callback) {
 92  
 
 93  0
                 initializeTable(listResultMetadata, resultIdKey);
 94  
 
 95  0
                 searchRequest.setNeededTotalResults(false);
 96  
 
 97  0
                 if (pagingScrollTable != null) {
 98  0
                         pagingScrollTable.setEmptyTableWidget(new Label(
 99  
                                         "Processing Search..."));
 100  
                 }
 101  
                 
 102  
                 // Window.alert ("About to invoke asynch search...");
 103  0
                 searchRpcServiceAsync.search(searchRequest,
 104  0
                                 new KSAsyncCallback<SearchResult>() {
 105  
 
 106  
                                         @Override
 107  
                                         public void onSuccess(SearchResult searchResults) {
 108  
                                                 // Window.alert ("Got back search results...");
 109  0
                                                 resultRows.clear();
 110  0
                                                 if (searchResults != null) {
 111  0
                                                         for (SearchResultRow searchResultRow : searchResults
 112  
                                                                         .getRows()) {
 113  
                                                                 // Window.alert ("adding row");
 114  0
                                                                 ResultRow resultRow = new ResultRow();
 115  0
                                                                 for (SearchResultCell searchResultCell : searchResultRow
 116  
                                                                                 .getCells()) {
 117  0
                                                                         if (searchResultCell.getKey().equals(
 118  
                                                                                         resultIdColumnKey)) {
 119  0
                                                                                 resultRow.setId(searchResultCell
 120  
                                                                                                 .getValue());
 121  
                                                                         }
 122  0
                                                                         resultRow.setValue(
 123  
                                                                                         searchResultCell.getKey(),
 124  
                                                                                         searchResultCell.getValue());
 125  
                                                                 }
 126  0
                                                                 resultRows.add(resultRow);
 127  0
                                                         }
 128  
                                                 }
 129  
                                                 // Window.alert ("about to redraw...");
 130  0
                                                 allResults.addAll(resultRows);
 131  0
                                                 redraw();
 132  0
                                                 callback.exec(true);
 133  0
                                         }
 134  
 
 135  
                                 });
 136  0
         }
 137  
         
 138  
         private void initializeTable(List<LookupResultMetadata> listResultMetadata,
 139  
                         String resultIdKey) {
 140  0
                 clearTable();
 141  
 
 142  0
                 this.resultIdColumnKey = resultIdKey;
 143  0
                 builder = new PagingScrollTableBuilder<ResultRow>();
 144  0
                 builder.tablePixelSize(900, defaultHeight); // width, height
 145  0
                 builder.setSelectionPolicy(selectionPolicy);
 146  
 
 147  0
                 columnDefs = new ArrayList<AbstractColumnDefinition<ResultRow, ?>>();
 148  0
                 for (LookupResultMetadata r : listResultMetadata) {
 149  
                         // TODO: use this as a token to get a message from message service
 150  
                         // instead
 151  0
                         String header = r.getName();
 152  0
                         String key = r.getKey();
 153  0
                         if (!r.isHidden()) {
 154  0
                                 columnDefs.add(new SearchColumnDefinition(header, key));
 155  
                         }
 156  0
                 }
 157  0
                 if (columnDefs.size() == 1) {
 158  0
                         columnDefs.get(0).setMinimumColumnWidth(370);
 159  
                 }
 160  0
                 builder.columnDefinitions(columnDefs);
 161  0
                 tableModel.setColumnDefs(columnDefs);
 162  
 
 163  0
                 redraw();
 164  0
         }
 165  
 
 166  
         public void redraw() {
 167  0
                 tableModel.setRows(resultRows);
 168  0
                 pagingScrollTable = builder.build(tableModel);
 169  0
                 pagingScrollTable.setResizePolicy(ResizePolicy.FILL_WIDTH);                
 170  0
                 if(tableStyleName != "")
 171  0
                         pagingScrollTable.setStyleName(tableStyleName);
 172  0
                 layout.clear();
 173  0
                 layout.add(pagingScrollTable);
 174  0
                 pagingScrollTable.fillWidth();
 175  0
                 pagingScrollTable.reloadPage();
 176  0
         }
 177  
 
 178  
         public void addSelectionHandler(RowSelectionHandler selectionHandler) {
 179  0
                 pagingScrollTable.getDataTable().addRowSelectionHandler(
 180  
                                 selectionHandler);
 181  0
         }
 182  
 
 183  
         public List<ResultRow> getSelectedRows() {
 184  0
                 List<ResultRow> rows = new ArrayList<ResultRow>();
 185  0
                 Set<Integer> selectedRows = pagingScrollTable.getDataTable()
 186  
                                 .getSelectedRows();
 187  0
                 for (Integer i : selectedRows) {
 188  0
                         rows.add(pagingScrollTable.getRowValue(i));
 189  
                 }
 190  0
                 return rows;
 191  
         }
 192  
 
 193  
         public List<String> getSelectedIds() {
 194  0
                 List<String> ids = new ArrayList<String>();
 195  0
                 Set<Integer> selectedRows = pagingScrollTable.getDataTable()
 196  
                                 .getSelectedRows();
 197  0
                 for (Integer i : selectedRows) {
 198  0
                         ids.add(pagingScrollTable.getRowValue(i).getId());
 199  
                 }
 200  0
                 return ids;
 201  
         }
 202  
 
 203  
         public List<String> getAllIds() {
 204  0
                 List<String> ids = new ArrayList<String>();
 205  0
                 for (ResultRow r : resultRows) {
 206  0
                         ids.add(r.getId());
 207  
                 }
 208  0
                 return ids;
 209  
         }
 210  
 
 211  
         public List<ResultRow> getAllRows() {
 212  0
                 List<ResultRow> rows = new ArrayList<ResultRow>();
 213  0
                 for (ResultRow r : resultRows) {
 214  0
                         rows.add(r);
 215  
                 }
 216  0
                 return rows;
 217  
         }
 218  
 
 219  
         public List<ResultRow> getAllResults() {
 220  0
                 return allResults;
 221  
         }
 222  
 
 223  
         public List<ResultRow> getResultRows() {
 224  0
                 return resultRows;
 225  
         }
 226  
 
 227  
         public void setDefaultHeight(int defaultHeight) {
 228  0
                 this.defaultHeight = defaultHeight;
 229  0
         }
 230  
 
 231  
     public void setSelectionPolicy(SelectionPolicy selectionPolicy){
 232  0
             this.selectionPolicy = selectionPolicy;
 233  0
     }
 234  
 
 235  
     public void setTableStyleName(String tableStyleName){
 236  0
             this.tableStyleName = tableStyleName;
 237  0
     }
 238  
 }