001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.purap.web.struts.action;
017:
018: import javax.servlet.http.HttpServletRequest;
019: import javax.servlet.http.HttpServletResponse;
020:
021: import org.apache.commons.lang.StringUtils;
022: import org.apache.struts.action.ActionForm;
023: import org.apache.struts.action.ActionForward;
024: import org.apache.struts.action.ActionMapping;
025: import org.kuali.core.service.KualiRuleService;
026: import org.kuali.core.service.PersistenceService;
027: import org.kuali.core.util.ObjectUtils;
028: import org.kuali.core.web.struts.form.KualiDocumentFormBase;
029: import org.kuali.kfs.KFSConstants;
030: import org.kuali.kfs.KFSPropertyConstants;
031: import org.kuali.kfs.bo.AccountingLine;
032: import org.kuali.kfs.bo.SourceAccountingLine;
033: import org.kuali.kfs.context.SpringContext;
034: import org.kuali.kfs.rule.event.AddAccountingLineEvent;
035: import org.kuali.kfs.web.struts.action.KualiAccountingDocumentActionBase;
036: import org.kuali.kfs.web.struts.form.KualiAccountingDocumentFormBase;
037: import org.kuali.kfs.web.ui.AccountingLineDecorator;
038: import org.kuali.module.purap.PurapPropertyConstants;
039: import org.kuali.module.purap.bo.PurApAccountingLine;
040: import org.kuali.module.purap.bo.PurApItem;
041: import org.kuali.module.purap.document.PurchasingAccountsPayableDocument;
042: import org.kuali.module.purap.service.PurapAccountingService;
043: import org.kuali.module.purap.service.PurapService;
044: import org.kuali.module.purap.web.struts.form.PurchasingAccountsPayableFormBase;
045:
046: import edu.iu.uis.eden.exception.WorkflowException;
047:
048: /**
049: * Struts Action for Purchasing and Accounts Payable documents
050: */
051: public class PurchasingAccountsPayableActionBase extends
052: KualiAccountingDocumentActionBase {
053:
054: /**
055: * @see org.kuali.core.web.struts.action.KualiDocumentActionBase#loadDocument(org.kuali.core.web.struts.form.KualiDocumentFormBase)
056: */
057: @Override
058: protected void loadDocument(
059: KualiDocumentFormBase kualiDocumentFormBase)
060: throws WorkflowException {
061: super .loadDocument(kualiDocumentFormBase);
062: PurchasingAccountsPayableFormBase purapForm = (PurchasingAccountsPayableFormBase) kualiDocumentFormBase;
063: PurchasingAccountsPayableDocument document = (PurchasingAccountsPayableDocument) purapForm
064: .getDocument();
065:
066: // refresh the account summary (note this also updates the account amounts)
067: purapForm.refreshAccountSummmary();
068:
069: for (org.kuali.core.bo.Note note : (java.util.List<org.kuali.core.bo.Note>) document
070: .getDocumentBusinessObject().getBoNotes()) {
071: note.refreshReferenceObject("attachment");
072: }
073:
074: // sort the below the line
075: SpringContext.getBean(PurapService.class).sortBelowTheLine(
076: document);
077:
078: updateBaseline(
079: document,
080: (PurchasingAccountsPayableFormBase) kualiDocumentFormBase);
081: }
082:
083: /**
084: * Updates the baseline accounts on form and doc.
085: *
086: * @param document A descendant of PurchasingAccountsPayableDocument
087: */
088: private <T extends PurchasingAccountsPayableDocument, V extends KualiAccountingDocumentFormBase> void updateBaseline(
089: T document, V form) {
090: // clear out the old lines first
091: form.getBaselineSourceAccountingLines().clear();
092: for (PurApItem item : document.getItems()) {
093: // clear out the old lines first
094: item.getBaselineSourceAccountingLines().clear();
095:
096: for (PurApAccountingLine sourceAccount : item
097: .getSourceAccountingLines()) {
098: PurApAccountingLine baselineAccount = (PurApAccountingLine) ObjectUtils
099: .deepCopy(sourceAccount);
100: item.getBaselineSourceAccountingLines().add(
101: baselineAccount);
102: form.getBaselineSourceAccountingLines().add(
103: baselineAccount);
104: }
105: }
106: }
107:
108: /**
109: * Invokes a service method to refresh the account summary.
110: *
111: * @param mapping An ActionMapping
112: * @param form An ActionForm
113: * @param request The HttpServletRequest
114: * @param response The HttpServletResponse
115: * @throws Exception
116: * @return An ActionForward
117: */
118: public ActionForward refreshAccountSummary(ActionMapping mapping,
119: ActionForm form, HttpServletRequest request,
120: HttpServletResponse response) throws Exception {
121: PurchasingAccountsPayableFormBase purapForm = (PurchasingAccountsPayableFormBase) form;
122: PurchasingAccountsPayableDocument document = (PurchasingAccountsPayableDocument) purapForm
123: .getDocument();
124: SpringContext.getBean(PurapAccountingService.class)
125: .updateAccountAmounts(document);
126: purapForm.refreshAccountSummmary();
127: return mapping.findForward(KFSConstants.MAPPING_BASIC);
128: }
129:
130: /**
131: * @see org.kuali.kfs.web.struts.action.KualiAccountingDocumentActionBase#insertSourceLine(org.apache.struts.action.ActionMapping,
132: * org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
133: */
134: @Override
135: public ActionForward insertSourceLine(ActionMapping mapping,
136: ActionForm form, HttpServletRequest request,
137: HttpServletResponse response) throws Exception {
138: // It would be preferable to find a way to genericize the KualiAccountingDocument methods but this will work for now
139: PurchasingAccountsPayableFormBase purapForm = (PurchasingAccountsPayableFormBase) form;
140:
141: // index of item selected
142: int itemIndex = getSelectedLine(request);
143: PurApItem item = null;
144:
145: // if custom processing of an accounting line is not done then insert a line generically.
146: if (processCustomInsertAccountingLine(purapForm, request) == false) {
147:
148: item = (PurApItem) ((PurchasingAccountsPayableDocument) purapForm
149: .getDocument()).getItem((itemIndex));
150: PurApAccountingLine line = (PurApAccountingLine) ObjectUtils
151: .deepCopy(item.getNewSourceLine());
152:
153: String errorPrefix = KFSPropertyConstants.DOCUMENT + "."
154: + PurapPropertyConstants.ITEM + "["
155: + Integer.toString(itemIndex) + "]."
156: + KFSConstants.NEW_SOURCE_ACCT_LINE_PROPERTY_NAME;
157: boolean rulePassed = SpringContext.getBean(
158: KualiRuleService.class).applyRules(
159: new AddAccountingLineEvent(errorPrefix, purapForm
160: .getDocument(), (AccountingLine) line));
161:
162: if (rulePassed) {
163: // add accountingLine
164: SpringContext.getBean(PersistenceService.class)
165: .retrieveNonKeyFields(line);
166: insertAccountingLine(purapForm, item, line);
167:
168: // clear the temp account
169: item.resetAccount();
170: }
171: }
172:
173: return mapping.findForward(KFSConstants.MAPPING_BASIC);
174: }
175:
176: /**
177: * Insert the given Accounting Line in several appropriate places in the given item and given form.
178: *
179: * @param financialDocumentForm A form that inherits from PurchasingAccountsPaybleFormBase
180: * @param item A PurApItem
181: * @param line A PurApAccountingLine
182: */
183: protected void insertAccountingLine(
184: PurchasingAccountsPayableFormBase financialDocumentForm,
185: PurApItem item, PurApAccountingLine line) {
186: PurchasingAccountsPayableDocument preq = (PurchasingAccountsPayableDocument) financialDocumentForm
187: .getDocument();
188: // this decorator stuff should be moved out in parent class so we don't need to copy it here
189: // create and init a decorator
190: AccountingLineDecorator decorator = new AccountingLineDecorator();
191: decorator.setRevertible(false);
192:
193: // add it to the item
194: item.getSourceAccountingLines().add(line);
195: // add it to the baseline on item
196: item.getBaselineSourceAccountingLines().add(line);
197:
198: // add it to the baseline, to prevent generation of spurious update events
199: financialDocumentForm.getBaselineSourceAccountingLines().add(
200: line);
201:
202: // add the decorator
203: financialDocumentForm.getSourceLineDecorators().add(decorator);
204:
205: }
206:
207: /**
208: * Allows the custom processing of an accounting line during a call to insert source line. If a custom method for inserting an
209: * accounting line was performed, then a value of true must be returned.
210: *
211: * @param purapForm
212: * @param request
213: * @return boolean indicating if validation succeeded
214: */
215: public boolean processCustomInsertAccountingLine(
216: PurchasingAccountsPayableFormBase purapForm,
217: HttpServletRequest request) {
218: return false;
219: }
220:
221: /**
222: * Insert the given Accounting Line in several appropriate places in the given item and given form.
223: *
224: * @param financialDocumentForm A form that inherits from KualiAccountingDocumentFormBase
225: * @param item A PurApItem
226: * @param line A PurApAccountingLine
227: */
228: protected void insertAccountingLine(
229: KualiAccountingDocumentFormBase financialDocumentForm,
230: PurApItem item, PurApAccountingLine line) {
231: // this decorator stuff should be moved out in parent class so we don't need to copy it here
232: // create and init a decorator
233: AccountingLineDecorator decorator = new AccountingLineDecorator();
234: decorator.setRevertible(false);
235:
236: // add it to the item
237: item.getSourceAccountingLines().add(line);
238:
239: // add it to the baseline, to prevent generation of spurious update events
240: item.getBaselineSourceAccountingLines().add(line);
241:
242: // add the decorator
243: financialDocumentForm.getSourceLineDecorators().add(decorator);
244:
245: }
246:
247: /**
248: * @see org.kuali.kfs.web.struts.action.KualiAccountingDocumentActionBase#deleteSourceLine(org.apache.struts.action.ActionMapping,
249: * org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
250: */
251: @Override
252: public ActionForward deleteSourceLine(ActionMapping mapping,
253: ActionForm form, HttpServletRequest request,
254: HttpServletResponse response) throws Exception {
255: PurchasingAccountsPayableFormBase purapForm = (PurchasingAccountsPayableFormBase) form;
256:
257: String[] indexes = getSelectedLineForAccounts(request);
258: int itemIndex = Integer.parseInt(indexes[0]);
259: int accountIndex = Integer.parseInt(indexes[1]);
260:
261: PurApItem item = (PurApItem) ((PurchasingAccountsPayableDocument) purapForm
262: .getDocument()).getItem((itemIndex));
263: item.getSourceAccountingLines().remove(accountIndex);
264:
265: // add it to the baseline, to prevent generation of spurious update events
266: item.getBaselineSourceAccountingLines().remove(accountIndex);
267:
268: // remove the decorator
269: // financialDocumentForm.getSourceLineDecorators().remove(decorator);
270:
271: return mapping.findForward(KFSConstants.MAPPING_BASIC);
272: }
273:
274: /**
275: * @see org.kuali.kfs.web.struts.action.KualiAccountingDocumentActionBase#getSourceAccountingLine(org.apache.struts.action.ActionForm,
276: * javax.servlet.http.HttpServletRequest)
277: */
278: @Override
279: public SourceAccountingLine getSourceAccountingLine(
280: ActionForm form, HttpServletRequest request) {
281: String[] indexes = getSelectedLineForAccounts(request);
282: int itemIndex = Integer.parseInt(indexes[0]);
283: int accountIndex = Integer.parseInt(indexes[1]);
284: PurchasingAccountsPayableFormBase purchasingAccountsPayableForm = (PurchasingAccountsPayableFormBase) form;
285: SourceAccountingLine line;
286: if (itemIndex == -2) {
287: line = customAccountRetrieval(accountIndex,
288: purchasingAccountsPayableForm);
289: } else {
290: PurApItem item = (PurApItem) ((PurchasingAccountsPayableDocument) purchasingAccountsPayableForm
291: .getDocument()).getItem((itemIndex));
292: line = (SourceAccountingLine) ObjectUtils.deepCopy(item
293: .getSourceAccountingLines().get(accountIndex));
294: }
295: return line;
296: }
297:
298: /**
299: * Perform custom processing on accounting lines. See <code>getSelectedLineForAccounts</code>.
300: *
301: * @param accountIndex The index of the account into the request parameter
302: * @param purchasingAccountsPayableForm A form which inherits from PurchasingAccountsPayableFormBase
303: * @return A SourceAccountingLine
304: */
305: protected SourceAccountingLine customAccountRetrieval(
306: int accountIndex,
307: PurchasingAccountsPayableFormBase purchasingAccountsPayableForm) {
308: // default impl returns null
309: return null;
310: }
311:
312: /**
313: * Will return an array of Strings containing 2 indexes, the first String is the item index and the second String is the account
314: * index. These are obtained by parsing the method to call parameter from the request, between the word ".line" and "." The
315: * indexes are separated by a semicolon (:)
316: *
317: * @param request The HttpServletRequest
318: * @return An array of Strings containing pairs of two indices, an item index and a account index
319: */
320: protected String[] getSelectedLineForAccounts(
321: HttpServletRequest request) {
322: String accountString = new String();
323: String parameterName = (String) request
324: .getAttribute(KFSConstants.METHOD_TO_CALL_ATTRIBUTE);
325: if (StringUtils.isNotBlank(parameterName)) {
326: accountString = StringUtils.substringBetween(parameterName,
327: ".line", ".");
328: }
329: String[] result = StringUtils.split(accountString, ":");
330:
331: return result;
332: }
333:
334: /**
335: * @see org.kuali.core.web.struts.action.KualiDocumentActionBase#downloadBOAttachment(org.apache.struts.action.ActionMapping,
336: * org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
337: */
338: @Override
339: public ActionForward downloadBOAttachment(ActionMapping mapping,
340: ActionForm form, HttpServletRequest request,
341: HttpServletResponse response) throws Exception {
342: PurchasingAccountsPayableDocument document = (PurchasingAccountsPayableDocument) ((PurchasingAccountsPayableFormBase) form)
343: .getDocument();
344:
345: for (org.kuali.core.bo.Note note : (java.util.List<org.kuali.core.bo.Note>) document
346: .getDocumentBusinessObject().getBoNotes()) {
347: note.refreshReferenceObject("attachment");
348: }
349:
350: return super.downloadBOAttachment(mapping, form, request,
351: response);
352: }
353:
354: }
|