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