<commit_before>/*
 * Copyright 2010 The Kuali Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.kfs.module.endow.document.validation.impl;

import org.apache.commons.lang.StringUtils;
import org.kuali.kfs.module.endow.EndowConstants;
import org.kuali.kfs.module.endow.EndowKeyConstants;
import org.kuali.kfs.module.endow.EndowPropertyConstants;
import org.kuali.kfs.module.endow.businessobject.EndowmentSourceTransactionLine;
import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionCode;
import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine;
import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionSecurity;
import org.kuali.kfs.module.endow.businessobject.KEMID;
import org.kuali.kfs.module.endow.businessobject.Security;
import org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocument;
import org.kuali.kfs.module.endow.document.EndowmentTransactionalDocument;
import org.kuali.kfs.module.endow.document.service.EndowmentTransactionCodeService;
import org.kuali.kfs.module.endow.document.service.KEMIDService;
import org.kuali.kfs.module.endow.document.service.SecurityService;
import org.kuali.kfs.module.endow.document.service.EndowmentTransactionLinesDocumentService;
import org.kuali.kfs.module.endow.document.validation.AddTransactionLineRule;
import org.kuali.kfs.module.endow.document.validation.DeleteTransactionLineRule;
import org.kuali.kfs.sys.KFSConstants;
import org.kuali.kfs.sys.context.SpringContext;
import org.kuali.rice.kns.bo.BusinessObject;
import org.kuali.rice.kns.document.Document;
import org.kuali.rice.kns.service.DictionaryValidationService;
import org.kuali.rice.kns.util.AbstractKualiDecimal;
import org.kuali.rice.kns.util.GlobalVariables;
import org.kuali.rice.kns.util.MessageMap;

public class EndowmentTransactionLinesDocumentBaseRules extends EndowmentTransactionalDocumentBaseRule implements AddTransactionLineRule<EndowmentTransactionLinesDocument, EndowmentTransactionLine>, DeleteTransactionLineRule<EndowmentTransactionLinesDocument, EndowmentTransactionLine> {

    /**
     * @see org.kuali.kfs.module.endow.document.validation.DeleteTransactionLineRule#processDeleteTransactionLineRules(org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocument, org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine)
     */
    public boolean processDeleteTransactionLineRules(EndowmentTransactionLinesDocument EndowmentTransactionLinesDocument, EndowmentTransactionLine EndowmentTransactionLine) {
        // TODO Auto-generated method stub
        return true;
    }

    /**
     * @see org.kuali.kfs.module.endow.document.validation.AddTransactionLineRule#processAddTransactionLineRules(org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocument, org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine)
     */
    public boolean processAddTransactionLineRules(EndowmentTransactionLinesDocument transLine, EndowmentTransactionLine line) 
    {
        boolean isValid = true; 
        isValid &= !GlobalVariables.getMessageMap().hasErrors(); 
        
        //Obtain Prefix for Error fields in UI.
        String ERROR_PREFIX = null;
        if( line instanceof EndowmentSourceTransactionLine)
            ERROR_PREFIX = EndowPropertyConstants.SOURCE_TRANSACTION_LINE_PREFIX;
        else
            ERROR_PREFIX = EndowPropertyConstants.TARGET_TRANSACTION_LINE_PREFIX;            
            
        if(isValid)
        {
            //Is Income or Principal drop down value selected.
            if(!SpringContext.getBean(DictionaryValidationService.class).isBusinessObjectValid(line, null))
                return false;    
            
            //Validate KemID
            if(!validateKemId(line,ERROR_PREFIX))
                return false;
         
            //Active Kemid
            isValid &= isActiveKemId(line,ERROR_PREFIX);
            
            //Validate ETran code
            if(!validateEndowmentTransactionCode(line,ERROR_PREFIX))
                return false;

            //Refresh all references for the given KemId
            //line.getKemidObj().refreshNonUpdateableReferences();

            //Validate ETran code as E or I
            isValid &= validateEndowmentTransactionTypeCode(line,ERROR_PREFIX);
            
            //Validate no restriction transaction restriction
            isValid &= validateNoTransactionRestriction(line,ERROR_PREFIX);
            
            //Validate Greater then Zero(thus positive) value
            isValid &= validateTransactionAmountGreaterThanZero(line,ERROR_PREFIX);
            
            //Validate if a KEMID can have a principal transaction when IP indicator is P
            isValid &= canKEMIDHaveAPrincipalTransaction(line,ERROR_PREFIX);
        } 
        
        return GlobalVariables.getMessageMap().getErrorCount() == 0;
    }

    /**
     * This method validates the KEMID code. 
     * 
     * @param tranSecurity
     * @return
     */
    protected boolean isKemIdCodeEmpty(EndowmentTransactionLine line, String prefix)
    {
        if( StringUtils.isEmpty(line.getKemid()) )
        {
            putFieldError(prefix + EndowPropertyConstants.KEMID, EndowKeyConstants.EndowmentTransactionDocumentConstants.ERROR_TRANSACTION_LINE_KEMID_REQUIRED);
            return true;        
        }
        
        return false;
    }
    
    /**
     * This method validates the KemId code and tries to create a KEMID object from the code.
     * 
     * @param line<commit_msg>KULENDOW 296: KEM Liability Increase Transaction. Called Bonnie's Rule Impl for General Rules 9 & 11.<commit_after>/*
 * Copyright 2010 The Kuali Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.kfs.module.endow.document.validation.impl;

import org.apache.commons.lang.StringUtils;
import org.kuali.kfs.module.endow.EndowConstants;
import org.kuali.kfs.module.endow.EndowKeyConstants;
import org.kuali.kfs.module.endow.EndowPropertyConstants;
import org.kuali.kfs.module.endow.businessobject.EndowmentSourceTransactionLine;
import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionCode;
import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine;
import org.kuali.kfs.module.endow.businessobject.EndowmentTransactionSecurity;
import org.kuali.kfs.module.endow.businessobject.KEMID;
import org.kuali.kfs.module.endow.businessobject.Security;
import org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocument;
import org.kuali.kfs.module.endow.document.EndowmentTransactionalDocument;
import org.kuali.kfs.module.endow.document.service.EndowmentTransactionCodeService;
import org.kuali.kfs.module.endow.document.service.EndowmentTransactionDocumentService;
import org.kuali.kfs.module.endow.document.service.EndowmentTransactionLinesDocumentService;
import org.kuali.kfs.module.endow.document.service.KEMIDService;
import org.kuali.kfs.module.endow.document.service.SecurityService;
import org.kuali.kfs.module.endow.document.service.EndowmentTransactionLinesDocumentService;
import org.kuali.kfs.module.endow.document.validation.AddTransactionLineRule;
import org.kuali.kfs.module.endow.document.validation.DeleteTransactionLineRule;
import org.kuali.kfs.sys.KFSConstants;
import org.kuali.kfs.sys.context.SpringContext;
import org.kuali.rice.kns.bo.BusinessObject;
import org.kuali.rice.kns.document.Document;
import org.kuali.rice.kns.service.DictionaryValidationService;
import org.kuali.rice.kns.util.AbstractKualiDecimal;
import org.kuali.rice.kns.util.GlobalVariables;
import org.kuali.rice.kns.util.MessageMap;

public class EndowmentTransactionLinesDocumentBaseRules extends EndowmentTransactionalDocumentBaseRule implements AddTransactionLineRule<EndowmentTransactionLinesDocument, EndowmentTransactionLine>, DeleteTransactionLineRule<EndowmentTransactionLinesDocument, EndowmentTransactionLine> {

    /**
     * @see org.kuali.kfs.module.endow.document.validation.DeleteTransactionLineRule#processDeleteTransactionLineRules(org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocument, org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine)
     */
    public boolean processDeleteTransactionLineRules(EndowmentTransactionLinesDocument EndowmentTransactionLinesDocument, EndowmentTransactionLine EndowmentTransactionLine) {
        // TODO Auto-generated method stub
        return true;
    }

    /**
     * @see org.kuali.kfs.module.endow.document.validation.AddTransactionLineRule#processAddTransactionLineRules(org.kuali.kfs.module.endow.document.EndowmentTransactionLinesDocument, org.kuali.kfs.module.endow.businessobject.EndowmentTransactionLine)
     */
    public boolean processAddTransactionLineRules(EndowmentTransactionLinesDocument transLine, EndowmentTransactionLine line) 
    {
        boolean isValid = true; 
        isValid &= !GlobalVariables.getMessageMap().hasErrors(); 
        
        //Obtain Prefix for Error fields in UI.
        String ERROR_PREFIX = null;
        if( line instanceof EndowmentSourceTransactionLine)
            ERROR_PREFIX = EndowPropertyConstants.SOURCE_TRANSACTION_LINE_PREFIX;
        else
            ERROR_PREFIX = EndowPropertyConstants.TARGET_TRANSACTION_LINE_PREFIX;            
            
        if(isValid)
        {
            //General not null validation for KemID, Etran and Income/Principal DropDown.
            if(!SpringContext.getBean(DictionaryValidationService.class).isBusinessObjectValid(line, null))
                return false;    
            
            //Validate KemID
            if(!validateKemId(line,ERROR_PREFIX))
                return false;
         
            //Active Kemid
            isValid &= isActiveKemId(line,ERROR_PREFIX);
            
            //Validate no restriction transaction restriction
            isValid &= validateNoTransactionRestriction(line,ERROR_PREFIX);
            
            //Validate ETran code
            if(!validateEndowmentTransactionCode(line,ERROR_PREFIX))
                return false;

            //Validate GL object Code
            isValid &=  SpringContext.getBean(EndowmentTransactionDocumentService.class).matchChartBetweenKEMIDAndETranCode(line.getKemid(), line.getEtranCode(), line.getTransactionIPIndicatorCode());
            
            //Set Corpus Indicator  
            line.setCorpusIndicator(SpringContext.getBean(EndowmentTransactionLinesDocumentService.class).getCorpusIndicatorValueforAnEndowmentTransactionLine(line.getKemid(), line.getEtranCode(), line.getTransactionIPIndicatorCode()));
            
            //Refresh all references for the given KemId
            //line.getKemidObj().refreshNonUpdateableReferences();

            //Validate ETran code as E or I
            isValid &= validateEndowmentTransactionTypeCode(line,ERROR_PREFIX);
            
            //Validate Greater then Zero(thus positive) value
            isValid &= validateTransactionAmountGreaterThanZero(line,ERROR_PREFIX);
            
            //Validate Units is Greater then Zero(thus positive) value
            isValid &= validateTransactionUnitsGreaterThanZero(line,ERROR_PREFIX);
            
            //Validate if a KEMID can have a principal transaction when IP indicator is P
            isValid &= canKEMIDHaveAPrincipalTransaction(line,ERROR_PREFIX);
        } 
        
        return GlobalVariables.getMessageMap().getErrorCount() == 0;
    }

    /**
     * This method validates the KEMID code. 
     * 
     * @param tranSecurity
     * @return
     */
    protected boolean isKemIdCodeEmpty(EndowmentTransactionLine line, String prefix)
    {
        if( StringUtils.isEmpty(line.getKemid()) )
        {
            putFieldError(prefix + EndowPropertyConstants.KEMID, EndowKeyConstants.EndowmentTransactionDocumentConstants.ERROR_TRANSACTION_LINE_KEMID_REQUIRED);
            return true;        
        }
        
        return false;
    }
    
    /**
     * This method validates the KemId code and tries to create a KEMID object from the code.
     * 
     * @param line
{'fraction_non_alphanumeric': 0.07854755393497417, 'fraction_numerical': 0.001671224551807961, 'mean_word_length': 3.5302821748107363, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6749528', 'n_tokens_mistral': 3685, 'n_tokens_neox': 3621, 'n_words': 679}