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 java.util.Iterator;
019:
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022:
023: import org.apache.struts.action.ActionForm;
024: import org.apache.struts.action.ActionForward;
025: import org.apache.struts.action.ActionMapping;
026: import org.kuali.core.util.GlobalVariables;
027: import org.kuali.core.util.KualiDecimal;
028: import org.kuali.kfs.KFSConstants;
029: import org.kuali.kfs.KFSPropertyConstants;
030: import org.kuali.kfs.web.struts.action.KualiAccountingDocumentActionBase;
031: import org.kuali.module.financial.bo.AdvanceDepositDetail;
032: import org.kuali.module.financial.document.AdvanceDepositDocument;
033: import org.kuali.module.financial.rules.AdvanceDepositDocumentRuleUtil;
034: import org.kuali.module.financial.web.struts.form.AdvanceDepositForm;
035:
036: /**
037: * This is the action class for the Advance Deposit document.
038: */
039: public class AdvanceDepositAction extends
040: KualiAccountingDocumentActionBase {
041: /**
042: * Adds handling for advance deposit detail amount updates.
043: *
044: * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm,
045: * javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
046: */
047: public ActionForward execute(ActionMapping mapping,
048: ActionForm form, HttpServletRequest request,
049: HttpServletResponse response) throws Exception {
050: AdvanceDepositForm adForm = (AdvanceDepositForm) form;
051:
052: if (adForm.hasDocumentId()) {
053: AdvanceDepositDocument adDoc = adForm
054: .getAdvanceDepositDocument();
055:
056: adDoc
057: .setTotalAdvanceDepositAmount(calculateAdvanceDepositTotal(adDoc)); // recalc b/c changes to the amounts could
058: // have happened
059: }
060:
061: // proceed as usual
062: return super .execute(mapping, form, request, response);
063: }
064:
065: /**
066: * Adds a AdvanceDepositDetail instance created from the current "new advanceDeposit" line to the document
067: *
068: * @param mapping
069: * @param form
070: * @param request
071: * @param response
072: * @return ActionForward
073: * @throws Exception
074: */
075: public ActionForward addAdvanceDeposit(ActionMapping mapping,
076: ActionForm form, HttpServletRequest request,
077: HttpServletResponse response) throws Exception {
078: AdvanceDepositForm adForm = (AdvanceDepositForm) form;
079: AdvanceDepositDocument adDoc = adForm
080: .getAdvanceDepositDocument();
081:
082: AdvanceDepositDetail newAdvanceDeposit = adForm
083: .getNewAdvanceDeposit();
084: adDoc.prepareNewAdvanceDeposit(newAdvanceDeposit);
085:
086: // advanceDeposit business rules
087: boolean rulePassed = validateNewAdvanceDeposit(newAdvanceDeposit);
088: if (rulePassed) {
089: // add advanceDeposit
090: adDoc.addAdvanceDeposit(newAdvanceDeposit);
091:
092: // clear the used advanceDeposit
093: adForm.setNewAdvanceDeposit(new AdvanceDepositDetail());
094: }
095:
096: return mapping.findForward(KFSConstants.MAPPING_BASIC);
097: }
098:
099: /**
100: * Deletes the selected advanceDeposit (line) from the document
101: *
102: * @param mapping
103: * @param form
104: * @param request
105: * @param response
106: * @return ActionForward
107: * @throws Exception
108: */
109: public ActionForward deleteAdvanceDeposit(ActionMapping mapping,
110: ActionForm form, HttpServletRequest request,
111: HttpServletResponse response) throws Exception {
112: AdvanceDepositForm adForm = (AdvanceDepositForm) form;
113: AdvanceDepositDocument adDoc = adForm
114: .getAdvanceDepositDocument();
115:
116: int deleteIndex = getLineToDelete(request);
117: // delete advanceDeposit
118: adDoc.removeAdvanceDeposit(deleteIndex);
119:
120: return mapping.findForward(KFSConstants.MAPPING_BASIC);
121: }
122:
123: /**
124: * This method validates a new advance deposit detail record.
125: *
126: * @param advanceDeposit
127: * @return boolean
128: */
129: private boolean validateNewAdvanceDeposit(
130: AdvanceDepositDetail advanceDeposit) {
131: GlobalVariables.getErrorMap().addToErrorPath(
132: KFSPropertyConstants.NEW_ADVANCE_DEPOSIT);
133: boolean isValid = AdvanceDepositDocumentRuleUtil
134: .validateAdvanceDeposit(advanceDeposit);
135: GlobalVariables.getErrorMap().removeFromErrorPath(
136: KFSPropertyConstants.NEW_ADVANCE_DEPOSIT);
137: return isValid;
138: }
139:
140: /**
141: * Recalculates the advance deposit total since user could have changed it during their update.
142: *
143: * @param advanceDepositDocument
144: */
145: private KualiDecimal calculateAdvanceDepositTotal(
146: AdvanceDepositDocument advanceDepositDocument) {
147: KualiDecimal total = KualiDecimal.ZERO;
148: Iterator<AdvanceDepositDetail> deposits = advanceDepositDocument
149: .getAdvanceDeposits().iterator();
150: while (deposits.hasNext()) {
151: AdvanceDepositDetail deposit = deposits.next();
152: total = total.add(deposit
153: .getFinancialDocumentAdvanceDepositAmount());
154: }
155: return total;
156: }
157: }
|