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.EndowConstants;
019    import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine;
020    import org.kuali.kfs.module.endow.document.CashTransferDocument;
021    import org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocument;
022    import org.kuali.kfs.sys.KFSKeyConstants;
023    import org.kuali.rice.kns.document.Document;
024    import org.kuali.rice.kns.util.GlobalVariables;
025    
026    public class CashTransferDocumentRules extends CashDocumentBaseRules {
027    
028        /**
029         * @see org.kuali.rice.kns.rules.DocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.Document)
030         */
031        @Override
032        public boolean processCustomRouteDocumentBusinessRules(Document document) {
033            boolean isValid = super.processCustomRouteDocumentBusinessRules(document);
034            isValid &= !GlobalVariables.getMessageMap().hasErrors();
035    
036            if (isValid) {
037                CashTransferDocument cashTransferDocument = (CashTransferDocument) document;
038    
039                // check if the total of the transaction amount (Income plus Principal) in the From transaction lines
040                // equals the total of the transaction amount in the To transaction lines.
041                if (!cashTransferDocument.getTargetTotalAmount().equals(cashTransferDocument.getSourceTotalAmount())) {
042                    GlobalVariables.getMessageMap().putError(EndowConstants.TRANSACTION_LINE_ERRORS, KFSKeyConstants.ERROR_DOCUMENT_BALANCE);
043                }
044    
045                // check must have one source tranaction line
046                if (!transactionLineSizeGreaterThanZero(cashTransferDocument, true))
047                    return false;
048    
049                // check must have one source tranaction line
050                if (!transactionLineSizeGreaterThanZero(cashTransferDocument, false))
051                    return false;
052    
053                // Checks if Security field is not empty, security code must be valid.
054                if (!isSecurityCodeEmpty(cashTransferDocument, true)) {
055                    if (!validateSecurityCode(cashTransferDocument, true))
056                        return false;
057                }
058    
059                for (int i = 0; i < cashTransferDocument.getSourceTransactionLines().size(); i++) {
060                    EndowmentTransactionLine sourceTransactionLine = cashTransferDocument.getSourceTransactionLines().get(i);
061                    isValid &= validateCashTransactionLine(cashTransferDocument, sourceTransactionLine, i);
062                }
063    
064                for (int i = 0; i < cashTransferDocument.getTargetTransactionLines().size(); i++) {
065                    EndowmentTransactionLine targetTransactionLine = cashTransferDocument.getTargetTransactionLines().get(i);
066                    isValid &= validateCashTransactionLine(cashTransferDocument, targetTransactionLine, i);
067                }
068    
069            }
070    
071            return isValid;
072        }
073    
074    
075        /**
076         * @see org.kuali.kfs.module.endow.document.validation.impl.CashDocumentBaseRules#validateCashTransactionLine(org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocumentBase,
077         *      org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine)
078         */
079        @Override
080        protected boolean validateCashTransactionLine(EndowmentTransactionLinesDocument document, EndowmentTransactionLine line, int index) {
081    
082            boolean isValid = super.validateCashTransactionLine(document, line, index);
083            isValid &= !GlobalVariables.getMessageMap().hasErrors();
084    
085            if (isValid) {
086                // Obtain Prefix for Error fields in UI.
087                String ERROR_PREFIX = getErrorPrefix(line, index);
088    
089                if (EndowConstants.TRANSACTION_LINE_TYPE_SOURCE.equalsIgnoreCase(line.getTransactionLineTypeCode())) {
090                    checkWhetherReducePermanentlyRestrictedFund(line, ERROR_PREFIX);
091                    checkWhetherHaveSufficientFundsForCashBasedTransaction(line, ERROR_PREFIX);
092                }
093            }
094            return GlobalVariables.getMessageMap().getErrorCount() == 0;
095    
096        }
097    
098    
099    }