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