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