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.apache.commons.lang.StringUtils;
019    import org.kuali.kfs.module.endow.EndowConstants;
020    import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine;
021    import org.kuali.kfs.module.endow.document.EndowmentToGLTransferOfFundsDocument;
022    import org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocument;
023    import org.kuali.kfs.module.endow.document.service.EndowmentTransactionLinesDocumentService;
024    import org.kuali.kfs.sys.context.SpringContext;
025    import org.kuali.rice.kns.document.Document;
026    
027    public class EndowmentToGLTransferOfFundsDocumentRules extends EndowmentAccountingLinesDocumentBaseRules {
028    
029        /**
030         * @see org.kuali.kfs.module.endow.document.validation.impl.EndowmentTransactionLinesDocumentBaseRules#validateTransactionLine(org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocument,
031         *      org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine, int)
032         */
033        @Override
034        protected boolean validateTransactionLine(EndowmentTransactionLinesDocument endowmentTransactionLinesDocument, EndowmentTransactionLine line, int index) {
035            boolean isValid = super.validateTransactionLine(endowmentTransactionLinesDocument, line, index);
036            // Obtain Prefix for Error fields in UI.
037            String ERROR_PREFIX = getErrorPrefix(line, index);
038    
039            if (isValid) {
040    
041                // Is Etran code empty
042                if (isEndowmentTransactionCodeEmpty(line, ERROR_PREFIX))
043                    return false;
044    
045                // Validate ETran code
046                if (!validateEndowmentTransactionCode(line, ERROR_PREFIX))
047                    return false;
048    
049                // Validate ETran code as E or I
050                isValid &= validateEndowmentTransactionTypeCode(endowmentTransactionLinesDocument, line, ERROR_PREFIX);
051    
052                // Validate if a KEMID can have a principal transaction when IP indicator is P
053                if (!canKEMIDHaveAPrincipalTransaction(line, ERROR_PREFIX))
054                    return false;
055    
056                // Validate if the chart is matched between the KEMID and EtranCode
057                isValid &= validateChartMatch(line, ERROR_PREFIX);
058    
059                // Validate Amount is Greater than Zero.
060                isValid &= validateTransactionAmountGreaterThanZero(line, ERROR_PREFIX);
061    
062                // Set Corpus Indicator
063                line.setCorpusIndicator(SpringContext.getBean(EndowmentTransactionLinesDocumentService.class).getCorpusIndicatorValueforAnEndowmentTransactionLine(line.getKemid(), line.getEtranCode(), line.getTransactionIPIndicatorCode()));
064    
065                if (isValid) {
066                    if (EndowConstants.TRANSACTION_LINE_TYPE_SOURCE.equalsIgnoreCase(line.getTransactionLineTypeCode())) {
067                        checkWhetherReducePermanentlyRestrictedFund(line, ERROR_PREFIX);
068                        checkWhetherHaveSufficientFundsForCashBasedTransaction(line, ERROR_PREFIX);
069                    }
070                }
071            }
072    
073            return isValid;
074        }
075    
076        /**
077         * @see org.kuali.rice.kns.rules.DocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.Document)
078         */
079        @Override
080        protected boolean processCustomRouteDocumentBusinessRules(Document document) {
081            boolean isValid = super.processCustomRouteDocumentBusinessRules(document);
082            EndowmentToGLTransferOfFundsDocument transferOfFundsDocument = (EndowmentToGLTransferOfFundsDocument) document;
083    
084            // if security is not empty validate that security is valid
085            if (StringUtils.isNotBlank(transferOfFundsDocument.getSourceTransactionSecurity().getSecurityID())) {
086                // Validates Security Code.
087                if (!validateSecurityCode(transferOfFundsDocument, true))
088                    return false;
089    
090                // Checks if Security is Active
091                isValid &= isSecurityActive(transferOfFundsDocument, true);
092            }
093    
094            // if registration code is not empty validate that registration code are valid
095            if (StringUtils.isNotBlank(transferOfFundsDocument.getSourceTransactionSecurity().getRegistrationCode())) {
096                // Validate Registration code.
097                if (!validateRegistrationCode(transferOfFundsDocument, true))
098                    return false;
099    
100                // Checks if registration code is active
101                isValid &= isRegistrationCodeActive(transferOfFundsDocument, true);
102            }
103    
104            // validate the document has at least one source transaction line
105            if (!transactionLineSizeGreaterThanZero(transferOfFundsDocument, true))
106                return false;
107    
108            // validate that the document has at least one target accounting line
109            if (!validateAccountingLinesSizeGreaterThanZero(transferOfFundsDocument, false)) {
110                return false;
111            }
112    
113            return isValid;
114        }
115    
116    }