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.search;
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.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.widgets.pagetable.GenericTableModel;
32  import org.kuali.student.common.ui.client.widgets.pagetable.PagingScrollTableBuilder;
33  import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow;
34  import org.kuali.student.common.ui.client.widgets.searchtable.SearchColumnDefinition;
35  
36  import com.google.gwt.core.client.GWT;
37  import com.google.gwt.gen2.table.client.AbstractColumnDefinition;
38  import com.google.gwt.gen2.table.client.PagingOptions;
39  import com.google.gwt.gen2.table.client.PagingScrollTable;
40  import com.google.gwt.gen2.table.client.AbstractScrollTable.ResizePolicy;
41  import com.google.gwt.gen2.table.event.client.RowSelectionHandler;
42  import com.google.gwt.user.client.Window;
43  import com.google.gwt.user.client.ui.Composite;
44  import com.google.gwt.user.client.ui.Label;
45  import com.google.gwt.user.client.ui.VerticalPanel;
46  
47  public class TempSearchBackedTable extends Composite{
48  
49      private List<ResultRow> resultRows = new ArrayList<ResultRow>();
50      private List<AbstractColumnDefinition<ResultRow, ?>> columnDefs = new ArrayList<AbstractColumnDefinition<ResultRow, ?>>();
51      private GenericTableModel<ResultRow> tableModel = new GenericTableModel<ResultRow>(resultRows);
52      private PagingScrollTableBuilder<ResultRow> builder = new PagingScrollTableBuilder<ResultRow>();
53      private String resultIdColumnKey;
54      protected PagingScrollTable<ResultRow> pagingScrollTable;
55      private VerticalPanel layout = new VerticalPanel();
56      private PagingOptions pagingOptions;
57  
58      private SearchRpcServiceAsync searchRpcServiceAsync = CachingSearchService.getSearchService();
59  
60      private PagingOptions createPagingOptions(PagingScrollTable<ResultRow> pagingScrollTable) {
61          PagingOptions pagingOptions = new PagingOptions(pagingScrollTable);
62          pagingOptions.setPixelSize(pagingScrollTable.getOffsetWidth(), pagingOptions.getOffsetHeight());
63          return pagingOptions;
64      }
65  
66      public TempSearchBackedTable(){
67          super();
68          redraw();
69          layout.setWidth("100%");
70          initWidget(layout);
71      }
72  
73      public void clearTable(){
74          resultRows.clear();
75          this.redraw();
76      }
77  
78      public void removeSelected(){
79          for(ResultRow r: getSelectedRows()){
80              resultRows.remove(r);
81          }
82          this.redraw();
83      }
84  
85      public void performSearch(SearchRequest searchRequest, List<LookupResultMetadata> listResultMetadata, String resultIdKey){
86  
87      	initializeTable(listResultMetadata, resultIdKey);
88  
89      	searchRequest.setNeededTotalResults(false);
90  
91          if(pagingScrollTable != null){
92              pagingScrollTable.setEmptyTableWidget(new Label("Processing Search..."));
93          }
94  
95  		searchRpcServiceAsync.search(searchRequest, new KSAsyncCallback<SearchResult>(){
96  
97  		    @Override
98  		    public void handleFailure(Throwable cause) {
99  		    	GWT.log("Failed to perform search", cause); //FIXME more detail info here
100 		    	Window.alert("Failed to perform search");
101 		    }
102 
103 		    @Override
104 		    public void onSuccess(SearchResult results) {
105 
106 		    	resultRows.clear();
107 		        if(results != null){
108 		            for (SearchResultRow r: results.getRows()){
109 		                ResultRow theRow = new ResultRow();
110 		                for(SearchResultCell c: r.getCells()){
111 		                    if(c.getKey().equals(resultIdColumnKey)){
112 		                        theRow.setId(c.getValue());
113 		                    }
114 		                    theRow.setValue(c.getKey(), c.getValue());
115 		                }
116 		                resultRows.add(theRow);
117 		            }
118 		        }
119 		        redraw();
120 		    }
121 		});
122     }
123 
124     public void initializeTable(List<LookupResultMetadata> listResultMetadata, String resultIdKey) {
125     	clearTable();
126 
127         this.resultIdColumnKey = resultIdKey;
128         builder = new PagingScrollTableBuilder<ResultRow>();
129         builder.tablePixelSize(400, 300);
130 
131         columnDefs = new ArrayList<AbstractColumnDefinition<ResultRow, ?>>();
132         for (LookupResultMetadata r: listResultMetadata){
133             String header = Application.getApplicationContext().getUILabel("", null, null, r.getName());
134             String key = r.getKey();
135             if(!r.isHidden()){
136                 columnDefs.add(new SearchColumnDefinition(header, key));
137             }
138         }
139         if(columnDefs.size() == 1){
140             //FIXME auto adjusting width to fill table does not work with 1 column bug in incubator???
141             columnDefs.get(0).setMinimumColumnWidth(370);
142         }
143         builder.columnDefinitions(columnDefs);
144         tableModel.setColumnDefs(columnDefs);
145 
146         redraw();
147     }
148 
149     public void redraw(){
150         tableModel.setRows(resultRows);
151         pagingScrollTable = builder.build(tableModel); // FIXME do we really need to recreate the table for every refresh?
152         pagingScrollTable.setResizePolicy(ResizePolicy.FILL_WIDTH);
153         pagingOptions = createPagingOptions(pagingScrollTable);
154         layout.clear();
155         layout.add(pagingOptions);
156         layout.add(pagingScrollTable);
157         pagingScrollTable.reloadPage();		//FIXME Undesirable solution to work with GWT 2.0
158         pagingScrollTable.fillWidth();
159     }
160 
161     public void addSelectionHandler(RowSelectionHandler selectionHandler){
162         pagingScrollTable.getDataTable().addRowSelectionHandler(selectionHandler);
163     }
164 
165     public List<ResultRow> getSelectedRows(){
166         List<ResultRow> rows = new ArrayList<ResultRow>();
167         Set<Integer> selectedRows = pagingScrollTable.getDataTable().getSelectedRows();
168         for(Integer i: selectedRows){
169             rows.add(pagingScrollTable.getRowValue(i));
170         }
171         return rows;
172     }
173 
174     public List<String> getSelectedIds(){
175         List<String> ids = new ArrayList<String>();
176         Set<Integer> selectedRows = pagingScrollTable.getDataTable().getSelectedRows();
177         for(Integer i: selectedRows){
178             ids.add(pagingScrollTable.getRowValue(i).getId());
179         }
180         return ids;
181     }
182 
183     public List<String> getAllIds(){
184         List<String> ids = new ArrayList<String>();
185         for(ResultRow r: resultRows){
186             ids.add(r.getId());
187         }
188         return ids;
189     }
190 
191     public List<ResultRow> getAllRows(){
192         List<ResultRow> rows = new ArrayList<ResultRow>();
193         for(ResultRow r: resultRows){
194             rows.add(r);
195         }
196         return rows;
197     }
198 
199 }