View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.gl.web.struts;
20  
21  import java.util.Enumeration;
22  import java.util.HashSet;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import javax.servlet.http.HttpServletRequest;
27  
28  import org.apache.commons.lang.StringUtils;
29  import org.kuali.kfs.sys.KFSConstants;
30  import org.kuali.rice.kns.lookup.LookupUtils;
31  import org.kuali.rice.kns.web.struts.form.KualiTableRenderFormMetadata;
32  import org.kuali.rice.kns.web.struts.form.LookupForm;
33  
34  /**
35   * This class is the action form for balance inquiry lookup results
36   * 
37   */
38  public class BalanceInquiryLookupResults extends LookupForm implements LookupResultsSelectable {
39      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BalanceInquiryLookupResults.class);
40  
41      private KualiTableRenderFormMetadata tableMetadata;
42  
43      private String lookupResultsSequenceNumber;
44  
45      /**
46       * The number of rows that match the query criteria
47       */
48      private int resultsActualSize;
49  
50      /**
51       * The number of rows that match the query criteria or the max results limit size (if applicable), whichever is less
52       */
53      private int resultsLimitedSize;
54  
55      /**
56       * when the looked results screen was rendered, the index of the column that the results were sorted on. -1 for unknown, index
57       * numbers starting at 0
58       */
59      private String previouslySortedColumnIndex;
60  
61      /**
62       * Comment for <code>columnToSortIndex</code>
63       */
64      private int columnToSortIndex;
65  
66      /**
67       * the name of the collection being looked up by the calling page. This value will be returned unmodified to the calling page
68       * (indicated by super.getBackLocation()), which should use it to determine in which collection the selected results will be
69       * returned.
70       */
71      private String lookedUpCollectionName;
72  
73      /**
74       * Those object IDs that were selected before the current page is rendered
75       */
76      private Set<String> previouslySelectedObjectIdSet;
77      /**
78       * Those object IDs that are rendered on the current page
79       */
80      private Set<String> displayedObjectIdSet;
81      /**
82       * Those object IDs that are selected/checked on the current page
83       */
84      private Set<String> selectedObjectIdSet;
85      /**
86       * The object IDs that are selected after the struts action is complete; the obj IDs in the keys of this Map will be considered
87       * checked in the UI
88       */
89      private Map<String, String> compositeObjectIdMap;
90  
91      public BalanceInquiryLookupResults() {
92          tableMetadata = new KualiTableRenderFormMetadata();
93      }
94  
95      protected String getMethodToCall(HttpServletRequest request) {
96          return request.getParameter(KFSConstants.DISPATCH_REQUEST_PARAMETER);
97      }
98  
99      /**
100      * Named appropriately as it is intended to be called from a <code>{@link LookupForm}</code>
101      * 
102      * @param request HttpServletRequest
103      */
104     public void populate(HttpServletRequest request) {
105         super.populate(request);
106 
107         if (StringUtils.isNotBlank(request.getParameter(KFSConstants.TableRenderConstants.VIEWED_PAGE_NUMBER))) {
108             setViewedPageNumber(Integer.parseInt(request.getParameter(KFSConstants.TableRenderConstants.VIEWED_PAGE_NUMBER)));
109         }
110         else {
111             setViewedPageNumber(0); // first page is page 0
112         }
113 
114         if (KFSConstants.TableRenderConstants.SWITCH_TO_PAGE_METHOD.equals(getMethodToCall(request))) {
115             // look for the page number to switch to
116             setSwitchToPageNumber(-1);
117 
118             // the param we're looking for looks like: methodToCall.switchToPage.1.x , where 1 is the page nbr
119             String paramPrefix = KFSConstants.DISPATCH_REQUEST_PARAMETER + "." + KFSConstants.TableRenderConstants.SWITCH_TO_PAGE_METHOD + ".";
120             for (Enumeration i = request.getParameterNames(); i.hasMoreElements();) {
121                 String parameterName = (String) i.nextElement();
122                 if (parameterName.startsWith(paramPrefix) && parameterName.endsWith(".x")) {
123                     String switchToPageNumberStr = StringUtils.substringBetween(parameterName, paramPrefix, ".");
124                     setSwitchToPageNumber(Integer.parseInt(switchToPageNumberStr));
125                 }
126             }
127             if (getSwitchToPageNumber() == -1) {
128                 throw new RuntimeException("Couldn't find page number");
129             }
130         }
131 
132         if (KFSConstants.TableRenderConstants.SORT_METHOD.equals(getMethodToCall(request))) {
133             setColumnToSortIndex(-1);
134 
135             // the param we're looking for looks like: methodToCall.sort.1.x , where 1 is the column to sort on
136             String paramPrefix = KFSConstants.DISPATCH_REQUEST_PARAMETER + "." + KFSConstants.TableRenderConstants.SORT_METHOD + ".";
137             for (Enumeration i = request.getParameterNames(); i.hasMoreElements();) {
138                 String parameterName = (String) i.nextElement();
139                 if (parameterName.startsWith(paramPrefix) && parameterName.endsWith(".x")) {
140                     String columnToSortStr = StringUtils.substringBetween(parameterName, paramPrefix, ".");
141                     setColumnToSortIndex(Integer.parseInt(columnToSortStr));
142                 }
143             }
144             if (getColumnToSortIndex() == -1) {
145                 throw new RuntimeException("Couldn't find column to sort");
146             }
147         }
148 
149         setPreviouslySelectedObjectIdSet(parsePreviouslySelectedObjectIds(request));
150         setSelectedObjectIdSet(parseSelectedObjectIdSet(request));
151         setDisplayedObjectIdSet(parseDisplayedObjectIdSet(request));
152 
153         setSearchUsingOnlyPrimaryKeyValues(parseSearchUsingOnlyPrimaryKeyValues(request));
154         if (isSearchUsingOnlyPrimaryKeyValues()) {
155             setPrimaryKeyFieldLabels(getLookupable().getPrimaryKeyFieldLabels());
156         }
157     }
158 
159     /**
160      * This method converts the composite object IDs into a String
161      * 
162      * @return String for composite list of selected object IDs
163      */
164     public String getCompositeSelectedObjectIds() {
165         return LookupUtils.convertSetOfObjectIdsToString(getCompositeObjectIdMap().keySet());
166     }
167 
168     /**
169      * Parses a list of previously selected object IDs
170      * 
171      * @param request
172      * @return Set containing list of previously selected object IDs
173      */
174     protected Set<String> parsePreviouslySelectedObjectIds(HttpServletRequest request) {
175         String previouslySelectedObjectIds = request.getParameter(KFSConstants.MULTIPLE_VALUE_LOOKUP_PREVIOUSLY_SELECTED_OBJ_IDS_PARAM);
176         return LookupUtils.convertStringOfObjectIdsToSet(previouslySelectedObjectIds);
177     }
178 
179     /**
180      * Parses a list of selected object IDs
181      * 
182      * @param request
183      * @return Set containing list of selected object IDs
184      */
185     protected Set<String> parseSelectedObjectIdSet(HttpServletRequest request) {
186         Set<String> set = new HashSet<String>();
187 
188         Enumeration paramNames = request.getParameterNames();
189         while (paramNames.hasMoreElements()) {
190             String paramName = (String) paramNames.nextElement();
191             if (paramName.startsWith(KFSConstants.MULTIPLE_VALUE_LOOKUP_SELECTED_OBJ_ID_PARAM_PREFIX) && StringUtils.isNotBlank(request.getParameter(paramName))) {
192                 set.add(StringUtils.substringAfter(paramName, KFSConstants.MULTIPLE_VALUE_LOOKUP_SELECTED_OBJ_ID_PARAM_PREFIX));
193             }
194         }
195         return set;
196     }
197 
198     /**
199      * Parses a list of displayed object IDs
200      * 
201      * @param request
202      * @return Set containing list of displayed object IDs
203      */
204     protected Set<String> parseDisplayedObjectIdSet(HttpServletRequest request) {
205         Set<String> set = new HashSet<String>();
206 
207         Enumeration paramNames = request.getParameterNames();
208         while (paramNames.hasMoreElements()) {
209             String paramName = (String) paramNames.nextElement();
210             if (paramName.startsWith(KFSConstants.MULTIPLE_VALUE_LOOKUP_DISPLAYED_OBJ_ID_PARAM_PREFIX) && StringUtils.isNotBlank(request.getParameter(paramName))) {
211                 set.add(StringUtils.substringAfter(paramName, KFSConstants.MULTIPLE_VALUE_LOOKUP_DISPLAYED_OBJ_ID_PARAM_PREFIX));
212             }
213         }
214         return set;
215     }
216 
217     /**
218      * Iterates through the request params, looks for the parameter representing the method to call in the format like
219      * methodToCall.sort.1.(::;true;::).x, and returns the boolean value in the (::; and ;::) delimiters.
220      * 
221      * @see MultipleValueLookupForm#parseSearchUsingOnlyPrimaryKeyValues(String)
222      * @param request
223      * @return
224      */
225     protected boolean parseSearchUsingOnlyPrimaryKeyValues(HttpServletRequest request) {
226         // the param we're looking for looks like: methodToCall.sort.1.(::;true;::).x , we want to parse out the "true" component
227         String paramPrefix = KFSConstants.DISPATCH_REQUEST_PARAMETER + "." + getMethodToCall(request) + ".";
228         for (Enumeration i = request.getParameterNames(); i.hasMoreElements();) {
229             String parameterName = (String) i.nextElement();
230             if (parameterName.startsWith(paramPrefix) && parameterName.endsWith(".x")) {
231                 return parseSearchUsingOnlyPrimaryKeyValues(parameterName);
232             }
233         }
234         // maybe doing an initial search, so no value will be present
235         return false;
236     }
237 
238     /**
239      * Parses the method to call parameter passed in as a post parameter The parameter should be something like
240      * methodToCall.sort.1.(::;true;::).x, this method will return the value between (::; and ;::) as a boolean
241      * 
242      * @param methodToCallParam the method to call in a format described above
243      * @return the value between the delimiters, false if there are no delimiters
244      */
245     protected boolean parseSearchUsingOnlyPrimaryKeyValues(String methodToCallParam) {
246         String searchUsingOnlyPrimaryKeyValuesStr = StringUtils.substringBetween(methodToCallParam, KFSConstants.METHOD_TO_CALL_PARM12_LEFT_DEL, KFSConstants.METHOD_TO_CALL_PARM12_RIGHT_DEL);
247         if (StringUtils.isBlank(searchUsingOnlyPrimaryKeyValuesStr)) {
248             return false;
249         }
250         return Boolean.parseBoolean(searchUsingOnlyPrimaryKeyValuesStr);
251     }
252 
253     /**
254      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#getViewedPageNumber()
255      */
256     public int getViewedPageNumber() {
257         return tableMetadata.getViewedPageNumber();
258     }
259 
260     /**
261      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#setViewedPageNumber(int)
262      */
263     public void setViewedPageNumber(int pageNumberBeingViewedForMultivalueLookups) {
264         tableMetadata.setViewedPageNumber(pageNumberBeingViewedForMultivalueLookups);
265     }
266 
267     /**
268      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#getLookupResultsSequenceNumber()
269      */
270     public String getLookupResultsSequenceNumber() {
271         return lookupResultsSequenceNumber;
272     }
273 
274     /**
275      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#setLookupResultsSequenceNumber(java.lang.String)
276      */
277     public void setLookupResultsSequenceNumber(String lookupResultSequenceNumber) {
278         this.lookupResultsSequenceNumber = lookupResultSequenceNumber;
279     }
280 
281     /**
282      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#getTotalNumberOfPages()
283      */
284     public int getTotalNumberOfPages() {
285         return tableMetadata.getTotalNumberOfPages();
286     }
287 
288     /**
289      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#setTotalNumberOfPages(int)
290      */
291     public void setTotalNumberOfPages(int totalNumberOfPages) {
292         tableMetadata.setTotalNumberOfPages(totalNumberOfPages);
293     }
294 
295     /**
296      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#getFirstRowIndex()
297      */
298     public int getFirstRowIndex() {
299         return tableMetadata.getFirstRowIndex();
300     }
301 
302     /**
303      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#setFirstRowIndex(int)
304      */
305     public void setFirstRowIndex(int firstRowIndex) {
306         tableMetadata.setFirstRowIndex(firstRowIndex);
307     }
308 
309     /**
310      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#getLastRowIndex()
311      */
312     public int getLastRowIndex() {
313         return tableMetadata.getLastRowIndex();
314     }
315 
316     /**
317      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#setLastRowIndex(int)
318      */
319     public void setLastRowIndex(int lastRowIndex) {
320         tableMetadata.setLastRowIndex(lastRowIndex);
321     }
322 
323     /**
324      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#getSwitchToPageNumber()
325      */
326     public int getSwitchToPageNumber() {
327         return tableMetadata.getSwitchToPageNumber();
328     }
329 
330     /**
331      * This method sets the switchToPageNumber attribute for the metadata
332      * 
333      * @param switchToPageNumber
334      */
335     protected void setSwitchToPageNumber(int switchToPageNumber) {
336         tableMetadata.setSwitchToPageNumber(switchToPageNumber);
337     }
338 
339     /**
340      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#getPreviouslySelectedObjectIdSet()
341      */
342     public Set<String> getPreviouslySelectedObjectIdSet() {
343         return previouslySelectedObjectIdSet;
344     }
345 
346     /**
347      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#setPreviouslySelectedObjectIdSet(java.util.Set)
348      */
349     public void setPreviouslySelectedObjectIdSet(Set<String> previouslySelectedObjectIds) {
350         this.previouslySelectedObjectIdSet = previouslySelectedObjectIds;
351     }
352 
353     /**
354      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#getSelectedObjectIdSet()
355      */
356     public Set<String> getSelectedObjectIdSet() {
357         return selectedObjectIdSet;
358     }
359 
360     /**
361      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#setSelectedObjectIdSet(java.util.Set)
362      */
363     public void setSelectedObjectIdSet(Set<String> selectedObjectIdSet) {
364         this.selectedObjectIdSet = selectedObjectIdSet;
365     }
366 
367     /**
368      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#getDisplayedObjectIdSet()
369      */
370     public Set<String> getDisplayedObjectIdSet() {
371         return displayedObjectIdSet;
372     }
373 
374     /**
375      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#setDisplayedObjectIdSet(java.util.Set)
376      */
377     public void setDisplayedObjectIdSet(Set<String> displayedObjectIdSet) {
378         this.displayedObjectIdSet = displayedObjectIdSet;
379     }
380 
381     /**
382      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#getCompositeObjectIdMap()
383      */
384     public Map<String, String> getCompositeObjectIdMap() {
385         return compositeObjectIdMap;
386     }
387 
388     /**
389      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#setCompositeObjectIdMap(java.util.Map)
390      */
391     public void setCompositeObjectIdMap(Map<String, String> compositeObjectIdMap) {
392         this.compositeObjectIdMap = compositeObjectIdMap;
393     }
394 
395     /**
396      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#getColumnToSortIndex()
397      */
398     public int getColumnToSortIndex() {
399         return columnToSortIndex;
400     }
401 
402     /**
403      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#setColumnToSortIndex(int)
404      */
405     public void setColumnToSortIndex(int columnToSortIndex) {
406         this.columnToSortIndex = columnToSortIndex;
407     }
408 
409     /**
410      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#getPreviouslySortedColumnIndex()
411      */
412     public String getPreviouslySortedColumnIndex() {
413         return previouslySortedColumnIndex;
414     }
415 
416     /**
417      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#setPreviouslySortedColumnIndex(java.lang.String)
418      */
419     public void setPreviouslySortedColumnIndex(String previouslySortedColumnIndex) {
420         this.previouslySortedColumnIndex = previouslySortedColumnIndex;
421     }
422 
423     /**
424      * gets the name of the collection being looked up by the calling page. This value will be returned unmodified to the calling
425      * page (indicated by super.getBackLocation()), which should use it to determine in which collection the selected results will
426      * be returned.
427      * 
428      * @return
429      */
430     public String getLookedUpCollectionName() {
431         return lookedUpCollectionName;
432     }
433 
434     /**
435      * sets the name of the collection being looked up by the calling page. This value will be returned unmodified to the calling
436      * page (indicated by super.getBackLocation()), which should use it to determine in which collection the selected results will
437      * be returned
438      * 
439      * @param lookedUpCollectionName
440      */
441     public void setLookedUpCollectionName(String lookedUpCollectionName) {
442         this.lookedUpCollectionName = lookedUpCollectionName;
443     }
444 
445     /**
446      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#getResultsActualSize()
447      */
448     public int getResultsActualSize() {
449         return resultsActualSize;
450     }
451 
452     /**
453      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#setResultsActualSize(int)
454      */
455     public void setResultsActualSize(int resultsActualSize) {
456         this.resultsActualSize = resultsActualSize;
457     }
458 
459     /**
460      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#getResultsLimitedSize()
461      */
462     public int getResultsLimitedSize() {
463         return resultsLimitedSize;
464     }
465 
466     /**
467      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#setResultsLimitedSize(int)
468      */
469     public void setResultsLimitedSize(int resultsLimitedSize) {
470         this.resultsLimitedSize = resultsLimitedSize;
471     }
472 
473     /**
474      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#jumpToFirstPage(int, int)
475      */
476     public void jumpToFirstPage(int listSize, int maxRowsPerPage) {
477         tableMetadata.jumpToFirstPage(listSize, maxRowsPerPage);
478     }
479 
480     /**
481      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#jumpToLastPage(int, int)
482      */
483     public void jumpToLastPage(int listSize, int maxRowsPerPage) {
484         tableMetadata.jumpToLastPage(listSize, maxRowsPerPage);
485     }
486 
487     /**
488      * @see org.kuali.kfs.gl.web.struts.LookupResultsSelectable#jumpToPage(int, int, int)
489      */
490     public void jumpToPage(int pageNumber, int listSize, int maxRowsPerPage) {
491         tableMetadata.jumpToPage(pageNumber, listSize, maxRowsPerPage);
492     }
493 
494 }