Coverage Report - org.kuali.student.common.ui.client.widgets.search.SearchPanel
 
Classes in this File Line Coverage Branch Coverage Complexity
SearchPanel
0%
0/174
0%
0/56
2.493
SearchPanel$1
0%
0/5
0%
0/2
2.493
SearchPanel$2
0%
0/7
0%
0/4
2.493
SearchPanel$3
0%
0/8
0%
0/2
2.493
SearchPanel$4
0%
0/4
N/A
2.493
SearchPanel$5
0%
0/4
N/A
2.493
SearchPanel$6
0%
0/31
0%
0/18
2.493
SearchPanel$6$1
0%
0/5
N/A
2.493
SearchPanel$AdvancedSearch
0%
0/67
0%
0/58
2.493
SearchPanel$AdvancedSearch$1
0%
0/4
0%
0/2
2.493
SearchPanel$CustomLine
0%
0/30
0%
0/8
2.493
SearchPanel$CustomLine$1
0%
0/6
N/A
2.493
SearchPanel$CustomizedSearch
0%
0/53
0%
0/26
2.493
SearchPanel$CustomizedSearch$1
0%
0/7
N/A
2.493
SearchPanel$HasSearchParam
N/A
N/A
2.493
SearchPanel$ParamListItems
0%
0/27
0%
0/10
2.493
SearchPanel$SearchField
0%
0/24
0%
0/8
2.493
SearchPanel$SearchParametersWidget
N/A
N/A
2.493
SearchPanel$SearchStyle
0%
0/1
N/A
2.493
 
 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  0
 public class SearchPanel extends Composite{
 71  
 
 72  
     //Layout configuration
 73  0
     private VerticalFlowPanel layout = new VerticalFlowPanel();
 74  0
     private SimplePanel searchSelectorPanel = new SimplePanel();
 75  0
     private VerticalFlowPanel resultsTablePanel = new VerticalFlowPanel();
 76  0
     private HorizontalBlockFlowPanel enteredCriteriaString = new HorizontalBlockFlowPanel();
 77  
     private CollapsablePanel modifySearchPanel;
 78  0
     private String criteriaInstructions = getMessage("searchPanelEnterFields");
 79  0
     private KSLabel enteredCriteriaHeading = new KSLabel(getMessage("searchPanelCriteria"));
 80  
     private SearchResultsTable table;
 81  0
     private boolean isMultiSelect = true;
 82  
 
 83  0
         public static enum SearchStyle{ADVANCED, CUSTOM}; 
 84  
     private ActionCancelGroup actionCancelButtons;
 85  
 
 86  0
     private String actionLabel = getMessage("search");  //set default action label
 87  0
     private boolean resultsSelected = false;
 88  
     
 89  
     //Search data
 90  0
     private List<LookupMetadata> lookups = new ArrayList<LookupMetadata>();
 91  0
     private boolean multiSelect = false;
 92  0
     private boolean resultsShown = false;    
 93  0
     private SearchParametersWidget activeSearchParametersWidget = null;
 94  
     // uses "name" of the lookup metadata to lookup the widget that layouts the search UI
 95  0
     private Map<String, SearchParametersWidget> searchParameterWidgetMap = new HashMap<String, SearchParametersWidget>();
 96  0
     private List<SearchField> searchFields = new ArrayList<SearchField>();
 97  0
     private List<Callback<LookupMetadata>> lookupChangedCallbacks = new ArrayList<Callback<LookupMetadata>>();    
 98  
     private String selectedLookupName;
 99  0
     private List<Callback<List<SelectedResults>>> selectedCompleteCallbacks = new ArrayList<Callback<List<SelectedResults>>>();  
 100  0
     private List<Callback<Boolean>> actionCompletedCallbacks = new ArrayList<Callback<Boolean>>();    
 101  
 
 102  0
         private Callback<ButtonEnumerations.ButtonEnum> actionCancelCallback = new Callback<ButtonEnumerations.ButtonEnum>(){
 103  
 
 104  
                 @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  0
        }
 111  
         };
 112  
    
 113  
     interface SearchParametersWidget {
 114  
         public SearchRequest getSearchRequest();
 115  
         public LookupMetadata getLookupMetadata();
 116  
         public List<HasSearchParam> getSearchParams();
 117  
     }
 118  
 
 119  0
     public SearchPanel(LookupMetadata meta){
 120  0
         lookups.add(meta);
 121  
    //     setupSearch();
 122  0
         this.initWidget(layout);
 123  0
     }
 124  
 
 125  0
     public SearchPanel(List<LookupMetadata> metas){
 126  0
         lookups = metas;       
 127  
    //     setupSearch();
 128  0
         this.initWidget(layout);
 129  0
     }
 130  
 
 131  
     @SuppressWarnings("unchecked")
 132  
         public ButtonGroup getButtons(){
 133  0
             return actionCancelButtons;
 134  
     }
 135  
     
 136  
     public void setMutipleSelect(boolean isMultiSelect){
 137  0
             this.isMultiSelect = isMultiSelect;
 138  0
     }
 139  
     
 140  
     public void setupButtons() {
 141  0
         if (actionCancelButtons != null) {
 142  0
             actionCancelButtons.setButtonText(ButtonEnumerations.SearchCancelEnum.SEARCH, getActionLabel());
 143  0
             actionCancelButtons.addCallback(actionCancelCallback);            
 144  
         }            
 145  0
     }
 146  
     
 147  
     public void setupSearch() {
 148  
                 
 149  0
         resultsTablePanel.clear();
 150  0
         layout.clear();
 151  0
         resultsShown = false;
 152  
 
 153  
         //create search panel
 154  
         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  
                 @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  0
                 }
 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  
                         @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  0
                         }
 217  
                 });
 218  
         
 219  0
         resultsSelected = false;
 220  0
         actionCancelButtons.setButtonText(ButtonEnumerations.SearchCancelEnum.SEARCH, getActionLabel());
 221  0
     }
 222  
 
 223  
     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  
                     @Override
 237  
                     public void onClick(ClickEvent event) {
 238  0
                         resultsTablePanel.setVisible(false);
 239  0
                         activeSearchParametersWidget = customizedSearch;
 240  0
                     }});
 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  
                     @Override
 248  
                     public void onClick(ClickEvent event) {
 249  0
                         resultsTablePanel.setVisible(false);
 250  0
                         activeSearchParametersWidget = advancedSearch;
 251  0
                     }});
 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  0
     private class CustomizedSearch extends Composite implements SearchParametersWidget {
 262  
 
 263  0
         private List<CustomLine> lines = new ArrayList<CustomLine>();
 264  0
         private List<HasSearchParam> searchParams = new ArrayList<HasSearchParam>();
 265  0
         private VerticalPanel layout = new VerticalPanel();
 266  0
         private VerticalPanel linePanel = new VerticalPanel();
 267  
         private LookupMetadata meta;
 268  
 
 269  0
         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  
                 @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  0
                 }
 293  
             });
 294  
 
 295  0
             addCriteria.addStyleName("ks-form-module-single-line-margin");
 296  0
             layout.add(addCriteria);    
 297  
             
 298  0
             this.initWidget(layout);
 299  0
         }
 300  
         
 301  
         public LookupMetadata getLookupMetadata() {
 302  0
             return meta;
 303  
         }
 304  
 
 305  
         @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  0
             }
 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  0
                 }
 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  0
                     }
 349  
                 }
 350  
             }
 351  
 
 352  0
             sr.setParams(params);
 353  0
             sr.setSearchKey(meta.getSearchTypeId());
 354  0
             return sr;
 355  
         }
 356  
 
 357  
         @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  0
     private static class CustomLine extends Composite implements HasSearchParam{
 371  0
         private KSDropDown paramSelector = new KSDropDown();
 372  0
         private SimplePanel widgetPanel = new SimplePanel();
 373  0
         private Widget widget = null;
 374  
         private String key;
 375  0
         private HorizontalBlockFlowPanel layout = new HorizontalBlockFlowPanel();
 376  
         private ParamListItems listItems;
 377  
 
 378  0
         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  
                 @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  0
                 }
 411  
             });
 412  0
             layout.add(paramSelector);
 413  0
             layout.add(widgetPanel);
 414  0
             this.initWidget(layout);
 415  0
         }
 416  
 
 417  
         public SearchParam getSearchParam(){
 418  0
             return SearchPanel.getSearchParam(widget, key);
 419  
         }
 420  
 
 421  
         public String getKey(){
 422  0
             return key;
 423  
         }
 424  
 
 425  
         public String getFieldName(){
 426  0
             String id = paramSelector.getSelectedItem();
 427  0
             return listItems.getItemText(id);
 428  
         }
 429  
 
 430  
         @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  0
         private List<HasSearchParam> searchParams = new ArrayList<HasSearchParam>();
 439  
 
 440  0
         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  0
                 }
 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  0
         }
 483  
         
 484  
         public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) {
 485  0
                 return addDomHandler(handler, KeyDownEvent.getType());
 486  
             }
 487  
         
 488  
         public LookupMetadata getLookupMetadata() {
 489  0
             return meta;
 490  
         }        
 491  
         
 492  0
         private KeyDownHandler downHandler = new KeyDownHandler(){
 493  
 
 494  
                     @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  0
                     }
 499  
                     
 500  
             };
 501  
 
 502  
         @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  0
             }
 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  0
                 }
 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  0
                     }
 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  
         @Override
 562  
         public List<HasSearchParam> getSearchParams() {
 563  0
             return searchParams;
 564  
         }
 565  
     }
 566  
 
 567  
     private class SearchField extends Composite implements HasSearchParam{
 568  
 
 569  0
         private Widget widget = null;
 570  0
         private LookupParamMetadata meta = null;
 571  0
         private VerticalFlowPanel panel = new VerticalFlowPanel();
 572  
         private String fieldName;            
 573  
 
 574  
         public SearchParam getSearchParam(){
 575  0
             return SearchPanel.getSearchParam(widget, meta.getKey());
 576  
         }
 577  
 
 578  0
         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  0
         }
 605  
 
 606  
         public Widget getFieldPanel(){
 607  0
             return panel;
 608  
         }
 609  
 
 610  
         public String getFieldName() {
 611  0
             return fieldName;
 612  
         }
 613  
 
 614  
         @Override
 615  
         public String getSearchText() {
 616  0
             return SearchPanel.getSearchText(widget);
 617  
         }
 618  
     }
 619  
 
 620  
     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  0
         }
 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  0
         }
 649  
         else {
 650  0
             param.setValue("");
 651  
         }
 652  
 
 653  0
         return param;
 654  
     }
 655  
     
 656  
     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  0
         }
 673  0
         else if (widget instanceof KSPicker){
 674  0
             return ((KSPicker)widget).getDisplayValue();
 675  
         }
 676  0
         return "";
 677  
     }
 678  
 
 679  
     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  0
         }
 694  0
     }
 695  
 
 696  
     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  
     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  0
             }
 717  
         }
 718  
 
 719  0
         return selectedValues;
 720  
     }
 721  
 
 722  
     public boolean isMultiSelect() {
 723  0
         return multiSelect;
 724  
     }
 725  
 
 726  
     public void setMultiSelect(boolean multiSelect) {
 727  0
         this.multiSelect = multiSelect;
 728  0
     }
 729  
 
 730  
     private static class ParamListItems implements ListItems{
 731  
 
 732  0
         private List<LookupParamMetadata> params = new ArrayList<LookupParamMetadata>();
 733  
 
 734  0
         public ParamListItems(LookupMetadata meta){
 735  0
             params = meta.getParams();
 736  0
         }
 737  
 
 738  0
         public ParamListItems(List<LookupParamMetadata> params){
 739  0
             this.params = params;
 740  0
         }
 741  
         
 742  
         @Override
 743  
         public List<String> getAttrKeys() {
 744  0
             return new ArrayList<String>();
 745  
         }
 746  
 
 747  
         @Override
 748  
         public String getItemAttribute(String id, String attrkey) {
 749  0
             return "";
 750  
         }
 751  
 
 752  
         @Override
 753  
         public int getItemCount() {
 754  0
             return params.size();
 755  
         }
 756  
 
 757  
         @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  
         @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  
         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  
         public List<LookupParamMetadata> getParams() {
 791  0
             return params;
 792  
         }
 793  
     }
 794  
 
 795  
     private String getMessage(final String msgKey) {
 796  0
         return Application.getApplicationContext().getMessage(msgKey);
 797  
     }
 798  
 
 799  
     public void addLookupChangedCallback(Callback<LookupMetadata> callback) {
 800  0
         lookupChangedCallbacks.add(callback);
 801  0
     }
 802  
     
 803  
     public Callback<Boolean> getActionCompleteCallback() {
 804  0
         return new Callback<Boolean>() {
 805  
             
 806  
             @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  0
                 }
 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  
                         @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  0
                         }});
 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  0
             }
 866  
         };
 867  
     }
 868  
     
 869  
     public SearchRequest getSearchRequest() {
 870  0
         if (activeSearchParametersWidget != null) {
 871  0
             return activeSearchParametersWidget.getSearchRequest();
 872  
         }
 873  0
         return null;
 874  
     }    
 875  
     
 876  
     public void setActionCancelButtonGroup(ActionCancelGroup actionCancelButtons) {
 877  0
         this.actionCancelButtons = actionCancelButtons;
 878  0
     }
 879  
         
 880  
     public String getSelectedLookupName() {
 881  0
         return selectedLookupName;
 882  
     }
 883  
 
 884  
     public void setSelectedLookupName(String selectedLookupName) {
 885  0
         this.selectedLookupName = selectedLookupName;
 886  0
     }        
 887  
     
 888  
     public void addSelectionCompleteCallback(Callback<List<SelectedResults>> callback){
 889  0
         selectedCompleteCallbacks.add(callback);
 890  0
     }   
 891  
     
 892  
     public void addActionCompleteCallback(Callback<Boolean> callback){
 893  0
         actionCompletedCallbacks.add(callback);
 894  0
     }
 895  
 
 896  
     public String getActionLabel() {
 897  0
         return actionLabel;
 898  
     }
 899  
 
 900  
     public void setActionLabel(String actionLabel) {
 901  0
         if ((actionLabel != null) && (actionLabel.trim().length() > 0)) {
 902  0
             this.actionLabel = actionLabel;
 903  
         }
 904  0
     }
 905  
 
 906  
 
 907  
 }