View Javadoc

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.ui.client.mvc.Callback;
27  import org.kuali.student.common.ui.client.widgets.KSButton;
28  import org.kuali.student.common.ui.client.widgets.search.SelectedResults;
29  import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow;
30  import org.kuali.student.r1.common.assembly.data.LookupMetadata;
31  import org.kuali.student.r1.common.assembly.data.LookupParamMetadata;
32  import org.kuali.student.r2.core.search.dto.SearchParamInfo;
33  import org.kuali.student.r2.core.search.dto.SearchRequestInfo;
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  	private LookupMetadata lookupMetadata = null;
47  	private SearchBackedTable searchBackedTable = null;
48  	private boolean multiSelect = false;
49  	public Map<String, Object> parameters;
50  	private int tableHeight = 200;;
51  
52  	public interface OnSelectedCallback {
53  		public void selected(List<String> selectedIds);
54  	}
55  
56  	private OnSelectedCallback onSelectectedCallback;
57  
58  	public BrowsePanel(LookupMetadata lookupMetadata) {
59  		this.lookupMetadata = lookupMetadata;
60  		layout = new FlowPanel();
61  		layout.addStyleName("KS-Picker-Border");
62  		layout.addStyleName("KS-Advanced-Search-Panel");
63  		tablePanel = new FlowPanel();
64  		this.initWidget(layout);
65  	}
66  
67  	public BrowsePanel(LookupMetadata lookupMetadata, int tableHeight) {
68  		this(lookupMetadata);
69  		this.tableHeight = tableHeight;
70  	}
71  
72  	public Map<String, Object> getParameters() {
73  		if (parameters == null) {
74  			parameters = new LinkedHashMap<String, Object>();
75  		}
76  		return parameters;
77  	}
78  
79  	public void setParameters(Map<String, Object> parameters) {
80  		this.parameters = parameters;
81  	}
82  
83  	private Map<String, Object> getDefaultParameters() {
84  		Map<String, Object> defParms = new LinkedHashMap<String, Object>();
85  		for (LookupParamMetadata paramMeta : lookupMetadata.getParams()) {
86  			if (paramMeta.getDefaultValueString() != null) {
87  				defParms.put(paramMeta.getKey(),
88  						paramMeta.getDefaultValueString());
89  				continue;
90  			}
91  			if (paramMeta.getDefaultValueList() != null) {
92  				defParms.put(paramMeta.getKey(),
93  						paramMeta.getDefaultValueList());
94  			}
95  		}
96  		return defParms;
97  	}
98  
99  	public OnSelectedCallback getOnSelectectedCallback() {
100 		return onSelectectedCallback;
101 	}
102 
103 	public void setOnSelectectedCallback(
104 			OnSelectedCallback onSelectectedCallback) {
105 		this.onSelectectedCallback = onSelectectedCallback;
106 	}
107 
108 	private class SelectButtonClickHandler implements ClickHandler {
109 
110 		private OnSelectedCallback callback;
111 		private SearchBackedTable searchBackedTable;
112 
113 		public SelectButtonClickHandler(OnSelectedCallback callback,
114 				SearchBackedTable searchBackedTable) {
115 			this.callback = callback;
116 			this.searchBackedTable = searchBackedTable;
117 		}
118 
119 		@Override
120 		public void onClick(ClickEvent event) {
121 			this.callback.selected(this.searchBackedTable.getSelectedIds());
122 		}
123 
124 	}
125 
126 	@SuppressWarnings("unchecked")
127 	public void executeSearch(Callback<Boolean> callback) {
128 
129 		tablePanel.clear();
130 		layout.clear();
131 		tablePanel.setVisible(false);
132 		layout.add(tablePanel);
133 		// layout.addStyleName ("KS-Picker-Border");
134 		// layout.addStyleName (Style.BROWSE.getStyle ());
135 		searchBackedTable = new SearchBackedTable(tableHeight);
136 		searchBackedTable.addStyleName("KS-Advanced-Search-Results-Table");
137 		searchBackedTable.setTableStyleName("gwt-PagingScrollTable");
138 		searchBackedTable.setSelectionPolicy(SelectionPolicy.ONE_ROW);
139 		KSButton selectButton = new KSButton("Select",
140 				new SelectButtonClickHandler(this.onSelectectedCallback,
141 						this.searchBackedTable));
142 		tablePanel.add(searchBackedTable);
143 		tablePanel.add(selectButton);
144 		tablePanel.setVisible(false);
145 
146 		SearchRequestInfo searchRequest = new SearchRequestInfo();
147 		List<SearchParamInfo> searchParams = new ArrayList<SearchParamInfo>();
148 		Map<String, Object> parms = getDefaultParameters();
149 		parms.putAll(getParameters());
150 		for (Entry<String, Object> entry : parms.entrySet()) {
151 			Object value = entry.getValue();
152 			SearchParamInfo searchParam = new SearchParamInfo();
153 			searchParam.setKey(entry.getKey());
154 			if (value instanceof String) {
155 				searchParam.getValues().add((String) value);
156 			} else {
157 				searchParam.setValues((List<String>) value);
158 			}
159 			searchParams.add(searchParam);
160 		}
161 		searchRequest.setParams(searchParams);
162 		searchRequest.setSearchKey(lookupMetadata.getSearchTypeId());
163         searchRequest.setSortColumn(this.lookupMetadata.getResultSortKey());
164         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 (SearchParamInfo 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 		searchBackedTable.performSearch(searchRequest,
183 				lookupMetadata.getResults(),
184 				lookupMetadata.getResultReturnKey(), callback);
185 		tablePanel.setVisible(true);
186 		layout.setVisible(true);
187 	}
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 		Map<String,Integer> filterCount=new HashMap<String,Integer>();
197 		for(ResultRow resultRow:getAllResultRows())
198 		{
199 			for(String columnName:resultRow.getColumnValues().keySet())
200 			{
201 					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 					while(columnValue!=null&&columnValue.indexOf("<br/>")!=-1)
209 					{
210 						int f=columnValue.indexOf("<br/>");
211 						if(!filterCount.containsKey(columnValue.substring(0,f)))
212 						{
213 							filterCount.put(columnValue.substring(0,f), 1);
214 						}
215 						else{
216 							int a=filterCount.get(columnValue.substring(0,f)).intValue();
217 							a++;
218 							filterCount.put(columnValue.substring(0,f), a);
219 						}
220 						columnValue=columnValue.substring(f+5);
221 					}
222 
223 
224 					if(!filterCount.containsKey(columnValue))
225 					{
226 						filterCount.put(columnValue, 1);
227 					}
228 					else{
229 						int a=filterCount.get(columnValue).intValue();
230 						a++;
231 						filterCount.put(columnValue, a);
232 					}
233 			}
234 		}
235 		return filterCount;
236 	}
237 
238 	public List<String> getSelectedIds() {
239 		List<String> ids = new ArrayList<String>();
240 		if (searchBackedTable != null) {
241 			ids = searchBackedTable.getSelectedIds();
242 		}
243 		return ids;
244 	}
245 
246 	public List<SelectedResults> getSelectedValues() {
247 
248 		List<SelectedResults> selectedValues = new ArrayList<SelectedResults>();
249 		if (searchBackedTable != null) {
250 			List<ResultRow> selectedRows = searchBackedTable.getSelectedRows();
251 			for (ResultRow row : selectedRows) {
252 				String displayKey = row.getValue(lookupMetadata
253 						.getResultDisplayKey());
254 				String returnKey = row.getValue(lookupMetadata
255 						.getResultReturnKey());
256 				selectedValues.add(new SelectedResults(displayKey, returnKey));
257 				if (multiSelect == false) {
258 					break;
259 				}
260 			}
261 		}
262 
263 		return selectedValues;
264 	}
265 
266 	public void showAllRows() {
267 		searchBackedTable.getResultRows().clear();
268 		searchBackedTable.getResultRows().addAll(
269 				searchBackedTable.getAllResults());
270 		searchBackedTable.redraw();
271 	}
272 
273 	public void showOnlyRows(HashSet<String> rowKeys) {
274 		searchBackedTable.getResultRows().clear();
275 		for (ResultRow resultRow : searchBackedTable.getAllResults()) {
276 			if (rowKeys.contains(resultRow.getId())) {
277 				searchBackedTable.getResultRows().add(resultRow);
278 			}
279 		}
280 		searchBackedTable.redraw();
281 	}
282 
283 	public List<ResultRow> getAllResultRows() {
284 		return searchBackedTable.getAllResults();
285 	}
286 
287 	public boolean isMultiSelect() {
288 		return multiSelect;
289 	}
290 
291 	public void setMultiSelect(boolean multiSelect) {
292 		this.multiSelect = multiSelect;
293 	}
294 
295 }