1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.fp.document.web.struts;
17
18 import java.util.ArrayList;
19
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.struts.action.ActionForm;
25 import org.apache.struts.action.ActionForward;
26 import org.apache.struts.action.ActionMapping;
27 import org.kuali.ole.fp.businessobject.ProcurementCardTargetAccountingLine;
28 import org.kuali.ole.fp.businessobject.ProcurementCardTransactionDetail;
29 import org.kuali.ole.fp.document.ProcurementCardDocument;
30 import org.kuali.ole.sys.OLEConstants;
31 import org.kuali.ole.sys.OLEKeyConstants;
32 import org.kuali.ole.sys.businessobject.AccountingLine;
33 import org.kuali.ole.sys.businessobject.TargetAccountingLine;
34 import org.kuali.ole.sys.context.SpringContext;
35 import org.kuali.ole.sys.document.AccountingDocument;
36 import org.kuali.ole.sys.document.validation.event.AddAccountingLineEvent;
37 import org.kuali.ole.sys.document.validation.event.DeleteAccountingLineEvent;
38 import org.kuali.ole.sys.web.struts.KualiAccountingDocumentFormBase;
39 import org.kuali.rice.core.api.util.type.KualiDecimal;
40 import org.kuali.rice.kew.api.exception.WorkflowException;
41 import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
42 import org.kuali.rice.krad.service.KualiRuleService;
43 import org.kuali.rice.krad.service.PersistenceService;
44 import org.kuali.rice.krad.util.GlobalVariables;
45 import org.kuali.rice.krad.util.KRADConstants;
46
47
48
49
50 public class ProcurementCardAction extends CapitalAccountingLinesActionBase {
51 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProcurementCardAction.class);
52
53 @Override
54 protected void loadDocument(KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
55 super.loadDocument(kualiDocumentFormBase);
56 ProcurementCardDocument procureCardDocument = (ProcurementCardDocument)kualiDocumentFormBase.getDocument();
57 int transactionsCount = procureCardDocument.getTransactionEntries().size();
58 ProcurementCardForm procurementCardForm = (ProcurementCardForm)kualiDocumentFormBase;
59 procurementCardForm.buildNewTargetAccountingLines(transactionsCount);
60 }
61
62
63
64
65
66
67 @Override
68 protected void processAccountingLineOverrides(KualiAccountingDocumentFormBase transForm) {
69 ProcurementCardForm procurementCardForm = (ProcurementCardForm) transForm;
70
71 processAccountingLineOverrides(procurementCardForm.getNewSourceLine());
72 processAccountingLineOverrides(procurementCardForm.getNewTargetLines());
73 if (procurementCardForm.hasDocumentId()) {
74 AccountingDocument financialDocument = (AccountingDocument) procurementCardForm.getDocument();
75
76 processAccountingLineOverrides(financialDocument.getSourceAccountingLines());
77 processAccountingLineOverrides(financialDocument.getTargetAccountingLines());
78 }
79 }
80
81
82
83
84
85
86
87 @Override
88 public ActionForward insertTargetLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
89 ProcurementCardForm procurementCardForm = (ProcurementCardForm) form;
90 ProcurementCardDocument procurementCardDocument = (ProcurementCardDocument) procurementCardForm.getDocument();
91
92 int targetContainerIndex = this.getSelectedContainer(request);
93 ProcurementCardTargetAccountingLine line = (ProcurementCardTargetAccountingLine)procurementCardForm.getNewTargetLines().get(targetContainerIndex);
94
95 ProcurementCardTransactionDetail transactionDetail = (ProcurementCardTransactionDetail) procurementCardDocument.getTransactionEntries().get(targetContainerIndex);
96 line.setFinancialDocumentTransactionLineNumber(transactionDetail.getFinancialDocumentTransactionLineNumber());
97
98
99 boolean rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new AddAccountingLineEvent(OLEConstants.NEW_TARGET_ACCT_LINES_PROPERTY_NAME + "[" + Integer.toString(targetContainerIndex) + "]", procurementCardForm.getDocument(), (AccountingLine) line));
100
101 if (rulePassed) {
102
103 SpringContext.getBean(PersistenceService.class).retrieveNonKeyFields(line);
104 insertAccountingLine(false, procurementCardForm, line);
105
106 ProcurementCardTargetAccountingLine newLine = new ProcurementCardTargetAccountingLine();
107 newLine.setTransactionContainerIndex(targetContainerIndex);
108
109 procurementCardForm.getNewTargetLines().set(targetContainerIndex, newLine);
110 }
111
112 return mapping.findForward(OLEConstants.MAPPING_BASIC);
113 }
114
115
116
117
118
119 @Override
120 public ActionForward performBalanceInquiryForTargetLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
121 ProcurementCardForm procurementCardForm = (ProcurementCardForm) form;
122 ProcurementCardDocument procurementCardDocument = (ProcurementCardDocument) procurementCardForm.getDocument();
123
124 int targetContainerIndex = this.getSelectedContainer(request);
125 ProcurementCardTransactionDetail ProcurementCardTransactionDetail = (ProcurementCardTransactionDetail) procurementCardDocument.getTransactionEntries().get(targetContainerIndex);
126
127 int targetIndex = super.getSelectedLine(request);
128 TargetAccountingLine targetLine = (ProcurementCardTargetAccountingLine) ProcurementCardTransactionDetail.getTargetAccountingLines().get(targetIndex);
129
130 return performBalanceInquiryForAccountingLine(mapping, form, request, targetLine);
131 }
132
133
134
135
136
137
138
139
140 @Override
141 protected void insertAccountingLine(boolean isSource, KualiAccountingDocumentFormBase financialDocumentForm, AccountingLine line) {
142 AccountingDocument tdoc = financialDocumentForm.getFinancialDocument();
143
144
145 tdoc.addTargetAccountingLine((TargetAccountingLine) line);
146 }
147
148
149
150
151
152
153 @Override
154 public ActionForward deleteTargetLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
155 int targetContainerIndex = this.getSelectedContainer(request);
156 int targetIndex = this.getSelectedLine(request);
157
158 KualiAccountingDocumentFormBase financialDocumentForm = (KualiAccountingDocumentFormBase) form;
159
160 String errorPath = OLEConstants.DOCUMENT_PROPERTY_NAME + "." + OLEConstants.EXISTING_TARGET_ACCT_LINE_PROPERTY_NAME + "[" + targetIndex + "]";
161 boolean rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new DeleteAccountingLineEvent(errorPath, financialDocumentForm.getDocument(), ((AccountingDocument) financialDocumentForm.getDocument()).getTargetAccountingLine(targetIndex), false));
162
163
164 if (rulePassed) {
165 deleteAccountingLineFromTransactionContainer(financialDocumentForm, targetContainerIndex, targetIndex);
166 }
167 else {
168 String[] errorParams = new String[] { "target", Integer.toString(targetIndex + 1) };
169 GlobalVariables.getMessageMap().putError(errorPath, OLEKeyConstants.ERROR_ACCOUNTINGLINE_DELETERULE_INVALIDACCOUNT, errorParams);
170 }
171
172 return mapping.findForward(OLEConstants.MAPPING_BASIC);
173 }
174
175
176
177
178
179
180
181 @Override
182 protected void deleteAccountingLine(boolean isSource, KualiAccountingDocumentFormBase financialDocumentForm, int deleteIndex) {
183 ProcurementCardDocument procurementCardDocument = (ProcurementCardDocument) financialDocumentForm.getDocument();
184 procurementCardDocument.removeTargetAccountingLine(deleteIndex);
185 }
186
187
188
189
190
191
192
193 @Override
194 public ActionForward reload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
195 ProcurementCardForm procurementCardForm = (ProcurementCardForm) form;
196 procurementCardForm.setNewTargetLines(new ArrayList<ProcurementCardTargetAccountingLine>());
197
198 return super.reload(mapping, procurementCardForm, request, response);
199 }
200
201
202 protected int getSelectedContainer(HttpServletRequest request) {
203 int selectedContainer = -1;
204 String parameterName = (String) request.getAttribute(KRADConstants.METHOD_TO_CALL_ATTRIBUTE);
205 if (StringUtils.isNotBlank(parameterName)) {
206 String lineNumber = StringUtils.substringBetween(parameterName, ".transactionEntries[", "].");
207 selectedContainer = Integer.parseInt(lineNumber);
208 }
209
210 return selectedContainer;
211 }
212
213
214
215
216
217
218 protected void deleteAccountingLineFromTransactionContainer(KualiAccountingDocumentFormBase financialDocumentForm, int targetContainerIndex, int targetIndex) {
219 ProcurementCardDocument procurementCardDocument = (ProcurementCardDocument) financialDocumentForm.getDocument();
220 ProcurementCardTransactionDetail procurementCardTransactionDetail = (ProcurementCardTransactionDetail) procurementCardDocument.getTransactionEntries().get(targetContainerIndex);
221 procurementCardTransactionDetail.getTargetAccountingLines().remove(targetIndex);
222 }
223 }