001: /*
002: * Copyright 2006-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.action;
017:
018: import java.io.IOException;
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025:
026: import javax.servlet.ServletException;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.apache.struts.action.ActionForm;
031: import org.apache.struts.action.ActionForward;
032: import org.apache.struts.action.ActionMapping;
033: import org.kuali.core.lookup.CollectionIncomplete;
034: import org.kuali.core.lookup.Lookupable;
035: import org.kuali.core.service.KualiConfigurationService;
036: import org.kuali.core.util.GlobalVariables;
037: import org.kuali.core.web.struts.action.KualiAction;
038: import org.kuali.core.web.struts.form.LookupForm;
039: import org.kuali.core.web.ui.Field;
040: import org.kuali.core.web.ui.ResultRow;
041: import org.kuali.core.web.ui.Row;
042: import org.kuali.kfs.KFSConstants;
043: import org.kuali.kfs.KFSKeyConstants;
044: import org.kuali.kfs.KFSPropertyConstants;
045: import org.kuali.kfs.context.SpringContext;
046: import org.kuali.module.gl.bo.AccountBalance;
047: import org.kuali.module.gl.util.ObjectHelper;
048: import org.kuali.module.gl.web.lookupable.AccountBalanceByConsolidationLookupableHelperServiceImpl;
049: import org.kuali.module.gl.web.struts.form.BalanceInquiryForm;
050:
051: /**
052: * This class handles Actions for lookup flow
053: */
054:
055: public class BalanceInquiryAction extends KualiAction {
056: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
057: .getLogger(BalanceInquiryAction.class);
058:
059: private static final String TOTALS_TABLE_KEY = "totalsTable";
060:
061: private KualiConfigurationService kualiConfigurationService;
062: private String[] totalTitles;
063:
064: public BalanceInquiryAction() {
065: super ();
066: kualiConfigurationService = SpringContext
067: .getBean(KualiConfigurationService.class);
068: }
069:
070: /**
071: * Sets up total titles
072: */
073: private void setTotalTitles() {
074: totalTitles = new String[7];
075:
076: totalTitles[0] = kualiConfigurationService
077: .getPropertyString(KFSKeyConstants.AccountBalanceService.INCOME);
078: totalTitles[1] = kualiConfigurationService
079: .getPropertyString(KFSKeyConstants.AccountBalanceService.INCOME_FROM_TRANSFERS);
080: totalTitles[2] = kualiConfigurationService
081: .getPropertyString(KFSKeyConstants.AccountBalanceService.INCOME_TOTAL);
082: totalTitles[3] = kualiConfigurationService
083: .getPropertyString(KFSKeyConstants.AccountBalanceService.EXPENSE);
084: totalTitles[4] = kualiConfigurationService
085: .getPropertyString(KFSKeyConstants.AccountBalanceService.EXPENSE_FROM_TRANSFERS);
086: totalTitles[5] = kualiConfigurationService
087: .getPropertyString(KFSKeyConstants.AccountBalanceService.EXPENSE_TOTAL);
088: totalTitles[6] = kualiConfigurationService
089: .getPropertyString(KFSKeyConstants.AccountBalanceService.TOTAL);
090:
091: }
092:
093: /**
094: * Returns an array of total titles
095: *
096: * @return array of total titles
097: */
098: private String[] getTotalTitles() {
099: if (null == totalTitles) {
100: setTotalTitles();
101: }
102:
103: return totalTitles;
104: }
105:
106: public ActionForward start(ActionMapping mapping, ActionForm form,
107: HttpServletRequest request, HttpServletResponse response)
108: throws Exception {
109: return mapping.findForward(KFSConstants.MAPPING_BASIC);
110: }
111:
112: /**
113: * Search - sets the values of the data entered on the form on the jsp into a map and then searches for the results.
114: *
115: * @param mapping
116: * @param form
117: * @param request
118: * @param response
119: * @return
120: * @throws Exception
121: */
122: public ActionForward search(ActionMapping mapping, ActionForm form,
123: HttpServletRequest request, HttpServletResponse response)
124: throws Exception {
125: BalanceInquiryForm lookupForm = (BalanceInquiryForm) form;
126:
127: Lookupable lookupable = lookupForm.getLookupable();
128:
129: if (lookupable == null) {
130: LOG.error("Lookupable is null.");
131: throw new RuntimeException("Lookupable is null.");
132: }
133:
134: Collection displayList = new ArrayList();
135: List<ResultRow> resultTable = new ArrayList<ResultRow>();
136:
137: lookupable.validateSearchParameters(lookupForm.getFields());
138:
139: try {
140: displayList = lookupable.performLookup(lookupForm,
141: resultTable, true);
142:
143: Object[] resultTableAsArray = resultTable.toArray();
144:
145: CollectionIncomplete incompleteDisplayList = (CollectionIncomplete) displayList;
146: Long totalSize = ((CollectionIncomplete) displayList)
147: .getActualSizeIfTruncated();
148:
149: request
150: .setAttribute(
151: KFSConstants.REQUEST_SEARCH_RESULTS_SIZE,
152: totalSize);
153:
154: // TODO: use inheritance instead of this if statement
155: if (lookupable.getLookupableHelperService() instanceof AccountBalanceByConsolidationLookupableHelperServiceImpl) {
156:
157: Collection totalsTable = new ArrayList();
158:
159: int listIndex = 0;
160: int arrayIndex = 0;
161: int listSize = incompleteDisplayList.size();
162:
163: for (; listIndex < listSize;) {
164:
165: AccountBalance balance = (AccountBalance) incompleteDisplayList
166: .get(listIndex);
167:
168: boolean ok = ObjectHelper.isOneOf(balance
169: .getTitle(), getTotalTitles());
170: if (ok) {
171:
172: if (totalSize > 7) {
173: totalsTable
174: .add(resultTableAsArray[arrayIndex]);
175: }
176: resultTable
177: .remove(resultTableAsArray[arrayIndex]);
178:
179: incompleteDisplayList.remove(balance);
180: // account for the removal of the balance which resizes the list
181: listIndex--;
182: listSize--;
183:
184: }
185:
186: listIndex++;
187: arrayIndex++;
188:
189: }
190:
191: request.setAttribute(
192: KFSConstants.REQUEST_SEARCH_RESULTS,
193: resultTable);
194:
195: request.setAttribute(TOTALS_TABLE_KEY, totalsTable);
196: GlobalVariables.getUserSession().addObject(
197: TOTALS_TABLE_KEY, totalsTable);
198:
199: } else {
200:
201: request.setAttribute(
202: KFSConstants.REQUEST_SEARCH_RESULTS,
203: resultTable);
204:
205: }
206:
207: if (request
208: .getParameter(KFSConstants.SEARCH_LIST_REQUEST_KEY) != null) {
209: GlobalVariables
210: .getUserSession()
211: .removeObject(
212: request
213: .getParameter(KFSConstants.SEARCH_LIST_REQUEST_KEY));
214: }
215:
216: request.setAttribute(KFSConstants.SEARCH_LIST_REQUEST_KEY,
217: GlobalVariables.getUserSession().addObject(
218: resultTable));
219:
220: } catch (NumberFormatException e) {
221: GlobalVariables
222: .getErrorMap()
223: .putError(
224: KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR,
225: KFSKeyConstants.ERROR_CUSTOM,
226: new String[] { "Fiscal Year must be a four-digit number" });
227: } catch (Exception e) {
228: GlobalVariables.getErrorMap().putError(
229: KFSConstants.DOCUMENT_ERRORS,
230: KFSKeyConstants.ERROR_CUSTOM,
231: new String[] { "Please report the server error." });
232: LOG.error("Application Errors", e);
233: }
234: return mapping.findForward(KFSConstants.MAPPING_BASIC);
235: }
236:
237: /**
238: * Refresh - is called when one quickFinder returns to the previous one. Sets all the values and performs the new search.
239: *
240: * @see org.kuali.core.web.struts.action.KualiAction#refresh(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
241: */
242: @Override
243: public ActionForward refresh(ActionMapping mapping,
244: ActionForm form, HttpServletRequest request,
245: HttpServletResponse response) throws Exception {
246: LookupForm lookupForm = (LookupForm) form;
247: Lookupable lookupable = lookupForm.getLookupable();
248: if (lookupable == null) {
249: LOG.error("Lookupable is null.");
250: throw new RuntimeException("Lookupable is null.");
251: }
252:
253: Map fieldValues = new HashMap();
254: Map values = lookupForm.getFields();
255:
256: for (Iterator iter = lookupable.getRows().iterator(); iter
257: .hasNext();) {
258: Row row = (Row) iter.next();
259:
260: for (Iterator iterator = row.getFields().iterator(); iterator
261: .hasNext();) {
262: Field field = (Field) iterator.next();
263:
264: if (field.getPropertyName() != null
265: && !field.getPropertyName().equals("")) {
266: if (request.getParameter(field.getPropertyName()) != null) {
267: field.setPropertyValue(request
268: .getParameter(field.getPropertyName()));
269: } else if (values.get(field.getPropertyName()) != null) {
270: field.setPropertyValue(values.get(field
271: .getPropertyName()));
272: }
273: }
274: fieldValues.put(field.getPropertyName(), field
275: .getPropertyValue());
276: }
277: }
278: fieldValues.put(KFSConstants.DOC_FORM_KEY, lookupForm
279: .getFormKey());
280: fieldValues.put(KFSConstants.BACK_LOCATION, lookupForm
281: .getBackLocation());
282:
283: if (lookupable.checkForAdditionalFields(fieldValues)) {
284: for (Iterator iter = lookupable.getRows().iterator(); iter
285: .hasNext();) {
286: Row row = (Row) iter.next();
287: for (Iterator iterator = row.getFields().iterator(); iterator
288: .hasNext();) {
289: Field field = (Field) iterator.next();
290: if (field.getPropertyName() != null
291: && !field.getPropertyName().equals("")) {
292: if (request.getParameter(field
293: .getPropertyName()) != null) {
294: field.setPropertyValue(request
295: .getParameter(field
296: .getPropertyName()));
297: fieldValues.put(field.getPropertyName(),
298: request.getParameter(field
299: .getPropertyName()));
300: } else if (values.get(field.getPropertyName()) != null) {
301: field.setPropertyValue(values.get(field
302: .getPropertyName()));
303: }
304: }
305: }
306: }
307: }
308:
309: return mapping.findForward(KFSConstants.MAPPING_BASIC);
310: }
311:
312: /**
313: * Returns as if return with no value was selected.
314: *
315: * @param mapping
316: * @param form
317: * @param request
318: * @param response
319: * @return
320: * @throws Exception
321: */
322: public ActionForward cancel(ActionMapping mapping, ActionForm form,
323: HttpServletRequest request, HttpServletResponse response)
324: throws Exception {
325: LookupForm lookupForm = (LookupForm) form;
326:
327: String backUrl = lookupForm.getBackLocation()
328: + "?methodToCall=refresh&docFormKey="
329: + lookupForm.getFormKey();
330: return new ActionForward(backUrl, true);
331: }
332:
333: /**
334: * Clears the values of all the fields on the jsp.
335: *
336: * @param mapping
337: * @param form
338: * @param request
339: * @param response
340: * @return
341: * @throws IOException
342: * @throws ServletException
343: */
344: public ActionForward clearValues(ActionMapping mapping,
345: ActionForm form, HttpServletRequest request,
346: HttpServletResponse response) throws IOException,
347: ServletException {
348: LookupForm lookupForm = (LookupForm) form;
349: Lookupable lookupable = lookupForm.getLookupable();
350: if (lookupable == null) {
351: LOG.error("Lookupable is null.");
352: throw new RuntimeException("Lookupable is null.");
353: }
354:
355: for (Iterator iter = lookupable.getRows().iterator(); iter
356: .hasNext();) {
357: Row row = (Row) iter.next();
358: for (Iterator iterator = row.getFields().iterator(); iterator
359: .hasNext();) {
360: Field field = (Field) iterator.next();
361: if (!field.getFieldType().equals(Field.RADIO)) {
362: field.setPropertyValue(field.getDefaultValue());
363: }
364: }
365: }
366:
367: return mapping.findForward(KFSConstants.MAPPING_BASIC);
368: }
369:
370: /**
371: * View results from balance inquiry action
372: *
373: * @param mapping
374: * @param form
375: * @param request
376: * @param response
377: * @return
378: * @throws Exception
379: */
380: public ActionForward viewResults(ActionMapping mapping,
381: ActionForm form, HttpServletRequest request,
382: HttpServletResponse response) throws Exception {
383: request
384: .setAttribute(
385: KFSConstants.SEARCH_LIST_REQUEST_KEY,
386: request
387: .getParameter(KFSConstants.SEARCH_LIST_REQUEST_KEY));
388: request
389: .setAttribute(
390: KFSConstants.REQUEST_SEARCH_RESULTS,
391: GlobalVariables
392: .getUserSession()
393: .retrieveObject(
394: request
395: .getParameter(KFSConstants.SEARCH_LIST_REQUEST_KEY)));
396: request
397: .setAttribute(
398: KFSConstants.REQUEST_SEARCH_RESULTS_SIZE,
399: request
400: .getParameter(KFSConstants.REQUEST_SEARCH_RESULTS_SIZE));
401:
402: // TODO: use inheritance instead of this if statement
403: if (((BalanceInquiryForm) form).getLookupable()
404: .getLookupableHelperService() instanceof AccountBalanceByConsolidationLookupableHelperServiceImpl) {
405: Object totalsTable = GlobalVariables.getUserSession()
406: .retrieveObject(TOTALS_TABLE_KEY);
407: request.setAttribute(TOTALS_TABLE_KEY, totalsTable);
408: }
409:
410: return mapping.findForward(KFSConstants.MAPPING_BASIC);
411: }
412:
413: public void setKualiConfigurationService(
414: KualiConfigurationService kcs) {
415: kualiConfigurationService = kcs;
416: }
417:
418: }
|