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.EndowKeyConstants;
021    import org.kuali.kfs.module.endow.EndowPropertyConstants;
022    import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine;
023    import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionSecurity;
024    import org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocument;
025    import org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocumentBase;
026    import org.kuali.kfs.module.endow.document.EndowmentTransactionalDocument;
027    import org.kuali.kfs.module.endow.document.service.EndowmentTransactionLinesDocumentService;
028    import org.kuali.kfs.sys.context.SpringContext;
029    import org.kuali.rice.kns.document.Document;
030    import org.kuali.rice.kns.util.GlobalVariables;
031    
032    public class CashDocumentBaseRules extends EndowmentTransactionLinesDocumentBaseRules {
033    
034        protected boolean isSecurityCodeEmpty(EndowmentTransactionalDocument document, boolean isSource) {
035            EndowmentTransactionSecurity tranSecurity = getEndowmentTransactionSecurity(document, isSource);
036    
037            if (StringUtils.isEmpty(tranSecurity.getSecurityID()))
038                return true;
039            else
040                return false;
041    
042        }
043    
044        /**
045         * @see org.kuali.kfs.module.endow.document.validation.impl.EndowmentTransactionLinesDocumentBaseRules#processAddTransactionLineRules(org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocument,
046         *      org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine)
047         */
048        @Override
049        public boolean processAddTransactionLineRules(EndowmentTransactionLinesDocument document, EndowmentTransactionLine line) {
050    
051            boolean isValid = super.processAddTransactionLineRules(document, line);
052            isValid &= !GlobalVariables.getMessageMap().hasErrors();
053    
054            if (isValid) {
055                isValid &= validateCashTransactionLine(document, line, -1);
056            }
057    
058            return isValid;
059    
060        }
061    
062        /**
063         * This method should be overridden by children rule classes as a hook to implement document specific business rule checks for
064         * the general validating a transaction line.
065         * 
066         * @param line
067         * @param index
068         * @return boolean True if the rules checks passed, false otherwise.
069         */
070        protected boolean validateCashTransactionLine(EndowmentTransactionLinesDocument document, EndowmentTransactionLine line, int index) {
071            boolean isValid = true;
072    
073            if (isValid) {
074                // Obtain Prefix for Error fields in UI.
075                String ERROR_PREFIX = getErrorPrefix(line, index);
076    
077                // Is Etran code empty
078                if (isEndowmentTransactionCodeEmpty(line, ERROR_PREFIX))
079                    return false;
080    
081                // Validate ETran code
082                if (!validateEndowmentTransactionCode(line, ERROR_PREFIX))
083                    return false;
084    
085                // Validate ETran code as E or I
086                isValid &= validateEndowmentTransactionTypeCode(document, line, ERROR_PREFIX);
087    
088                // Validate if a KEMID can have a principal transaction when IP indicator is P
089                if (!canKEMIDHaveAPrincipalTransaction(line, ERROR_PREFIX))
090                    return false;
091    
092                // Validate if the chart is matched between the KEMID and EtranCode
093                isValid &= validateChartMatch(line, ERROR_PREFIX);
094    
095                if (document.isErrorCorrectedDocument()) {
096                    // Validate Amount is Less than Zero.
097                    isValid &= validateTransactionAmountLessThanZero(line, ERROR_PREFIX);
098                }
099                else {
100                    // Validate Amount is Greater than Zero.
101                    isValid &= validateTransactionAmountGreaterThanZero(line, ERROR_PREFIX);
102                }
103    
104    
105                // Set Corpus Indicator
106                line.setCorpusIndicator(SpringContext.getBean(EndowmentTransactionLinesDocumentService.class).getCorpusIndicatorValueforAnEndowmentTransactionLine(line.getKemid(), line.getEtranCode(), line.getTransactionIPIndicatorCode()));
107            }
108    
109            return GlobalVariables.getMessageMap().getErrorCount() == 0;
110        }
111    
112        /**
113         * This method...
114         * @param endowmentTransactionLinesDocument
115         * @param line
116         * @param prefix
117         * @return
118         */
119        protected boolean validateEtranTypeBasedOnDocSource(EndowmentTransactionLinesDocument endowmentTransactionLinesDocument, EndowmentTransactionLine line, String prefix) {
120            // when created from batch allow ASSET etran type too
121            if (EndowConstants.TransactionSourceTypeCode.AUTOMATED.equalsIgnoreCase(endowmentTransactionLinesDocument.getTransactionSourceTypeCode())) {
122                if (line.getEtranCodeObj().getEndowmentTransactionTypeCode().equalsIgnoreCase(EndowConstants.EndowmentTransactionTypeCodes.INCOME_TYPE_CODE) || line.getEtranCodeObj().getEndowmentTransactionTypeCode().equalsIgnoreCase(EndowConstants.EndowmentTransactionTypeCodes.EXPENSE_TYPE_CODE) || line.getEtranCodeObj().getEndowmentTransactionTypeCode().equalsIgnoreCase(EndowConstants.EndowmentTransactionTypeCodes.ASSET_TYPE_CODE))
123                    return true;
124                else {
125                    putFieldError(prefix + EndowPropertyConstants.TRANSACTION_LINE_ENDOWMENT_TRANSACTION_CODE, EndowKeyConstants.EndowmentTransactionDocumentConstants.ERROR_ENDOWMENT_TRANSACTION_TYPE_CODE_VALIDITY_INCOME_EXPENSE_ASSET);
126                    return false;
127                }
128            }
129            else
130                return super.validateEndowmentTransactionTypeCode(endowmentTransactionLinesDocument, line, prefix);
131        }
132    }