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