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