1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.lum.lu.ui.tools.client.widgets; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.HashSet; |
20 | |
import java.util.LinkedHashMap; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
import java.util.Map.Entry; |
24 | |
|
25 | |
import org.kuali.student.common.assembly.data.LookupMetadata; |
26 | |
import org.kuali.student.common.assembly.data.LookupParamMetadata; |
27 | |
import org.kuali.student.common.search.dto.SearchParam; |
28 | |
import org.kuali.student.common.search.dto.SearchRequest; |
29 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
30 | |
import org.kuali.student.common.ui.client.widgets.KSButton; |
31 | |
import org.kuali.student.common.ui.client.widgets.search.SelectedResults; |
32 | |
import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow; |
33 | |
|
34 | |
import com.google.gwt.event.dom.client.ClickEvent; |
35 | |
import com.google.gwt.event.dom.client.ClickHandler; |
36 | |
import com.google.gwt.user.client.ui.Composite; |
37 | |
import com.google.gwt.user.client.ui.FlowPanel; |
38 | |
|
39 | |
public class BrowsePanel extends Composite { |
40 | |
|
41 | |
|
42 | |
private FlowPanel layout; |
43 | |
private FlowPanel tablePanel; |
44 | 0 | private LookupMetadata lookupMetadata = null; |
45 | 0 | private SearchBackedTable searchBackedTable = null; |
46 | 0 | private boolean multiSelect = false; |
47 | |
public Map<String, Object> parameters; |
48 | 0 | private int tableHeight = 200;; |
49 | |
|
50 | |
public interface OnSelectedCallback { |
51 | |
public void selected(List<String> selectedIds); |
52 | |
} |
53 | |
|
54 | |
private OnSelectedCallback onSelectectedCallback; |
55 | |
|
56 | 0 | public BrowsePanel(LookupMetadata lookupMetadata) { |
57 | 0 | this.lookupMetadata = lookupMetadata; |
58 | 0 | layout = new FlowPanel(); |
59 | 0 | layout.addStyleName("KS-Picker-Border"); |
60 | 0 | layout.addStyleName("KS-Advanced-Search-Panel"); |
61 | 0 | tablePanel = new FlowPanel(); |
62 | 0 | this.initWidget(layout); |
63 | 0 | } |
64 | |
|
65 | |
public BrowsePanel(LookupMetadata lookupMetadata, int tableHeight) { |
66 | 0 | this(lookupMetadata); |
67 | 0 | this.tableHeight = tableHeight; |
68 | 0 | } |
69 | |
|
70 | |
public Map<String, Object> getParameters() { |
71 | 0 | if (parameters == null) { |
72 | 0 | parameters = new LinkedHashMap<String, Object>(); |
73 | |
} |
74 | 0 | return parameters; |
75 | |
} |
76 | |
|
77 | |
public void setParameters(Map<String, Object> parameters) { |
78 | 0 | this.parameters = parameters; |
79 | 0 | } |
80 | |
|
81 | |
private Map<String, Object> getDefaultParameters() { |
82 | 0 | Map<String, Object> defParms = new LinkedHashMap<String, Object>(); |
83 | 0 | for (LookupParamMetadata paramMeta : lookupMetadata.getParams()) { |
84 | 0 | if (paramMeta.getDefaultValueString() != null) { |
85 | 0 | defParms.put(paramMeta.getKey(), |
86 | |
paramMeta.getDefaultValueString()); |
87 | 0 | continue; |
88 | |
} |
89 | 0 | if (paramMeta.getDefaultValueList() != null) { |
90 | 0 | defParms.put(paramMeta.getKey(), |
91 | |
paramMeta.getDefaultValueList()); |
92 | |
} |
93 | |
} |
94 | 0 | return defParms; |
95 | |
} |
96 | |
|
97 | |
public OnSelectedCallback getOnSelectectedCallback() { |
98 | 0 | return onSelectectedCallback; |
99 | |
} |
100 | |
|
101 | |
public void setOnSelectectedCallback( |
102 | |
OnSelectedCallback onSelectectedCallback) { |
103 | 0 | this.onSelectectedCallback = onSelectectedCallback; |
104 | 0 | } |
105 | |
|
106 | |
private class SelectButtonClickHandler implements ClickHandler { |
107 | |
|
108 | |
private OnSelectedCallback callback; |
109 | |
private SearchBackedTable searchBackedTable; |
110 | |
|
111 | |
public SelectButtonClickHandler(OnSelectedCallback callback, |
112 | 0 | SearchBackedTable searchBackedTable) { |
113 | 0 | this.callback = callback; |
114 | 0 | this.searchBackedTable = searchBackedTable; |
115 | 0 | } |
116 | |
|
117 | |
@Override |
118 | |
public void onClick(ClickEvent event) { |
119 | 0 | this.callback.selected(this.searchBackedTable.getSelectedIds()); |
120 | 0 | } |
121 | |
|
122 | |
} |
123 | |
|
124 | |
@SuppressWarnings("unchecked") |
125 | |
public void executeSearch(Callback<Boolean> callback) { |
126 | |
|
127 | 0 | tablePanel.clear(); |
128 | 0 | layout.clear(); |
129 | 0 | tablePanel.setVisible(false); |
130 | 0 | layout.add(tablePanel); |
131 | |
|
132 | |
|
133 | 0 | searchBackedTable = new SearchBackedTable(tableHeight); |
134 | 0 | searchBackedTable.addStyleName("KS-Advanced-Search-Results-Table"); |
135 | 0 | KSButton selectButton = new KSButton("Select", |
136 | |
new SelectButtonClickHandler(this.onSelectectedCallback, |
137 | |
this.searchBackedTable)); |
138 | 0 | tablePanel.add(searchBackedTable); |
139 | 0 | tablePanel.add(selectButton); |
140 | 0 | tablePanel.setVisible(false); |
141 | |
|
142 | 0 | SearchRequest searchRequest = new SearchRequest(); |
143 | 0 | List<SearchParam> searchParams = new ArrayList<SearchParam>(); |
144 | 0 | Map<String, Object> parms = getDefaultParameters(); |
145 | 0 | parms.putAll(getParameters()); |
146 | 0 | for (Entry<String, Object> entry : parms.entrySet()) { |
147 | 0 | Object value = entry.getValue(); |
148 | 0 | SearchParam searchParam = new SearchParam(); |
149 | 0 | searchParam.setKey(entry.getKey()); |
150 | 0 | if (value instanceof String) { |
151 | 0 | searchParam.setValue((String) value); |
152 | |
} else { |
153 | 0 | searchParam.setValue((List<String>) value); |
154 | |
} |
155 | 0 | searchParams.add(searchParam); |
156 | 0 | } |
157 | 0 | searchRequest.setParams(searchParams); |
158 | 0 | searchRequest.setSearchKey(lookupMetadata.getSearchTypeId()); |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | 0 | searchBackedTable.performSearch(searchRequest, |
177 | |
lookupMetadata.getResults(), |
178 | |
lookupMetadata.getResultReturnKey(), callback); |
179 | 0 | tablePanel.setVisible(true); |
180 | 0 | layout.setVisible(true); |
181 | 0 | } |
182 | |
|
183 | |
public List<String> getSelectedIds() { |
184 | 0 | List<String> ids = new ArrayList<String>(); |
185 | 0 | if (searchBackedTable != null) { |
186 | 0 | ids = searchBackedTable.getSelectedIds(); |
187 | |
} |
188 | 0 | return ids; |
189 | |
} |
190 | |
|
191 | |
public List<SelectedResults> getSelectedValues() { |
192 | |
|
193 | 0 | List<SelectedResults> selectedValues = new ArrayList<SelectedResults>(); |
194 | 0 | if (searchBackedTable != null) { |
195 | 0 | List<ResultRow> selectedRows = searchBackedTable.getSelectedRows(); |
196 | 0 | for (ResultRow row : selectedRows) { |
197 | 0 | String displayKey = row.getValue(lookupMetadata |
198 | |
.getResultDisplayKey()); |
199 | 0 | String returnKey = row.getValue(lookupMetadata |
200 | |
.getResultReturnKey()); |
201 | 0 | selectedValues.add(new SelectedResults(displayKey, returnKey)); |
202 | 0 | if (multiSelect == false) { |
203 | 0 | break; |
204 | |
} |
205 | 0 | } |
206 | |
} |
207 | |
|
208 | 0 | return selectedValues; |
209 | |
} |
210 | |
|
211 | |
public void showAllRows() { |
212 | 0 | searchBackedTable.getResultRows().clear(); |
213 | 0 | searchBackedTable.getResultRows().addAll( |
214 | |
searchBackedTable.getAllResults()); |
215 | 0 | searchBackedTable.redraw(); |
216 | 0 | } |
217 | |
|
218 | |
public void showOnlyRows(HashSet<String> rowKeys) { |
219 | 0 | searchBackedTable.getResultRows().clear(); |
220 | 0 | for (ResultRow resultRow : searchBackedTable.getAllResults()) { |
221 | 0 | if (rowKeys.contains(resultRow.getId())) { |
222 | 0 | searchBackedTable.getResultRows().add(resultRow); |
223 | |
} |
224 | |
} |
225 | 0 | searchBackedTable.redraw(); |
226 | 0 | } |
227 | |
|
228 | |
public List<ResultRow> getAllResultRows() { |
229 | 0 | return searchBackedTable.getAllResults(); |
230 | |
} |
231 | |
|
232 | |
public boolean isMultiSelect() { |
233 | 0 | return multiSelect; |
234 | |
} |
235 | |
|
236 | |
public void setMultiSelect(boolean multiSelect) { |
237 | 0 | this.multiSelect = multiSelect; |
238 | 0 | } |
239 | |
|
240 | |
} |