<commit_before>/*
 * Copyright (c) 2004, 2005 The National Association of College and University Business Officers,
 * Cornell University, Trustees of Indiana University, Michigan State University Board of Trustees,
 * Trustees of San Joaquin Delta College, University of Hawai'i, The Arizona Board of Regents on
 * behalf of the University of Arizona, and the r*smart group.
 * 
 * Licensed under the Educational Community License Version 1.0 (the "License"); By obtaining,
 * using and/or copying this Original Work, you agree that you have read, understand, and will
 * comply with the terms and conditions of the Educational Community License.
 * 
 * You may obtain a copy of the License at:
 * 
 * http://kualiproject.org/license.html
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package org.kuali.module.chart.rules;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;

import org.apache.commons.lang.StringUtils;
import org.kuali.KeyConstants;
import org.kuali.core.bo.PostalZipCode;
import org.kuali.core.bo.user.KualiUser;
import org.kuali.core.document.MaintenanceDocument;
import org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase;
import org.kuali.core.util.GlobalVariables;
import org.kuali.core.util.ObjectUtils;
import org.kuali.core.util.SpringServiceLocator;
import org.kuali.module.chart.bo.Account;
import org.kuali.module.chart.bo.AccountGuideline;
import org.kuali.module.chart.bo.SubFundGroup;
import org.kuali.module.financial.rules.KualiParameterRule;

/**
 * Business rule(s) applicable to AccountMaintenance documents.
 * 
 * @author <NAME> (<EMAIL>)
 */
