Coverage Report - org.kuali.student.lum.lu.ui.tools.client.widgets.BrowsePanel
 
Classes in this File Line Coverage Branch Coverage Complexity
BrowsePanel
0%
0/113
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  0
         searchRequest.setSortColumn(this.lookupMetadata.getResultSortKey());
 164  0
         searchRequest.setSortDirection(this.lookupMetadata.getSortDirection());
 165  
 
 166  
                 // StringBuilder builder = new StringBuilder ();
 167  
                 // builder.append ("About to invoke search: type=");
 168  
                 // builder.append (lookupMetadata.getSearchTypeId ());
 169  
                 // builder.append (" with ");
 170  
                 // builder.append (searchParams.size ());
 171  
                 // builder.append (" parametrs.");
 172  
                 // String comma = "\n";
 173  
                 // for (SearchParam param : searchParams)
 174  
                 // {
 175  
                 // builder.append (comma);
 176  
                 // builder.append (param.getKey ());
 177  
                 // builder.append ("=");
 178  
                 // builder.append (param.getValue ());
 179  
                 // }
 180  
                 // Window.alert (builder.toString ());
 181  
 
 182  0
                 searchBackedTable.performSearch(searchRequest,
 183  
                                 lookupMetadata.getResults(),
 184  
                                 lookupMetadata.getResultReturnKey(), callback);
 185  0
                 tablePanel.setVisible(true);
 186  0
                 layout.setVisible(true);
 187  0
         }
 188  
         /*
 189  
          * Parameter is a list of column names from backed table.
 190  
          * Method checks the values of these columns names and tabulates the
 191  
          * number of occurrences of each value.
 192  
          * Returns a map of each value and it's occurrence count
 193  
          */
 194  
         public Map<String,Integer> getFilterCount()
 195  
         {
 196  0
                 Map<String,Integer> filterCount=new HashMap<String,Integer>();
 197  0
                 for(ResultRow resultRow:getAllResultRows())
 198  
                 {
 199  0
                         for(String columnName:resultRow.getColumnValues().keySet())
 200  
                         {
 201  0
                                         String columnValue=resultRow.getValue(columnName);
 202  
                                         /*
 203  
                                          * Some values are a string separated by </br> statements.
 204  
                                          * The loop removes the </br> statements so values are stored correctly.
 205  
                                          * Ex:Campus Location is "NO</br>SO"
 206  
                                          */
 207  
 
 208  0
                                         while(columnValue!=null&&columnValue.indexOf("<br/>")!=-1)
 209  
                                         {
 210  0
                                                 int f=columnValue.indexOf("<br/>");
 211  0
                                                 if(!filterCount.containsKey(columnValue.substring(0,f)))
 212  
                                                 {
 213  0
                                                         filterCount.put(columnValue.substring(0,f), 1);
 214  
                                                 }
 215  
                                                 else{
 216  0
                                                         int a=filterCount.get(columnValue.substring(0,f)).intValue();
 217  0
                                                         a++;
 218  0
                                                         filterCount.put(columnValue.substring(0,f), a);
 219  
                                                 }
 220  0
                                                 columnValue=columnValue.substring(f+5);
 221  0
                                         }
 222  
 
 223  
 
 224  0
                                         if(!filterCount.containsKey(columnValue))
 225  
                                         {
 226  0
                                                 filterCount.put(columnValue, 1);
 227  
                                         }
 228  
                                         else{
 229  0
                                                 int a=filterCount.get(columnValue).intValue();
 230  0
                                                 a++;
 231  0
                                                 filterCount.put(columnValue, a);
 232  
                                         }
 233  0
                         }
 234  
                 }
 235  0
                 return filterCount;
 236  
         }
 237  
 
 238  
         public List<String> getSelectedIds() {
 239  0
                 List<String> ids = new ArrayList<String>();
 240  0
                 if (searchBackedTable != null) {
 241  0
                         ids = searchBackedTable.getSelectedIds();
 242  
                 }
 243  0
                 return ids;
 244  
         }
 245  
 
 246  
         public List<SelectedResults> getSelectedValues() {
 247  
 
 248  0
                 List<SelectedResults> selectedValues = new ArrayList<SelectedResults>();
 249  0
                 if (searchBackedTable != null) {
 250  0
                         List<ResultRow> selectedRows = searchBackedTable.getSelectedRows();
 251  0
                         for (ResultRow row : selectedRows) {
 252  0
                                 String displayKey = row.getValue(lookupMetadata
 253  
                                                 .getResultDisplayKey());
 254  0
                                 String returnKey = row.getValue(lookupMetadata
 255  
                                                 .getResultReturnKey());
 256  0
                                 selectedValues.add(new SelectedResults(displayKey, returnKey));
 257  0
                                 if (multiSelect == false) {
 258  0
                                         break;
 259  
                                 }
 260  0
                         }
 261  
                 }
 262  
 
 263  0
                 return selectedValues;
 264  
         }
 265  
 
 266  
         public void showAllRows() {
 267  0
                 searchBackedTable.getResultRows().clear();
 268  0
                 searchBackedTable.getResultRows().addAll(
 269  
                                 searchBackedTable.getAllResults());
 270  0
                 searchBackedTable.redraw();
 271  0
         }
 272  
 
 273  
         public void showOnlyRows(HashSet<String> rowKeys) {
 274  0
                 searchBackedTable.getResultRows().clear();
 275  0
                 for (ResultRow resultRow : searchBackedTable.getAllResults()) {
 276  0
                         if (rowKeys.contains(resultRow.getId())) {
 277  0
                                 searchBackedTable.getResultRows().add(resultRow);
 278  
                         }
 279  
                 }
 280  0
                 searchBackedTable.redraw();
 281  0
         }
 282  
 
 283  
         public List<ResultRow> getAllResultRows() {
 284  0
                 return searchBackedTable.getAllResults();
 285  
         }
 286  
 
 287  
         public boolean isMultiSelect() {
 288  0
                 return multiSelect;
 289  
         }
 290  
 
 291  
         public void setMultiSelect(boolean multiSelect) {
 292  0
                 this.multiSelect = multiSelect;
 293  0
         }
 294  
 
 295  
 }