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