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