001: /*
002: * Copyright 2006-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.financial.web.struts.action;
017:
018: import javax.servlet.http.HttpServletRequest;
019: import javax.servlet.http.HttpServletResponse;
020:
021: import org.apache.struts.action.ActionForm;
022: import org.apache.struts.action.ActionForward;
023: import org.apache.struts.action.ActionMapping;
024: import org.kuali.core.service.KualiRuleService;
025: import org.kuali.core.service.PersistenceService;
026: import org.kuali.core.util.TypedArrayList;
027: import org.kuali.kfs.KFSConstants;
028: import org.kuali.kfs.bo.AccountingLine;
029: import org.kuali.kfs.bo.TargetAccountingLine;
030: import org.kuali.kfs.context.SpringContext;
031: import org.kuali.kfs.document.AccountingDocument;
032: import org.kuali.kfs.rule.event.AddAccountingLineEvent;
033: import org.kuali.kfs.web.struts.action.KualiAccountingDocumentActionBase;
034: import org.kuali.kfs.web.struts.form.KualiAccountingDocumentFormBase;
035: import org.kuali.kfs.web.ui.AccountingLineDecorator;
036: import org.kuali.module.financial.bo.ProcurementCardTargetAccountingLine;
037: import org.kuali.module.financial.document.ProcurementCardDocument;
038: import org.kuali.module.financial.web.struts.form.ProcurementCardForm;
039:
040: /**
041: * This class handles specific Actions requests for the ProcurementCard.
042: */
043: public class ProcurementCardAction extends
044: KualiAccountingDocumentActionBase {
045: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
046: .getLogger(ProcurementCardAction.class);
047:
048: /**
049: * Override to accomodate multiple target lines.
050: *
051: * @param transForm
052: */
053: @Override
054: protected void processAccountingLineOverrides(
055: KualiAccountingDocumentFormBase transForm) {
056: ProcurementCardForm procurementCardForm = (ProcurementCardForm) transForm;
057:
058: processAccountingLineOverrides(procurementCardForm
059: .getNewSourceLine());
060: processAccountingLineOverrides(procurementCardForm
061: .getNewTargetLines());
062: if (procurementCardForm.hasDocumentId()) {
063: AccountingDocument financialDocument = (AccountingDocument) procurementCardForm
064: .getDocument();
065:
066: processAccountingLineOverrides(financialDocument
067: .getSourceAccountingLines());
068: processAccountingLineOverrides(financialDocument
069: .getTargetAccountingLines());
070: }
071: }
072:
073: /**
074: * Override to add the new accounting line to the correct transaction
075: *
076: * @see org.kuali.module.financial.web.struts.action.KualiFinancialDocumentActionBase#insertTargetLine(org.apache.struts.action.ActionMapping,
077: * org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
078: */
079: @Override
080: public ActionForward insertTargetLine(ActionMapping mapping,
081: ActionForm form, HttpServletRequest request,
082: HttpServletResponse response) throws Exception {
083: ProcurementCardForm procurementCardForm = (ProcurementCardForm) form;
084:
085: // get index of new target line
086: int newTargetIndex = super .getSelectedLine(request);
087:
088: ProcurementCardTargetAccountingLine line = (ProcurementCardTargetAccountingLine) procurementCardForm
089: .getNewTargetLines().get(newTargetIndex);
090:
091: // check any business rules
092: boolean rulePassed = SpringContext
093: .getBean(KualiRuleService.class)
094: .applyRules(
095: new AddAccountingLineEvent(
096: KFSConstants.NEW_TARGET_ACCT_LINES_PROPERTY_NAME
097: + "["
098: + Integer
099: .toString(newTargetIndex)
100: + "]", procurementCardForm
101: .getDocument(),
102: (AccountingLine) line));
103:
104: if (rulePassed) {
105: // add accountingLine
106: SpringContext.getBean(PersistenceService.class)
107: .retrieveNonKeyFields(line);
108: insertAccountingLine(false, procurementCardForm, line);
109:
110: // clear the used newTargetIndex
111: procurementCardForm.getNewTargetLines().set(newTargetIndex,
112: new ProcurementCardTargetAccountingLine());
113: }
114:
115: return mapping.findForward(KFSConstants.MAPPING_BASIC);
116: }
117:
118: /**
119: * Override to resync base accounting lines. New lines on the PCDO document can be inserted anywhere in the list, not necessary
120: * at the end.
121: *
122: * @see org.kuali.module.financial.web.struts.action.KualiFinancialDocumentActionBase#insertAccountingLine(boolean,
123: * org.kuali.module.financial.web.struts.form.KualiFinancialDocumentFormBase, org.kuali.core.bo.AccountingLine)
124: */
125: @Override
126: protected void insertAccountingLine(boolean isSource,
127: KualiAccountingDocumentFormBase financialDocumentForm,
128: AccountingLine line) {
129: AccountingDocument tdoc = financialDocumentForm
130: .getFinancialDocument();
131:
132: // create and init a decorator
133: AccountingLineDecorator decorator = new AccountingLineDecorator();
134: decorator.setRevertible(false);
135:
136: // add it to the document
137: tdoc.addTargetAccountingLine((TargetAccountingLine) line);
138:
139: // get the index of the inserted line
140: int newLineIndex = tdoc.getTargetAccountingLines()
141: .indexOf(line);
142:
143: // add it to the baseline, to prevent generation of spurious update events
144: financialDocumentForm.getBaselineTargetAccountingLines().add(
145: newLineIndex, line);
146:
147: // add the decorator
148: financialDocumentForm.getTargetLineDecorators().add(
149: newLineIndex, decorator);
150: }
151:
152: /**
153: * Override to remove the accounting line from the correct transaction
154: *
155: * @see org.kuali.module.financial.web.struts.action.KualiFinancialDocumentActionBase#deleteAccountingLine(boolean,
156: * org.kuali.module.financial.web.struts.form.KualiFinancialDocumentFormBase, int)
157: */
158: @Override
159: protected void deleteAccountingLine(boolean isSource,
160: KualiAccountingDocumentFormBase financialDocumentForm,
161: int deleteIndex) {
162: ProcurementCardDocument procurementCardDocument = (ProcurementCardDocument) financialDocumentForm
163: .getDocument();
164: procurementCardDocument.removeTargetAccountingLine(deleteIndex);
165:
166: // remove baseline duplicate and decorator
167: financialDocumentForm.getBaselineTargetAccountingLines()
168: .remove(deleteIndex);
169: financialDocumentForm.getTargetLineDecorators().remove(
170: deleteIndex);
171: }
172:
173: /**
174: * Ensures that ProcurementCardForm.newTargetLines is cleared. Otherwise works like super.reload.
175: *
176: * @see org.kuali.core.web.struts.action.KualiDocumentActionBase#reload(org.apache.struts.action.ActionMapping,
177: * org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
178: */
179: @Override
180: public ActionForward reload(ActionMapping mapping, ActionForm form,
181: HttpServletRequest request, HttpServletResponse response)
182: throws Exception {
183: ProcurementCardForm procurementCardForm = (ProcurementCardForm) form;
184: procurementCardForm.setNewTargetLines(new TypedArrayList(
185: ProcurementCardTargetAccountingLine.class));
186:
187: return super.reload(mapping, procurementCardForm, request,
188: response);
189: }
190: }
|