1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.gl.web.struts;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Properties;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32 import org.kuali.ole.gl.GeneralLedgerConstants;
33 import org.kuali.ole.gl.ObjectHelper;
34 import org.kuali.ole.gl.businessobject.AccountBalance;
35 import org.kuali.ole.gl.businessobject.lookup.AccountBalanceByConsolidationLookupableHelperServiceImpl;
36 import org.kuali.ole.sys.OLEConstants;
37 import org.kuali.ole.sys.OLEKeyConstants;
38 import org.kuali.ole.sys.OLEPropertyConstants;
39 import org.kuali.ole.sys.context.SpringContext;
40 import org.kuali.rice.core.api.config.property.ConfigurationService;
41 import org.kuali.rice.kns.lookup.LookupResultsService;
42 import org.kuali.rice.kns.lookup.Lookupable;
43 import org.kuali.rice.kns.web.struts.action.KualiMultipleValueLookupAction;
44 import org.kuali.rice.kns.web.struts.form.MultipleValueLookupForm;
45 import org.kuali.rice.kns.web.ui.Column;
46 import org.kuali.rice.kns.web.ui.ResultRow;
47 import org.kuali.rice.krad.lookup.CollectionIncomplete;
48 import org.kuali.rice.krad.service.SequenceAccessorService;
49 import org.kuali.rice.krad.util.GlobalVariables;
50 import org.kuali.rice.krad.util.KRADConstants;
51 import org.kuali.rice.krad.util.UrlFactory;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public class BalanceInquiryLookupAction extends KualiMultipleValueLookupAction {
67 private static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(BalanceInquiryLookupAction.class);
68
69 private static final String TOTALS_TABLE_KEY = "totalsTable";
70
71
72
73
74
75
76 public static final int DEFAULT_MAX_ROWS_PER_PAGE = 50;
77
78 private ConfigurationService kualiConfigurationService;
79 private String[] totalTitles;
80
81 public BalanceInquiryLookupAction() {
82 super();
83 kualiConfigurationService = SpringContext.getBean(ConfigurationService.class);
84 }
85
86 private void setTotalTitles() {
87 totalTitles = new String[7];
88
89 totalTitles[0] = kualiConfigurationService.getPropertyValueAsString(OLEKeyConstants.AccountBalanceService.INCOME);
90 totalTitles[1] = kualiConfigurationService.getPropertyValueAsString(OLEKeyConstants.AccountBalanceService.INCOME_FROM_TRANSFERS);
91 totalTitles[2] = kualiConfigurationService.getPropertyValueAsString(OLEKeyConstants.AccountBalanceService.INCOME_TOTAL);
92 totalTitles[3] = kualiConfigurationService.getPropertyValueAsString(OLEKeyConstants.AccountBalanceService.EXPENSE);
93 totalTitles[4] = kualiConfigurationService.getPropertyValueAsString(OLEKeyConstants.AccountBalanceService.EXPENSE_FROM_TRANSFERS);
94 totalTitles[5] = kualiConfigurationService.getPropertyValueAsString(OLEKeyConstants.AccountBalanceService.EXPENSE_TOTAL);
95 totalTitles[6] = kualiConfigurationService.getPropertyValueAsString(OLEKeyConstants.AccountBalanceService.TOTAL);
96
97 }
98
99 private String[] getTotalTitles() {
100 if (null == totalTitles) {
101 setTotalTitles();
102 }
103
104 return totalTitles;
105 }
106
107
108
109
110
111
112
113
114
115
116 @Override
117 public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
118 BalanceInquiryLookupForm lookupForm = (BalanceInquiryLookupForm) form;
119 Lookupable lookupable = lookupForm.getLookupable();
120
121 if (lookupable == null) {
122 LOG.error("Lookupable is null.");
123 throw new RuntimeException("Lookupable is null.");
124 }
125
126 Collection displayList = new ArrayList();
127 CollectionIncomplete incompleteDisplayList;
128 List<ResultRow> resultTable = new ArrayList<ResultRow>();
129 Long totalSize;
130 boolean bounded = true;
131
132 lookupable.validateSearchParameters(lookupForm.getFields());
133
134 displayList = performMultipleValueLookup(lookupForm, resultTable, getMaxRowsPerPage(lookupForm), bounded);
135 incompleteDisplayList = (CollectionIncomplete) displayList;
136 totalSize = incompleteDisplayList.getActualSizeIfTruncated();
137
138 if (lookupable.isSearchUsingOnlyPrimaryKeyValues()) {
139 lookupForm.setSearchUsingOnlyPrimaryKeyValues(true);
140 lookupForm.setPrimaryKeyFieldLabels(lookupable.getPrimaryKeyFieldLabels());
141 }
142 else {
143 lookupForm.setSearchUsingOnlyPrimaryKeyValues(false);
144 lookupForm.setPrimaryKeyFieldLabels(OLEConstants.EMPTY_STRING);
145 }
146
147
148
149 if (lookupable.getLookupableHelperService() instanceof AccountBalanceByConsolidationLookupableHelperServiceImpl) {
150 Object[] resultTableAsArray = resultTable.toArray();
151 Collection totalsTable = new ArrayList();
152
153 int arrayIndex = 0;
154
155 try {
156 for (int listIndex = 0; listIndex < incompleteDisplayList.size(); listIndex++) {
157 AccountBalance balance = (AccountBalance) incompleteDisplayList.get(listIndex);
158 boolean ok = ObjectHelper.isOneOf(balance.getTitle(), getTotalTitles());
159 if (ok) {
160 if (totalSize > 7) {
161 totalsTable.add(resultTableAsArray[arrayIndex]);
162 }
163 resultTable.remove(resultTableAsArray[arrayIndex]);
164 incompleteDisplayList.remove(balance);
165 }
166 arrayIndex++;
167 }
168
169 request.setAttribute(TOTALS_TABLE_KEY, totalsTable);
170 GlobalVariables.getUserSession().addObject(TOTALS_TABLE_KEY, totalsTable);
171 }
172 catch (NumberFormatException e) {
173 GlobalVariables.getMessageMap().putError(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, OLEKeyConstants.ERROR_CUSTOM, new String[] { "Fiscal Year must be a four-digit number" });
174 }
175 catch (Exception e) {
176 GlobalVariables.getMessageMap().putError(OLEConstants.DOCUMENT_ERRORS, OLEKeyConstants.ERROR_CUSTOM, new String[] { "Please report the server error." });
177 LOG.error("Application Errors", e);
178 }
179 }
180
181 request.setAttribute(OLEConstants.REQUEST_SEARCH_RESULTS_SIZE, totalSize);
182 request.setAttribute(OLEConstants.REQUEST_SEARCH_RESULTS, resultTable);
183 lookupForm.setResultsActualSize((int) totalSize.longValue());
184 lookupForm.setResultsLimitedSize(resultTable.size());
185
186 if (lookupForm.isSegmented()) {
187 LOG.debug("I'm segmented");
188 request.setAttribute(GeneralLedgerConstants.LookupableBeanKeys.SEGMENTED_LOOKUP_FLAG_NAME, Boolean.TRUE);
189 }
190
191 if (request.getParameter(OLEConstants.SEARCH_LIST_REQUEST_KEY) != null) {
192 GlobalVariables.getUserSession().removeObject(request.getParameter(OLEConstants.SEARCH_LIST_REQUEST_KEY));
193 request.setAttribute(OLEConstants.SEARCH_LIST_REQUEST_KEY, GlobalVariables.getUserSession().addObjectWithGeneratedKey(resultTable));
194 }
195
196 return mapping.findForward(OLEConstants.MAPPING_BASIC);
197 }
198
199
200
201
202
203
204
205
206
207
208
209 @Override
210 public ActionForward prepareToReturnNone(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
211 MultipleValueLookupForm multipleValueLookupForm = (MultipleValueLookupForm) form;
212 prepareToReturnNone(multipleValueLookupForm);
213
214
215 Properties parameters = new Properties();
216 parameters.put(OLEConstants.DOC_FORM_KEY, multipleValueLookupForm.getFormKey());
217 parameters.put(OLEConstants.DISPATCH_REQUEST_PARAMETER, OLEConstants.RETURN_METHOD_TO_CALL);
218 parameters.put(OLEConstants.REFRESH_CALLER, OLEConstants.MULTIPLE_VALUE);
219 parameters.put(OLEConstants.ANCHOR, multipleValueLookupForm.getLookupAnchor());
220
221 String backUrl = UrlFactory.parameterizeUrl(multipleValueLookupForm.getBackLocation(), parameters);
222 return new ActionForward(backUrl, true);
223 }
224
225
226
227
228
229
230
231
232
233
234
235 @Override
236 public ActionForward prepareToReturnSelectedResults(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
237 MultipleValueLookupForm multipleValueLookupForm = (MultipleValueLookupForm) form;
238 if (StringUtils.isBlank(multipleValueLookupForm.getLookupResultsSequenceNumber())) {
239
240 return prepareToReturnNone(mapping, form, request, response);
241 }
242
243 prepareToReturnSelectedResultBOs(multipleValueLookupForm);
244
245
246 Properties parameters = new Properties();
247 parameters.put(OLEConstants.LOOKUP_RESULTS_BO_CLASS_NAME, multipleValueLookupForm.getBusinessObjectClassName());
248 parameters.put(OLEConstants.LOOKUP_RESULTS_SEQUENCE_NUMBER, multipleValueLookupForm.getLookupResultsSequenceNumber());
249 parameters.put(OLEConstants.DOC_FORM_KEY, multipleValueLookupForm.getFormKey());
250 parameters.put(OLEConstants.DISPATCH_REQUEST_PARAMETER, OLEConstants.RETURN_METHOD_TO_CALL);
251 parameters.put(OLEConstants.REFRESH_CALLER, OLEConstants.MULTIPLE_VALUE);
252 parameters.put(OLEConstants.ANCHOR, multipleValueLookupForm.getLookupAnchor());
253 String backUrl = UrlFactory.parameterizeUrl(multipleValueLookupForm.getBackLocation(), parameters);
254 return new ActionForward(backUrl, true);
255 }
256
257
258
259
260
261 @Override
262 public ActionForward sort(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
263 request.setAttribute(GeneralLedgerConstants.LookupableBeanKeys.SEGMENTED_LOOKUP_FLAG_NAME, Boolean.TRUE);
264 return super.sort(mapping, form, request, response);
265 }
266
267
268
269
270
271 @Override
272 public ActionForward selectAll(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
273 request.setAttribute(GeneralLedgerConstants.LookupableBeanKeys.SEGMENTED_LOOKUP_FLAG_NAME, Boolean.TRUE);
274 return super.selectAll(mapping, form, request, response);
275 }
276
277
278
279
280
281 @Override
282 public ActionForward unselectAll(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
283 request.setAttribute(GeneralLedgerConstants.LookupableBeanKeys.SEGMENTED_LOOKUP_FLAG_NAME, Boolean.TRUE);
284 return super.unselectAll(mapping, form, request, response);
285 }
286
287
288
289
290
291 @Override
292 public ActionForward switchToPage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
293 request.setAttribute(GeneralLedgerConstants.LookupableBeanKeys.SEGMENTED_LOOKUP_FLAG_NAME, Boolean.TRUE);
294 return super.switchToPage(mapping, form, request, response);
295 }
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312 @Override
313 protected Collection performMultipleValueLookup(MultipleValueLookupForm multipleValueLookupForm, List<ResultRow> resultTable, int maxRowsPerPage, boolean bounded) {
314 Lookupable lookupable = multipleValueLookupForm.getLookupable();
315 Collection displayList = lookupable.performLookup(multipleValueLookupForm, resultTable, bounded);
316
317 List defaultSortColumns = lookupable.getDefaultSortColumns();
318 if (defaultSortColumns != null && !defaultSortColumns.isEmpty() && resultTable != null && !resultTable.isEmpty()) {
319
320 String firstSortColumn = (String) defaultSortColumns.get(0);
321
322
323
324 int firstSortColumnIdx = -1;
325 List<Column> columnsForFirstResultRow = resultTable.get(0).getColumns();
326 for (int i = 0; i < columnsForFirstResultRow.size(); i++) {
327 if (StringUtils.equals(firstSortColumn, columnsForFirstResultRow.get(i).getPropertyName())) {
328 firstSortColumnIdx = i;
329 break;
330 }
331 }
332 multipleValueLookupForm.setColumnToSortIndex(firstSortColumnIdx);
333 }
334 else {
335
336 multipleValueLookupForm.setColumnToSortIndex(-1);
337 }
338
339
340 multipleValueLookupForm.jumpToFirstPage(resultTable.size(), maxRowsPerPage);
341
342 SequenceAccessorService sequenceAccessorService = SpringContext.getBean(SequenceAccessorService.class);
343 String lookupResultsSequenceNumber = String.valueOf(sequenceAccessorService.getNextAvailableSequenceNumber(KRADConstants.LOOKUP_RESULTS_SEQUENCE));
344 multipleValueLookupForm.setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
345 try {
346 LookupResultsService lookupResultsService = SpringContext.getBean(LookupResultsService.class);
347 lookupResultsService.persistResultsTable(lookupResultsSequenceNumber, resultTable, GlobalVariables.getUserSession().getPerson().getPrincipalId());
348 }
349 catch (Exception e) {
350 LOG.error("error occured trying to persist multiple lookup results", e);
351 throw new RuntimeException("error occured trying to persist multiple lookup results");
352 }
353
354
355 multipleValueLookupForm.setCompositeObjectIdMap(new HashMap<String, String>());
356
357 return displayList;
358 }
359
360
361
362
363
364
365
366
367
368 @Override
369 protected List<ResultRow> selectAll(MultipleValueLookupForm multipleValueLookupForm, int maxRowsPerPage) {
370 List<ResultRow> resultTable = null;
371 try {
372 LookupResultsService lookupResultsService = SpringContext.getBean(LookupResultsService.class);
373 String lookupResultsSequenceNumber = multipleValueLookupForm.getLookupResultsSequenceNumber();
374
375 resultTable = lookupResultsService.retrieveResultsTable(lookupResultsSequenceNumber, GlobalVariables.getUserSession().getPerson().getPrincipalId());
376 }
377 catch (Exception e) {
378 LOG.error("error occured trying to export multiple lookup results", e);
379 throw new RuntimeException("error occured trying to export multiple lookup results");
380 }
381
382 Map<String, String> selectedObjectIds = this.getSelectedObjectIds(multipleValueLookupForm, resultTable);
383
384 multipleValueLookupForm.jumpToPage(multipleValueLookupForm.getViewedPageNumber(), resultTable.size(), maxRowsPerPage);
385 multipleValueLookupForm.setColumnToSortIndex(Integer.parseInt(multipleValueLookupForm.getPreviouslySortedColumnIndex()));
386 multipleValueLookupForm.setCompositeObjectIdMap(selectedObjectIds);
387
388 return resultTable;
389 }
390
391
392
393
394
395
396
397
398
399
400
401
402 private Map<String, String> getSelectedObjectIds(MultipleValueLookupForm multipleValueLookupForm, List<ResultRow> resultTable) {
403 String businessObjectClassName = multipleValueLookupForm.getBusinessObjectClassName();
404
405
406
407
408
409
410 throw new RuntimeException("Fail to create an object of " + businessObjectClassName);
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431 }
432 }
433