Coverage Report - org.kuali.student.lum.lu.ui.tools.client.widgets.BrowsePanel
 
Classes in this File Line Coverage Branch Coverage Complexity
BrowsePanel
0%
0/111
0%
0/36
1.947
BrowsePanel$OnSelectedCallback
N/A
N/A
1.947
BrowsePanel$SelectButtonClickHandler
0%
0/6
N/A
1.947
 
 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.HashMap;
 20  
 import java.util.HashSet;
 21  
 import java.util.LinkedHashMap;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 import java.util.Map.Entry;
 25  
 
 26  
 import org.kuali.student.common.assembly.data.LookupMetadata;
 27  
 import org.kuali.student.common.assembly.data.LookupParamMetadata;
 28  
 import org.kuali.student.common.search.dto.SearchParam;
 29  
 import org.kuali.student.common.search.dto.SearchRequest;
 30  
 import org.kuali.student.common.ui.client.mvc.Callback;
 31  
 import org.kuali.student.common.ui.client.widgets.KSButton;
 32  
 import org.kuali.student.common.ui.client.widgets.search.SelectedResults;
 33  
 import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow;
 34  
 
 35  
 import com.google.gwt.event.dom.client.ClickEvent;
 36  
 import com.google.gwt.event.dom.client.ClickHandler;
 37  
 import com.google.gwt.gen2.table.client.SelectionGrid.SelectionPolicy;
 38  
 import com.google.gwt.user.client.ui.Composite;
 39  
 import com.google.gwt.user.client.ui.FlowPanel;
 40  
 
 41  
 public class BrowsePanel extends Composite {
 42  
         // Layout configuration
 43  
 
 44  
         private FlowPanel layout;
 45  
         private FlowPanel tablePanel;
 46  0
         private LookupMetadata lookupMetadata = null;
 47  0
         private SearchBackedTable searchBackedTable = null;
 48  0
         private boolean multiSelect = false;
 49  
         public Map<String, Object> parameters;
 50  0
         private int tableHeight = 200;;
 51  
 
 52  
         public interface OnSelectedCallback {
 53  
                 public void selected(List<String> selectedIds);
 54  
         }
 55  
 
 56  
         private OnSelectedCallback onSelectectedCallback;
 57  
 
 58  0
         public BrowsePanel(LookupMetadata lookupMetadata) {
 59  0
                 this.lookupMetadata = lookupMetadata;
 60  0
                 layout = new FlowPanel();
 61  0
                 layout.addStyleName("KS-Picker-Border");
 62  0
                 layout.addStyleName("KS-Advanced-Search-Panel");
 63  0
                 tablePanel = new FlowPanel();
 64  0
                 this.initWidget(layout);
 65  0
         }
 66  
 
 67  
         public BrowsePanel(LookupMetadata lookupMetadata, int tableHeight) {
 68  0
                 this(lookupMetadata);
 69  0
                 this.tableHeight = tableHeight;
 70  0
         }
 71  
 
 72  
         public Map<String, Object> getParameters() {
 73  0
                 if (parameters == null) {
 74  0
                         parameters = new LinkedHashMap<String, Object>();
 75  
                 }
 76  0
                 return parameters;
 77  
         }
 78  
 
 79  
         public void setParameters(Map<String, Object> parameters) {
 80  0
                 this.parameters = parameters;
 81  0
         }
 82  
 
 83  
         private Map<String, Object> getDefaultParameters() {
 84  0
                 Map<String, Object> defParms = new LinkedHashMap<String, Object>();
 85  0
                 for (LookupParamMetadata paramMeta : lookupMetadata.getParams()) {
 86  0
                         if (paramMeta.getDefaultValueString() != null) {
 87  0
                                 defParms.put(paramMeta.getKey(),
 88  
                                                 paramMeta.getDefaultValueString());
 89  0
                                 continue;
 90  
                         }
 91  0
                         if (paramMeta.getDefaultValueList() != null) {
 92  0
                                 defParms.put(paramMeta.getKey(),
 93  
                                                 paramMeta.getDefaultValueList());
 94  
                         }
 95  
                 }
 96  0
                 return defParms;
 97  
         }
 98  
 
 99  
         public OnSelectedCallback getOnSelectectedCallback() {
 100  0
                 return onSelectectedCallback;
 101  
         }
 102  
 
 103  
         public void setOnSelectectedCallback(
 104  
                         OnSelectedCallback onSelectectedCallback) {
 105  0
                 this.onSelectectedCallback = onSelectectedCallback;
 106  0
         }
 107  
 
 108  
         private class SelectButtonClickHandler implements ClickHandler {
 109  
 
 110  
                 private OnSelectedCallback callback;
 111  
                 private SearchBackedTable searchBackedTable;
 112  
 
 113  
                 public SelectButtonClickHandler(OnSelectedCallback callback,
 114  0
                                 SearchBackedTable searchBackedTable) {
 115  0
                         this.callback = callback;
 116  0
                         this.searchBackedTable = searchBackedTable;
 117  0
                 }
 118  
 
 119  
                 @Override
 120  
                 public void onClick(ClickEvent event) {
 121  0
                         this.callback.selected(this.searchBackedTable.getSelectedIds());
 122  0
                 }
 123  
 
 124  
         }
 125  
 
 126  
         @SuppressWarnings("unchecked")
 127  
         public void executeSearch(Callback<Boolean> callback) {
 128  
 
 129  0
                 tablePanel.clear();
 130  0
                 layout.clear();
 131  0
                 tablePanel.setVisible(false);
 132  0
                 layout.add(tablePanel);
 133  
                 // layout.addStyleName ("KS-Picker-Border");
 134  
                 // layout.addStyleName (Style.BROWSE.getStyle ());
 135  0
                 searchBackedTable = new SearchBackedTable(tableHeight);
 136  0
                 searchBackedTable.addStyleName("KS-Advanced-Search-Results-Table");
 137  0
                 searchBackedTable.setTableStyleName("gwt-PagingScrollTable");
 138  0
                 searchBackedTable.setSelectionPolicy(SelectionPolicy.ONE_ROW);
 139  0
                 KSButton selectButton = new KSButton("Select",
 140  
                                 new SelectButtonClickHandler(this.onSelectectedCallback,
 141  
                                                 this.searchBackedTable));
 142  0
                 tablePanel.add(searchBackedTable);
 143  0
                 tablePanel.add(selectButton);
 144  0
                 tablePanel.setVisible(false);
 145  
 
 146  0
                 SearchRequest searchRequest = new SearchRequest();
 147  0
                 List<SearchParam> searchParams = new ArrayList<SearchParam>();
 148  0
                 Map<String, Object> parms = getDefaultParameters();
 149  0
                 parms.putAll(getParameters());
 150  0
                 for (Entry<String, Object> entry : parms.entrySet()) {
 151  0
                         Object value = entry.getValue();
 152  0
                         SearchParam searchParam = new SearchParam();
 153  0
                         searchParam.setKey(entry.getKey());
 154  0
                         if (value instanceof String) {
 155  0
                                 searchParam.setValue((String) value);
 156  
                         } else {
 157  0
                                 searchParam.setValue((List<String>) value);
 158  
                         }
 159  0
                         searchParams.add(searchParam);
 160  0
                 }
 161  0
                 searchRequest.setParams(searchParams);
 162  0
                 searchRequest.setSearchKey(lookupMetadata.getSearchTypeId());
 163  
 
 164  
                 // StringBuilder builder = new StringBuilder ();
 165  
                 // builder.append ("About to invoke search: type=");
 166  
                 // builder.append (lookupMetadata.getSearchTypeId ());
 167  
                 // builder.append (" with ");
 168  
                 // builder.append (searchParams.size ());
 169  
                 // builder.append (" parametrs.");
 170  
                 // String comma = "\n";
 171  
                 // for (SearchParam param : searchParams)
 172  
                 // {
 173  
                 // builder.append (comma);
 174  
                 // builder.append (param.getKey ());
 175  
                 // builder.append ("=");
 176  
                 // builder.append (param.getValue ());
 177  
                 // }
 178  
                 // Window.alert (builder.toString ());
 179  
 
 180  0
                 searchBackedTable.performSearch(searchRequest,
 181  
                                 lookupMetadata.getResults(),
 182  
                                 lookupMetadata.getResultReturnKey(), callback);
 183  0
                 tablePanel.setVisible(true);
 184  0
                 layout.setVisible(true);
 185  0
         }
 186  
         /*
 187  
          * Parameter is a list of column names from backed table.
 188  
          * Method checks the values of these columns names and tabulates the
 189  
          * number of occurrences of each value.
 190  
          * Returns a map of each value and it's occurrence count
 191  
          */
 192  
         public Map<String,Integer> getFilterCount()
 193  
         {
 194  0
                 Map<String,Integer> filterCount=new HashMap<String,Integer>();
 195  0
                 for(ResultRow resultRow:getAllResultRows())
 196  
                 {
 197  0
                         for(String columnName:resultRow.getColumnValues().keySet())
 198  
                         {
 199  0
                                         String columnValue=resultRow.getValue(columnName);
 200  
                                         /*
 201  
                                          * Some values are a string separated by </br> statements.
 202  
                                          * The loop removes the </br> statements so values are stored correctly.
 203  
                                          * Ex:Campus Location is "NO</br>SO"
 204  
                                          */
 205  
 
 206  0
                                         while(columnValue!=null&&columnValue.indexOf("<br/>")!=-1)
 207  
                                         {
 208  0
                                                 int f=columnValue.indexOf("<br/>");
 209  0
                                                 if(!filterCount.containsKey(columnValue.substring(0,f)))
 210  
                                                 {
 211  0
                                                         filterCount.put(columnValue.substring(0,f), 1);
 212  
                                                 }
 213  
                                                 else{
 214  0
                                                         int a=filterCount.get(columnValue.substring(0,f)).intValue();
 215  0
                                                         a++;
 216  0
                                                         filterCount.put(columnValue.substring(0,f), a);
 217  
                                                 }
 218  0
                                                 columnValue=columnValue.substring(f+5);
 219  0
                                         }
 220  
 
 221  
 
 222  0
                                         if(!filterCount.containsKey(columnValue))
 223  
                                         {
 224  0
                                                 filterCount.put(columnValue, 1);
 225  
                                         }
 226  
                                         else{
 227  0
                                                 int a=filterCount.get(columnValue).intValue();
 228  0
                                                 a++;
 229  0
                                                 filterCount.put(columnValue, a);
 230  
                                         }
 231  0
                         }
 232  
                 }
 233  0
                 return filterCount;
 234  
         }
 235  
 
 236  
         public List<String> getSelectedIds() {
 237  0
                 List<String> ids = new ArrayList<String>();
 238  0
                 if (searchBackedTable != null) {
 239  0
                         ids = searchBackedTable.getSelectedIds();
 240  
                 }
 241  0
                 return ids;
 242  
         }
 243  
 
 244  
         public List<SelectedResults> getSelectedValues() {
 245  
 
 246  0
                 List<SelectedResults> selectedValues = new ArrayList<SelectedResults>();
 247  0
                 if (searchBackedTable != null) {
 248  0
                         List<ResultRow> selectedRows = searchBackedTable.getSelectedRows();
 249  0
                         for (ResultRow row : selectedRows) {
 250  0
                                 String displayKey = row.getValue(lookupMetadata
 251  
                                                 .getResultDisplayKey());
 252  0
                                 String returnKey = row.getValue(lookupMetadata
 253  
                                                 .getResultReturnKey());
 254  0
                                 selectedValues.add(new SelectedResults(displayKey, returnKey));
 255  0
                                 if (multiSelect == false) {
 256  0
                                         break;
 257  
                                 }
 258  0
                         }
 259  
                 }
 260  
 
 261  0
                 return selectedValues;
 262  
         }
 263  
 
 264  
         public void showAllRows() {
 265  0
                 searchBackedTable.getResultRows().clear();
 266  0
                 searchBackedTable.getResultRows().addAll(
 267  
                                 searchBackedTable.getAllResults());
 268  0
                 searchBackedTable.redraw();
 269  0
         }
 270  
 
 271  
         public void showOnlyRows(HashSet<String> rowKeys) {
 272  0
                 searchBackedTable.getResultRows().clear();
 273  0
                 for (ResultRow resultRow : searchBackedTable.getAllResults()) {
 274  0
                         if (rowKeys.contains(resultRow.getId())) {
 275  0
                                 searchBackedTable.getResultRows().add(resultRow);
 276  
                         }
 277  
                 }
 278  0
                 searchBackedTable.redraw();
 279  0
         }
 280  
 
 281  
         public List<ResultRow> getAllResultRows() {
 282  0
                 return searchBackedTable.getAllResults();
 283  
         }
 284  
 
 285  
         public boolean isMultiSelect() {
 286  0
                 return multiSelect;
 287  
         }
 288  
 
 289  
         public void setMultiSelect(boolean multiSelect) {
 290  0
                 this.multiSelect = multiSelect;
 291  0
         }
 292  
 
 293  
 }