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.module.ar.web.struts;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  
29  import javax.servlet.ServletException;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  import org.apache.struts.action.ActionForm;
34  import org.apache.struts.action.ActionForward;
35  import org.apache.struts.action.ActionMapping;
36  import org.kuali.kfs.module.ar.businessobject.lookup.CustomerAgingReportLookupableHelperServiceImpl;
37  import org.kuali.kfs.sys.KFSConstants;
38  import org.kuali.kfs.sys.KFSKeyConstants;
39  import org.kuali.kfs.sys.KFSPropertyConstants;
40  import org.kuali.rice.kns.lookup.Lookupable;
41  import org.kuali.rice.kns.lookup.LookupableHelperService;
42  import org.kuali.rice.kns.web.struts.action.KualiAction;
43  import org.kuali.rice.kns.web.struts.form.LookupForm;
44  import org.kuali.rice.kns.web.ui.Field;
45  import org.kuali.rice.kns.web.ui.ResultRow;
46  import org.kuali.rice.kns.web.ui.Row;
47  import org.kuali.rice.krad.lookup.CollectionIncomplete;
48  import org.kuali.rice.krad.util.GlobalVariables;
49  import org.kuali.rice.krad.util.ObjectUtils;
50  
51  
52  /**
53   * This class handles Actions for lookup flow for CustomerAgingReport.
54   */
55  
56  public class CustomerAgingReportAction extends KualiAction {
57      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CustomerAgingReportAction.class);
58  
59      private static final String TOTALS_TABLE_KEY = "totalsTable";
60  
61      public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
62          return mapping.findForward(KFSConstants.MAPPING_BASIC);
63      }
64  
65      /**
66       * Search - sets the values of the data entered on the form on the jsp into a map and then searches for the results.
67       *
68       * @param mapping
69       * @param form
70       * @param request
71       * @param response
72       * @return
73       * @throws Exception
74       *
75       * KRAD Conversion: Lookupable performs customization of the results.
76       *
77       * Fields are in data dictionary.
78       */
79     public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
80          CustomerAgingReportForm lookupForm = (CustomerAgingReportForm) form;
81  
82          Lookupable lookupable = lookupForm.getLookupable();
83  
84          if (lookupable == null) {
85              LOG.error("Lookupable is null.");
86              throw new RuntimeException("Lookupable is null.");
87          }
88  
89          LookupableHelperService lookupablehelper =  lookupable.getLookupableHelperService();
90          Collection displayList = new ArrayList();
91          List<ResultRow> resultTable = new ArrayList<ResultRow>();
92  
93          try {
94              displayList = lookupable.performLookup(lookupForm, resultTable, true);
95  
96              Object[] resultTableAsArray = resultTable.toArray();
97  
98  
99              CollectionIncomplete incompleteDisplayList = (CollectionIncomplete) displayList;
100             Long totalSize = ((CollectionIncomplete) displayList).getActualSizeIfTruncated();
101 
102             request.setAttribute(KFSConstants.REQUEST_SEARCH_RESULTS_SIZE, totalSize);
103 
104             request.setAttribute(KFSConstants.REQUEST_SEARCH_RESULTS, resultTable);
105 
106             String resultsKey = request.getParameter(KFSConstants.SEARCH_LIST_REQUEST_KEY);
107             if (resultsKey != null) {
108                 GlobalVariables.getUserSession().removeObject(resultsKey);
109                 GlobalVariables.getUserSession().removeObject(resultsKey+".form");
110             }
111 
112             resultsKey = GlobalVariables.getUserSession().addObjectWithGeneratedKey(resultTable);
113             GlobalVariables.getUserSession().addObject(resultsKey+".form", form);
114             request.setAttribute(KFSConstants.SEARCH_LIST_REQUEST_KEY, resultsKey);
115         }
116         catch (NumberFormatException e) {
117             GlobalVariables.getMessageMap().putError(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, KFSKeyConstants.ERROR_CUSTOM, new String[] { "Fiscal Year must be a four-digit number" });
118         }
119         return mapping.findForward(KFSConstants.MAPPING_BASIC);
120     }
121 
122     /**
123      * Refresh - is called when one quickFinder returns to the previous one. Sets all the values and performs the new search.
124      *
125      * @see org.kuali.rice.kns.web.struts.action.KualiAction#refresh(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
126      *
127      * KRAD Conversion: Lookupable performs customization of the fields and check for additional fields.
128      *
129      * Data dictionary is used to retrieve the field properties.
130      */
131     @Override
132     public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
133         LookupForm lookupForm = (LookupForm) form;
134         Lookupable lookupable = lookupForm.getLookupable();
135         if (ObjectUtils.isNull(lookupable)) {
136             LOG.error("Lookupable is null.");
137             throw new RuntimeException("Lookupable is null.");
138         }
139 
140         Map fieldValues = new HashMap();
141         Map values = lookupForm.getFields();
142 
143         for (Iterator iter = lookupable.getRows().iterator(); iter.hasNext();) {
144             Row row = (Row) iter.next();
145 
146             for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) {
147                 Field field = (Field) iterator.next();
148 
149                 if (field.getPropertyName() != null && !field.getPropertyName().equals("")) {
150                     if (request.getParameter(field.getPropertyName()) != null) {
151                         field.setPropertyValue(request.getParameter(field.getPropertyName()));
152                     }
153                     else if (values.get(field.getPropertyName()) != null) {
154                         field.setPropertyValue(values.get(field.getPropertyName()));
155                     }
156                 }
157                 fieldValues.put(field.getPropertyName(), field.getPropertyValue());
158             }
159         }
160         fieldValues.put(KFSConstants.DOC_FORM_KEY, lookupForm.getFormKey());
161         fieldValues.put(KFSConstants.BACK_LOCATION, lookupForm.getBackLocation());
162 
163         if (lookupable.checkForAdditionalFields(fieldValues)) {
164             for (Iterator iter = lookupable.getRows().iterator(); iter.hasNext();) {
165                 Row row = (Row) iter.next();
166                 for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) {
167                     Field field = (Field) iterator.next();
168                     if (field.getPropertyName() != null && !field.getPropertyName().equals("")) {
169                         if (request.getParameter(field.getPropertyName()) != null) {
170                             field.setPropertyValue(request.getParameter(field.getPropertyName()));
171                             fieldValues.put(field.getPropertyName(), request.getParameter(field.getPropertyName()));
172                         }
173                         else if (values.get(field.getPropertyName()) != null) {
174                             field.setPropertyValue(values.get(field.getPropertyName()));
175                         }
176                     }
177                 }
178             }
179         }
180 
181         return mapping.findForward(KFSConstants.MAPPING_BASIC);
182     }
183 
184     /**
185      * Cancels the action and returns to portal main page.
186      *
187      * @param mapping
188      * @param form
189      * @param request
190      * @param response
191      * @return
192      * @throws Exception
193      */
194     public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
195         return mapping.findForward(KFSConstants.MAPPING_CANCEL);
196     }
197 
198 
199     /**
200      * Clears the values of all the fields on the jsp.
201      *
202      * @param mapping
203      * @param form
204      * @param request
205      * @param response
206      * @return
207      * @throws IOException
208      * @throws ServletException
209      *
210      * KRAD Conversion: Lookupable performs customization of the fields.
211      *
212      * Data dictionary is used to retrieve the field properties.
213      */
214     public ActionForward clearValues(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
215         LookupForm lookupForm = (LookupForm) form;
216         Lookupable lookupable = lookupForm.getLookupable();
217         if (ObjectUtils.isNull(lookupable)) {
218             LOG.error("Lookupable is null.");
219             throw new RuntimeException("Lookupable is null.");
220         }
221 
222         for (Iterator iter = lookupable.getRows().iterator(); iter.hasNext();) {
223             Row row = (Row) iter.next();
224             for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) {
225                 Field field = (Field) iterator.next();
226                 if (!field.getFieldType().equals(Field.RADIO)) {
227                     field.setPropertyValue(field.getDefaultValue());
228                 }
229             }
230         }
231 
232         return mapping.findForward(KFSConstants.MAPPING_BASIC);
233     }
234 
235     /**
236      * View results from balance inquiry action
237      *
238      * @param mapping
239      * @param form
240      * @param request
241      * @param response
242      * @return
243      * @throws Exception
244      */
245  public ActionForward viewResults(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
246         CustomerAgingReportForm customerAgeForm = (CustomerAgingReportForm) form;
247         String resultKey = request.getParameter(KFSConstants.SEARCH_LIST_REQUEST_KEY);
248         request.setAttribute(KFSConstants.SEARCH_LIST_REQUEST_KEY, resultKey);
249         request.setAttribute(KFSConstants.REQUEST_SEARCH_RESULTS, GlobalVariables.getUserSession().retrieveObject(resultKey));
250         CustomerAgingReportForm prevForm = (CustomerAgingReportForm) GlobalVariables.getUserSession().retrieveObject(resultKey+".form");
251         customerAgeForm.setTotal0to30(prevForm.getTotal0to30());
252         customerAgeForm.setTotal31to60(prevForm.getTotal31to60());
253         customerAgeForm.setTotal61to90(prevForm.getTotal61to90());
254         customerAgeForm.setTotal91toSYSPR(prevForm.getTotal91toSYSPR());
255         customerAgeForm.setTotalSYSPRplus1orMore(prevForm.getTotalSYSPRplus1orMore());
256         request.setAttribute(KFSConstants.REQUEST_SEARCH_RESULTS_SIZE, request.getParameter(KFSConstants.REQUEST_SEARCH_RESULTS_SIZE));
257 
258         // TODO: use inheritance instead of this if statement
259         if (customerAgeForm.getLookupable().getLookupableHelperService() instanceof CustomerAgingReportLookupableHelperServiceImpl) {
260             Object totalsTable = GlobalVariables.getUserSession().retrieveObject(TOTALS_TABLE_KEY);
261             request.setAttribute(TOTALS_TABLE_KEY, totalsTable);
262         }
263 
264         return mapping.findForward(KFSConstants.MAPPING_BASIC);
265     }
266 
267 }