001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.module.gl.web.struts.form;
017:
018: import java.util.Enumeration;
019: import java.util.HashSet;
020: import java.util.Map;
021: import java.util.Set;
022:
023: import javax.servlet.http.HttpServletRequest;
024:
025: import org.apache.commons.lang.StringUtils;
026: import org.kuali.core.lookup.LookupUtils;
027: import org.kuali.core.web.struts.form.KualiTableRenderFormMetadata;
028: import org.kuali.core.web.struts.form.LookupForm;
029: import org.kuali.kfs.KFSConstants;
030:
031: /**
032: * This class is the action form for balance inquiry lookup results
033: *
034: */
035: public class BalanceInquiryLookupResults extends LookupForm implements
036: LookupResultsSelectable {
037: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
038: .getLogger(BalanceInquiryLookupResults.class);
039:
040: private KualiTableRenderFormMetadata tableMetadata;
041:
042: private String lookupResultsSequenceNumber;
043:
044: /**
045: * The number of rows that match the query criteria
046: */
047: private int resultsActualSize;
048:
049: /**
050: * The number of rows that match the query criteria or the max results limit size (if applicable), whichever is less
051: */
052: private int resultsLimitedSize;
053:
054: /**
055: * when the looked results screen was rendered, the index of the column that the results were sorted on. -1 for unknown, index
056: * numbers starting at 0
057: */
058: private String previouslySortedColumnIndex;
059:
060: /**
061: * Comment for <code>columnToSortIndex</code>
062: */
063: private int columnToSortIndex;
064:
065: /**
066: * the name of the collection being looked up by the calling page. This value will be returned unmodified to the calling page
067: * (indicated by super.getBackLocation()), which should use it to determine in which collection the selected results will be
068: * returned.
069: */
070: private String lookedUpCollectionName;
071:
072: /**
073: * Those object IDs that were selected before the current page is rendered
074: */
075: private Set<String> previouslySelectedObjectIdSet;
076: /**
077: * Those object IDs that are rendered on the current page
078: */
079: private Set<String> displayedObjectIdSet;
080: /**
081: * Those object IDs that are selected/checked on the current page
082: */
083: private Set<String> selectedObjectIdSet;
084: /**
085: * The object IDs that are selected after the struts action is complete; the obj IDs in the keys of this Map will be considered
086: * checked in the UI
087: */
088: private Map<String, String> compositeObjectIdMap;
089:
090: public BalanceInquiryLookupResults() {
091: tableMetadata = new KualiTableRenderFormMetadata();
092: }
093:
094: protected String getMethodToCall(HttpServletRequest request) {
095: return request
096: .getParameter(KFSConstants.DISPATCH_REQUEST_PARAMETER);
097: }
098:
099: /**
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
108: .isNotBlank(request
109: .getParameter(KFSConstants.TableRenderConstants.VIEWED_PAGE_NUMBER))) {
110: setViewedPageNumber(Integer
111: .parseInt(request
112: .getParameter(KFSConstants.TableRenderConstants.VIEWED_PAGE_NUMBER)));
113: } else {
114: setViewedPageNumber(0); // first page is page 0
115: }
116:
117: if (KFSConstants.TableRenderConstants.SWITCH_TO_PAGE_METHOD
118: .equals(getMethodToCall(request))) {
119: // look for the page number to switch to
120: setSwitchToPageNumber(-1);
121:
122: // the param we're looking for looks like: methodToCall.switchToPage.1.x , where 1 is the page nbr
123: String paramPrefix = KFSConstants.DISPATCH_REQUEST_PARAMETER
124: + "."
125: + KFSConstants.TableRenderConstants.SWITCH_TO_PAGE_METHOD
126: + ".";
127: for (Enumeration i = request.getParameterNames(); i
128: .hasMoreElements();) {
129: String parameterName = (String) i.nextElement();
130: if (parameterName.startsWith(paramPrefix)
131: && parameterName.endsWith(".x")) {
132: String switchToPageNumberStr = StringUtils
133: .substringBetween(parameterName,
134: paramPrefix, ".");
135: setSwitchToPageNumber(Integer
136: .parseInt(switchToPageNumberStr));
137: }
138: }
139: if (getSwitchToPageNumber() == -1) {
140: throw new RuntimeException("Couldn't find page number");
141: }
142: }
143:
144: if (KFSConstants.TableRenderConstants.SORT_METHOD
145: .equals(getMethodToCall(request))) {
146: setColumnToSortIndex(-1);
147:
148: // the param we're looking for looks like: methodToCall.sort.1.x , where 1 is the column to sort on
149: String paramPrefix = KFSConstants.DISPATCH_REQUEST_PARAMETER
150: + "."
151: + KFSConstants.TableRenderConstants.SORT_METHOD
152: + ".";
153: for (Enumeration i = request.getParameterNames(); i
154: .hasMoreElements();) {
155: String parameterName = (String) i.nextElement();
156: if (parameterName.startsWith(paramPrefix)
157: && parameterName.endsWith(".x")) {
158: String columnToSortStr = StringUtils
159: .substringBetween(parameterName,
160: paramPrefix, ".");
161: setColumnToSortIndex(Integer
162: .parseInt(columnToSortStr));
163: }
164: }
165: if (getColumnToSortIndex() == -1) {
166: throw new RuntimeException(
167: "Couldn't find column to sort");
168: }
169: }
170:
171: setPreviouslySelectedObjectIdSet(parsePreviouslySelectedObjectIds(request));
172: setSelectedObjectIdSet(parseSelectedObjectIdSet(request));
173: setDisplayedObjectIdSet(parseDisplayedObjectIdSet(request));
174:
175: setSearchUsingOnlyPrimaryKeyValues(parseSearchUsingOnlyPrimaryKeyValues(request));
176: if (isSearchUsingOnlyPrimaryKeyValues()) {
177: setPrimaryKeyFieldLabels(getLookupable()
178: .getPrimaryKeyFieldLabels());
179: }
180: }
181:
182: /**
183: * This method converts the composite object IDs into a String
184: *
185: * @return String for composite list of selected object IDs
186: */
187: public String getCompositeSelectedObjectIds() {
188: return LookupUtils
189: .convertSetOfObjectIdsToString(getCompositeObjectIdMap()
190: .keySet());
191: }
192:
193: /**
194: * Parses a list of previously selected object IDs
195: *
196: * @param request
197: * @return Set containing list of previously selected object IDs
198: */
199: protected Set<String> parsePreviouslySelectedObjectIds(
200: HttpServletRequest request) {
201: String previouslySelectedObjectIds = request
202: .getParameter(KFSConstants.MULTIPLE_VALUE_LOOKUP_PREVIOUSLY_SELECTED_OBJ_IDS_PARAM);
203: return LookupUtils
204: .convertStringOfObjectIdsToSet(previouslySelectedObjectIds);
205: }
206:
207: /**
208: * Parses a list of selected object IDs
209: *
210: * @param request
211: * @return Set containing list of selected object IDs
212: */
213: protected Set<String> parseSelectedObjectIdSet(
214: HttpServletRequest request) {
215: Set<String> set = new HashSet<String>();
216:
217: Enumeration paramNames = request.getParameterNames();
218: while (paramNames.hasMoreElements()) {
219: String paramName = (String) paramNames.nextElement();
220: if (paramName
221: .startsWith(KFSConstants.MULTIPLE_VALUE_LOOKUP_SELECTED_OBJ_ID_PARAM_PREFIX)
222: && StringUtils.isNotBlank(request
223: .getParameter(paramName))) {
224: set
225: .add(StringUtils
226: .substringAfter(
227: paramName,
228: KFSConstants.MULTIPLE_VALUE_LOOKUP_SELECTED_OBJ_ID_PARAM_PREFIX));
229: }
230: }
231: return set;
232: }
233:
234: /**
235: * Parses a list of displayed object IDs
236: *
237: * @param request
238: * @return Set containing list of displayed object IDs
239: */
240: protected Set<String> parseDisplayedObjectIdSet(
241: HttpServletRequest request) {
242: Set<String> set = new HashSet<String>();
243:
244: Enumeration paramNames = request.getParameterNames();
245: while (paramNames.hasMoreElements()) {
246: String paramName = (String) paramNames.nextElement();
247: if (paramName
248: .startsWith(KFSConstants.MULTIPLE_VALUE_LOOKUP_DISPLAYED_OBJ_ID_PARAM_PREFIX)
249: && StringUtils.isNotBlank(request
250: .getParameter(paramName))) {
251: set
252: .add(StringUtils
253: .substringAfter(
254: paramName,
255: KFSConstants.MULTIPLE_VALUE_LOOKUP_DISPLAYED_OBJ_ID_PARAM_PREFIX));
256: }
257: }
258: return set;
259: }
260:
261: /**
262: * Iterates through the request params, looks for the parameter representing the method to call in the format like
263: * methodToCall.sort.1.(::;true;::).x, and returns the boolean value in the (::; and ;::) delimiters.
264: *
265: * @see MultipleValueLookupForm#parseSearchUsingOnlyPrimaryKeyValues(String)
266: * @param request
267: * @return
268: */
269: protected boolean parseSearchUsingOnlyPrimaryKeyValues(
270: HttpServletRequest request) {
271: // the param we're looking for looks like: methodToCall.sort.1.(::;true;::).x , we want to parse out the "true" component
272: String paramPrefix = KFSConstants.DISPATCH_REQUEST_PARAMETER
273: + "." + getMethodToCall(request) + ".";
274: for (Enumeration i = request.getParameterNames(); i
275: .hasMoreElements();) {
276: String parameterName = (String) i.nextElement();
277: if (parameterName.startsWith(paramPrefix)
278: && parameterName.endsWith(".x")) {
279: return parseSearchUsingOnlyPrimaryKeyValues(parameterName);
280: }
281: }
282: // maybe doing an initial search, so no value will be present
283: return false;
284: }
285:
286: /**
287: * Parses the method to call parameter passed in as a post parameter The parameter should be something like
288: * methodToCall.sort.1.(::;true;::).x, this method will return the value between (::; and ;::) as a boolean
289: *
290: * @param methodToCallParam the method to call in a format described above
291: * @return the value between the delimiters, false if there are no delimiters
292: */
293: protected boolean parseSearchUsingOnlyPrimaryKeyValues(
294: String methodToCallParam) {
295: String searchUsingOnlyPrimaryKeyValuesStr = StringUtils
296: .substringBetween(methodToCallParam,
297: KFSConstants.METHOD_TO_CALL_PARM12_LEFT_DEL,
298: KFSConstants.METHOD_TO_CALL_PARM12_RIGHT_DEL);
299: if (StringUtils.isBlank(searchUsingOnlyPrimaryKeyValuesStr)) {
300: return false;
301: }
302: return Boolean.parseBoolean(searchUsingOnlyPrimaryKeyValuesStr);
303: }
304:
305: /**
306: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#getViewedPageNumber()
307: */
308: public int getViewedPageNumber() {
309: return tableMetadata.getViewedPageNumber();
310: }
311:
312: /**
313: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#setViewedPageNumber(int)
314: */
315: public void setViewedPageNumber(
316: int pageNumberBeingViewedForMultivalueLookups) {
317: tableMetadata
318: .setViewedPageNumber(pageNumberBeingViewedForMultivalueLookups);
319: }
320:
321: /**
322: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#getLookupResultsSequenceNumber()
323: */
324: public String getLookupResultsSequenceNumber() {
325: return lookupResultsSequenceNumber;
326: }
327:
328: /**
329: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#setLookupResultsSequenceNumber(java.lang.String)
330: */
331: public void setLookupResultsSequenceNumber(
332: String lookupResultSequenceNumber) {
333: this .lookupResultsSequenceNumber = lookupResultSequenceNumber;
334: }
335:
336: /**
337: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#getTotalNumberOfPages()
338: */
339: public int getTotalNumberOfPages() {
340: return tableMetadata.getTotalNumberOfPages();
341: }
342:
343: /**
344: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#setTotalNumberOfPages(int)
345: */
346: public void setTotalNumberOfPages(int totalNumberOfPages) {
347: tableMetadata.setTotalNumberOfPages(totalNumberOfPages);
348: }
349:
350: /**
351: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#getFirstRowIndex()
352: */
353: public int getFirstRowIndex() {
354: return tableMetadata.getFirstRowIndex();
355: }
356:
357: /**
358: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#setFirstRowIndex(int)
359: */
360: public void setFirstRowIndex(int firstRowIndex) {
361: tableMetadata.setFirstRowIndex(firstRowIndex);
362: }
363:
364: /**
365: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#getLastRowIndex()
366: */
367: public int getLastRowIndex() {
368: return tableMetadata.getLastRowIndex();
369: }
370:
371: /**
372: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#setLastRowIndex(int)
373: */
374: public void setLastRowIndex(int lastRowIndex) {
375: tableMetadata.setLastRowIndex(lastRowIndex);
376: }
377:
378: /**
379: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#getSwitchToPageNumber()
380: */
381: public int getSwitchToPageNumber() {
382: return tableMetadata.getSwitchToPageNumber();
383: }
384:
385: /**
386: * This method sets the switchToPageNumber attribute for the metadata
387: *
388: * @param switchToPageNumber
389: */
390: protected void setSwitchToPageNumber(int switchToPageNumber) {
391: tableMetadata.setSwitchToPageNumber(switchToPageNumber);
392: }
393:
394: /**
395: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#getPreviouslySelectedObjectIdSet()
396: */
397: public Set<String> getPreviouslySelectedObjectIdSet() {
398: return previouslySelectedObjectIdSet;
399: }
400:
401: /**
402: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#setPreviouslySelectedObjectIdSet(java.util.Set)
403: */
404: public void setPreviouslySelectedObjectIdSet(
405: Set<String> previouslySelectedObjectIds) {
406: this .previouslySelectedObjectIdSet = previouslySelectedObjectIds;
407: }
408:
409: /**
410: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#getSelectedObjectIdSet()
411: */
412: public Set<String> getSelectedObjectIdSet() {
413: return selectedObjectIdSet;
414: }
415:
416: /**
417: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#setSelectedObjectIdSet(java.util.Set)
418: */
419: public void setSelectedObjectIdSet(Set<String> selectedObjectIdSet) {
420: this .selectedObjectIdSet = selectedObjectIdSet;
421: }
422:
423: /**
424: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#getDisplayedObjectIdSet()
425: */
426: public Set<String> getDisplayedObjectIdSet() {
427: return displayedObjectIdSet;
428: }
429:
430: /**
431: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#setDisplayedObjectIdSet(java.util.Set)
432: */
433: public void setDisplayedObjectIdSet(Set<String> displayedObjectIdSet) {
434: this .displayedObjectIdSet = displayedObjectIdSet;
435: }
436:
437: /**
438: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#getCompositeObjectIdMap()
439: */
440: public Map<String, String> getCompositeObjectIdMap() {
441: return compositeObjectIdMap;
442: }
443:
444: /**
445: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#setCompositeObjectIdMap(java.util.Map)
446: */
447: public void setCompositeObjectIdMap(
448: Map<String, String> compositeObjectIdMap) {
449: this .compositeObjectIdMap = compositeObjectIdMap;
450: }
451:
452: /**
453: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#getColumnToSortIndex()
454: */
455: public int getColumnToSortIndex() {
456: return columnToSortIndex;
457: }
458:
459: /**
460: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#setColumnToSortIndex(int)
461: */
462: public void setColumnToSortIndex(int columnToSortIndex) {
463: this .columnToSortIndex = columnToSortIndex;
464: }
465:
466: /**
467: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#getPreviouslySortedColumnIndex()
468: */
469: public String getPreviouslySortedColumnIndex() {
470: return previouslySortedColumnIndex;
471: }
472:
473: /**
474: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#setPreviouslySortedColumnIndex(java.lang.String)
475: */
476: public void setPreviouslySortedColumnIndex(
477: String previouslySortedColumnIndex) {
478: this .previouslySortedColumnIndex = previouslySortedColumnIndex;
479: }
480:
481: /**
482: * gets the name of the collection being looked up by the calling page. This value will be returned unmodified to the calling
483: * page (indicated by super.getBackLocation()), which should use it to determine in which collection the selected results will
484: * be returned.
485: *
486: * @return
487: */
488: public String getLookedUpCollectionName() {
489: return lookedUpCollectionName;
490: }
491:
492: /**
493: * sets the name of the collection being looked up by the calling page. This value will be returned unmodified to the calling
494: * page (indicated by super.getBackLocation()), which should use it to determine in which collection the selected results will
495: * be returned
496: *
497: * @param lookedUpCollectionName
498: */
499: public void setLookedUpCollectionName(String lookedUpCollectionName) {
500: this .lookedUpCollectionName = lookedUpCollectionName;
501: }
502:
503: /**
504: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#getResultsActualSize()
505: */
506: public int getResultsActualSize() {
507: return resultsActualSize;
508: }
509:
510: /**
511: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#setResultsActualSize(int)
512: */
513: public void setResultsActualSize(int resultsActualSize) {
514: this .resultsActualSize = resultsActualSize;
515: }
516:
517: /**
518: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#getResultsLimitedSize()
519: */
520: public int getResultsLimitedSize() {
521: return resultsLimitedSize;
522: }
523:
524: /**
525: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#setResultsLimitedSize(int)
526: */
527: public void setResultsLimitedSize(int resultsLimitedSize) {
528: this .resultsLimitedSize = resultsLimitedSize;
529: }
530:
531: /**
532: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#jumpToFirstPage(int, int)
533: */
534: public void jumpToFirstPage(int listSize, int maxRowsPerPage) {
535: tableMetadata.jumpToFirstPage(listSize, maxRowsPerPage);
536: }
537:
538: /**
539: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#jumpToLastPage(int, int)
540: */
541: public void jumpToLastPage(int listSize, int maxRowsPerPage) {
542: tableMetadata.jumpToLastPage(listSize, maxRowsPerPage);
543: }
544:
545: /**
546: * @see org.kuali.module.gl.web.struts.form.LookupResultsSelectable#jumpToPage(int, int, int)
547: */
548: public void jumpToPage(int pageNumber, int listSize,
549: int maxRowsPerPage) {
550: tableMetadata.jumpToPage(pageNumber, listSize, maxRowsPerPage);
551: }
552:
553: }
|