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: /*
017: * Created on Aug 2, 2004
018: *
019: */
020: package org.kuali.module.pdp.action.paymentsearch;
021:
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027: import javax.servlet.http.HttpSession;
028:
029: import org.apache.struts.action.ActionErrors;
030: import org.apache.struts.action.ActionForm;
031: import org.apache.struts.action.ActionForward;
032: import org.apache.struts.action.ActionMapping;
033: import org.apache.struts.action.ActionMessage;
034: import org.kuali.kfs.context.SpringContext;
035: import org.kuali.kfs.service.ParameterService;
036: import org.kuali.kfs.service.impl.ParameterConstants;
037: import org.kuali.module.pdp.PdpConstants;
038: import org.kuali.module.pdp.action.BaseAction;
039: import org.kuali.module.pdp.bo.Batch;
040: import org.kuali.module.pdp.bo.BatchSearch;
041: import org.kuali.module.pdp.form.paymentsearch.BatchSearchForm;
042: import org.kuali.module.pdp.service.BatchMaintenanceService;
043: import org.kuali.module.pdp.service.BatchSearchService;
044: import org.kuali.module.pdp.service.SecurityRecord;
045: import org.kuali.module.pdp.utilities.GeneralUtilities;
046:
047: /**
048: * @author delyea
049: */
050: public class BatchSearchAction extends BaseAction {
051: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
052: .getLogger(BatchSearchAction.class);
053:
054: private BatchSearchService batchSearchService;
055: private BatchMaintenanceService batchMaintenanceService;
056:
057: public BatchSearchAction() {
058: setBatchSearchService(SpringContext
059: .getBean(BatchSearchService.class));
060: setBatchMaintenanceService(SpringContext
061: .getBean(BatchMaintenanceService.class));
062: }
063:
064: protected boolean isAuthorized(ActionMapping mapping,
065: ActionForm form, HttpServletRequest request,
066: HttpServletResponse response) {
067: SecurityRecord sr = getSecurityRecord(request);
068: return sr.isLimitedViewRole() || sr.isViewAllRole()
069: || sr.isViewIdRole() || sr.isViewBankRole();
070: }
071:
072: protected ActionForward executeLogic(ActionMapping mapping,
073: ActionForm form, HttpServletRequest request,
074: HttpServletResponse response) throws Exception {
075: LOG.info("executeLogic() starting");
076: String forward = "search";
077: HttpSession session = request.getSession();
078: List searchResults = null;
079: Object perPage = session.getAttribute("perPage");
080: if ((perPage == null) || (perPage.toString() == "")) {
081: session.setAttribute("perPage", getSearchResultsPerPage());
082: }
083:
084: ActionErrors actionErrors = new ActionErrors();
085: String buttonPressed = GeneralUtilities
086: .whichButtonWasPressed(request);
087:
088: BatchSearchForm bsf = (BatchSearchForm) form;
089: LOG.debug("executeLogic() bsf is " + bsf);
090: LOG.debug("executeLogic() Type of search is Batch");
091: LOG.debug("executeLogic() buttonPressed is " + buttonPressed);
092:
093: if ((buttonPressed.endsWith("Hold"))
094: || (buttonPressed.endsWith("Cancel"))) {
095: // Code to display updated individual search results
096: LOG
097: .debug("executeLogic() Returned to Search from Update Screen");
098: session.removeAttribute("batchIndivSearchResults");
099: bsf = (BatchSearchForm) session
100: .getAttribute("BatchSearchFormSession");
101: if (bsf == null) {
102: LOG
103: .info("executeLogic() BatchSearchForm 'bsf' from session is null");
104: return mapping.findForward("pdp_session_timeout");
105: } else {
106: Batch btch = (Batch) session
107: .getAttribute("BatchDetail");
108: Integer bId = null;
109: if (btch != null) {
110: // If we still have the batch then get the payments in it again to update statuses
111: bId = btch.getId();
112: LOG
113: .debug("executeLogic() Batch ID being tested is "
114: + bId);
115: searchResults = batchSearchService
116: .getAllSingleBatchPayments(bId);
117: searchResults = checkList(searchResults, session,
118: "Indiv", actionErrors);
119: if (batchMaintenanceService
120: .doBatchPaymentsHaveHeldStatus(bId)) {
121: request.setAttribute("allAreHeld", "true");
122: }
123: return mapping.findForward("batchdetail");
124: } else {
125: BatchSearch bs = bsf.getBatchSearch();
126: searchResults = batchSearchService
127: .getAllBatchesForSearchCriteria(bs);
128: searchResults = checkList(searchResults, session,
129: "", actionErrors);
130: }
131: }
132: } else if (buttonPressed.startsWith("btnUpdate")) {
133: // Code to display updated individual search results
134: LOG
135: .debug("executeLogic() Returned to Search from Update Screen");
136: session.removeAttribute("batchIndivSearchResults");
137: bsf = (BatchSearchForm) session
138: .getAttribute("BatchSearchFormSession");
139: if (bsf.equals(null)) {
140: LOG
141: .info("executeLogic() BatchSearchForm 'bsf' from session is null");
142: return mapping.findForward("pdp_session_timeout");
143: } else {
144: Batch btch = (Batch) session
145: .getAttribute("BatchDetail");
146: Integer bId = null;
147: if (btch != null) {
148: // If we still have the batch then get the payments in it again to update statuses
149: bId = btch.getId();
150: LOG
151: .debug("executeLogic() Batch ID being tested is "
152: + bId);
153: searchResults = batchSearchService
154: .getAllSingleBatchPayments(bId);
155: searchResults = checkList(searchResults, session,
156: "Indiv", actionErrors);
157: if (batchMaintenanceService
158: .doBatchPaymentsHaveHeldStatus(bId)) {
159: request.setAttribute("allAreHeld", "true");
160: }
161: return mapping.findForward("batchdetail");
162: } else {
163: BatchSearch bs = bsf.getBatchSearch();
164: searchResults = batchSearchService
165: .getAllBatchesForSearchCriteria(bs);
166: searchResults = checkList(searchResults, session,
167: "", actionErrors);
168: }
169: }
170: } else {
171: if (buttonPressed.startsWith("btnSearch")) {
172: // Code for Searching for Batches
173: clearObjects(session, actionErrors);
174: BatchSearch bs = bsf.getBatchSearch();
175: searchResults = batchSearchService
176: .getAllBatchesForSearchCriteria(bs);
177:
178: searchResults = checkList(searchResults, session, "",
179: actionErrors);
180: session.setAttribute("BatchSearchFormSession", bsf);
181: } else if (buttonPressed.startsWith("btnBatchDetail")) {
182: session.removeAttribute("batchIndivSearchResults");
183: Integer bId = GeneralUtilities
184: .convertStringToInteger(request
185: .getParameter("BatchId"));
186: List l = (List) session
187: .getAttribute("batchSearchResults");
188:
189: if (l == null) {
190: LOG
191: .info("executeLogic() Batch Search Results variable 'l' from session is null");
192: return mapping.findForward("pdp_session_timeout");
193: } else {
194: if (bId == null) {
195: return mapping.findForward("pdp_system_error");
196: } else {
197: for (Iterator iter = l.iterator(); iter
198: .hasNext();) {
199: Batch element = (Batch) iter.next();
200: if (element.getId().equals(bId)) {
201: LOG
202: .debug("executeLogic() Found Batch relating to batchId "
203: + bId);
204: session.setAttribute("BatchDetail",
205: element);
206: LOG
207: .debug("executeLogic() Batch contains "
208: + element
209: .getPaymentCount()
210: + " payment(s)");
211: }
212: }
213: searchResults = batchSearchService
214: .getAllSingleBatchPayments(bId);
215: searchResults = checkList(searchResults,
216: session, "Indiv", actionErrors);
217: if (batchMaintenanceService
218: .doBatchPaymentsHaveHeldStatus(bId)) {
219: request.setAttribute("allAreHeld", "true");
220: }
221: }
222: }
223: return mapping.findForward("batchdetail");
224: } else if (buttonPressed.startsWith("btnBack")) {
225: bsf = (BatchSearchForm) session
226: .getAttribute("BatchSearchFormSession");
227: if (bsf != null) {
228: // Code to use BreadCrumb Links
229: if (buttonPressed.endsWith("Indiv")) {
230: return mapping.findForward("batchdetail");
231: }
232: } else {
233: LOG
234: .info("executeLogic() BatchSearchForm 'bsf' from session is null");
235: return mapping.findForward("pdp_session_timeout");
236: }
237: } else if (buttonPressed.startsWith("btnClear")) {
238: // Code to clear the form
239: bsf.clearForm();
240: session.removeAttribute("BatchSearchFormSession");
241: } else {
242: clearObjects(session, actionErrors);
243: }
244: }
245:
246: // If we had errors, save them.
247: if (!actionErrors.isEmpty()) {
248: saveErrors(request, actionErrors);
249: for (Iterator iter = actionErrors.get(); iter.hasNext();) {
250: ActionMessage element = (ActionMessage) iter.next();
251: LOG.debug("executeLogic() ActionErrors Element = "
252: + element.getKey());
253: }
254: }
255:
256: request.setAttribute("BatchSearchForm", bsf);
257: return mapping.findForward(forward);
258: }
259:
260: /**
261: * Clear stored session objects as well as actionErrors.
262: *
263: * @param request
264: * @return
265: */
266: protected void clearObjects(HttpSession session,
267: ActionErrors actionErrors) {
268: // Individual Search Variables in Session
269: session.removeAttribute("indivSearchResults");
270: session.removeAttribute("PaymentDetailSearchFormSession");
271:
272: // Batch Search Variables in Session
273: session.removeAttribute("batchSearchResults");
274: session.removeAttribute("batchIndivSearchResults");
275: session.removeAttribute("BatchDetail");
276: session.removeAttribute("BatchSearchFormSession");
277:
278: actionErrors.clear();
279: }
280:
281: /**
282: * Takes in the list from Search & the Search Type and updates appropriate variables and the list itself.
283: *
284: * @param request
285: * @param searchResults
286: * @param searchType
287: * @return searchResults
288: */
289: protected List checkList(List searchResults, HttpSession session,
290: String resultListType, ActionErrors actionErrors) {
291: session.removeAttribute("batch" + resultListType
292: + "SearchResults");
293: int searchSize = getMaxSearchTotal();
294: int maxSize = searchSize + 1;
295: int returnSize = searchSize - 1;
296:
297: if (searchResults != null) {
298: LOG.debug("executeLogic() Search returned having found "
299: + searchResults.size() + " results");
300: if (searchResults.size() == 0) {
301: actionErrors.add("errors", new ActionMessage(
302: "BatchSearchAction.emptyresults.invalid"));
303: } else if (searchResults.size() < maxSize) {
304: session.setAttribute("batch" + resultListType
305: + "SearchResults", searchResults);
306: } else {
307: if (resultListType != "") {
308: session.setAttribute("batch" + resultListType
309: + "SearchResults", searchResults);
310: } else {
311: actionErrors.add("errors", new ActionMessage(
312: "BatchSearchAction.listover.invalid"));
313: session.setAttribute("batch" + resultListType
314: + "SearchResults", searchResults.subList(0,
315: returnSize));
316: }
317: }
318: } else {
319: actionErrors.add("errors", new ActionMessage(
320: "BatchSearchAction.emptyresults.invalid"));
321: }
322: return searchResults;
323: }
324:
325: private int getSearchResultsPerPage() {
326: return GeneralUtilities
327: .getParameterInteger(
328: SpringContext.getBean(ParameterService.class),
329: ParameterConstants.PRE_DISBURSEMENT_LOOKUP.class,
330: PdpConstants.ApplicationParameterKeys.SEARCH_RESULTS_PER_PAGE);
331: }
332:
333: private int getMaxSearchTotal() {
334: return GeneralUtilities
335: .getParameterInteger(
336: SpringContext.getBean(ParameterService.class),
337: ParameterConstants.PRE_DISBURSEMENT_LOOKUP.class,
338: PdpConstants.ApplicationParameterKeys.SEARCH_RESULTS_TOTAL);
339: }
340:
341: public void setBatchSearchService(BatchSearchService b) {
342: batchSearchService = b;
343: }
344:
345: /**
346: * @param batchMaintenanceService The batchMaintenanceService to set.
347: */
348: public void setBatchMaintenanceService(
349: BatchMaintenanceService batchMaintenanceService) {
350: this.batchMaintenanceService = batchMaintenanceService;
351: }
352: }
|