Coverage Report - org.kuali.student.common.ui.client.widgets.search.SearchPanel
 
Classes in this File Line Coverage Branch Coverage Complexity
SearchPanel
0%
0/153
0%
0/44
2.452
SearchPanel$1
0%
0/4
0%
0/2
2.452
SearchPanel$2
0%
0/7
0%
0/4
2.452
SearchPanel$3
0%
0/4
N/A
2.452
SearchPanel$4
0%
0/4
N/A
2.452
SearchPanel$5
0%
0/29
0%
0/16
2.452
SearchPanel$5$1
0%
0/5
N/A
2.452
SearchPanel$AdvancedSearch
0%
0/64
0%
0/58
2.452
SearchPanel$CustomLine
0%
0/29
0%
0/8
2.452
SearchPanel$CustomLine$1
0%
0/6
N/A
2.452
SearchPanel$CustomizedSearch
0%
0/53
0%
0/26
2.452
SearchPanel$CustomizedSearch$1
0%
0/7
N/A
2.452
SearchPanel$HasSearchParam
N/A
N/A
2.452
SearchPanel$ParamListItems
0%
0/27
0%
0/10
2.452
SearchPanel$SearchField
0%
0/23
0%
0/8
2.452
SearchPanel$SearchParametersWidget
N/A
N/A
2.452
SearchPanel$SearchStyle
0%
0/1
N/A
2.452
 
 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.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  
     //Layout configuration
 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");  //set default action label
 79  0
     private boolean resultsSelected = false;
 80  
     
 81  
     //Search data
 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  
     // uses "name" of the lookup metadata to lookup the widget that layouts the search UI
 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  
    //     setupSearch();
 104  0
         this.initWidget(layout);
 105  0
     }
 106  
 
 107  0
     public SearchPanel(List<LookupMetadata> metas){
 108  0
         lookups = metas;       
 109  
    //     setupSearch();
 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
                         getActionCompleteCallback().exec(true);                                 
 130  
                     }
 131  0
                }
 132  
            });            
 133  
         }            
 134  0
     }
 135  
     
 136  
     public void setupSearch() {
 137  
                 
 138  0
         resultsTablePanel.clear();
 139  0
         layout.clear();
 140  0
         resultsShown = false;
 141  
 
 142  
         //create search panel
 143  
         Widget searchParamPanel;        
 144  0
         if (lookups.size() == 1) {
 145  0
             searchParamPanel = createSearchParamPanel(lookups.get(0));
 146  0
             selectedLookupName = lookups.get(0).getName();
 147  0
             activeSearchParametersWidget = searchParameterWidgetMap.get(selectedLookupName);
 148  
         } else {
 149  0
             LinkedHashMap<String, Widget> searches = new LinkedHashMap<String, Widget>();
 150  0
             LinkedHashMap<String, LookupMetadata> searchLookups = new LinkedHashMap<String, LookupMetadata>();
 151  0
             for(LookupMetadata lookup: lookups){
 152  0
                 searches.put(lookup.getName(), createSearchParamPanel(lookup));
 153  0
                 searchLookups.put(lookup.getName(), lookup);
 154  
             }
 155  0
             selectedLookupName = lookups.get(0).getName();
 156  
             // Sets the activeSearchParametersWidget to be the first search
 157  0
             activeSearchParametersWidget = searchParameterWidgetMap.get(selectedLookupName);
 158  0
             String actionLabel = (lookups.get(0) == null)? null : lookups.get(0)
 159  
                     .getWidgetOptionValue(LookupMetadata.WidgetOption.ADVANCED_LIGHTBOX_ACTION_LABEL);
 160  0
             setActionLabel(actionLabel);
 161  0
             searchParamPanel = new SwappablePanel(searches);
 162  0
             ((SwappablePanel)searchParamPanel).setSearchLookups(searchLookups);
 163  0
             ((SwappablePanel)searchParamPanel).addLookupChangedCallback(new Callback<LookupMetadata>() {
 164  
                 @Override
 165  
                 public void exec(LookupMetadata selectedLookup) {
 166  0
                     activeSearchParametersWidget = searchParameterWidgetMap.get(selectedLookup.getName());
 167  0
                     selectedLookupName = selectedLookup.getName();
 168  0
                     if (lookupChangedCallbacks != null) {
 169  0
                         for (Callback<LookupMetadata> callback : lookupChangedCallbacks) {
 170  0
                             callback.exec(selectedLookup);
 171  
                         }
 172  
                     }
 173  0
                 }
 174  
             });
 175  
         }
 176  0
         searchSelectorPanel.setWidget(searchParamPanel);
 177  0
         layout.add(searchSelectorPanel);
 178  
         
 179  
         //create layout for results screen
 180  0
         enteredCriteriaHeading.addStyleName("ks-form-module-single-line-margin");
 181  0
         enteredCriteriaHeading.addStyleName("KS-Advanced-Search-Search-Criteria-Label");
 182  0
         resultsTablePanel.add(enteredCriteriaHeading);
 183  0
         resultsTablePanel.add(enteredCriteriaString);
 184  0
         resultsTablePanel.setVisible(false);        
 185  0
         table = new SearchResultsTable();
 186  0
         table.setMutipleSelect(isMultiSelect);
 187  0
         table.addStyleName("KS-Advanced-Search-Results-Table");
 188  0
         resultsTablePanel.add(table);
 189  0
         layout.add(resultsTablePanel); 
 190  
         
 191  0
         resultsSelected = false;
 192  0
         actionCancelButtons.setButtonText(ButtonEnumerations.SearchCancelEnum.SEARCH, getActionLabel());
 193  0
     }
 194  
 
 195  
     private Widget createSearchParamPanel(LookupMetadata meta){
 196  0
         ParamListItems listItems = new ParamListItems(meta);
 197  0
         final AdvancedSearch advancedSearch = new AdvancedSearch(meta);
 198  0
         LinkPanel panel = new LinkPanel(SearchStyle.ADVANCED, advancedSearch);
 199  0
         searchParameterWidgetMap.put(meta.getName(), advancedSearch);
 200  
 
 201  
         //check whether we need custom tab i.e. whether we have at least one parameter that should appear on custom tab
 202  0
         for(LookupParamMetadata metaParam: meta.getParams()){
 203  0
             if ((metaParam.getUsage() == Usage.CUSTOM) || (metaParam.getUsage() == Usage.ADVANCED_CUSTOM)) {
 204  0
                 final CustomizedSearch customizedSearch = new CustomizedSearch(meta, listItems);
 205  0
                 KSButton button = panel.addLinkToPanel(SearchStyle.ADVANCED, getMessage("searchPanelCustomizeSearch"), SearchStyle.CUSTOM);
 206  0
                 button.addClickHandler(new ClickHandler(){
 207  
 
 208  
                     @Override
 209  
                     public void onClick(ClickEvent event) {
 210  0
                         resultsTablePanel.setVisible(false);
 211  0
                         activeSearchParametersWidget = customizedSearch;
 212  0
                     }});
 213  0
                 button.addStyleName("KS-Advanced-Search-Link");
 214  0
                 button.getParent().addStyleName("clearfix");
 215  0
                 panel.addPanel(SearchStyle.CUSTOM, customizedSearch);
 216  0
                 button = panel.addLinkToPanel(SearchStyle.CUSTOM, getMessage("searchPanelReturnToAdvancedSearch"), SearchStyle.ADVANCED);
 217  0
                 button.addClickHandler(new ClickHandler(){
 218  
 
 219  
                     @Override
 220  
                     public void onClick(ClickEvent event) {
 221  0
                         resultsTablePanel.setVisible(false);
 222  0
                         activeSearchParametersWidget = advancedSearch;
 223  0
                     }});
 224  0
                 button.addStyleName("KS-Advanced-Search-Link");
 225  0
                 button.getParent().addStyleName("clearfix");
 226  0
                 break;
 227  
             }
 228  
         }
 229  
 
 230  0
         return panel;
 231  
     }
 232  
 
 233  0
     private class CustomizedSearch extends Composite implements SearchParametersWidget {
 234  
 
 235  0
         private List<CustomLine> lines = new ArrayList<CustomLine>();
 236  0
         private List<HasSearchParam> searchParams = new ArrayList<HasSearchParam>();
 237  0
         private VerticalPanel layout = new VerticalPanel();
 238  0
         private VerticalPanel linePanel = new VerticalPanel();
 239  
         private LookupMetadata meta;
 240  
 
 241  0
         public CustomizedSearch(final LookupMetadata meta, final ParamListItems listItems){
 242  
 
 243  0
             KSLabel instrLabel = new KSLabel(criteriaInstructions);
 244  0
             layout.add(instrLabel);
 245  
 
 246  0
             layout.add(linePanel);
 247  0
             CustomLine line = new CustomLine(meta, listItems);
 248  0
             line.addStyleName("ks-form-module-single-line-margin");
 249  0
             linePanel.add(line);
 250  0
             lines.add(line);
 251  0
             searchParams.add(line);
 252  0
             this.meta = meta;
 253  
 
 254  0
             KSButton addCriteria = new KSButton(getMessage("searchPanelAddCriteria"), ButtonStyle.SECONDARY);
 255  0
             addCriteria.addClickHandler(new ClickHandler(){
 256  
 
 257  
                 @Override
 258  
                 public void onClick(ClickEvent event) {
 259  0
                     CustomLine line = new CustomLine(meta, listItems);
 260  0
                     line.addStyleName("ks-form-module-single-line-margin");
 261  0
                     linePanel.add(line);
 262  0
                     lines.add(line);
 263  0
                     searchParams.add(line);
 264  0
                 }
 265  
             });
 266  
 
 267  0
             addCriteria.addStyleName("ks-form-module-single-line-margin");
 268  0
             layout.add(addCriteria);    
 269  
             
 270  0
             this.initWidget(layout);
 271  0
         }
 272  
         
 273  
         public LookupMetadata getLookupMetadata() {
 274  0
             return meta;
 275  
         }
 276  
 
 277  
         @Override
 278  
         public SearchRequest getSearchRequest() {
 279  
             //Create search request and then pass it to the table
 280  
             //TODO pass search to the table
 281  0
             SearchRequest sr = new SearchRequest();
 282  0
             List<SearchParam> params = new ArrayList<SearchParam>();
 283  0
             for(CustomLine field: lines){
 284  0
                 SearchParam param = field.getSearchParam();
 285  
                 //TODO is this check needed here? probably. assuming string here
 286  0
                 if((param.getValue() != null)){
 287  0
                     params.add(param);
 288  
                 }
 289  0
             }
 290  
 
 291  
             //add search criteria widgets to the custom tab
 292  0
             for(LookupParamMetadata metaParam: meta.getParams()){
 293  
 
 294  
                 //select only parameters shown on custom search tab
 295  0
                 if ((metaParam.getUsage() != Usage.CUSTOM) && (metaParam.getUsage() != Usage.ADVANCED_CUSTOM)) {
 296  0
                     continue;
 297  
                 }
 298  
 
 299  0
                 if(metaParam.getWriteAccess() == WriteAccess.NEVER){
 300  0
                     SearchParam param = new SearchParam();
 301  0
                     param.setKey(metaParam.getKey());
 302  0
                     if(metaParam.getDefaultValueList()==null){
 303  0
                         param.setValue(metaParam.getDefaultValueString());
 304  
                     }else{
 305  0
                         param.setValue(metaParam.getDefaultValueList());
 306  
                     }
 307  0
                     params.add(param);
 308  0
                 }
 309  0
                 else if(metaParam.getWriteAccess() == WriteAccess.WHEN_NULL){
 310  0
                     if((metaParam.getDefaultValueString() != null && !metaParam.getDefaultValueString().isEmpty())||
 311  
                        (metaParam.getDefaultValueList() != null && !metaParam.getDefaultValueList().isEmpty())){
 312  0
                         SearchParam param = new SearchParam();
 313  0
                         param.setKey(metaParam.getKey());
 314  0
                         if(metaParam.getDefaultValueList()==null){
 315  0
                             param.setValue(metaParam.getDefaultValueString());
 316  
                         }else{
 317  0
                             param.setValue(metaParam.getDefaultValueList());
 318  
                         }
 319  0
                         params.add(param);
 320  0
                     }
 321  
                 }
 322  
             }
 323  
 
 324  0
             sr.setParams(params);
 325  0
             sr.setSearchKey(meta.getSearchTypeId());
 326  0
             return sr;
 327  
         }
 328  
 
 329  
         @Override
 330  
         public List<HasSearchParam> getSearchParams() {
 331  0
             return searchParams;
 332  
         }
 333  
 
 334  
     }
 335  
 
 336  
     private interface HasSearchParam{
 337  
         public SearchParam getSearchParam();
 338  
         public String getFieldName();
 339  
     }
 340  
 
 341  0
     private static class CustomLine extends Composite implements HasSearchParam{
 342  0
         private KSDropDown paramSelector = new KSDropDown();
 343  0
         private SimplePanel widgetPanel = new SimplePanel();
 344  0
         private Widget widget = null;
 345  
         private String key;
 346  0
         private HorizontalBlockFlowPanel layout = new HorizontalBlockFlowPanel();
 347  
         private ParamListItems listItems;
 348  
 
 349  0
         public CustomLine(LookupMetadata meta, final ParamListItems listItems){
 350  
             
 351  0
             List<LookupParamMetadata> customParams = new ArrayList<LookupParamMetadata>();
 352  
 
 353  0
             for (LookupParamMetadata lookupParamMetadata : listItems.getParams()) {
 354  0
                 if (lookupParamMetadata.getWriteAccess() != WriteAccess.NEVER){
 355  0
                     if (lookupParamMetadata.getUsage() == Usage.CUSTOM || lookupParamMetadata.getUsage() == Usage.ADVANCED_CUSTOM ) {
 356  0
                         customParams.add(lookupParamMetadata);
 357  
                     }                   
 358  
                 } 
 359  
             }
 360  
             
 361  0
             ParamListItems customParamList = new ParamListItems(customParams);
 362  
             
 363  0
             this.listItems = customParamList;
 364  0
             paramSelector.setBlankFirstItem(false);
 365  0
             paramSelector.setListItems(customParamList);
 366  
 
 367  0
             String id = meta.getParams().get(0).getKey();
 368  0
             paramSelector.selectItem(id);
 369  0
             widget = listItems.getWidget(id);
 370  0
             key = id;
 371  0
             widgetPanel.setWidget(widget);
 372  0
             paramSelector.addSelectionChangeHandler(new SelectionChangeHandler(){
 373  
 
 374  
                 @Override
 375  
                 public void onSelectionChange(SelectionChangeEvent event) {
 376  0
                     String id = ((KSSelectItemWidgetAbstract)event.getWidget()).getSelectedItem();
 377  0
                     widget = listItems.getWidget(id);
 378  0
                     widgetPanel.setWidget(widget);
 379  0
                     key = id;
 380  
 
 381  0
                 }
 382  
             });
 383  0
             layout.add(paramSelector);
 384  0
             layout.add(widgetPanel);
 385  0
             this.initWidget(layout);
 386  0
         }
 387  
 
 388  
         public SearchParam getSearchParam(){
 389  0
             return SearchPanel.getSearchParam(widget, key);
 390  
         }
 391  
 
 392  
         public String getKey(){
 393  0
             return key;
 394  
         }
 395  
 
 396  
         public String getFieldName(){
 397  0
             String id = paramSelector.getSelectedItem();
 398  0
             return listItems.getItemText(id);
 399  
         }
 400  
     }
 401  
 
 402  
     private class AdvancedSearch extends Composite implements SearchParametersWidget {
 403  
         private LookupMetadata meta;
 404  0
         private List<HasSearchParam> searchParams = new ArrayList<HasSearchParam>();
 405  
 
 406  0
         public AdvancedSearch(final LookupMetadata meta){
 407  0
             VerticalPanel panel = new VerticalPanel();
 408  
 
 409  0
             KSLabel instrLabel = new KSLabel();
 410  0
             panel.add(instrLabel);
 411  0
             this.meta = meta;            
 412  
 
 413  
             //add widget for each search criteria to the advanced tab
 414  0
             boolean allFieldsRequired = true;
 415  0
             for(LookupParamMetadata param: meta.getParams()){
 416  
 
 417  
                 //select only parameters shown on advanced search tab
 418  0
                 if ((param.getUsage() != Usage.ADVANCED) && (param.getUsage() != Usage.ADVANCED_CUSTOM)) {
 419  0
                     continue;
 420  
                 }
 421  
 
 422  0
                 if ((param.getWriteAccess() == WriteAccess.ALWAYS) || (param.getWriteAccess() == WriteAccess.REQUIRED)){
 423  0
                     SearchField paramField = new SearchField(param);
 424  0
                     searchFields.add(paramField);
 425  0
                     panel.add(paramField);
 426  0
                     searchParams.add(paramField);
 427  0
                 }
 428  0
                 else if (param.getWriteAccess() == WriteAccess.WHEN_NULL){
 429  0
                     if(param.getDefaultValueString() == null && param.getDefaultValueList() == null){
 430  0
                         SearchField paramField = new SearchField(param);
 431  0
                         searchFields.add(paramField);
 432  0
                         panel.add(paramField);
 433  0
                         searchParams.add(paramField);
 434  
                     }
 435  
                 }
 436  
 
 437  0
                 if (param.getWriteAccess() != Metadata.WriteAccess.REQUIRED) {
 438  0
                     allFieldsRequired = false;
 439  
                 }
 440  
             }
 441  
 
 442  
             //do not show criteria instructions if we have only one criteria field or in case all fields are required
 443  0
             if ((searchFields.size() > 1) || (allFieldsRequired == false)) {
 444  0
                 instrLabel.setText(criteriaInstructions);
 445  
             }    
 446  
             
 447  0
             this.initWidget(panel);
 448  0
         }
 449  
         
 450  
         public LookupMetadata getLookupMetadata() {
 451  0
             return meta;
 452  
         }        
 453  
 
 454  
         @Override
 455  
         public SearchRequest getSearchRequest() {
 456  0
             SearchRequest sr = new SearchRequest();
 457  0
             List<SearchParam> params = new ArrayList<SearchParam>();
 458  0
             List<HasSearchParam> searchParams = getSearchParams();
 459  
 
 460  
             //initialize search parameters if user entered values into search criteria fields in UI
 461  
             
 462  0
             for(HasSearchParam field: searchParams){
 463  0
                 SearchParam param = field.getSearchParam();
 464  
                 //TODO is this null check needed here? probably. assuming string here
 465  
                 //TODO make check more robust here/inserting params more robust
 466  
                 //do not pass to the search parameters that are empty
 467  
                 //FIXME hack - comparison to 'optional' - replace with check against 'optional' field and update related lookup metadata
 468  0
                 if ((param.getValue() != null) && ((param.getValue().toString().trim().isEmpty() == false) || (param.getKey().toLowerCase().indexOf("optional") == -1))) {
 469  0
                     params.add(param);
 470  
                 }
 471  0
             }
 472  
 
 473  
             //initialize search parameters that are hidden from the UI because they are set to default context specific values
 474  0
             for(LookupParamMetadata metaParam: meta.getParams()){
 475  0
                 if(metaParam.getWriteAccess() == WriteAccess.NEVER){
 476  0
                     if ((metaParam.getDefaultValueString() == null || metaParam.getDefaultValueString().isEmpty())&&
 477  
                         (metaParam.getDefaultValueList() == null || metaParam.getDefaultValueList().isEmpty())) {
 478  
                         //FIXME throw an exception?
 479  0
                         GWT.log("Key = " + metaParam.getKey() + " has write access NEVER but has no default value!", null);
 480  0
                         continue;
 481  
                     }
 482  0
                     SearchParam param = new SearchParam();
 483  0
                     param.setKey(metaParam.getKey());
 484  0
                     if(metaParam.getDefaultValueList()==null){
 485  0
                         param.setValue(metaParam.getDefaultValueString());
 486  
                     }else{
 487  0
                         param.setValue(metaParam.getDefaultValueList());
 488  
                     }
 489  0
                     params.add(param);
 490  0
                 }
 491  0
                 else if(metaParam.getWriteAccess() == WriteAccess.WHEN_NULL){
 492  0
                     if((metaParam.getDefaultValueString() != null && !metaParam.getDefaultValueString().isEmpty())||
 493  
                        (metaParam.getDefaultValueList() != null && !metaParam.getDefaultValueList().isEmpty())){
 494  0
                         SearchParam param = new SearchParam();
 495  0
                         param.setKey(metaParam.getKey());
 496  0
                         if(metaParam.getDefaultValueList()==null){
 497  0
                             param.setValue(metaParam.getDefaultValueString());
 498  
                         }else{
 499  0
                             param.setValue(metaParam.getDefaultValueList());
 500  
                         }
 501  0
                         params.add(param);
 502  0
                     }
 503  
                 }
 504  
             }
 505  0
             sr.setParams(params);
 506  0
             if (meta.getResultSortKey() != null) {
 507  0
                 sr.setSortColumn(meta.getResultSortKey());
 508  
             }
 509  0
             sr.setSearchKey(meta.getSearchTypeId());
 510  0
             return sr;
 511  
         }
 512  
 
 513  
         @Override
 514  
         public List<HasSearchParam> getSearchParams() {
 515  0
             return searchParams;
 516  
         }
 517  
     }
 518  
 
 519  
     private static class SearchField extends Composite implements HasSearchParam{
 520  
 
 521  0
         private Widget widget = null;
 522  0
         private LookupParamMetadata meta = null;
 523  0
         private VerticalFlowPanel panel = new VerticalFlowPanel();
 524  
         private String fieldName;
 525  
 
 526  
         public SearchParam getSearchParam(){
 527  0
             return SearchPanel.getSearchParam(widget, meta.getKey());
 528  
         }
 529  
 
 530  0
         public SearchField(LookupParamMetadata param){
 531  0
             meta = param;
 532  
             //TODO use message call here
 533  0
             fieldName = param.getName();
 534  0
             widget = DefaultWidgetFactory.getInstance().getWidget(param);
 535  0
             if(param.getDefaultValueString() != null){
 536  
                 //TODO Add handling of default value lists here
 537  0
                 if(widget instanceof HasText){
 538  0
                     ((HasText) widget).setText(param.getDefaultValueString().toString());
 539  
                 }
 540  0
                 else if(widget instanceof HasValue){
 541  0
                     ((HasValue) widget).setValue(param.getDefaultValueString());
 542  
                 }
 543  
             }
 544  
 
 545  
             //FIXME: remove because required field '*' indication will be part of FieldElement class
 546  0
             if (param.getWriteAccess() == Metadata.WriteAccess.REQUIRED) {
 547  0
                 fieldName += " *";
 548  
             }
 549  
 
 550  0
             FieldElement fieldElement = new FieldElement(fieldName, widget);
 551  0
             fieldElement.getTitleWidget().addStyleName("KS-Picker-Criteria-Text");
 552  0
             panel.add(fieldElement);
 553  0
             panel.addStyleName("clear");
 554  
 
 555  0
             this.initWidget(panel);
 556  0
         }
 557  
 
 558  
         public Widget getFieldPanel(){
 559  0
             return panel;
 560  
         }
 561  
 
 562  
         public String getFieldName() {
 563  0
             return fieldName;
 564  
         }
 565  
     }
 566  
 
 567  
     private static SearchParam getSearchParam(final Widget widget, String key){
 568  0
         SearchParam param = new SearchParam();
 569  0
         param.setKey(key);
 570  0
         if(widget instanceof HasText){
 571  0
             param.setValue(((HasText) widget).getText());
 572  
         }
 573  0
         else if(widget instanceof HasValue){
 574  0
             Object value = ((HasValue) widget).getValue();
 575  0
             if(value != null){
 576  
             //TODO need to handle date and other types here, how they are converted for search, etc
 577  0
                 if(value instanceof String){
 578  0
                     param.setValue((String)value);
 579  
                 }
 580  
                 else{
 581  0
                     param.setValue(value.toString());
 582  0
                     GWT.log("Fields in search probably(?) shouldnt have values other than string", null);
 583  
                 }
 584  
             }
 585  0
         }
 586  0
         else if (widget instanceof KSPicker){
 587  0
             param.setValue(((KSPicker)widget).getDisplayValue());
 588  
         }
 589  
         else {
 590  0
             param.setValue("");
 591  
         }
 592  
 
 593  0
         return param;
 594  
     }
 595  
 
 596  
     private void showCriteriaChosen(List<HasSearchParam> fields){
 597  0
         enteredCriteriaString.clear();
 598  0
         boolean first = true;;
 599  0
         for(HasSearchParam field: fields){
 600  0
             String name = field.getFieldName();
 601  
             //TODO Should be string only, needs type safety
 602  0
             String value = field.getSearchParam().getValue().toString();
 603  0
             if(!value.isEmpty()){
 604  0
                 HTMLPanel label = new HTMLPanel(name + ": <b>" + value + "</b> ");
 605  0
                 if (!first) {
 606  0
                     label.addStyleName("KS-Advanced-Search-Search-Criteria-Text");
 607  
                 }
 608  0
                 enteredCriteriaString.add(label);
 609  0
                 first = false;
 610  
             }
 611  0
         }
 612  0
     }
 613  
 
 614  
     public List<String> getSelectedIds(){
 615  0
         List<String> ids = new ArrayList<String>();
 616  0
         if(table != null){
 617  0
             ids = table.getSelectedIds();
 618  
         }
 619  0
         return ids;
 620  
     }
 621  
 
 622  
     public List<SelectedResults> getSelectedValues() {
 623  
 
 624  0
         List<SelectedResults> selectedValues = new ArrayList<SelectedResults>();
 625  0
         if (table != null) {
 626  0
             List<ResultRow> selectedRows = table.getSelectedRows();
 627  0
             for (ResultRow row : selectedRows) {
 628  0
                 String displayKey = row.getValue(activeSearchParametersWidget.getLookupMetadata().getResultDisplayKey());
 629  0
                 String returnKey = row.getValue(activeSearchParametersWidget.getLookupMetadata().getResultReturnKey());
 630  0
                 selectedValues.add(new SelectedResults(displayKey, returnKey, row));
 631  0
                 if (multiSelect == false) {
 632  0
                     break;
 633  
                 }
 634  0
             }
 635  
         }
 636  
 
 637  0
         return selectedValues;
 638  
     }
 639  
 
 640  
     public boolean isMultiSelect() {
 641  0
         return multiSelect;
 642  
     }
 643  
 
 644  
     public void setMultiSelect(boolean multiSelect) {
 645  0
         this.multiSelect = multiSelect;
 646  0
     }
 647  
 
 648  
     private static class ParamListItems implements ListItems{
 649  
 
 650  0
         private List<LookupParamMetadata> params = new ArrayList<LookupParamMetadata>();
 651  
 
 652  0
         public ParamListItems(LookupMetadata meta){
 653  0
             params = meta.getParams();
 654  0
         }
 655  
 
 656  0
         public ParamListItems(List<LookupParamMetadata> params){
 657  0
             this.params = params;
 658  0
         }
 659  
         
 660  
         @Override
 661  
         public List<String> getAttrKeys() {
 662  0
             return new ArrayList<String>();
 663  
         }
 664  
 
 665  
         @Override
 666  
         public String getItemAttribute(String id, String attrkey) {
 667  0
             return "";
 668  
         }
 669  
 
 670  
         @Override
 671  
         public int getItemCount() {
 672  0
             return params.size();
 673  
         }
 674  
 
 675  
         @Override
 676  
         public List<String> getItemIds() {
 677  0
             List<String> ids = new ArrayList<String>();
 678  0
             for(LookupParamMetadata param: params){
 679  0
                 ids.add(param.getKey());
 680  
             }
 681  0
             return ids;
 682  
         }
 683  
 
 684  
         @Override
 685  
         public String getItemText(String id) {
 686  0
             String itemText = id;
 687  0
             for(LookupParamMetadata param: params){
 688  0
                 if(param.getKey().equals(id)){
 689  
                     //TODO this should be a message key
 690  0
                     itemText = param.getName();
 691  0
                     break;
 692  
                 }
 693  
             }
 694  0
             return itemText;
 695  
         }
 696  
 
 697  
         public Widget getWidget(String id){
 698  0
             Widget w = null;
 699  0
             for(LookupParamMetadata param: params){
 700  0
                 if(param.getKey().equals(id)){
 701  0
                     w = DefaultWidgetFactory.getInstance().getWidget(param);
 702  0
                     break;
 703  
                 }
 704  
             }
 705  0
             return w;
 706  
         }
 707  
 
 708  
         public List<LookupParamMetadata> getParams() {
 709  0
             return params;
 710  
         }
 711  
     }
 712  
 
 713  
     private String getMessage(final String msgKey) {
 714  0
         return Application.getApplicationContext().getMessage(msgKey);
 715  
     }
 716  
 
 717  
     public void addLookupChangedCallback(Callback<LookupMetadata> callback) {
 718  0
         lookupChangedCallbacks.add(callback);
 719  0
     }
 720  
     
 721  
     public Callback<Boolean> getActionCompleteCallback() {
 722  0
         return new Callback<Boolean>() {
 723  
             
 724  
             @Override
 725  
             public void exec(Boolean result) {                               
 726  
                 
 727  0
                 if (resultsSelected == true) {
 728  0
                     List<SelectedResults> selectedItems = getSelectedValues();
 729  0
                     for(Callback<List<SelectedResults>> callback: selectedCompleteCallbacks){
 730  0
                         callback.exec(selectedItems);
 731  
                     }  
 732  0
                     return;
 733  
                 }
 734  
                 
 735  0
                 actionCancelButtons.setButtonText(ButtonEnumerations.SearchCancelEnum.SEARCH, getMessage("select"));
 736  0
                 resultsSelected = true;
 737  
                 
 738  0
                 SearchRequest sr = getSearchRequest();
 739  0
                 table.performSearch(sr, activeSearchParametersWidget.getLookupMetadata().getResults(), activeSearchParametersWidget.getLookupMetadata().getResultReturnKey());
 740  0
                 resultsTablePanel.setVisible(true);
 741  0
                 List<HasSearchParam> userCriteria = new ArrayList<HasSearchParam>();
 742  0
                 List<HasSearchParam> searchParams = activeSearchParametersWidget.getSearchParams();
 743  
 
 744  
                 //initialize search parameters if user entered values into search criteria fields in UI
 745  0
                 for(HasSearchParam field: searchParams){
 746  0
                     SearchParam param = field.getSearchParam();
 747  
                     //TODO is this null check needed here? probably. assuming string here
 748  
                     //TODO make check more robust here/inserting params more robust
 749  
                     //do not pass to the search parameters that are empty
 750  
                     //FIXME hack - comparison to 'optional' - replace with check against 'optional' field and update related lookup metadata
 751  0
                     if ((param.getValue() != null) && ((param.getValue().toString().trim().isEmpty() == false) || (param.getKey().toLowerCase().indexOf("optional") == -1))) {
 752  0
                         userCriteria.add(field);
 753  
                     }
 754  0
                 }
 755  0
                 showCriteriaChosen(userCriteria);
 756  
 
 757  0
                 if(!resultsShown){
 758  0
                     searchSelectorPanel.removeFromParent();
 759  0
                     modifySearchPanel = new CollapsablePanel(getMessage("searchPanelModifySearch"), searchSelectorPanel, false);
 760  0
                     modifySearchPanel.getLabel().addClickHandler(new ClickHandler(){
 761  
                         @Override
 762  
                         public void onClick(ClickEvent event) {
 763  0
                             resultsTablePanel.setVisible(false);
 764  0
                             actionCancelButtons.setButtonText(ButtonEnumerations.SearchCancelEnum.SEARCH, getActionLabel());
 765  0
                             resultsSelected = false;
 766  0
                         }});
 767  0
                     SearchPanel.this.layout.insert(modifySearchPanel, 0);
 768  
                 }
 769  
                 else{
 770  0
                     modifySearchPanel.close();
 771  
                 }
 772  0
                 resultsShown = true; 
 773  
                 
 774  0
                 for(Callback<Boolean> callback: actionCompletedCallbacks){
 775  0
                     callback.exec( Boolean.valueOf(true));
 776  
                 }                
 777  0
             }
 778  
         };
 779  
     }
 780  
     
 781  
     public SearchRequest getSearchRequest() {
 782  0
         if (activeSearchParametersWidget != null) {
 783  0
             return activeSearchParametersWidget.getSearchRequest();
 784  
         }
 785  0
         return null;
 786  
     }    
 787  
     
 788  
     public void setActionCancelButtonGroup(ActionCancelGroup actionCancelButtons) {
 789  0
         this.actionCancelButtons = actionCancelButtons;
 790  0
     }
 791  
         
 792  
     public String getSelectedLookupName() {
 793  0
         return selectedLookupName;
 794  
     }
 795  
 
 796  
     public void setSelectedLookupName(String selectedLookupName) {
 797  0
         this.selectedLookupName = selectedLookupName;
 798  0
     }        
 799  
     
 800  
     public void addSelectionCompleteCallback(Callback<List<SelectedResults>> callback){
 801  0
         selectedCompleteCallbacks.add(callback);
 802  0
     }   
 803  
     
 804  
     public void addActionCompleteCallback(Callback<Boolean> callback){
 805  0
         actionCompletedCallbacks.add(callback);
 806  0
     }
 807  
 
 808  
     public String getActionLabel() {
 809  0
         return actionLabel;
 810  
     }
 811  
 
 812  
     public void setActionLabel(String actionLabel) {
 813  0
         if ((actionLabel != null) && (actionLabel.trim().length() > 0)) {
 814  0
             this.actionLabel = actionLabel;
 815  
         }
 816  0
     }
 817  
 
 818  
 
 819  
 }