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.ui.client.application.Application; |
22 | |
import org.kuali.student.common.ui.client.application.KSAsyncCallback; |
23 | |
import org.kuali.student.common.ui.client.service.SearchRpcService; |
24 | |
import org.kuali.student.common.ui.client.service.SearchRpcServiceAsync; |
25 | |
import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow; |
26 | |
import org.kuali.student.common.ui.client.widgets.table.scroll.Column; |
27 | |
import org.kuali.student.common.ui.client.widgets.table.scroll.DefaultTableModel; |
28 | |
import org.kuali.student.common.ui.client.widgets.table.scroll.RetrieveAdditionalDataHandler; |
29 | |
import org.kuali.student.common.ui.client.widgets.table.scroll.Row; |
30 | |
import org.kuali.student.common.ui.client.widgets.table.scroll.RowComparator; |
31 | |
import org.kuali.student.common.ui.client.widgets.table.scroll.Table; |
32 | |
import org.kuali.student.core.assembly.data.LookupResultMetadata; |
33 | |
import org.kuali.student.core.assembly.data.Data.DataType; |
34 | |
import org.kuali.student.core.search.dto.SearchRequest; |
35 | |
import org.kuali.student.core.search.dto.SearchResult; |
36 | |
import org.kuali.student.core.search.dto.SearchResultCell; |
37 | |
import org.kuali.student.core.search.dto.SearchResultRow; |
38 | |
|
39 | |
import com.google.gwt.core.client.GWT; |
40 | |
import com.google.gwt.user.client.Window; |
41 | |
import com.google.gwt.user.client.ui.Composite; |
42 | |
import com.google.gwt.user.client.ui.VerticalPanel; |
43 | |
|
44 | 0 | public class SearchResultsTable extends Composite{ |
45 | |
|
46 | 0 | private final int PAGE_SIZE = 10; |
47 | |
|
48 | 0 | private SearchRpcServiceAsync searchRpcServiceAsync = GWT.create(SearchRpcService.class); |
49 | |
|
50 | 0 | private VerticalPanel layout = new VerticalPanel(); |
51 | |
|
52 | |
private DefaultTableModel tableModel; |
53 | |
private String resultIdColumnKey; |
54 | |
private SearchRequest searchRequest; |
55 | 0 | private Table table = new Table(); |
56 | 0 | private boolean isMultiSelect = true; |
57 | |
|
58 | |
public SearchResultsTable(){ |
59 | 0 | super(); |
60 | 0 | redraw(); |
61 | 0 | layout.setWidth("100%"); |
62 | 0 | initWidget(layout); |
63 | 0 | } |
64 | |
|
65 | |
public void redraw(){ |
66 | 0 | layout.clear(); |
67 | 0 | } |
68 | |
|
69 | |
public void setMutipleSelect(boolean isMultiSelect){ |
70 | 0 | this.isMultiSelect = isMultiSelect; |
71 | 0 | } |
72 | |
|
73 | |
|
74 | |
public void initializeTable(List<LookupResultMetadata> listResultMetadata, String resultIdKey){ |
75 | 0 | table = new Table(); |
76 | 0 | this.resultIdColumnKey = resultIdKey; |
77 | |
|
78 | 0 | tableModel = new DefaultTableModel(); |
79 | 0 | tableModel.setMultipleSelectable(isMultiSelect); |
80 | |
|
81 | |
|
82 | 0 | for (LookupResultMetadata r: listResultMetadata){ |
83 | 0 | if(!r.isHidden()){ |
84 | 0 | Column col1 = new Column(); |
85 | 0 | col1.setId(r.getKey()); |
86 | 0 | String header = Application.getApplicationContext().getUILabel("", null, null, r.getName()); |
87 | 0 | col1.setName(header); |
88 | 0 | col1.setId(r.getKey()); |
89 | 0 | col1.setWidth("100px"); |
90 | 0 | col1.setAscendingRowComparator(new FieldAscendingRowComparator(r.getKey(), r.getDataType())); |
91 | 0 | col1.setDescendingRowComparator(new FieldDescendingRowComparator(r.getKey(), r.getDataType())); |
92 | |
|
93 | 0 | tableModel.addColumn(col1); |
94 | 0 | } |
95 | |
} |
96 | |
|
97 | |
|
98 | 0 | if (this.searchRequest.getSearchKey().toLowerCase().contains("cross")) { |
99 | 0 | tableModel.setMoreData(false); |
100 | |
} |
101 | 0 | if(isMultiSelect){ |
102 | 0 | tableModel.installCheckBoxRowHeaderColumn(); |
103 | |
} |
104 | |
|
105 | 0 | table.getScrollPanel().setHeight("300px"); |
106 | 0 | table.setTableModel(tableModel); |
107 | |
|
108 | 0 | table.addRetrieveAdditionalDataHandler(new RetrieveAdditionalDataHandler(){ |
109 | |
@Override |
110 | |
public void onAdditionalDataRequest() { |
111 | 0 | performOnDemandSearch(tableModel.getRowCount(), PAGE_SIZE); |
112 | |
|
113 | 0 | } |
114 | |
}); |
115 | |
|
116 | 0 | redraw(); |
117 | 0 | layout.add(table); |
118 | 0 | } |
119 | |
|
120 | |
public void performSearch(SearchRequest searchRequest, List<LookupResultMetadata> listResultMetadata, String resultIdKey, boolean pagedResults){ |
121 | 0 | this.searchRequest = searchRequest; |
122 | 0 | initializeTable(listResultMetadata, resultIdKey); |
123 | 0 | if (this.searchRequest.getSearchKey().toLowerCase().contains("cross")) { |
124 | |
|
125 | |
|
126 | |
|
127 | 0 | performOnDemandSearch(0, 0); |
128 | |
} |
129 | 0 | else if(pagedResults){ |
130 | 0 | performOnDemandSearch(0, PAGE_SIZE); |
131 | |
} |
132 | |
else{ |
133 | 0 | performOnDemandSearch(0, 0); |
134 | |
} |
135 | 0 | } |
136 | |
|
137 | |
public void performSearch(SearchRequest searchRequest, List<LookupResultMetadata> listResultMetadata, String resultIdKey){ |
138 | 0 | this.performSearch(searchRequest, listResultMetadata, resultIdKey, true); |
139 | 0 | } |
140 | |
|
141 | |
private void performOnDemandSearch(int startAt, int size) { |
142 | |
|
143 | 0 | table.displayLoading(true); |
144 | 0 | searchRequest.setStartAt(startAt); |
145 | 0 | if (size != 0) { |
146 | 0 | searchRequest.setNeededTotalResults(false); |
147 | 0 | searchRequest.setMaxResults(size); |
148 | |
} else { |
149 | 0 | searchRequest.setNeededTotalResults(true); |
150 | |
} |
151 | |
|
152 | 0 | searchRpcServiceAsync.search(searchRequest, new KSAsyncCallback<SearchResult>(){ |
153 | |
|
154 | |
@Override |
155 | |
public void handleFailure(Throwable cause) { |
156 | 0 | GWT.log("Failed to perform search", cause); |
157 | 0 | Window.alert("Failed to perform search"); |
158 | 0 | table.displayLoading(false); |
159 | 0 | } |
160 | |
|
161 | |
@Override |
162 | |
public void onSuccess(SearchResult results) { |
163 | |
|
164 | 0 | if(results != null && results.getRows() != null && results.getRows().size() != 0){ |
165 | 0 | for (SearchResultRow r: results.getRows()){ |
166 | 0 | ResultRow theRow = new ResultRow(); |
167 | 0 | for(SearchResultCell c: r.getCells()){ |
168 | 0 | if(c.getKey().equals(resultIdColumnKey)){ |
169 | 0 | theRow.setId(c.getValue()); |
170 | |
} |
171 | 0 | theRow.setValue(c.getKey(), c.getValue()); |
172 | |
} |
173 | 0 | tableModel.addRow(new SearchResultsRow(theRow)); |
174 | 0 | } |
175 | |
} else { |
176 | 0 | tableModel.setMoreData(false); |
177 | |
} |
178 | 0 | tableModel.fireTableDataChanged(); |
179 | 0 | table.displayLoading(false); |
180 | 0 | } |
181 | |
}); |
182 | 0 | } |
183 | |
|
184 | |
public List<ResultRow> getSelectedRows(){ |
185 | 0 | List<ResultRow> rows = new ArrayList<ResultRow>(); |
186 | 0 | for(Row row : tableModel.getSelectedRows()){ |
187 | 0 | rows.add(((SearchResultsRow)row).getResultRow()); |
188 | |
} |
189 | 0 | return rows; |
190 | |
} |
191 | |
|
192 | |
public List<String> getSelectedIds(){ |
193 | 0 | List<String> ids = new ArrayList<String>(); |
194 | 0 | for(Row row : tableModel.getSelectedRows()){ |
195 | 0 | ids.add(((SearchResultsRow)row).getResultRow().getId()); |
196 | |
} |
197 | 0 | return ids; |
198 | |
} |
199 | |
} |
200 | |
|
201 | |
class SearchResultsRow extends Row { |
202 | |
|
203 | |
ResultRow row; |
204 | |
|
205 | 0 | public SearchResultsRow(ResultRow row){ |
206 | 0 | this.row = row; |
207 | 0 | } |
208 | |
|
209 | |
@Override |
210 | |
public Object getCellData(String columnId) { |
211 | 0 | return row.getValue(columnId); |
212 | |
} |
213 | |
|
214 | |
@Override |
215 | |
public void setCellData(String columnId, Object newValue) { |
216 | 0 | row.setValue(columnId, newValue.toString()); |
217 | 0 | } |
218 | |
|
219 | |
@Override |
220 | |
public String toString(){ |
221 | 0 | return row.toString(); |
222 | |
} |
223 | |
|
224 | |
public ResultRow getResultRow() { |
225 | 0 | return row; |
226 | |
} |
227 | |
} |
228 | |
|
229 | 0 | class FieldAscendingRowComparator extends RowComparator{ |
230 | |
|
231 | |
String columnId; |
232 | |
DataType type; |
233 | |
|
234 | 0 | FieldAscendingRowComparator(String columnId, DataType type) { |
235 | 0 | this.columnId = columnId; |
236 | 0 | this.type = type; |
237 | 0 | } |
238 | |
|
239 | |
@Override |
240 | |
public int compare(Row row0, Row row1) { |
241 | |
String id0, id1; |
242 | |
|
243 | 0 | if (type.equals(DataType.STRING)) { |
244 | 0 | id0 = (String)row0.getCellData(columnId); |
245 | 0 | id1 = (String)row1.getCellData(columnId); |
246 | |
} else { |
247 | 0 | id0 = (String)row0.getCellData(columnId); |
248 | 0 | id1 = (String)row1.getCellData(columnId); |
249 | |
} |
250 | 0 | return id0.compareTo(id1); |
251 | |
} |
252 | |
} |
253 | |
|
254 | 0 | class FieldDescendingRowComparator extends RowComparator{ |
255 | |
|
256 | |
String columnId; |
257 | |
DataType type; |
258 | |
|
259 | 0 | FieldDescendingRowComparator(String columnId, DataType type) { |
260 | 0 | this.columnId = columnId; |
261 | 0 | this.type = type; |
262 | 0 | } |
263 | |
|
264 | |
@Override |
265 | |
public int compare(Row row0, Row row1) { |
266 | |
String id0, id1; |
267 | |
|
268 | 0 | if (type.equals(DataType.STRING)) { |
269 | 0 | id0 = (String)row0.getCellData(columnId); |
270 | 0 | id1 = (String)row1.getCellData(columnId); |
271 | |
} else { |
272 | 0 | id0 = (String)row0.getCellData(columnId); |
273 | 0 | id1 = (String)row1.getCellData(columnId); |
274 | |
} |
275 | 0 | return id1.compareTo(id0); |
276 | |
} |
277 | |
} |