1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.kuali.kfs.module.cam.document.web.struts;
20
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
33 import org.apache.commons.lang.StringUtils;
34 import org.apache.struts.action.ActionForm;
35 import org.apache.struts.action.ActionForward;
36 import org.apache.struts.action.ActionMapping;
37 import org.kuali.kfs.module.cam.CamsKeyConstants;
38 import org.kuali.kfs.module.cam.CamsPropertyConstants;
39 import org.kuali.kfs.module.cam.businessobject.Asset;
40 import org.kuali.kfs.module.cam.businessobject.AssetPaymentAssetDetail;
41 import org.kuali.kfs.module.cam.businessobject.AssetPaymentDetail;
42 import org.kuali.kfs.module.cam.document.AssetPaymentDocument;
43 import org.kuali.kfs.module.cam.document.service.AssetPaymentService;
44 import org.kuali.kfs.module.cam.document.validation.event.AssetPaymentAddAssetEvent;
45 import org.kuali.kfs.module.cam.document.validation.event.AssetPaymentAllocationEvent;
46 import org.kuali.kfs.module.cam.document.validation.event.AssetPaymentManuallyAddAccountingLineEvent;
47 import org.kuali.kfs.module.cam.document.validation.event.AssetPaymentPrepareRouteEvent;
48 import org.kuali.kfs.sys.KFSConstants;
49 import org.kuali.kfs.sys.KFSKeyConstants;
50 import org.kuali.kfs.sys.businessobject.SourceAccountingLine;
51 import org.kuali.kfs.sys.context.SpringContext;
52 import org.kuali.kfs.sys.document.validation.event.AddAccountingLineEvent;
53 import org.kuali.kfs.sys.service.SegmentedLookupResultsService;
54 import org.kuali.kfs.sys.web.struts.KualiAccountingDocumentActionBase;
55 import org.kuali.rice.kew.api.KewApiConstants;
56 import org.kuali.rice.kew.api.exception.WorkflowException;
57 import org.kuali.rice.kns.service.DataDictionaryService;
58 import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
59 import org.kuali.rice.krad.bo.PersistableBusinessObject;
60 import org.kuali.rice.krad.service.KualiRuleService;
61 import org.kuali.rice.krad.service.PersistenceService;
62 import org.kuali.rice.krad.util.GlobalVariables;
63 import org.kuali.rice.krad.util.ObjectUtils;
64
65
66
67
68 public class AssetPaymentAction extends KualiAccountingDocumentActionBase {
69 protected static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AssetPaymentAction.class);
70
71 @Override
72 protected void createDocument(KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
73 super.createDocument(kualiDocumentFormBase);
74 ((AssetPaymentDocument) kualiDocumentFormBase.getDocument()).setAssetPaymentAllocationTypeCode(CamsPropertyConstants.AssetPaymentAllocation.ASSET_DISTRIBUTION_DEFAULT_CODE);
75 }
76
77 @Override
78 public ActionForward docHandler(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
79 ActionForward actionForward = super.docHandler(mapping, form, request, response);
80 AssetPaymentForm assetPaymentForm = (AssetPaymentForm) form;
81 String command = assetPaymentForm.getCommand();
82
83
84
85 if (KewApiConstants.INITIATE_COMMAND.equals(command) && ((assetPaymentForm.getCapitalAssetNumber() != null) && !assetPaymentForm.getCapitalAssetNumber().trim().equals(""))) {
86 List<AssetPaymentAssetDetail> assetPaymentAssetDetails = assetPaymentForm.getAssetPaymentDocument().getAssetPaymentAssetDetail();
87
88 AssetPaymentAssetDetail assetPaymentAssetDetail = new AssetPaymentAssetDetail();
89 assetPaymentAssetDetail.setDocumentNumber(assetPaymentForm.getAssetPaymentDocument().getDocumentNumber());
90 assetPaymentAssetDetail.setCapitalAssetNumber(new Long(assetPaymentForm.getCapitalAssetNumber()));
91 assetPaymentAssetDetail.refreshReferenceObject(CamsPropertyConstants.AssetPaymentDocument.ASSET);
92 assetPaymentAssetDetail.setPreviousTotalCostAmount(assetPaymentAssetDetail.getAsset().getTotalCostAmount());
93
94 assetPaymentAssetDetails.add(assetPaymentAssetDetail);
95 assetPaymentForm.getAssetPaymentDocument().setAssetPaymentAssetDetail(assetPaymentAssetDetails);
96 assetPaymentForm.setCapitalAssetNumber("");
97 }
98 return actionForward;
99 }
100
101
102
103
104
105
106
107 @Override
108 public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
109 super.refresh(mapping, form, request, response);
110
111 AssetPaymentForm assetPaymentForm = (AssetPaymentForm) form;
112
113 Collection<PersistableBusinessObject> rawValues = null;
114 Map<String, Set<String>> segmentedSelection = new HashMap<String, Set<String>>();
115
116
117 if (StringUtils.equals(KFSConstants.MULTIPLE_VALUE, assetPaymentForm.getRefreshCaller())) {
118 String lookupResultsSequenceNumber = assetPaymentForm.getLookupResultsSequenceNumber();
119
120 if (StringUtils.isNotBlank(lookupResultsSequenceNumber)) {
121
122 Set<String> selectedIds = SpringContext.getBean(SegmentedLookupResultsService.class).retrieveSetOfSelectedObjectIds(lookupResultsSequenceNumber, GlobalVariables.getUserSession().getPerson().getPrincipalId());
123 for (String selectedId : selectedIds) {
124 String selectedObjId = StringUtils.substringBefore(selectedId, ".");
125 String selectedMonthData = StringUtils.substringAfter(selectedId, ".");
126
127 if (!segmentedSelection.containsKey(selectedObjId)) {
128 segmentedSelection.put(selectedObjId, new HashSet<String>());
129 }
130 segmentedSelection.get(selectedObjId).add(selectedMonthData);
131 }
132
133 if (LOG.isDebugEnabled()) {
134 LOG.debug("Asking segmentation service for object ids " + segmentedSelection.keySet());
135 }
136 rawValues = SpringContext.getBean(SegmentedLookupResultsService.class).retrieveSelectedResultBOs(lookupResultsSequenceNumber, segmentedSelection.keySet(), Asset.class, GlobalVariables.getUserSession().getPerson().getPrincipalId());
137 }
138
139 List<AssetPaymentAssetDetail> assetPaymentAssetDetails = assetPaymentForm.getAssetPaymentDocument().getAssetPaymentAssetDetail();
140 if (rawValues != null) {
141 for (PersistableBusinessObject bo : rawValues) {
142 Asset asset = (Asset) bo;
143
144 boolean addIt = true;
145 for (AssetPaymentAssetDetail detail : assetPaymentAssetDetails) {
146 if (detail.getCapitalAssetNumber().compareTo(asset.getCapitalAssetNumber()) == 0) {
147 addIt = false;
148 break;
149 }
150 }
151
152
153 if (addIt) {
154 AssetPaymentAssetDetail assetPaymentAssetDetail = new AssetPaymentAssetDetail();
155 assetPaymentAssetDetail.setDocumentNumber(assetPaymentForm.getAssetPaymentDocument().getDocumentNumber());
156 assetPaymentAssetDetail.setCapitalAssetNumber(asset.getCapitalAssetNumber());
157 assetPaymentAssetDetail.refreshReferenceObject(CamsPropertyConstants.AssetPaymentDocument.ASSET);
158 assetPaymentAssetDetail.setPreviousTotalCostAmount(assetPaymentAssetDetail.getAsset().getTotalCostAmount());
159
160 assetPaymentAssetDetails.add(assetPaymentAssetDetail);
161 }
162 }
163 assetPaymentForm.getAssetPaymentDocument().setAssetPaymentAssetDetail(assetPaymentAssetDetails);
164 }
165 }
166
167 validateAllocations(assetPaymentForm);
168
169 return mapping.findForward(KFSConstants.MAPPING_BASIC);
170 }
171
172
173
174
175
176 @Override
177 protected void uploadAccountingLines(boolean isSource, ActionForm form) throws FileNotFoundException, IOException {
178 AssetPaymentForm assetPaymentForm = (AssetPaymentForm) form;
179 super.uploadAccountingLines(isSource, assetPaymentForm);
180 List<AssetPaymentDetail> assetPaymentDetails = assetPaymentForm.getAssetPaymentDocument().getSourceAccountingLines();
181 for (AssetPaymentDetail assetPaymentDetail : assetPaymentDetails) {
182 getAssetPaymentService().extractPostedDatePeriod(assetPaymentDetail);
183 }
184 }
185
186
187
188
189
190
191
192 @Override
193 public ActionForward insertSourceLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
194 AssetPaymentForm assetPaymentForm = (AssetPaymentForm) form;
195 SourceAccountingLine line = assetPaymentForm.getNewSourceLine();
196
197
198
199
200
201 boolean rulePassed = true;
202
203
204
205 rulePassed &= getRuleService().applyRules(new AssetPaymentManuallyAddAccountingLineEvent(KFSConstants.NEW_SOURCE_ACCT_LINE_PROPERTY_NAME, assetPaymentForm.getDocument(), line));
206 if (rulePassed) {
207 rulePassed &= getRuleService().applyRules(new AddAccountingLineEvent(KFSConstants.NEW_SOURCE_ACCT_LINE_PROPERTY_NAME, assetPaymentForm.getDocument(), line));
208 }
209 if (rulePassed) {
210
211 SpringContext.getBean(PersistenceService.class).refreshAllNonUpdatingReferences(line);
212 insertAccountingLine(true, assetPaymentForm, line);
213
214
215 assetPaymentForm.setNewSourceLine(new AssetPaymentDetail());
216 }
217
218 validateAllocations(assetPaymentForm);
219
220 return mapping.findForward(KFSConstants.MAPPING_BASIC);
221 }
222
223
224
225
226
227
228
229
230
231
232
233 public ActionForward insertAssetPaymentAssetDetail(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
234 AssetPaymentForm assetPaymentForm = (AssetPaymentForm) form;
235 AssetPaymentDocument assetPaymentDocument = assetPaymentForm.getAssetPaymentDocument();
236
237 List<AssetPaymentAssetDetail> assetPaymentDetails = assetPaymentForm.getAssetPaymentDocument().getAssetPaymentAssetDetail();
238
239 AssetPaymentAssetDetail newAssetPaymentAssetDetail = new AssetPaymentAssetDetail();
240 String sCapitalAssetNumber = assetPaymentForm.getCapitalAssetNumber();
241
242 String errorPath = CamsPropertyConstants.Asset.CAPITAL_ASSET_NUMBER;
243
244
245 Long capitalAssetNumber = null;
246 try {
247 capitalAssetNumber = Long.parseLong(sCapitalAssetNumber);
248 } catch (NumberFormatException e) {
249
250 if (ObjectUtils.isNull(sCapitalAssetNumber) || StringUtils.isBlank(sCapitalAssetNumber)) {
251 String label = SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getBusinessObjectEntry(AssetPaymentAssetDetail.class.getName()).getAttributeDefinition(CamsPropertyConstants.Asset.CAPITAL_ASSET_NUMBER).getLabel();
252 GlobalVariables.getMessageMap().putError(errorPath, KFSKeyConstants.ERROR_REQUIRED, label);
253 } else {
254
255 GlobalVariables.getMessageMap().putError(errorPath, CamsKeyConstants.AssetLocationGlobal.ERROR_INVALID_CAPITAL_ASSET_NUMBER, sCapitalAssetNumber);
256 }
257 return mapping.findForward(KFSConstants.MAPPING_BASIC);
258 }
259
260 boolean rulePassed = true;
261
262 newAssetPaymentAssetDetail.setDocumentNumber(assetPaymentDocument.getDocumentNumber());
263 newAssetPaymentAssetDetail.setCapitalAssetNumber(capitalAssetNumber);
264 newAssetPaymentAssetDetail.refreshReferenceObject(CamsPropertyConstants.AssetPaymentDocument.ASSET);
265
266 rulePassed &= getRuleService().applyRules(new AssetPaymentAddAssetEvent(errorPath, assetPaymentForm.getDocument(), newAssetPaymentAssetDetail));
267 if (rulePassed) {
268
269 newAssetPaymentAssetDetail.setPreviousTotalCostAmount(newAssetPaymentAssetDetail.getAsset().getTotalCostAmount());
270
271 assetPaymentForm.getAssetPaymentDocument().addAssetPaymentAssetDetail(newAssetPaymentAssetDetail);
272 assetPaymentForm.setCapitalAssetNumber("");
273 }
274
275 validateAllocations(assetPaymentForm);
276
277 return mapping.findForward(KFSConstants.MAPPING_BASIC);
278 }
279
280
281
282
283
284
285
286
287
288
289
290 public ActionForward deleteAssetPaymentAssetDetail(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
291 AssetPaymentForm assetPaymentForm = (AssetPaymentForm) form;
292
293 int deleteIndex = getLineToDelete(request);
294
295
296 Long capitalAssetNumber = assetPaymentForm.getAssetPaymentDocument().getAssetPaymentAssetDetail().get(deleteIndex).getCapitalAssetNumber();
297
298
299 assetPaymentForm.getAssetPaymentDocument().getAssetPaymentAssetDetail().remove(deleteIndex);
300
301 validateAllocations(assetPaymentForm);
302
303 return mapping.findForward(KFSConstants.MAPPING_BASIC);
304 }
305
306
307
308
309
310
311
312
313
314
315
316 public ActionForward updateAssetPaymentAssetDetail(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
317 validateAllocations(form);
318 return mapping.findForward(KFSConstants.MAPPING_BASIC);
319 }
320
321
322
323
324
325
326 private boolean validateAllocations(ActionForm form) {
327 AssetPaymentForm assetPaymentForm = (AssetPaymentForm) form;
328 AssetPaymentDocument assetPaymentDocument = assetPaymentForm.getAssetPaymentDocument();
329
330 assetPaymentDocument.getAssetPaymentDistributor().applyDistributionsToDocument();
331 String errorPath = KFSConstants.DOCUMENT_PROPERTY_NAME;
332 return getRuleService().applyRules(new AssetPaymentAllocationEvent(errorPath, assetPaymentDocument));
333 }
334
335
336
337
338
339
340
341 @Override
342 public ActionForward route(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
343 AssetPaymentDocument assetPaymentDocument = ((AssetPaymentForm) form).getAssetPaymentDocument();
344 String errorPath = KFSConstants.DOCUMENT_PROPERTY_NAME;
345
346 boolean rulePassed = getRuleService().applyRules(new AssetPaymentPrepareRouteEvent(errorPath, assetPaymentDocument));
347 rulePassed &= validateAllocations(form);
348 if (rulePassed) {
349
350
351
352 return super.route(mapping, form, request, response);
353 } else {
354 return mapping.findForward(KFSConstants.MAPPING_BASIC);
355 }
356 }
357
358 @Override
359 public ActionForward blanketApprove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
360 AssetPaymentDocument assetPaymentDocument = ((AssetPaymentForm) form).getAssetPaymentDocument();
361 String errorPath = KFSConstants.DOCUMENT_PROPERTY_NAME;
362
363 boolean rulePassed = getRuleService().applyRules(new AssetPaymentPrepareRouteEvent(errorPath, assetPaymentDocument));
364 rulePassed &= validateAllocations(form);
365 if (rulePassed) {
366 return super.blanketApprove(mapping, form, request, response);
367 } else {
368 return mapping.findForward(KFSConstants.MAPPING_BASIC);
369 }
370 }
371
372 @Override
373 public ActionForward deleteSourceLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
374 validateAllocations(form);
375 return super.deleteSourceLine(mapping, form, request, response);
376 }
377
378
379
380
381
382
383
384
385
386
387
388
389 public ActionForward selectAllocationType(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
390 AssetPaymentDocument assetPaymentDocument = ((AssetPaymentForm) form).getAssetPaymentDocument();
391 assetPaymentDocument.getAssetPaymentDistributor().applyDistributionsToDocument();
392 return mapping.findForward(KFSConstants.MAPPING_BASIC);
393 }
394
395
396
397
398
399
400 protected AssetPaymentService getAssetPaymentService() {
401 return SpringContext.getBean(AssetPaymentService.class);
402 }
403
404
405
406
407
408
409 protected KualiRuleService getRuleService() {
410 return SpringContext.getBean(KualiRuleService.class);
411 }
412
413 }