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