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