Coverage Report - org.kuali.student.lum.lu.ui.tools.client.widgets.BrowsePanel
 
Classes in this File Line Coverage Branch Coverage Complexity
BrowsePanel
0%
0/89
0%
0/24
1.667
BrowsePanel$OnSelectedCallback
N/A
N/A
1.667
BrowsePanel$SelectButtonClickHandler
0%
0/6
N/A
1.667
 
 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.lum.lu.ui.tools.client.widgets;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.HashSet;
 20  
 import java.util.LinkedHashMap;
 21  
 import java.util.List;
 22  
 import java.util.Map;
 23  
 import java.util.Map.Entry;
 24  
 
 25  
 import org.kuali.student.common.assembly.data.LookupMetadata;
 26  
 import org.kuali.student.common.assembly.data.LookupParamMetadata;
 27  
 import org.kuali.student.common.search.dto.SearchParam;
 28  
 import org.kuali.student.common.search.dto.SearchRequest;
 29  
 import org.kuali.student.common.ui.client.mvc.Callback;
 30  
 import org.kuali.student.common.ui.client.widgets.KSButton;
 31  
 import org.kuali.student.common.ui.client.widgets.search.SelectedResults;
 32  
 import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow;
 33  
 
 34  
 import com.google.gwt.event.dom.client.ClickEvent;
 35  
 import com.google.gwt.event.dom.client.ClickHandler;
 36  
 import com.google.gwt.user.client.ui.Composite;
 37  
 import com.google.gwt.user.client.ui.FlowPanel;
 38  
 
 39  
 public class BrowsePanel extends Composite {
 40  
         // Layout configuration
 41  
 
 42  
         private FlowPanel layout;
 43  
         private FlowPanel tablePanel;
 44  0
         private LookupMetadata lookupMetadata = null;
 45  0
         private SearchBackedTable searchBackedTable = null;
 46  0
         private boolean multiSelect = false;
 47  
         public Map<String, Object> parameters;
 48  0
         private int tableHeight = 200;;
 49  
 
 50  
         public interface OnSelectedCallback {
 51  
                 public void selected(List<String> selectedIds);
 52  
         }
 53  
 
 54  
         private OnSelectedCallback onSelectectedCallback;
 55  
 
 56  0
         public BrowsePanel(LookupMetadata lookupMetadata) {
 57  0
                 this.lookupMetadata = lookupMetadata;
 58  0
                 layout = new FlowPanel();
 59  0
                 layout.addStyleName("KS-Picker-Border");
 60  0
                 layout.addStyleName("KS-Advanced-Search-Panel");
 61  0
                 tablePanel = new FlowPanel();
 62  0
                 this.initWidget(layout);
 63  0
         }
 64  
 
 65  
         public BrowsePanel(LookupMetadata lookupMetadata, int tableHeight) {
 66  0
                 this(lookupMetadata);
 67  0
                 this.tableHeight = tableHeight;
 68  0
         }
 69  
 
 70  
         public Map<String, Object> getParameters() {
 71  0
                 if (parameters == null) {
 72  0
                         parameters = new LinkedHashMap<String, Object>();
 73  
                 }
 74  0
                 return parameters;
 75  
         }
 76  
 
 77  
         public void setParameters(Map<String, Object> parameters) {
 78  0
                 this.parameters = parameters;
 79  0
         }
 80  
 
 81  
         private Map<String, Object> getDefaultParameters() {
 82  0
                 Map<String, Object> defParms = new LinkedHashMap<String, Object>();
 83  0
                 for (LookupParamMetadata paramMeta : lookupMetadata.getParams()) {
 84  0
                         if (paramMeta.getDefaultValueString() != null) {
 85  0
                                 defParms.put(paramMeta.getKey(),
 86  
                                                 paramMeta.getDefaultValueString());
 87  0
                                 continue;
 88  
                         }
 89  0
                         if (paramMeta.getDefaultValueList() != null) {
 90  0
                                 defParms.put(paramMeta.getKey(),
 91  
                                                 paramMeta.getDefaultValueList());
 92  
                         }
 93  
                 }
 94  0
                 return defParms;
 95  
         }
 96  
 
 97  
         public OnSelectedCallback getOnSelectectedCallback() {
 98  0
                 return onSelectectedCallback;
 99  
         }
 100  
 
 101  
         public void setOnSelectectedCallback(
 102  
                         OnSelectedCallback onSelectectedCallback) {
 103  0
                 this.onSelectectedCallback = onSelectectedCallback;
 104  0
         }
 105  
 
 106  
         private class SelectButtonClickHandler implements ClickHandler {
 107  
 
 108  
                 private OnSelectedCallback callback;
 109  
                 private SearchBackedTable searchBackedTable;
 110  
 
 111  
                 public SelectButtonClickHandler(OnSelectedCallback callback,
 112  0
                                 SearchBackedTable searchBackedTable) {
 113  0
                         this.callback = callback;
 114  0
                         this.searchBackedTable = searchBackedTable;
 115  0
                 }
 116  
 
 117  
                 @Override
 118  
                 public void onClick(ClickEvent event) {
 119  0
                         this.callback.selected(this.searchBackedTable.getSelectedIds());
 120  0
                 }
 121  
 
 122  
         }
 123  
 
 124  
         @SuppressWarnings("unchecked")
 125  
         public void executeSearch(Callback<Boolean> callback) {
 126  
 
 127  0
                 tablePanel.clear();
 128  0
                 layout.clear();
 129  0
                 tablePanel.setVisible(false);
 130  0
                 layout.add(tablePanel);
 131  
                 // layout.addStyleName ("KS-Picker-Border");
 132  
                 // layout.addStyleName (Style.BROWSE.getStyle ());
 133  0
                 searchBackedTable = new SearchBackedTable(tableHeight);
 134  0
                 searchBackedTable.addStyleName("KS-Advanced-Search-Results-Table");
 135  0
                 KSButton selectButton = new KSButton("Select",
 136  
                                 new SelectButtonClickHandler(this.onSelectectedCallback,
 137  
                                                 this.searchBackedTable));
 138  0
                 tablePanel.add(searchBackedTable);
 139  0
                 tablePanel.add(selectButton);
 140  0
                 tablePanel.setVisible(false);
 141  
 
 142  0
                 SearchRequest searchRequest = new SearchRequest();
 143  0
                 List<SearchParam> searchParams = new ArrayList<SearchParam>();
 144  0
                 Map<String, Object> parms = getDefaultParameters();
 145  0
                 parms.putAll(getParameters());
 146  0
                 for (Entry<String, Object> entry : parms.entrySet()) {
 147  0
                         Object value = entry.getValue();
 148  0
                         SearchParam searchParam = new SearchParam();
 149  0
                         searchParam.setKey(entry.getKey());
 150  0
                         if (value instanceof String) {
 151  0
                                 searchParam.setValue((String) value);
 152  
                         } else {
 153  0
                                 searchParam.setValue((List<String>) value);
 154  
                         }
 155  0
                         searchParams.add(searchParam);
 156  0
                 }
 157  0
                 searchRequest.setParams(searchParams);
 158  0
                 searchRequest.setSearchKey(lookupMetadata.getSearchTypeId());
 159  
 
 160  
                 // StringBuilder builder = new StringBuilder ();
 161  
                 // builder.append ("About to invoke search: type=");
 162  
                 // builder.append (lookupMetadata.getSearchTypeId ());
 163  
                 // builder.append (" with ");
 164  
                 // builder.append (searchParams.size ());
 165  
                 // builder.append (" parametrs.");
 166  
                 // String comma = "\n";
 167  
                 // for (SearchParam param : searchParams)
 168  
                 // {
 169  
                 // builder.append (comma);
 170  
                 // builder.append (param.getKey ());
 171  
                 // builder.append ("=");
 172  
                 // builder.append (param.getValue ());
 173  
                 // }
 174  
                 // Window.alert (builder.toString ());
 175  
 
 176  0
                 searchBackedTable.performSearch(searchRequest,
 177  
                                 lookupMetadata.getResults(),
 178  
                                 lookupMetadata.getResultReturnKey(), callback);
 179  0
                 tablePanel.setVisible(true);
 180  0
                 layout.setVisible(true);
 181  0
         }
 182  
 
 183  
         public List<String> getSelectedIds() {
 184  0
                 List<String> ids = new ArrayList<String>();
 185  0
                 if (searchBackedTable != null) {
 186  0
                         ids = searchBackedTable.getSelectedIds();
 187  
                 }
 188  0
                 return ids;
 189  
         }
 190  
 
 191  
         public List<SelectedResults> getSelectedValues() {
 192  
 
 193  0
                 List<SelectedResults> selectedValues = new ArrayList<SelectedResults>();
 194  0
                 if (searchBackedTable != null) {
 195  0
                         List<ResultRow> selectedRows = searchBackedTable.getSelectedRows();
 196  0
                         for (ResultRow row : selectedRows) {
 197  0
                                 String displayKey = row.getValue(lookupMetadata
 198  
                                                 .getResultDisplayKey());
 199  0
                                 String returnKey = row.getValue(lookupMetadata
 200  
                                                 .getResultReturnKey());
 201  0
                                 selectedValues.add(new SelectedResults(displayKey, returnKey));
 202  0
                                 if (multiSelect == false) {
 203  0
                                         break;
 204  
                                 }
 205  0
                         }
 206  
                 }
 207  
 
 208  0
                 return selectedValues;
 209  
         }
 210  
 
 211  
         public void showAllRows() {
 212  0
                 searchBackedTable.getResultRows().clear();
 213  0
                 searchBackedTable.getResultRows().addAll(
 214  
                                 searchBackedTable.getAllResults());
 215  0
                 searchBackedTable.redraw();
 216  0
         }
 217  
 
 218  
         public void showOnlyRows(HashSet<String> rowKeys) {
 219  0
                 searchBackedTable.getResultRows().clear();
 220  0
                 for (ResultRow resultRow : searchBackedTable.getAllResults()) {
 221  0
                         if (rowKeys.contains(resultRow.getId())) {
 222  0
                                 searchBackedTable.getResultRows().add(resultRow);
 223  
                         }
 224  
                 }
 225  0
                 searchBackedTable.redraw();
 226  0
         }
 227  
 
 228  
         public List<ResultRow> getAllResultRows() {
 229  0
                 return searchBackedTable.getAllResults();
 230  
         }
 231  
 
 232  
         public boolean isMultiSelect() {
 233  0
                 return multiSelect;
 234  
         }
 235  
 
 236  
         public void setMultiSelect(boolean multiSelect) {
 237  0
                 this.multiSelect = multiSelect;
 238  0
         }
 239  
 
 240  
 }