Skip to content

Commit 92edae8

Browse files
committed
Merge pull request #1941 from zlamalp/searcher
GUI: Support searching facilities by attribute values
1 parent d488fed commit 92edae8

File tree

3 files changed

+505
-6
lines changed

3 files changed

+505
-6
lines changed
Lines changed: 370 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,370 @@
1+
package cz.metacentrum.perun.webgui.json.searcher;
2+
3+
import com.google.gwt.cell.client.FieldUpdater;
4+
import com.google.gwt.core.client.JavaScriptObject;
5+
import com.google.gwt.json.client.JSONObject;
6+
import com.google.gwt.json.client.JSONString;
7+
import com.google.gwt.user.cellview.client.CellTable;
8+
import com.google.gwt.user.cellview.client.Column;
9+
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
10+
import com.google.gwt.view.client.DefaultSelectionEventManager;
11+
import com.google.gwt.view.client.ListDataProvider;
12+
import com.google.gwt.view.client.MultiSelectionModel;
13+
import cz.metacentrum.perun.webgui.client.PerunWebSession;
14+
import cz.metacentrum.perun.webgui.client.resources.TableSorter;
15+
import cz.metacentrum.perun.webgui.json.JsonCallback;
16+
import cz.metacentrum.perun.webgui.json.JsonCallbackEvents;
17+
import cz.metacentrum.perun.webgui.json.JsonCallbackTable;
18+
import cz.metacentrum.perun.webgui.json.JsonPostClient;
19+
import cz.metacentrum.perun.webgui.json.JsonUtils;
20+
import cz.metacentrum.perun.webgui.json.keyproviders.GeneralKeyProvider;
21+
import cz.metacentrum.perun.webgui.model.PerunError;
22+
import cz.metacentrum.perun.webgui.model.Facility;
23+
import cz.metacentrum.perun.webgui.widgets.AjaxLoaderImage;
24+
import cz.metacentrum.perun.webgui.widgets.PerunTable;
25+
26+
import java.util.ArrayList;
27+
import java.util.Comparator;
28+
import java.util.HashMap;
29+
import java.util.Map;
30+
31+
/**
32+
* Searching for Facilities
33+
*
34+
* @author Vaclav Mach <374430@mail.muni.cz>
35+
*/
36+
public class GetFacilities implements JsonCallback, JsonCallbackTable<Facility> {
37+
38+
// session
39+
private PerunWebSession session = PerunWebSession.getInstance();
40+
// json url
41+
static private final String JSON_URL = "searcher/getFacilities";
42+
// Data provider
43+
private ListDataProvider<Facility> dataProvider = new ListDataProvider<Facility>();
44+
// table
45+
private PerunTable<Facility> table;
46+
// table data
47+
private ArrayList<Facility> list = new ArrayList<Facility>();
48+
// Selection model
49+
final MultiSelectionModel<Facility> selectionModel = new MultiSelectionModel<Facility>(new GeneralKeyProvider<Facility>());
50+
// External events
51+
private JsonCallbackEvents events = new JsonCallbackEvents();
52+
// Table field updater
53+
private FieldUpdater<Facility, String> tableFieldUpdater;
54+
// loader image
55+
private AjaxLoaderImage loaderImage = new AjaxLoaderImage(true, "Enter keywords to search.");
56+
57+
// checkable
58+
private boolean checkable = true;
59+
60+
// private hashmap attributes to search by
61+
private Map<String, String> attributesToSearchBy = new HashMap<String, String>();
62+
63+
64+
65+
/**
66+
* Creates a new request
67+
*/
68+
public GetFacilities() {
69+
}
70+
71+
/**
72+
* Creates a new request with custom events
73+
* @param events
74+
*/
75+
public GetFacilities(JsonCallbackEvents events) {
76+
this.events = events;
77+
}
78+
79+
/**
80+
* Returns table of users
81+
* @param
82+
*/
83+
public CellTable<Facility> getTable(FieldUpdater<Facility, String> fu){
84+
this.tableFieldUpdater = fu;
85+
return this.getTable();
86+
}
87+
88+
/**
89+
* Returns table of users.
90+
* @return
91+
*/
92+
public CellTable<Facility> getTable(){
93+
// retrieve data
94+
retrieveData();
95+
return getEmptyTable();
96+
97+
}
98+
99+
/**
100+
* Returns empty table definition
101+
* @return
102+
*/
103+
public CellTable<Facility> getEmptyTable(FieldUpdater<Facility, String> fu){
104+
this.tableFieldUpdater = fu;
105+
return this.getEmptyTable();
106+
};
107+
108+
/**
109+
* Returns empty table definition
110+
* @return
111+
*/
112+
public CellTable<Facility> getEmptyTable(){
113+
114+
// Table data provider.
115+
dataProvider = new ListDataProvider<Facility>(list);
116+
117+
// Cell table
118+
table = new PerunTable<Facility>(list);
119+
120+
// Connect the table to the data provider.
121+
dataProvider.addDataDisplay(table);
122+
123+
// Sorting
124+
ListHandler<Facility> columnSortHandler = new ListHandler<Facility>(dataProvider.getList());
125+
table.addColumnSortHandler(columnSortHandler);
126+
127+
// table selection
128+
table.setSelectionModel(selectionModel, DefaultSelectionEventManager.<Facility> createCheckboxManager());
129+
130+
// set empty content & loader
131+
table.setEmptyTableWidget(loaderImage);
132+
133+
// columns
134+
if (checkable) {
135+
table.addCheckBoxColumn();
136+
}
137+
table.addIdColumn("Facility ID", tableFieldUpdater);
138+
139+
// NAME COLUMN
140+
Column<Facility, String> nameColumn = JsonUtils.addColumn(new JsonUtils.GetValue<Facility, String>() {
141+
public String getValue(Facility facility) {
142+
return facility.getName(); // display full name with titles
143+
}
144+
},tableFieldUpdater);
145+
146+
nameColumn.setSortable(true);
147+
columnSortHandler.setComparator(nameColumn, new Comparator<Facility>() {
148+
public int compare(Facility o1, Facility o2) {
149+
return o1.getName().compareToIgnoreCase(o2.getName()); // sort by name without titles
150+
}
151+
});
152+
153+
// Description
154+
Column<Facility, String> descriptionColumn = JsonUtils.addColumn(new JsonUtils.GetValue<Facility, String>() {
155+
public String getValue(Facility facility) {
156+
return facility.getDescription(); // display full name with titles
157+
}
158+
},tableFieldUpdater);
159+
160+
descriptionColumn.setSortable(true);
161+
columnSortHandler.setComparator(descriptionColumn, new Comparator<Facility>() {
162+
public int compare(Facility o1, Facility o2) {
163+
return o1.getDescription().compareToIgnoreCase(o2.getDescription());
164+
}
165+
});
166+
167+
table.addColumn(nameColumn, "Name");
168+
table.addColumn(descriptionColumn, "Description");
169+
170+
return table;
171+
172+
}
173+
174+
/**
175+
* Do search
176+
*/
177+
public void search(){
178+
179+
loaderImage.setEmptyResultMessage("No facilities found.");
180+
181+
clearTable();
182+
retrieveData();
183+
}
184+
185+
/**
186+
* Add parameter
187+
*/
188+
public void addSearchParameter(String name, String value){
189+
attributesToSearchBy.put(name, value);
190+
}
191+
192+
/**
193+
* Retrieves data from RPC
194+
*/
195+
public void retrieveData() {
196+
// empty
197+
if(this.attributesToSearchBy.size() == 0){
198+
session.getUiElements().setLogText("No keywords.");
199+
return;
200+
}
201+
202+
// ok, start
203+
loaderImage.loadingStart();
204+
205+
// build request
206+
207+
JSONObject attributesWithSearchingValues = new JSONObject();
208+
for(Map.Entry<String, String> entry : attributesToSearchBy.entrySet()) {
209+
String name = entry.getKey();
210+
String value = entry.getValue();
211+
212+
attributesWithSearchingValues.put(name, new JSONString(value));
213+
}
214+
215+
JSONObject req = new JSONObject();
216+
req.put("attributesWithSearchingValues", attributesWithSearchingValues);
217+
218+
// send request
219+
JsonPostClient js = new JsonPostClient(new JsonCallbackEvents(){
220+
public void onError(PerunError error) {
221+
session.getUiElements().setLogErrorText("Error while loading facilities.");
222+
loaderImage.loadingError(error);
223+
events.onError(error);
224+
}
225+
public void onLoadingStart() {
226+
loaderImage.loadingStart();
227+
session.getUiElements().setLogText("Loading facilities started.");
228+
events.onLoadingStart();
229+
}
230+
public void onFinished(JavaScriptObject jso) {
231+
loaderImage.loadingFinished();
232+
setList(JsonUtils.<Facility>jsoAsList(jso));
233+
sortTable();
234+
session.getUiElements().setLogText("Facilities loaded: " + list.size());
235+
events.onFinished(jso);
236+
}
237+
238+
239+
});
240+
js.sendData(JSON_URL, req);
241+
242+
return;
243+
}
244+
245+
/**
246+
* Sorts table by objects Name
247+
*/
248+
public void sortTable() {
249+
list = new TableSorter<Facility>().sortByName(getList());
250+
dataProvider.flush();
251+
dataProvider.refresh();
252+
}
253+
254+
/**
255+
* Add object as new row to table
256+
*
257+
* @param object Facility to be added as new row
258+
*/
259+
public void addToTable(Facility object) {
260+
list.add(object);
261+
dataProvider.flush();
262+
dataProvider.refresh();
263+
}
264+
265+
/**
266+
* Removes object as row from table
267+
*
268+
* @param object Facility to be removed as row
269+
*/
270+
public void removeFromTable(Facility object) {
271+
list.remove(object);
272+
selectionModel.getSelectedSet().remove(object);
273+
dataProvider.flush();
274+
dataProvider.refresh();
275+
}
276+
277+
/**
278+
* Clear all table content
279+
*/
280+
public void clearTable(){
281+
list.clear();
282+
selectionModel.clear();
283+
dataProvider.flush();
284+
dataProvider.refresh();
285+
}
286+
287+
/**
288+
* Clears list of selected items
289+
*/
290+
public void clearTableSelectedSet(){
291+
selectionModel.clear();
292+
}
293+
294+
/**
295+
* Return selected items from list
296+
*
297+
* @return return list of checked items
298+
*/
299+
public ArrayList<Facility> getTableSelectedList(){
300+
return JsonUtils.setToList(selectionModel.getSelectedSet());
301+
}
302+
303+
/**
304+
* Called, when an error occurs
305+
*/
306+
public void onError(PerunError error) {
307+
session.getUiElements().setLogErrorText("Error while loading facilities.");
308+
loaderImage.loadingError(error);
309+
events.onError(error);
310+
}
311+
312+
/**
313+
* Called, when loading starts
314+
*/
315+
public void onLoadingStart() {
316+
session.getUiElements().setLogText("Loading facilities started.");
317+
events.onLoadingStart();
318+
}
319+
320+
/**
321+
* Called, when operation finishes successfully.
322+
*/
323+
public void onFinished(JavaScriptObject jso) {
324+
loaderImage.loadingFinished();
325+
setList(JsonUtils.<Facility>jsoAsList(jso));
326+
sortTable();
327+
session.getUiElements().setLogText("Facilities loaded: " + list.size());
328+
events.onFinished(jso);
329+
}
330+
331+
public void insertToTable(int index, Facility object) {
332+
list.add(index, object);
333+
dataProvider.flush();
334+
dataProvider.refresh();
335+
}
336+
337+
public void setEditable(boolean editable) {
338+
// TODO Auto-generated method stub
339+
}
340+
341+
public void setCheckable(boolean checkable) {
342+
// TODO Auto-generated method stub
343+
}
344+
345+
public void setList(ArrayList<Facility> list) {
346+
clearTable();
347+
this.list.addAll(list);
348+
dataProvider.flush();
349+
dataProvider.refresh();
350+
}
351+
352+
public ArrayList<Facility> getList() {
353+
return this.list;
354+
}
355+
356+
357+
public void setSelected(Facility facility) {
358+
selectionModel.setSelected(facility, true);
359+
}
360+
361+
public void setEvents(JsonCallbackEvents event) {
362+
this.events = event;
363+
}
364+
365+
public void clearParameters() {
366+
this.attributesToSearchBy.clear();
367+
}
368+
369+
370+
}

0 commit comments

Comments
 (0)