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.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 | |
|
97 | |
public void initializeTable(List<LookupResultMetadata> listResultMetadata, String resultIdKey){ |
98 | |
|
99 | |
|
100 | 0 | table = new Table(); |
101 | 0 | table.removeAllRows(); |
102 | 0 | this.resultIdColumnKey = resultIdKey; |
103 | |
|
104 | 0 | tableModel = new DefaultTableModel(); |
105 | 0 | tableModel.setMultipleSelectable(isMultiSelect); |
106 | |
|
107 | |
|
108 | 0 | for (LookupResultMetadata r: listResultMetadata){ |
109 | 0 | if(!r.isHidden()){ |
110 | 0 | Column col1 = new Column(); |
111 | 0 | col1.setId(r.getKey()); |
112 | 0 | String header = Application.getApplicationContext().getUILabel("", null, null, r.getName()); |
113 | 0 | col1.setName(header); |
114 | 0 | col1.setId(r.getKey()); |
115 | 0 | col1.setWidth("100px"); |
116 | 0 | col1.setAscendingRowComparator(new FieldAscendingRowComparator(r.getKey(), r.getDataType())); |
117 | 0 | col1.setDescendingRowComparator(new FieldDescendingRowComparator(r.getKey(), r.getDataType())); |
118 | |
|
119 | 0 | tableModel.addColumn(col1); |
120 | 0 | } |
121 | |
} |
122 | |
|
123 | |
|
124 | 0 | if (this.searchRequest.getSearchKey().toLowerCase().contains("cross")) { |
125 | 0 | tableModel.setMoreData(false); |
126 | |
} |
127 | 0 | if(isMultiSelect){ |
128 | 0 | tableModel.installCheckBoxRowHeaderColumn(); |
129 | |
} |
130 | |
|
131 | 0 | table.getScrollPanel().setHeight("300px"); |
132 | 0 | table.setTableModel(tableModel); |
133 | |
|
134 | 0 | table.addRetrieveAdditionalDataHandler(new RetrieveAdditionalDataHandler(){ |
135 | |
@Override |
136 | |
public void onAdditionalDataRequest() { |
137 | 0 | performOnDemandSearch(tableModel.getRowCount(), PAGE_SIZE); |
138 | |
|
139 | 0 | } |
140 | |
}); |
141 | |
|
142 | 0 | redraw(); |
143 | 0 | layout.add(table); |
144 | 0 | } |
145 | |
|
146 | |
public void performSearch(SearchRequest searchRequest, List<LookupResultMetadata> listResultMetadata, String resultIdKey, boolean pagedResults){ |
147 | 0 | this.searchRequest = searchRequest; |
148 | 0 | initializeTable(listResultMetadata, resultIdKey); |
149 | 0 | if (this.searchRequest.getSearchKey().toLowerCase().contains("cross")) { |
150 | |
|
151 | |
|
152 | |
|
153 | 0 | performOnDemandSearch(0, 0); |
154 | |
} |
155 | 0 | else if(pagedResults){ |
156 | 0 | performOnDemandSearch(0, PAGE_SIZE); |
157 | |
} |
158 | |
else{ |
159 | 0 | performOnDemandSearch(0, 0); |
160 | |
} |
161 | 0 | } |
162 | |
|
163 | |
public void performSearch(SearchRequest searchRequest, List<LookupResultMetadata> listResultMetadata, String resultIdKey){ |
164 | 0 | this.performSearch(searchRequest, listResultMetadata, resultIdKey, true); |
165 | 0 | } |
166 | |
|
167 | |
private void performOnDemandSearch(int startAt, int size) { |
168 | |
|
169 | 0 | table.displayLoading(true); |
170 | 0 | searchRequest.setStartAt(startAt); |
171 | 0 | if (size != 0) { |
172 | 0 | searchRequest.setNeededTotalResults(false); |
173 | 0 | searchRequest.setMaxResults(size); |
174 | |
} else { |
175 | 0 | searchRequest.setNeededTotalResults(true); |
176 | |
} |
177 | |
|
178 | 0 | searchRpcServiceAsync.search(searchRequest, new KSAsyncCallback<SearchResult>(){ |
179 | |
|
180 | |
@Override |
181 | |
public void handleFailure(Throwable cause) { |
182 | 0 | GWT.log("Failed to perform search", cause); |
183 | 0 | Window.alert("Failed to perform search"); |
184 | 0 | table.displayLoading(false); |
185 | 0 | } |
186 | |
|
187 | |
@Override |
188 | |
public void onSuccess(SearchResult results) { |
189 | 0 | table.addContent(); |
190 | |
|
191 | 0 | if(results != null && results.getRows() != null && results.getRows().size() != 0){ |
192 | 0 | for (SearchResultRow r: results.getRows()){ |
193 | 0 | ResultRow theRow = new ResultRow(); |
194 | 0 | for(SearchResultCell c: r.getCells()){ |
195 | 0 | if(c.getKey().equals(resultIdColumnKey)){ |
196 | 0 | theRow.setId(c.getValue()); |
197 | |
} |
198 | 0 | theRow.setValue(c.getKey(), c.getValue()); |
199 | |
} |
200 | 0 | tableModel.addRow(new SearchResultsRow(theRow)); |
201 | 0 | } |
202 | |
} else { |
203 | 0 | tableModel.setMoreData(false); |
204 | |
|
205 | |
|
206 | 0 | if(searchRequest.getStartAt() == 0){ |
207 | 0 | table.removeContent(); |
208 | 0 | VerticalFlowPanel noResultsPanel = new VerticalFlowPanel(); |
209 | 0 | noResultsPanel.add(new KSLabel("No matches found")); |
210 | 0 | if(withMslable) noResultsPanel.add(mslabel); |
211 | 0 | noResultsPanel.addStyleName("ks-no-results-message"); |
212 | 0 | table.getScrollPanel().add(noResultsPanel); |
213 | |
} |
214 | |
} |
215 | |
|
216 | 0 | tableModel.fireTableDataChanged(); |
217 | 0 | table.displayLoading(false); |
218 | 0 | } |
219 | |
}); |
220 | 0 | } |
221 | |
|
222 | |
public List<ResultRow> getSelectedRows(){ |
223 | 0 | List<ResultRow> rows = new ArrayList<ResultRow>(); |
224 | 0 | for(Row row : tableModel.getSelectedRows()){ |
225 | 0 | rows.add(((SearchResultsRow)row).getResultRow()); |
226 | |
} |
227 | 0 | return rows; |
228 | |
} |
229 | |
|
230 | |
public List<String> getSelectedIds(){ |
231 | 0 | List<String> ids = new ArrayList<String>(); |
232 | 0 | for(Row row : tableModel.getSelectedRows()){ |
233 | 0 | ids.add(((SearchResultsRow)row).getResultRow().getId()); |
234 | |
} |
235 | 0 | return ids; |
236 | |
} |
237 | |
} |
238 | |
|
239 | |
class SearchResultsRow extends Row { |
240 | |
|
241 | |
ResultRow row; |
242 | |
|
243 | 0 | public SearchResultsRow(ResultRow row){ |
244 | 0 | this.row = row; |
245 | 0 | } |
246 | |
|
247 | |
@Override |
248 | |
public Object getCellData(String columnId) { |
249 | 0 | return row.getValue(columnId); |
250 | |
} |
251 | |
|
252 | |
@Override |
253 | |
public void setCellData(String columnId, Object newValue) { |
254 | 0 | row.setValue(columnId, newValue.toString()); |
255 | 0 | } |
256 | |
|
257 | |
@Override |
258 | |
public String toString(){ |
259 | 0 | return row.toString(); |
260 | |
} |
261 | |
|
262 | |
public ResultRow getResultRow() { |
263 | 0 | return row; |
264 | |
} |
265 | |
} |
266 | |
|
267 | 0 | class FieldAscendingRowComparator extends RowComparator{ |
268 | |
|
269 | |
String columnId; |
270 | |
DataType type; |
271 | |
|
272 | 0 | FieldAscendingRowComparator(String columnId, DataType type) { |
273 | 0 | this.columnId = columnId; |
274 | 0 | this.type = type; |
275 | 0 | } |
276 | |
|
277 | |
@Override |
278 | |
public int compare(Row row0, Row row1) { |
279 | |
String id0, id1; |
280 | |
|
281 | 0 | if (type.equals(DataType.STRING)) { |
282 | 0 | id0 = (String)row0.getCellData(columnId); |
283 | 0 | id1 = (String)row1.getCellData(columnId); |
284 | |
} else { |
285 | 0 | id0 = (String)row0.getCellData(columnId); |
286 | 0 | id1 = (String)row1.getCellData(columnId); |
287 | |
} |
288 | 0 | return id0.compareTo(id1); |
289 | |
} |
290 | |
} |
291 | |
|
292 | 0 | class FieldDescendingRowComparator extends RowComparator{ |
293 | |
|
294 | |
String columnId; |
295 | |
DataType type; |
296 | |
|
297 | 0 | FieldDescendingRowComparator(String columnId, DataType type) { |
298 | 0 | this.columnId = columnId; |
299 | 0 | this.type = type; |
300 | 0 | } |
301 | |
|
302 | |
@Override |
303 | |
public int compare(Row row0, Row row1) { |
304 | |
String id0, id1; |
305 | |
|
306 | 0 | if (type.equals(DataType.STRING)) { |
307 | 0 | id0 = (String)row0.getCellData(columnId); |
308 | 0 | id1 = (String)row1.getCellData(columnId); |
309 | |
} else { |
310 | 0 | id0 = (String)row0.getCellData(columnId); |
311 | 0 | id1 = (String)row1.getCellData(columnId); |
312 | |
} |
313 | 0 | return id1.compareTo(id0); |
314 | |
} |
315 | |
} |