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.HashMap; |
20 | |
import java.util.LinkedHashMap; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
|
24 | |
import org.kuali.student.common.ui.client.application.Application; |
25 | |
import org.kuali.student.common.ui.client.configurable.mvc.DefaultWidgetFactory; |
26 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
27 | |
import org.kuali.student.common.ui.client.widgets.KSButton; |
28 | |
import org.kuali.student.common.ui.client.widgets.KSDropDown; |
29 | |
import org.kuali.student.common.ui.client.widgets.KSLabel; |
30 | |
import org.kuali.student.common.ui.client.widgets.KSButtonAbstract.ButtonStyle; |
31 | |
import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonEnumerations; |
32 | |
import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonEnumerations.ButtonEnum; |
33 | |
import org.kuali.student.common.ui.client.widgets.field.layout.button.ActionCancelGroup; |
34 | |
import org.kuali.student.common.ui.client.widgets.field.layout.button.ButtonGroup; |
35 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.FieldElement; |
36 | |
import org.kuali.student.common.ui.client.widgets.layout.HorizontalBlockFlowPanel; |
37 | |
import org.kuali.student.common.ui.client.widgets.layout.VerticalFlowPanel; |
38 | |
import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract; |
39 | |
import org.kuali.student.common.ui.client.widgets.list.ListItems; |
40 | |
import org.kuali.student.common.ui.client.widgets.list.SelectionChangeEvent; |
41 | |
import org.kuali.student.common.ui.client.widgets.list.SelectionChangeHandler; |
42 | |
import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow; |
43 | |
import org.kuali.student.core.assembly.data.LookupMetadata; |
44 | |
import org.kuali.student.core.assembly.data.LookupParamMetadata; |
45 | |
import org.kuali.student.core.assembly.data.Metadata; |
46 | |
import org.kuali.student.core.assembly.data.LookupMetadata.Usage; |
47 | |
import org.kuali.student.core.assembly.data.Metadata.WriteAccess; |
48 | |
import org.kuali.student.core.search.dto.SearchParam; |
49 | |
import org.kuali.student.core.search.dto.SearchRequest; |
50 | |
|
51 | |
import com.google.gwt.core.client.GWT; |
52 | |
import com.google.gwt.event.dom.client.ClickEvent; |
53 | |
import com.google.gwt.event.dom.client.ClickHandler; |
54 | |
import com.google.gwt.user.client.ui.Composite; |
55 | |
import com.google.gwt.user.client.ui.HTMLPanel; |
56 | |
import com.google.gwt.user.client.ui.HasText; |
57 | |
import com.google.gwt.user.client.ui.HasValue; |
58 | |
import com.google.gwt.user.client.ui.SimplePanel; |
59 | |
import com.google.gwt.user.client.ui.VerticalPanel; |
60 | |
import com.google.gwt.user.client.ui.Widget; |
61 | |
|
62 | 0 | public class SearchPanel extends Composite{ |
63 | |
|
64 | |
|
65 | 0 | private VerticalFlowPanel layout = new VerticalFlowPanel(); |
66 | 0 | private SimplePanel searchSelectorPanel = new SimplePanel(); |
67 | 0 | private VerticalFlowPanel resultsTablePanel = new VerticalFlowPanel(); |
68 | 0 | private HorizontalBlockFlowPanel enteredCriteriaString = new HorizontalBlockFlowPanel(); |
69 | |
private CollapsablePanel modifySearchPanel; |
70 | 0 | private String criteriaInstructions = getMessage("searchPanelEnterFields"); |
71 | 0 | private KSLabel enteredCriteriaHeading = new KSLabel(getMessage("searchPanelCriteria")); |
72 | |
private SearchResultsTable table; |
73 | 0 | private boolean isMultiSelect = true; |
74 | |
|
75 | 0 | public static enum SearchStyle{ADVANCED, CUSTOM}; |
76 | |
private ActionCancelGroup actionCancelButtons; |
77 | |
|
78 | 0 | private String actionLabel = getMessage("search"); |
79 | 0 | private boolean resultsSelected = false; |
80 | |
|
81 | |
|
82 | 0 | private List<LookupMetadata> lookups = new ArrayList<LookupMetadata>(); |
83 | 0 | private boolean multiSelect = false; |
84 | 0 | private boolean resultsShown = false; |
85 | 0 | private SearchParametersWidget activeSearchParametersWidget = null; |
86 | |
|
87 | 0 | private Map<String, SearchParametersWidget> searchParameterWidgetMap = new HashMap<String, SearchParametersWidget>(); |
88 | 0 | private List<SearchField> searchFields = new ArrayList<SearchField>(); |
89 | 0 | private List<Callback<LookupMetadata>> lookupChangedCallbacks = new ArrayList<Callback<LookupMetadata>>(); |
90 | |
private String selectedLookupName; |
91 | 0 | private List<Callback<List<SelectedResults>>> selectedCompleteCallbacks = new ArrayList<Callback<List<SelectedResults>>>(); |
92 | 0 | private List<Callback<Boolean>> actionCompletedCallbacks = new ArrayList<Callback<Boolean>>(); |
93 | |
|
94 | |
|
95 | |
interface SearchParametersWidget { |
96 | |
public SearchRequest getSearchRequest(); |
97 | |
public LookupMetadata getLookupMetadata(); |
98 | |
public List<HasSearchParam> getSearchParams(); |
99 | |
} |
100 | |
|
101 | 0 | public SearchPanel(LookupMetadata meta){ |
102 | 0 | lookups.add(meta); |
103 | |
|
104 | 0 | this.initWidget(layout); |
105 | 0 | } |
106 | |
|
107 | 0 | public SearchPanel(List<LookupMetadata> metas){ |
108 | 0 | lookups = metas; |
109 | |
|
110 | 0 | this.initWidget(layout); |
111 | 0 | } |
112 | |
|
113 | |
@SuppressWarnings("unchecked") |
114 | |
public ButtonGroup getButtons(){ |
115 | 0 | return actionCancelButtons; |
116 | |
} |
117 | |
|
118 | |
public void setMutipleSelect(boolean isMultiSelect){ |
119 | 0 | this.isMultiSelect = isMultiSelect; |
120 | 0 | } |
121 | |
|
122 | |
public void setupButtons() { |
123 | 0 | if (actionCancelButtons != null) { |
124 | 0 | actionCancelButtons.setButtonText(ButtonEnumerations.SearchCancelEnum.SEARCH, getActionLabel()); |
125 | 0 | actionCancelButtons.addCallback(new Callback<ButtonEnumerations.ButtonEnum>(){ |
126 | |
@Override |
127 | |
public void exec(ButtonEnum result) { |
128 | 0 | if (result == ButtonEnumerations.SearchCancelEnum.SEARCH) { |
129 | 0 | table.getContentTable().removeContent(); |
130 | 0 | getActionCompleteCallback().exec(true); |
131 | |
} |
132 | 0 | } |
133 | |
}); |
134 | |
} |
135 | 0 | } |
136 | |
|
137 | |
public void setupSearch() { |
138 | |
|
139 | 0 | resultsTablePanel.clear(); |
140 | 0 | layout.clear(); |
141 | 0 | resultsShown = false; |
142 | |
|
143 | |
|
144 | |
Widget searchParamPanel; |
145 | 0 | if (lookups.size() == 1) { |
146 | 0 | searchParamPanel = createSearchParamPanel(lookups.get(0)); |
147 | 0 | selectedLookupName = lookups.get(0).getName(); |
148 | 0 | activeSearchParametersWidget = searchParameterWidgetMap.get(selectedLookupName); |
149 | |
} else { |
150 | 0 | LinkedHashMap<String, Widget> searches = new LinkedHashMap<String, Widget>(); |
151 | 0 | LinkedHashMap<String, LookupMetadata> searchLookups = new LinkedHashMap<String, LookupMetadata>(); |
152 | 0 | for(LookupMetadata lookup: lookups){ |
153 | 0 | searches.put(lookup.getName(), createSearchParamPanel(lookup)); |
154 | 0 | searchLookups.put(lookup.getName(), lookup); |
155 | |
} |
156 | 0 | selectedLookupName = lookups.get(0).getName(); |
157 | |
|
158 | 0 | activeSearchParametersWidget = searchParameterWidgetMap.get(selectedLookupName); |
159 | 0 | String actionLabel = (lookups.get(0) == null)? null : lookups.get(0) |
160 | |
.getWidgetOptionValue(LookupMetadata.WidgetOption.ADVANCED_LIGHTBOX_ACTION_LABEL); |
161 | 0 | setActionLabel(actionLabel); |
162 | 0 | searchParamPanel = new SwappablePanel(searches); |
163 | 0 | ((SwappablePanel)searchParamPanel).setSearchLookups(searchLookups); |
164 | 0 | ((SwappablePanel)searchParamPanel).addLookupChangedCallback(new Callback<LookupMetadata>() { |
165 | |
@Override |
166 | |
public void exec(LookupMetadata selectedLookup) { |
167 | 0 | activeSearchParametersWidget = searchParameterWidgetMap.get(selectedLookup.getName()); |
168 | 0 | selectedLookupName = selectedLookup.getName(); |
169 | 0 | if (lookupChangedCallbacks != null) { |
170 | 0 | for (Callback<LookupMetadata> callback : lookupChangedCallbacks) { |
171 | 0 | callback.exec(selectedLookup); |
172 | |
} |
173 | |
} |
174 | 0 | } |
175 | |
}); |
176 | |
} |
177 | 0 | searchSelectorPanel.setWidget(searchParamPanel); |
178 | 0 | layout.add(searchSelectorPanel); |
179 | |
|
180 | |
|
181 | 0 | enteredCriteriaHeading.addStyleName("ks-form-module-single-line-margin"); |
182 | 0 | enteredCriteriaHeading.addStyleName("KS-Advanced-Search-Search-Criteria-Label"); |
183 | 0 | resultsTablePanel.add(enteredCriteriaHeading); |
184 | 0 | resultsTablePanel.add(enteredCriteriaString); |
185 | 0 | resultsTablePanel.setVisible(false); |
186 | 0 | table = new SearchResultsTable(); |
187 | 0 | table.setMutipleSelect(isMultiSelect); |
188 | 0 | table.addStyleName("KS-Advanced-Search-Results-Table"); |
189 | 0 | resultsTablePanel.add(table); |
190 | 0 | layout.add(resultsTablePanel); |
191 | |
|
192 | 0 | table.getMslabel().addClickHandler(new ClickHandler(){ |
193 | |
|
194 | |
@Override |
195 | |
public void onClick(ClickEvent event) { |
196 | 0 | if(modifySearchPanel.isOpen()){ |
197 | 0 | modifySearchPanel.close(); |
198 | |
} |
199 | |
else{ |
200 | 0 | modifySearchPanel.open(); |
201 | |
} |
202 | |
|
203 | 0 | resultsTablePanel.setVisible(false); |
204 | 0 | resultsSelected = false; |
205 | 0 | actionCancelButtons.setButtonText(ButtonEnumerations.SearchCancelEnum.SEARCH, getActionLabel()); |
206 | 0 | } |
207 | |
}); |
208 | |
|
209 | 0 | resultsSelected = false; |
210 | 0 | actionCancelButtons.setButtonText(ButtonEnumerations.SearchCancelEnum.SEARCH, getActionLabel()); |
211 | 0 | } |
212 | |
|
213 | |
private Widget createSearchParamPanel(LookupMetadata meta){ |
214 | 0 | ParamListItems listItems = new ParamListItems(meta); |
215 | 0 | final AdvancedSearch advancedSearch = new AdvancedSearch(meta); |
216 | 0 | LinkPanel panel = new LinkPanel(SearchStyle.ADVANCED, advancedSearch); |
217 | 0 | searchParameterWidgetMap.put(meta.getName(), advancedSearch); |
218 | |
|
219 | |
|
220 | 0 | for(LookupParamMetadata metaParam: meta.getParams()){ |
221 | 0 | if ((metaParam.getUsage() == Usage.CUSTOM) || (metaParam.getUsage() == Usage.ADVANCED_CUSTOM)) { |
222 | 0 | final CustomizedSearch customizedSearch = new CustomizedSearch(meta, listItems); |
223 | 0 | KSButton button = panel.addLinkToPanel(SearchStyle.ADVANCED, getMessage("searchPanelCustomizeSearch"), SearchStyle.CUSTOM); |
224 | 0 | button.addClickHandler(new ClickHandler(){ |
225 | |
|
226 | |
@Override |
227 | |
public void onClick(ClickEvent event) { |
228 | 0 | resultsTablePanel.setVisible(false); |
229 | 0 | activeSearchParametersWidget = customizedSearch; |
230 | 0 | }}); |
231 | 0 | button.addStyleName("KS-Advanced-Search-Link"); |
232 | 0 | button.getParent().addStyleName("clearfix"); |
233 | 0 | panel.addPanel(SearchStyle.CUSTOM, customizedSearch); |
234 | 0 | button = panel.addLinkToPanel(SearchStyle.CUSTOM, getMessage("searchPanelReturnToAdvancedSearch"), SearchStyle.ADVANCED); |
235 | 0 | button.addClickHandler(new ClickHandler(){ |
236 | |
|
237 | |
@Override |
238 | |
public void onClick(ClickEvent event) { |
239 | 0 | resultsTablePanel.setVisible(false); |
240 | 0 | activeSearchParametersWidget = advancedSearch; |
241 | 0 | }}); |
242 | 0 | button.addStyleName("KS-Advanced-Search-Link"); |
243 | 0 | button.getParent().addStyleName("clearfix"); |
244 | 0 | break; |
245 | |
} |
246 | |
} |
247 | |
|
248 | 0 | return panel; |
249 | |
} |
250 | |
|
251 | 0 | private class CustomizedSearch extends Composite implements SearchParametersWidget { |
252 | |
|
253 | 0 | private List<CustomLine> lines = new ArrayList<CustomLine>(); |
254 | 0 | private List<HasSearchParam> searchParams = new ArrayList<HasSearchParam>(); |
255 | 0 | private VerticalPanel layout = new VerticalPanel(); |
256 | 0 | private VerticalPanel linePanel = new VerticalPanel(); |
257 | |
private LookupMetadata meta; |
258 | |
|
259 | 0 | public CustomizedSearch(final LookupMetadata meta, final ParamListItems listItems){ |
260 | |
|
261 | 0 | KSLabel instrLabel = new KSLabel(criteriaInstructions); |
262 | 0 | layout.add(instrLabel); |
263 | |
|
264 | 0 | layout.add(linePanel); |
265 | 0 | CustomLine line = new CustomLine(meta, listItems); |
266 | 0 | line.addStyleName("ks-form-module-single-line-margin"); |
267 | 0 | linePanel.add(line); |
268 | 0 | lines.add(line); |
269 | 0 | searchParams.add(line); |
270 | 0 | this.meta = meta; |
271 | |
|
272 | 0 | KSButton addCriteria = new KSButton(getMessage("searchPanelAddCriteria"), ButtonStyle.SECONDARY); |
273 | 0 | addCriteria.addClickHandler(new ClickHandler(){ |
274 | |
|
275 | |
@Override |
276 | |
public void onClick(ClickEvent event) { |
277 | 0 | CustomLine line = new CustomLine(meta, listItems); |
278 | 0 | line.addStyleName("ks-form-module-single-line-margin"); |
279 | 0 | linePanel.add(line); |
280 | 0 | lines.add(line); |
281 | 0 | searchParams.add(line); |
282 | 0 | } |
283 | |
}); |
284 | |
|
285 | 0 | addCriteria.addStyleName("ks-form-module-single-line-margin"); |
286 | 0 | layout.add(addCriteria); |
287 | |
|
288 | 0 | this.initWidget(layout); |
289 | 0 | } |
290 | |
|
291 | |
public LookupMetadata getLookupMetadata() { |
292 | 0 | return meta; |
293 | |
} |
294 | |
|
295 | |
@Override |
296 | |
public SearchRequest getSearchRequest() { |
297 | |
|
298 | |
|
299 | 0 | SearchRequest sr = new SearchRequest(); |
300 | 0 | List<SearchParam> params = new ArrayList<SearchParam>(); |
301 | 0 | for(CustomLine field: lines){ |
302 | 0 | SearchParam param = field.getSearchParam(); |
303 | |
|
304 | 0 | if((param.getValue() != null)){ |
305 | 0 | params.add(param); |
306 | |
} |
307 | 0 | } |
308 | |
|
309 | |
|
310 | 0 | for(LookupParamMetadata metaParam: meta.getParams()){ |
311 | |
|
312 | |
|
313 | 0 | if ((metaParam.getUsage() != Usage.CUSTOM) && (metaParam.getUsage() != Usage.ADVANCED_CUSTOM)) { |
314 | 0 | continue; |
315 | |
} |
316 | |
|
317 | 0 | if(metaParam.getWriteAccess() == WriteAccess.NEVER){ |
318 | 0 | SearchParam param = new SearchParam(); |
319 | 0 | param.setKey(metaParam.getKey()); |
320 | 0 | if(metaParam.getDefaultValueList()==null){ |
321 | 0 | param.setValue(metaParam.getDefaultValueString()); |
322 | |
}else{ |
323 | 0 | param.setValue(metaParam.getDefaultValueList()); |
324 | |
} |
325 | 0 | params.add(param); |
326 | 0 | } |
327 | 0 | else if(metaParam.getWriteAccess() == WriteAccess.WHEN_NULL){ |
328 | 0 | if((metaParam.getDefaultValueString() != null && !metaParam.getDefaultValueString().isEmpty())|| |
329 | |
(metaParam.getDefaultValueList() != null && !metaParam.getDefaultValueList().isEmpty())){ |
330 | 0 | SearchParam param = new SearchParam(); |
331 | 0 | param.setKey(metaParam.getKey()); |
332 | 0 | if(metaParam.getDefaultValueList()==null){ |
333 | 0 | param.setValue(metaParam.getDefaultValueString()); |
334 | |
}else{ |
335 | 0 | param.setValue(metaParam.getDefaultValueList()); |
336 | |
} |
337 | 0 | params.add(param); |
338 | 0 | } |
339 | |
} |
340 | |
} |
341 | |
|
342 | 0 | sr.setParams(params); |
343 | 0 | sr.setSearchKey(meta.getSearchTypeId()); |
344 | 0 | return sr; |
345 | |
} |
346 | |
|
347 | |
@Override |
348 | |
public List<HasSearchParam> getSearchParams() { |
349 | 0 | return searchParams; |
350 | |
} |
351 | |
|
352 | |
} |
353 | |
|
354 | |
private interface HasSearchParam{ |
355 | |
public SearchParam getSearchParam(); |
356 | |
public String getFieldName(); |
357 | |
} |
358 | |
|
359 | 0 | private static class CustomLine extends Composite implements HasSearchParam{ |
360 | 0 | private KSDropDown paramSelector = new KSDropDown(); |
361 | 0 | private SimplePanel widgetPanel = new SimplePanel(); |
362 | 0 | private Widget widget = null; |
363 | |
private String key; |
364 | 0 | private HorizontalBlockFlowPanel layout = new HorizontalBlockFlowPanel(); |
365 | |
private ParamListItems listItems; |
366 | |
|
367 | 0 | public CustomLine(LookupMetadata meta, final ParamListItems listItems){ |
368 | |
|
369 | 0 | List<LookupParamMetadata> customParams = new ArrayList<LookupParamMetadata>(); |
370 | |
|
371 | 0 | for (LookupParamMetadata lookupParamMetadata : listItems.getParams()) { |
372 | 0 | if (lookupParamMetadata.getWriteAccess() != WriteAccess.NEVER){ |
373 | 0 | if (lookupParamMetadata.getUsage() == Usage.CUSTOM || lookupParamMetadata.getUsage() == Usage.ADVANCED_CUSTOM ) { |
374 | 0 | customParams.add(lookupParamMetadata); |
375 | |
} |
376 | |
} |
377 | |
} |
378 | |
|
379 | 0 | ParamListItems customParamList = new ParamListItems(customParams); |
380 | |
|
381 | 0 | this.listItems = customParamList; |
382 | 0 | paramSelector.setBlankFirstItem(false); |
383 | 0 | paramSelector.setListItems(customParamList); |
384 | |
|
385 | 0 | String id = meta.getParams().get(0).getKey(); |
386 | 0 | paramSelector.selectItem(id); |
387 | 0 | widget = listItems.getWidget(id); |
388 | 0 | key = id; |
389 | 0 | widgetPanel.setWidget(widget); |
390 | 0 | paramSelector.addSelectionChangeHandler(new SelectionChangeHandler(){ |
391 | |
|
392 | |
@Override |
393 | |
public void onSelectionChange(SelectionChangeEvent event) { |
394 | 0 | String id = ((KSSelectItemWidgetAbstract)event.getWidget()).getSelectedItem(); |
395 | 0 | widget = listItems.getWidget(id); |
396 | 0 | widgetPanel.setWidget(widget); |
397 | 0 | key = id; |
398 | |
|
399 | 0 | } |
400 | |
}); |
401 | 0 | layout.add(paramSelector); |
402 | 0 | layout.add(widgetPanel); |
403 | 0 | this.initWidget(layout); |
404 | 0 | } |
405 | |
|
406 | |
public SearchParam getSearchParam(){ |
407 | 0 | return SearchPanel.getSearchParam(widget, key); |
408 | |
} |
409 | |
|
410 | |
public String getKey(){ |
411 | 0 | return key; |
412 | |
} |
413 | |
|
414 | |
public String getFieldName(){ |
415 | 0 | String id = paramSelector.getSelectedItem(); |
416 | 0 | return listItems.getItemText(id); |
417 | |
} |
418 | |
} |
419 | |
|
420 | |
private class AdvancedSearch extends Composite implements SearchParametersWidget { |
421 | |
private LookupMetadata meta; |
422 | 0 | private List<HasSearchParam> searchParams = new ArrayList<HasSearchParam>(); |
423 | |
|
424 | 0 | public AdvancedSearch(final LookupMetadata meta){ |
425 | 0 | VerticalPanel panel = new VerticalPanel(); |
426 | |
|
427 | 0 | KSLabel instrLabel = new KSLabel(); |
428 | 0 | panel.add(instrLabel); |
429 | 0 | this.meta = meta; |
430 | |
|
431 | |
|
432 | 0 | boolean allFieldsRequired = true; |
433 | 0 | for(LookupParamMetadata param: meta.getParams()){ |
434 | |
|
435 | |
|
436 | 0 | if ((param.getUsage() != Usage.ADVANCED) && (param.getUsage() != Usage.ADVANCED_CUSTOM)) { |
437 | 0 | continue; |
438 | |
} |
439 | |
|
440 | 0 | if ((param.getWriteAccess() == WriteAccess.ALWAYS) || (param.getWriteAccess() == WriteAccess.REQUIRED)){ |
441 | 0 | SearchField paramField = new SearchField(param); |
442 | 0 | searchFields.add(paramField); |
443 | 0 | panel.add(paramField); |
444 | 0 | searchParams.add(paramField); |
445 | 0 | } |
446 | 0 | else if (param.getWriteAccess() == WriteAccess.WHEN_NULL){ |
447 | 0 | if(param.getDefaultValueString() == null && param.getDefaultValueList() == null){ |
448 | 0 | SearchField paramField = new SearchField(param); |
449 | 0 | searchFields.add(paramField); |
450 | 0 | panel.add(paramField); |
451 | 0 | searchParams.add(paramField); |
452 | |
} |
453 | |
} |
454 | |
|
455 | 0 | if (param.getWriteAccess() != Metadata.WriteAccess.REQUIRED) { |
456 | 0 | allFieldsRequired = false; |
457 | |
} |
458 | |
} |
459 | |
|
460 | |
|
461 | 0 | if ((searchFields.size() > 1) || (allFieldsRequired == false)) { |
462 | 0 | instrLabel.setText(criteriaInstructions); |
463 | |
} |
464 | |
|
465 | 0 | this.initWidget(panel); |
466 | 0 | } |
467 | |
|
468 | |
public LookupMetadata getLookupMetadata() { |
469 | 0 | return meta; |
470 | |
} |
471 | |
|
472 | |
@Override |
473 | |
public SearchRequest getSearchRequest() { |
474 | 0 | SearchRequest sr = new SearchRequest(); |
475 | 0 | List<SearchParam> params = new ArrayList<SearchParam>(); |
476 | 0 | List<HasSearchParam> searchParams = getSearchParams(); |
477 | |
|
478 | |
|
479 | |
|
480 | 0 | for(HasSearchParam field: searchParams){ |
481 | 0 | SearchParam param = field.getSearchParam(); |
482 | |
|
483 | |
|
484 | |
|
485 | |
|
486 | 0 | if ((param.getValue() != null) && ((param.getValue().toString().trim().isEmpty() == false) || (param.getKey().toLowerCase().indexOf("optional") == -1))) { |
487 | 0 | params.add(param); |
488 | |
} |
489 | 0 | } |
490 | |
|
491 | |
|
492 | 0 | for(LookupParamMetadata metaParam: meta.getParams()){ |
493 | 0 | if(metaParam.getWriteAccess() == WriteAccess.NEVER){ |
494 | 0 | if ((metaParam.getDefaultValueString() == null || metaParam.getDefaultValueString().isEmpty())&& |
495 | |
(metaParam.getDefaultValueList() == null || metaParam.getDefaultValueList().isEmpty())) { |
496 | |
|
497 | 0 | GWT.log("Key = " + metaParam.getKey() + " has write access NEVER but has no default value!", null); |
498 | 0 | continue; |
499 | |
} |
500 | 0 | SearchParam param = new SearchParam(); |
501 | 0 | param.setKey(metaParam.getKey()); |
502 | 0 | if(metaParam.getDefaultValueList()==null){ |
503 | 0 | param.setValue(metaParam.getDefaultValueString()); |
504 | |
}else{ |
505 | 0 | param.setValue(metaParam.getDefaultValueList()); |
506 | |
} |
507 | 0 | params.add(param); |
508 | 0 | } |
509 | 0 | else if(metaParam.getWriteAccess() == WriteAccess.WHEN_NULL){ |
510 | 0 | if((metaParam.getDefaultValueString() != null && !metaParam.getDefaultValueString().isEmpty())|| |
511 | |
(metaParam.getDefaultValueList() != null && !metaParam.getDefaultValueList().isEmpty())){ |
512 | 0 | SearchParam param = new SearchParam(); |
513 | 0 | param.setKey(metaParam.getKey()); |
514 | 0 | if(metaParam.getDefaultValueList()==null){ |
515 | 0 | param.setValue(metaParam.getDefaultValueString()); |
516 | |
}else{ |
517 | 0 | param.setValue(metaParam.getDefaultValueList()); |
518 | |
} |
519 | 0 | params.add(param); |
520 | 0 | } |
521 | |
} |
522 | |
} |
523 | 0 | sr.setParams(params); |
524 | 0 | if (meta.getResultSortKey() != null) { |
525 | 0 | sr.setSortColumn(meta.getResultSortKey()); |
526 | |
} |
527 | 0 | sr.setSearchKey(meta.getSearchTypeId()); |
528 | 0 | return sr; |
529 | |
} |
530 | |
|
531 | |
@Override |
532 | |
public List<HasSearchParam> getSearchParams() { |
533 | 0 | return searchParams; |
534 | |
} |
535 | |
} |
536 | |
|
537 | |
private static class SearchField extends Composite implements HasSearchParam{ |
538 | |
|
539 | 0 | private Widget widget = null; |
540 | 0 | private LookupParamMetadata meta = null; |
541 | 0 | private VerticalFlowPanel panel = new VerticalFlowPanel(); |
542 | |
private String fieldName; |
543 | |
|
544 | |
public SearchParam getSearchParam(){ |
545 | 0 | return SearchPanel.getSearchParam(widget, meta.getKey()); |
546 | |
} |
547 | |
|
548 | 0 | public SearchField(LookupParamMetadata param){ |
549 | 0 | meta = param; |
550 | |
|
551 | 0 | fieldName = param.getName(); |
552 | 0 | widget = DefaultWidgetFactory.getInstance().getWidget(param); |
553 | 0 | if(param.getDefaultValueString() != null){ |
554 | |
|
555 | 0 | if(widget instanceof HasText){ |
556 | 0 | ((HasText) widget).setText(param.getDefaultValueString().toString()); |
557 | |
} |
558 | 0 | else if(widget instanceof HasValue){ |
559 | 0 | ((HasValue) widget).setValue(param.getDefaultValueString()); |
560 | |
} |
561 | |
} |
562 | |
|
563 | |
|
564 | 0 | if (param.getWriteAccess() == Metadata.WriteAccess.REQUIRED) { |
565 | 0 | fieldName += " *"; |
566 | |
} |
567 | |
|
568 | 0 | FieldElement fieldElement = new FieldElement(fieldName, widget); |
569 | 0 | fieldElement.getTitleWidget().addStyleName("KS-Picker-Criteria-Text"); |
570 | 0 | panel.add(fieldElement); |
571 | 0 | panel.addStyleName("clear"); |
572 | |
|
573 | 0 | this.initWidget(panel); |
574 | 0 | } |
575 | |
|
576 | |
public Widget getFieldPanel(){ |
577 | 0 | return panel; |
578 | |
} |
579 | |
|
580 | |
public String getFieldName() { |
581 | 0 | return fieldName; |
582 | |
} |
583 | |
} |
584 | |
|
585 | |
private static SearchParam getSearchParam(final Widget widget, String key){ |
586 | 0 | SearchParam param = new SearchParam(); |
587 | 0 | param.setKey(key); |
588 | 0 | if(widget instanceof HasText){ |
589 | 0 | param.setValue(((HasText) widget).getText()); |
590 | |
} |
591 | 0 | else if(widget instanceof HasValue){ |
592 | 0 | Object value = ((HasValue) widget).getValue(); |
593 | 0 | if(value != null){ |
594 | |
|
595 | 0 | if(value instanceof String){ |
596 | 0 | param.setValue((String)value); |
597 | |
} |
598 | |
else{ |
599 | 0 | param.setValue(value.toString()); |
600 | 0 | GWT.log("Fields in search probably(?) shouldnt have values other than string", null); |
601 | |
} |
602 | |
} |
603 | 0 | } |
604 | 0 | else if (widget instanceof KSPicker){ |
605 | 0 | param.setValue(((KSPicker)widget).getDisplayValue()); |
606 | |
} |
607 | |
else { |
608 | 0 | param.setValue(""); |
609 | |
} |
610 | |
|
611 | 0 | return param; |
612 | |
} |
613 | |
|
614 | |
private void showCriteriaChosen(List<HasSearchParam> fields){ |
615 | 0 | enteredCriteriaString.clear(); |
616 | 0 | boolean first = true;; |
617 | 0 | for(HasSearchParam field: fields){ |
618 | 0 | String name = field.getFieldName(); |
619 | |
|
620 | 0 | String value = field.getSearchParam().getValue().toString(); |
621 | 0 | if(!value.isEmpty()){ |
622 | 0 | HTMLPanel label = new HTMLPanel(name + ": <b>" + value + "</b> "); |
623 | 0 | if (!first) { |
624 | 0 | label.addStyleName("KS-Advanced-Search-Search-Criteria-Text"); |
625 | |
} |
626 | 0 | enteredCriteriaString.add(label); |
627 | 0 | first = false; |
628 | |
} |
629 | 0 | } |
630 | 0 | } |
631 | |
|
632 | |
public List<String> getSelectedIds(){ |
633 | 0 | List<String> ids = new ArrayList<String>(); |
634 | 0 | if(table != null){ |
635 | 0 | ids = table.getSelectedIds(); |
636 | |
} |
637 | 0 | return ids; |
638 | |
} |
639 | |
|
640 | |
public List<SelectedResults> getSelectedValues() { |
641 | |
|
642 | 0 | List<SelectedResults> selectedValues = new ArrayList<SelectedResults>(); |
643 | 0 | if (table != null) { |
644 | 0 | List<ResultRow> selectedRows = table.getSelectedRows(); |
645 | 0 | for (ResultRow row : selectedRows) { |
646 | 0 | String displayKey = row.getValue(activeSearchParametersWidget.getLookupMetadata().getResultDisplayKey()); |
647 | 0 | String returnKey = row.getValue(activeSearchParametersWidget.getLookupMetadata().getResultReturnKey()); |
648 | 0 | selectedValues.add(new SelectedResults(displayKey, returnKey, row)); |
649 | 0 | if (multiSelect == false) { |
650 | 0 | break; |
651 | |
} |
652 | 0 | } |
653 | |
} |
654 | |
|
655 | 0 | return selectedValues; |
656 | |
} |
657 | |
|
658 | |
public boolean isMultiSelect() { |
659 | 0 | return multiSelect; |
660 | |
} |
661 | |
|
662 | |
public void setMultiSelect(boolean multiSelect) { |
663 | 0 | this.multiSelect = multiSelect; |
664 | 0 | } |
665 | |
|
666 | |
private static class ParamListItems implements ListItems{ |
667 | |
|
668 | 0 | private List<LookupParamMetadata> params = new ArrayList<LookupParamMetadata>(); |
669 | |
|
670 | 0 | public ParamListItems(LookupMetadata meta){ |
671 | 0 | params = meta.getParams(); |
672 | 0 | } |
673 | |
|
674 | 0 | public ParamListItems(List<LookupParamMetadata> params){ |
675 | 0 | this.params = params; |
676 | 0 | } |
677 | |
|
678 | |
@Override |
679 | |
public List<String> getAttrKeys() { |
680 | 0 | return new ArrayList<String>(); |
681 | |
} |
682 | |
|
683 | |
@Override |
684 | |
public String getItemAttribute(String id, String attrkey) { |
685 | 0 | return ""; |
686 | |
} |
687 | |
|
688 | |
@Override |
689 | |
public int getItemCount() { |
690 | 0 | return params.size(); |
691 | |
} |
692 | |
|
693 | |
@Override |
694 | |
public List<String> getItemIds() { |
695 | 0 | List<String> ids = new ArrayList<String>(); |
696 | 0 | for(LookupParamMetadata param: params){ |
697 | 0 | ids.add(param.getKey()); |
698 | |
} |
699 | 0 | return ids; |
700 | |
} |
701 | |
|
702 | |
@Override |
703 | |
public String getItemText(String id) { |
704 | 0 | String itemText = id; |
705 | 0 | for(LookupParamMetadata param: params){ |
706 | 0 | if(param.getKey().equals(id)){ |
707 | |
|
708 | 0 | itemText = param.getName(); |
709 | 0 | break; |
710 | |
} |
711 | |
} |
712 | 0 | return itemText; |
713 | |
} |
714 | |
|
715 | |
public Widget getWidget(String id){ |
716 | 0 | Widget w = null; |
717 | 0 | for(LookupParamMetadata param: params){ |
718 | 0 | if(param.getKey().equals(id)){ |
719 | 0 | w = DefaultWidgetFactory.getInstance().getWidget(param); |
720 | 0 | break; |
721 | |
} |
722 | |
} |
723 | 0 | return w; |
724 | |
} |
725 | |
|
726 | |
public List<LookupParamMetadata> getParams() { |
727 | 0 | return params; |
728 | |
} |
729 | |
} |
730 | |
|
731 | |
private String getMessage(final String msgKey) { |
732 | 0 | return Application.getApplicationContext().getMessage(msgKey); |
733 | |
} |
734 | |
|
735 | |
public void addLookupChangedCallback(Callback<LookupMetadata> callback) { |
736 | 0 | lookupChangedCallbacks.add(callback); |
737 | 0 | } |
738 | |
|
739 | |
public Callback<Boolean> getActionCompleteCallback() { |
740 | 0 | return new Callback<Boolean>() { |
741 | |
|
742 | |
@Override |
743 | |
public void exec(Boolean result) { |
744 | |
|
745 | 0 | if (resultsSelected == true) { |
746 | 0 | List<SelectedResults> selectedItems = getSelectedValues(); |
747 | 0 | for(Callback<List<SelectedResults>> callback: selectedCompleteCallbacks){ |
748 | 0 | callback.exec(selectedItems); |
749 | |
} |
750 | 0 | return; |
751 | |
} |
752 | |
|
753 | 0 | actionCancelButtons.setButtonText(ButtonEnumerations.SearchCancelEnum.SEARCH, getMessage("select")); |
754 | 0 | resultsSelected = true; |
755 | |
|
756 | 0 | SearchRequest sr = getSearchRequest(); |
757 | 0 | table.performSearch(sr, activeSearchParametersWidget.getLookupMetadata().getResults(), activeSearchParametersWidget.getLookupMetadata().getResultReturnKey()); |
758 | 0 | resultsTablePanel.setVisible(true); |
759 | 0 | List<HasSearchParam> userCriteria = new ArrayList<HasSearchParam>(); |
760 | 0 | List<HasSearchParam> searchParams = activeSearchParametersWidget.getSearchParams(); |
761 | |
|
762 | |
|
763 | 0 | for(HasSearchParam field: searchParams){ |
764 | 0 | SearchParam param = field.getSearchParam(); |
765 | |
|
766 | |
|
767 | |
|
768 | |
|
769 | 0 | if ((param.getValue() != null) && ((param.getValue().toString().trim().isEmpty() == false) || (param.getKey().toLowerCase().indexOf("optional") == -1))) { |
770 | 0 | userCriteria.add(field); |
771 | |
} |
772 | 0 | } |
773 | 0 | showCriteriaChosen(userCriteria); |
774 | |
|
775 | 0 | if(!resultsShown){ |
776 | 0 | searchSelectorPanel.removeFromParent(); |
777 | 0 | modifySearchPanel = new CollapsablePanel(getMessage("searchPanelModifySearch"), searchSelectorPanel, false); |
778 | 0 | modifySearchPanel.getLabel().addClickHandler(new ClickHandler(){ |
779 | |
@Override |
780 | |
public void onClick(ClickEvent event) { |
781 | 0 | resultsTablePanel.setVisible(false); |
782 | 0 | actionCancelButtons.setButtonText(ButtonEnumerations.SearchCancelEnum.SEARCH, getActionLabel()); |
783 | 0 | resultsSelected = false; |
784 | 0 | }}); |
785 | 0 | SearchPanel.this.layout.insert(modifySearchPanel, 0); |
786 | |
|
787 | |
} |
788 | |
else{ |
789 | 0 | modifySearchPanel.close(); |
790 | |
} |
791 | 0 | resultsShown = true; |
792 | |
|
793 | 0 | for(Callback<Boolean> callback: actionCompletedCallbacks){ |
794 | 0 | callback.exec( Boolean.valueOf(true)); |
795 | |
} |
796 | 0 | } |
797 | |
}; |
798 | |
} |
799 | |
|
800 | |
public SearchRequest getSearchRequest() { |
801 | 0 | if (activeSearchParametersWidget != null) { |
802 | 0 | return activeSearchParametersWidget.getSearchRequest(); |
803 | |
} |
804 | 0 | return null; |
805 | |
} |
806 | |
|
807 | |
public void setActionCancelButtonGroup(ActionCancelGroup actionCancelButtons) { |
808 | 0 | this.actionCancelButtons = actionCancelButtons; |
809 | 0 | } |
810 | |
|
811 | |
public String getSelectedLookupName() { |
812 | 0 | return selectedLookupName; |
813 | |
} |
814 | |
|
815 | |
public void setSelectedLookupName(String selectedLookupName) { |
816 | 0 | this.selectedLookupName = selectedLookupName; |
817 | 0 | } |
818 | |
|
819 | |
public void addSelectionCompleteCallback(Callback<List<SelectedResults>> callback){ |
820 | 0 | selectedCompleteCallbacks.add(callback); |
821 | 0 | } |
822 | |
|
823 | |
public void addActionCompleteCallback(Callback<Boolean> callback){ |
824 | 0 | actionCompletedCallbacks.add(callback); |
825 | 0 | } |
826 | |
|
827 | |
public String getActionLabel() { |
828 | 0 | return actionLabel; |
829 | |
} |
830 | |
|
831 | |
public void setActionLabel(String actionLabel) { |
832 | 0 | if ((actionLabel != null) && (actionLabel.trim().length() > 0)) { |
833 | 0 | this.actionLabel = actionLabel; |
834 | |
} |
835 | 0 | } |
836 | |
|
837 | |
|
838 | |
} |