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.module.endow.document.validation.impl;
017    
018    import org.kuali.kfs.module.endow.businessobject.EndowmentSourceTransactionLine;
019    import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine;
020    import org.kuali.kfs.module.endow.document.EndowmentTaxLotLinesDocumentBase;
021    import org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocument;
022    import org.kuali.kfs.module.endow.document.LiabilityDecreaseDocument;
023    import org.kuali.kfs.module.endow.document.service.LiabilityDocumentService;
024    import org.kuali.kfs.sys.context.SpringContext;
025    import org.kuali.rice.kns.document.Document;
026    import org.kuali.rice.kns.util.GlobalVariables;
027    
028    public class LiabilityDecreaseDocumentRules extends EndowmentTransactionLinesDocumentBaseRules {
029    
030    
031        /**
032         * @see org.kuali.kfs.module.endow.document.validation.AddTransactionLineRule#processAddTransactionLineRules(org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocument,
033         *      org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine)
034         */
035        @Override
036        public boolean processAddTransactionLineRules(EndowmentTransactionLinesDocument transLineDocument, EndowmentTransactionLine line) {
037            boolean isValid = true;
038    
039            String ERROR_PREFIX = getErrorPrefix(line, -1);
040    
041            isValid &= validateSecurity(isValid, (LiabilityDecreaseDocument) transLineDocument, true);
042    
043            isValid &= validateRegistration(isValid, (LiabilityDecreaseDocument) transLineDocument, true);
044    
045            if (isValid) {
046                isValid &= super.processAddTransactionLineRules(transLineDocument, line);
047            }
048    
049            if (!isValid)
050                return isValid;
051    
052            LiabilityDocumentService taxLotsService = SpringContext.getBean(LiabilityDocumentService.class);
053            LiabilityDecreaseDocument liabilityDecreaseDocument = (LiabilityDecreaseDocument) transLineDocument;
054            boolean isSource = line instanceof EndowmentSourceTransactionLine ? true : false;
055            taxLotsService.updateLiabilityDecreaseTransactionLineTaxLots(isSource, (EndowmentTaxLotLinesDocumentBase) liabilityDecreaseDocument, line);
056    
057            return GlobalVariables.getMessageMap().getErrorCount() == 0;
058        }
059    
060        /**
061         * This method validates the Tx line but from Liability Increase perspective.
062         * 
063         * @param endowmentTransactionLinesDocumentBase
064         * @param line
065         * @param index
066         * @return
067         */
068        @Override
069        protected boolean validateTransactionLine(EndowmentTransactionLinesDocument endowmentTransactionLinesDocument, EndowmentTransactionLine line, int index) {
070            boolean isValid = true;
071    
072            isValid &= super.validateTransactionLine(endowmentTransactionLinesDocument, line, index);
073    
074            if (isValid) {
075                // Obtain Prefix for Error fields in UI.
076                String ERROR_PREFIX = getErrorPrefix(line, index);
077    
078                // Ensure for cash Tx do not have a Etran.
079                isValid &= checkCashTransactionEndowmentCode(endowmentTransactionLinesDocument, line, ERROR_PREFIX);
080    
081                if (endowmentTransactionLinesDocument.isErrorCorrectedDocument()) {
082                    // Validate Amount is Less than Zero.
083                    isValid &= validateTransactionAmountLessThanZero(line, ERROR_PREFIX);
084    
085                    // Validate Units is Less than Zero.
086                    isValid &= validateTransactionUnitsLessThanZero(line, ERROR_PREFIX);
087                }
088                else {
089                    // Validate Amount is Greater than Zero.
090                    isValid &= validateTransactionAmountGreaterThanZero(line, ERROR_PREFIX);
091    
092                    // Validate Units is Greater than Zero.
093                    isValid &= validateTransactionUnitsGreaterThanZero(line, ERROR_PREFIX);
094                }
095    
096                // Validates Units & Amount are equal.
097                isValid &= validateTransactionUnitsAmountEqual(line, ERROR_PREFIX);
098            }
099    
100            return GlobalVariables.getMessageMap().getErrorCount() == 0;
101        }
102    
103        /**
104         * @see org.kuali.rice.kns.rules.DocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.Document)
105         */
106        @Override
107        protected boolean processCustomRouteDocumentBusinessRules(Document document) {
108            boolean isValid = super.processCustomRouteDocumentBusinessRules(document);
109            isValid &= !GlobalVariables.getMessageMap().hasErrors();
110    
111            LiabilityDecreaseDocument liabilityDecreaseDocument = (LiabilityDecreaseDocument) document;
112    
113            if (isValid) {
114                // Validate Security
115                isValid &= validateSecurity(isValid, liabilityDecreaseDocument, true);
116    
117                // Validate Registration code.
118                isValid &= validateRegistration(isValid, liabilityDecreaseDocument, true);
119    
120                if (!isValid)
121                    return isValid;
122    
123                // Empty out the Target Tx Line in weird case they got entered.
124                liabilityDecreaseDocument.getTargetTransactionLines().clear();
125    
126                // Validate atleast one Tx was entered.
127                if (!transactionLineSizeGreaterThanZero(liabilityDecreaseDocument, true))
128                    return false;
129    
130                isValid &= super.processCustomSaveDocumentBusinessRules(document);
131            }
132    
133            return GlobalVariables.getMessageMap().getErrorCount() == 0;
134        }
135    
136    }