public class AccountRule extends MaintenanceDocumentRuleBase {
    protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AccountRule.class);
    
    private KualiParameterRule validBudgetRule;
    private boolean ruleValuesSetup;
    
    public static String CHART_MAINTENANCE_EDOC = "ChartMaintenanceEDoc";
    public static String ACCT_BUDGET_CODES_RESTRICT = "Account.BudgetCodesRestriction";
    
    Account oldAccount;
    Account newAccount;
    AccountGuideline oldAccountGuideline;
    AccountGuideline newAccountGuideline;
    
    private static String CONTRACTS_GRANTS_CD = "CG";
    private static String GENERAL_FUND_CD = "GF";
    private static String RESTRICTED_FUND_CD = "RF";
    private static String ENDOWMENT_FUND_CD = "EN";
    private static String PLANT_FUND_CD = "PF";
    
    public AccountRule() {
        ruleValuesSetup = false;
    }
    
    /**
     * This method initializes our rule values for Account Maintenance
     * @param document
     */
    private void initializeRuleValues(MaintenanceDocument document) {
        if(!ruleValuesSetup) {
            validBudgetRule = SpringServiceLocator.getKualiConfigurationService().getApplicationParameterRule(
                    CHART_MAINTENANCE_EDOC, ACCT_BUDGET_CODES_RESTRICT);
        }
    }
    
    /**
     * 
     * This method sets the convenience objects like newAccount and oldAccount, so you
     * have short and easy handles to the new and old objects contained in the 
     * maintenance document.
     * 
     * @param document - the maintenanceDocument being evaluated
     * 
     */
    private void setupConvenienceObjects(MaintenanceDocument document) {
        
        //	setup oldAccount convenience objects
        oldAccount = (Account) document.getOldMaintainableObject().getBusinessObject();
        SpringServiceLocator.getPersistenceService().retrieveNonKeyFields(oldAccount);
        oldAccountGuideline = oldAccount.getAccountGuideline();

        //	setup newAccount convenience objects
        newAccount = (Account) document.getNewMaintainableObject().getBusinessObject();
        SpringServiceLocator.getPersistenceService().retrieveNonKeyFields(newAccount);
        newAccountGuideline = newAccount.getAccountGuideline();
    }
    
    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
        
        setupConvenienceObjects(document);
        initializeRuleValues(document);
        
        //	default to success
        boolean success = true;
        success &= checkEmptyValues(document);
        return success;
    }
    
    protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
        LOG.info("processCustomRouteDocumentBusinessRules called");
        setupConvenienceObjects(document);
        initializeRuleValues(document);
        
        //	default to success
        boolean success = true;
        
        success &= checkEmptyValues(document);
        success &= checkGeneralRules(document);
        success &= checkCloseAccount(document);
        success &= checkContractsAndGrants(document);
        success &= checkExpirationDate(document);
        success &= checkFundGroup(document);
        success &= checkSubFundGroup(document);
        
        return success;
    }

    /**
     * 
     * This method checks the basic rules for empty values in an account and associated
     * objects with this account
     * @param maintenanceDocument
     * @return
     */
    private boolean checkEmptyValues(MaintenanceDocument maintenanceDocument) {
        boolean success = true;
        
        success &= checkEmptyBOField("chartOfAccountsCode", newAccount.getChartOfAccountsCode(), "Chart of Accounts Code");
        success &= checkEmptyBOField("accountNumber", newAccount.getAccountNumber(), "Account Number");
        success &= checkEmptyBOField("accountName", newAccount.getAccountName(), "Account Name");
        success &= checkEmptyBOField("organizationCode", newAccount.getOrganizationCode(), "Organization Code");
        success &= checkEmptyBOField("accountPhysicalCampusCode", newAccount.getAccountPhysicalCampusCode(), "Campus Code");
        success &= checkEmptyBOField("accountEffectiveDate", newAccount.getAccountEffectiveDate(), "Effective Date");
        success &= checkEmptyBOField("accountCityName", newAccount.getAccountCityName(), "City Name");
        success &= checkEmptyBOField("accountStateCode", newAccount.getAccountStateCode(), "State Code");
        success &= checkEmptyBOField("accountStreetAddress", newAccount.getAccountStreetAddress(), "Address");
        success &= checkEmptyBOField("accountZipCode", newAccount.getAccountZipCode(), "Zip Code");
        success &= checkEmptyBOField("budgetRecordingLevelCode", newAccount.getBudgetRecordingLevelCode(), "Budget Recording Level");
        success &= checkEmptyBOField("accountSufficientFundsCode", newAccount.getAccountSufficientFundsCode(), "Sufficient Funds Code");
        success &= checkEmptyBOField("subFundGroupCode", newAccount.getSubFundGroupCode(), "Sub Fund Group");
        success &= checkEmptyBOField("financialHigherEdFunctionCd", newAccount.getFinancialHigherEdFunctionCd(), "Higher Ed Function Code");
        success &= checkEmptyBOField("accountRestrictedStatusCode", newAccount.getAccountRestrictedStatusCode(), "Restricted Status Code");
        //Please see KULCOA-301 for details, but these need to go elsewhere
        //success &= checkEmptyValue(, newAccount.getAcctIndirectCostRcvyTypeCd(), "ICR Type Code");
        //success &= checkEmptyValue(, newAccount.getFinancialIcrSeriesIdentifier(), "ICR Series Identifier");
        //success &= checkEmptyValue(, newAccount.getIndirectCostRecoveryAcctNbr(), "ICR Cost Recovery Account");<commit_msg>made one comment a TODO, so we dont lose it<commit_after>/*
 * Copyright (c) 2004, 2005 The National Association of College and University Business Officers,
 * Cornell University, Trustees of Indiana University, Michigan State University Board of Trustees,
 * Trustees of San Joaquin Delta College, University of Hawai'i, The Arizona Board of Regents on
 * behalf of the University of Arizona, and the r*smart group.
 * 
 * Licensed under the Educational Community License Version 1.0 (the "License"); By obtaining,
 * using and/or copying this Original Work, you agree that you have read, understand, and will
 * comply with the terms and conditions of the Educational Community License.
 * 
 * You may obtain a copy of the License at:
 * 
 * http://kualiproject.org/license.html
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package org.kuali.module.chart.rules;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;

import org.apache.commons.lang.StringUtils;
import org.kuali.KeyConstants;
import org.kuali.core.bo.PostalZipCode;
import org.kuali.core.bo.user.KualiUser;
import org.kuali.core.document.MaintenanceDocument;
import org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase;
import org.kuali.core.util.GlobalVariables;
import org.kuali.core.util.ObjectUtils;
import org.kuali.core.util.SpringServiceLocator;
import org.kuali.module.chart.bo.Account;
import org.kuali.module.chart.bo.AccountGuideline;
import org.kuali.module.chart.bo.SubFundGroup;
import org.kuali.module.financial.rules.KualiParameterRule;

/**
 * Business rule(s) applicable to AccountMaintenance documents.
 * 
 * @author Kuali Nervous System Team (<EMAIL>)
 */
public class AccountRule extends MaintenanceDocumentRuleBase {
    protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AccountRule.class);
    
    private KualiParameterRule validBudgetRule;
    private boolean ruleValuesSetup;
    
    public static String CHART_MAINTENANCE_EDOC = "ChartMaintenanceEDoc";
    public static String ACCT_BUDGET_CODES_RESTRICT = "Account.BudgetCodesRestriction";
    
    Account oldAccount;
    Account newAccount;
    AccountGuideline oldAccountGuideline;
    AccountGuideline newAccountGuideline;
    
    private static String CONTRACTS_GRANTS_CD = "CG";
    private static String GENERAL_FUND_CD = "GF";
    private static String RESTRICTED_FUND_CD = "RF";
    private static String ENDOWMENT_FUND_CD = "EN";
    private static String PLANT_FUND_CD = "PF";
    
    public AccountRule() {
        ruleValuesSetup = false;
    }
    
    /**
     * This method initializes our rule values for Account Maintenance
     * @param document
     */
    private void initializeRuleValues(MaintenanceDocument document) {
        if(!ruleValuesSetup) {
            validBudgetRule = SpringServiceLocator.getKualiConfigurationService().getApplicationParameterRule(
                    CHART_MAINTENANCE_EDOC, ACCT_BUDGET_CODES_RESTRICT);
        }
    }
    
    /**
     * 
     * This method sets the convenience objects like newAccount and oldAccount, so you
     * have short and easy handles to the new and old objects contained in the 
     * maintenance document.
     * 
     * @param document - the maintenanceDocument being evaluated
     * 
     */
    private void setupConvenienceObjects(MaintenanceDocument document) {
        
        //	setup oldAccount convenience objects
        oldAccount = (Account) document.getOldMaintainableObject().getBusinessObject();
        SpringServiceLocator.getPersistenceService().retrieveNonKeyFields(oldAccount);
        oldAccountGuideline = oldAccount.getAccountGuideline();

        //	setup newAccount convenience objects
        newAccount = (Account) document.getNewMaintainableObject().getBusinessObject();
        SpringServiceLocator.getPersistenceService().retrieveNonKeyFields(newAccount);
        newAccountGuideline = newAccount.getAccountGuideline();
    }
    
    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
        
        setupConvenienceObjects(document);
        initializeRuleValues(document);
        
        //	default to success
        boolean success = true;
        success &= checkEmptyValues(document);
        return success;
    }
    
    protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
        LOG.info("processCustomRouteDocumentBusinessRules called");
        setupConvenienceObjects(document);
        initializeRuleValues(document);
        
        //	default to success
        boolean success = true;
        
        success &= checkEmptyValues(document);
        success &= checkGeneralRules(document);
        success &= checkCloseAccount(document);
        success &= checkContractsAndGrants(document);
        success &= checkExpirationDate(document);
        success &= checkFundGroup(document);
        success &= checkSubFundGroup(document);
        
        return success;
    }

    /**
     * 
     * This method checks the basic rules for empty values in an account and associated
     * objects with this account
     * @param maintenanceDocument
     * @return
     */
    private boolean checkEmptyValues(MaintenanceDocument maintenanceDocument) {
        boolean success = true;
        
        success &= checkEmptyBOField("chartOfAccountsCode", newAccount.getChartOfAccountsCode(), "Chart of Accounts Code");
        success &= checkEmptyBOField("accountNumber", newAccount.getAccountNumber(), "Account Number");
        success &= checkEmptyBOField("accountName", newAccount.getAccountName(), "Account Name");
        success &= checkEmptyBOField("organizationCode", newAccount.getOrganizationCode(), "Organization Code");
        success &= checkEmptyBOField("accountPhysicalCampusCode", newAccount.getAccountPhysicalCampusCode(), "Campus Code");
        success &= checkEmptyBOField("accountEffectiveDate", newAccount.getAccountEffectiveDate(), "Effective Date");
        success &= checkEmptyBOField("accountCityName", newAccount.getAccountCityName(), "City Name");
        success &= checkEmptyBOField("accountStateCode", newAccount.getAccountStateCode(), "State Code");
        success &= checkEmptyBOField("accountStreetAddress", newAccount.getAccountStreetAddress(), "Address");
        success &= checkEmptyBOField("accountZipCode", newAccount.getAccountZipCode(), "Zip Code");
        success &= checkEmptyBOField("budgetRecordingLevelCode", newAccount.getBudgetRecordingLevelCode(), "Budget Recording Level");
        success &= checkEmptyBOField("accountSufficientFundsCode", newAccount.getAccountSufficientFundsCode(), "Sufficient Funds Code");
        success &= checkEmptyBOField("subFundGroupCode", newAccount.getSubFundGroupCode(), "Sub Fund Group");
        success &= checkEmptyBOField("financialHigherEdFunctionCd", newAccount.getFinancialHigherEdFunctionCd(), "Higher Ed Function Code");
        success &= checkEmptyBOField("accountRestrictedStatusCode", newAccount.getAccountRestrictedStatusCode(), "Restricted Status Code");
        //TODO: Please see KULCOA-301 for details, but these need to go elsewhere
        //success &= checkEmptyValue(, newAccount.getAcctIndirectCostRcvyTypeCd(), "ICR Type Code");
        //success &= checkEmptyValue(, newAccount.getFinancialIcrSeriesIdentifier(), "ICR Series Identifier");
        //success &= checkEmptyValue(, newAccount.getIndirectCostRecoveryAcctNbr(), "ICR Cost Recovery Account");
{'fraction_non_alphanumeric': 0.08467006353400013, 'fraction_numerical': 0.0018871485185884128, 'mean_word_length': 4.008821676118463, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6203450', 'n_tokens_mistral': 4211, 'n_tokens_neox': 4030, 'n_words': 1106}