<commit_before>/*
 * Copyright 2006 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.module.gl.service.impl.orgreversion;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.kuali.core.service.DateTimeService;
import org.kuali.core.service.KualiConfigurationService;
import org.kuali.core.service.PersistenceService;
import org.kuali.core.util.KualiDecimal;
import org.kuali.core.util.ObjectUtils;
import org.kuali.kfs.KFSConstants;
import org.kuali.kfs.KFSKeyConstants;
import org.kuali.kfs.KFSPropertyConstants;
import org.kuali.kfs.context.SpringContext;
import org.kuali.kfs.service.ParameterService;
import org.kuali.kfs.service.impl.ParameterConstants;
import org.kuali.module.chart.bo.AccountIntf;
import org.kuali.module.chart.bo.ObjectCode;
import org.kuali.module.chart.bo.OrganizationReversion;
import org.kuali.module.chart.bo.OrganizationReversionCategory;
import org.kuali.module.chart.bo.OrganizationReversionDetail;
import org.kuali.module.chart.service.OrganizationReversionService;
import org.kuali.module.chart.service.PriorYearAccountService;
import org.kuali.module.gl.GLConstants;
import org.kuali.module.gl.bo.Balance;
import org.kuali.module.gl.bo.OrgReversionUnitOfWork;
import org.kuali.module.gl.bo.OrgReversionUnitOfWorkCategoryAmount;
import org.kuali.module.gl.bo.OriginEntryFull;
import org.kuali.module.gl.bo.OriginEntryGroup;
import org.kuali.module.gl.service.BalanceService;
import org.kuali.module.gl.service.OrgReversionUnitOfWorkService;
import org.kuali.module.gl.service.OrganizationReversionCategoryLogic;
import org.kuali.module.gl.service.OriginEntryGroupService;
import org.kuali.module.gl.service.OriginEntryService;
import org.kuali.module.gl.util.FatalErrorException;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class OrganizationReversionProcess {
    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OrganizationReversionProcess.class);

    // Services
    private OrganizationReversionService organizationReversionService;
    private BalanceService balanceService;
    private OriginEntryGroupService originEntryGroupService;
    private OriginEntryService originEntryService;
    private PersistenceService persistenceService;
    private DateTimeService dateTimeService;
    private OrganizationReversionCategoryLogic cashOrganizationReversionCategoryLogic;
    private PriorYearAccountService priorYearAccountService;
    private OrgReversionUnitOfWorkService orgReversionUnitOfWorkService;

    private OriginEntryGroup outputGroup;
    private OrgReversionUnitOfWork unitOfWork;
    private Map<String, OrganizationReversionCategoryLogic> categories;
    private List<OrganizationReversionCategory> categoryList;
    private OrganizationReversion organizationReversion;
    private AccountIntf account;

    private Map jobParameters;
    private Map<String, Integer> organizationReversionCounts;

    private boolean endOfYear;

    private boolean holdGeneratedOriginEntries = false;
    private List<OriginEntryFull> generatedOriginEntries;

    public final String CARRY_FORWARD_OBJECT_CODE;
    public final String DEFAULT_FINANCIAL_DOCUMENT_TYPE_CODE;
    public final String DEFAULT_FINANCIAL_SYSTEM_ORIGINATION_CODE;
    public final String DEFAULT_FINANCIAL_BALANCE_TYPE_CODE;
    public final String DEFAULT_FINANCIAL_BALANCE_TYPE_CODE_YEAR_END;
    public final String DEFAULT_DOCUMENT_NUMBER_PREFIX;

    private final String CASH_REVERTED_TO_MESSAGE;
    private final String FUND_BALANCE_REVERTED_TO_MESSAGE;
    private final String CASH_REVERTED_FROM_MESSAGE;
    private final String FUND_BALANCE_REVERTED_FROM_MESSAGE;
    private final String FUND_CARRIED_MESSAGE;
    private final String FUND_REVERTED_TO_MESSAGE;
    private final String FUND_REVERTED_FROM_MESSAGE;

    public OrganizationReversionProcess() {
        super();

        ParameterService parameterService = SpringContext.getBean(ParameterService.class);
        this.CARRY_FORWARD_OBJECT_CODE = parameterService.getParameterValue(OrganizationReversion.class, GLConstants.OrganizationReversionProcess.CARRY_FORWARD_OBJECT_CODE);
        this.DEFAULT_FINANCIAL_DOCUMENT_TYPE_CODE = parameterService.getParameterValue(ParameterConstants.GENERAL_LEDGER_BATCH.class, GLConstants.ANNUAL_CLOSING_DOCUMENT_TYPE);
        this.DEFAULT_FINANCIAL_SYSTEM_ORIGINATION_CODE = parameterService.getParameterValue(OrganizationReversion.class, GLConstants.OrganizationReversionProcess.DEFAULT_FINANCIAL_SYSTEM_ORIGINATION_CODE);
        this.DEFAULT_FINANCIAL_BALANCE_TYPE_CODE = parameterService.getParameterValue(OrganizationReversion.class, GLConstants.OrganizationReversionProcess.DEFAULT_FINANCIAL_BALANCE_TYPE_CODE);
        this.DEFAULT_FINANCIAL_BALANCE_TYPE_CODE_YEAR_END = parameterService.getParameterValue(OrganizationReversion.class, GLConstants.OrganizationReversionProcess.DEFAULT_FINANCIAL_BALANCE_TYPE_CODE_YEAR_END);
        this.DEFAULT_DOCUMENT_NUMBER_PREFIX = parameterService.getParameterValue(OrganizationReversion.class, GLConstants.OrganizationReversionProcess.DEFAULT_DOCUMENT_NUMBER_PREFIX);
        KualiConfigurationService configurationService = SpringContext.getBean(KualiConfigurationService.class);
        this.CASH_REVERTED_TO_MESSAGE = configurationService.getPropertyString(KFSKeyConstants.OrganizationReversionProcess.CASH_REVERTED_TO);
        this.FUND_BALANCE_REVERTED_TO_MESSAGE = configurationService.getPropertyString(KFSKeyConstants.OrganizationReversionProcess.FUND_BALANCE_REVERTED_TO);
        this.CASH_REVERTED_FROM_MESSAGE = configurationService.getPropertyString(KFSKeyConstants.OrganizationReversionProcess.CASH_REVERTED_FROM);
        this.FUND_BALANCE_REVERTED_FROM_MESSAGE = configurationService.getPropertyString(KFSKeyConstants.OrganizationReversionProcess.FUND_BALANCE_REVERTED_FROM);
        this.FUND_CARRIED_MESSAGE = configurationService.getPropertyString(KFSKeyConstants.OrganizationReversionProcess.FUND_CARRIED);
        this.FUND_REVERTED_TO_MESSAGE = configurationService.getPropertyString(KFSKeyConstants.OrganizationReversionProcess.FUND_REVERTED_TO);
        this.FUND_REVERTED_FROM_MESSAGE = configurationService.getPropertyString(KFSKeyConstants.OrganizationReversionProcess.FUND_REVERTED_FROM);
    }

    public OrganizationReversionProcess(OriginEntryGroup outputGroup, boolean endOfYear, OrganizationReversionService ors, BalanceService bs, OriginEntryGroupService oegs, OriginEntryService oes, PersistenceService ps, DateTimeService dts, OrganizationReversionCategoryLogic corc, PriorYearAccountService pyas, OrgReversionUnitOfWorkService oruows, Map jobParameters, Map<String, Integer> organizationReversionCounts) {
        this();

        this.outputGroup = outputGroup;
        this.endOfYear = endOfYear;
        balanceService = bs;
        organizationReversionService = ors;
        originEntryGroupService = oegs;
        originEntryService = oes;
        persistenceService = ps;
        dateTimeService = dts;
        cashOrganizationReversionCategoryLogic = corc;
        priorYearAccountService = pyas;
        orgReversionUnitOfWorkService = oruows;
        this.jobParameters = jobParameters;
        this.organizationReversionCounts = organizationReversionCounts;
    }

    public void organizationReversionProcess() {
        LOG.debug("organizationReversionProcess() started");

        initializeProcess();

        Iterator<Balance> balances = balanceService.findOrganizationReversionBalancesForFiscalYear((Integer) jobParameters.get(KFSConstants.UNIV_FISCAL_YR), endOfYear);
        processBalances(balances);

    }

    public void processBalances(Iterator<Balance> balances) {
        boolean skipToNextUnitOfWork = false;
        unitOfWork = new OrgReversionUnitOfWork();
        unitOfWork.setCategories(categoryList);

        while (balances.hasNext()) {
            Balance bal = balances.next();
            incrementCount("balancesSelected");
            if (LOG.isDebugEnabled()) {
                LOG.debug("BALANCE SELECTED: "+bal.getUniversityFiscalYear()+bal.getChartOfAccountsCode()+bal.getAccountNumber()+bal.getSubAccountNumber()+bal.getObjectCode()+bal.getSubObjectCode()+bal.getBalanceTypeCode()+bal.getObjectTypeCode());
            }
            // TODO james, kill the code below
            if (bal.getObjectCode().equals("8000")) {
                LOG.debug("CASH BALANCE SELECTED: "+bal.getUniversityFiscalYear()+bal.getChartOfAccountsCode()+bal.getAccountNumber()+bal.getSubAccountNumber()+bal.getObjectCode()+bal.getSubObjectCode()+bal.getBalanceTypeCode()+bal.getObjectTypeCode()+": "+bal.getBeginningBalanceLineAmount().add(bal.getAccountLineAnnualBalanceAmount()));
            }

            try {
                if (!unitOfWork.isInitialized()) {<commit_msg>KULRNE-5652: even better temporary logging of cash balance selections<commit_after>/*
 * Copyright 2006 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.module.gl.service.impl.orgreversion;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.kuali.core.service.DateTimeService;
import org.kuali.core.service.KualiConfigurationService;
import org.kuali.core.service.PersistenceService;
import org.kuali.core.util.KualiDecimal;
import org.kuali.core.util.ObjectUtils;
import org.kuali.kfs.KFSConstants;
import org.kuali.kfs.KFSKeyConstants;
import org.kuali.kfs.KFSPropertyConstants;
import org.kuali.kfs.context.SpringContext;
import org.kuali.kfs.service.ParameterService;
import org.kuali.kfs.service.impl.ParameterConstants;
import org.kuali.module.chart.bo.AccountIntf;
import org.kuali.module.chart.bo.ObjectCode;
import org.kuali.module.chart.bo.OrganizationReversion;
import org.kuali.module.chart.bo.OrganizationReversionCategory;
import org.kuali.module.chart.bo.OrganizationReversionDetail;
import org.kuali.module.chart.service.OrganizationReversionService;
import org.kuali.module.chart.service.PriorYearAccountService;
import org.kuali.module.gl.GLConstants;
import org.kuali.module.gl.bo.Balance;
import org.kuali.module.gl.bo.OrgReversionUnitOfWork;
import org.kuali.module.gl.bo.OrgReversionUnitOfWorkCategoryAmount;
import org.kuali.module.gl.bo.OriginEntryFull;
import org.kuali.module.gl.bo.OriginEntryGroup;
import org.kuali.module.gl.service.BalanceService;
import org.kuali.module.gl.service.OrgReversionUnitOfWorkService;
import org.kuali.module.gl.service.OrganizationReversionCategoryLogic;
import org.kuali.module.gl.service.OriginEntryGroupService;
import org.kuali.module.gl.service.OriginEntryService;
import org.kuali.module.gl.util.FatalErrorException;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class OrganizationReversionProcess {
    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OrganizationReversionProcess.class);

    // Services
    private OrganizationReversionService organizationReversionService;
    private BalanceService balanceService;
    private OriginEntryGroupService originEntryGroupService;
    private OriginEntryService originEntryService;
    private PersistenceService persistenceService;
    private DateTimeService dateTimeService;
    private OrganizationReversionCategoryLogic cashOrganizationReversionCategoryLogic;
    private PriorYearAccountService priorYearAccountService;
    private OrgReversionUnitOfWorkService orgReversionUnitOfWorkService;

    private OriginEntryGroup outputGroup;
    private OrgReversionUnitOfWork unitOfWork;
    private Map<String, OrganizationReversionCategoryLogic> categories;
    private List<OrganizationReversionCategory> categoryList;
    private OrganizationReversion organizationReversion;
    private AccountIntf account;

    private Map jobParameters;
    private Map<String, Integer> organizationReversionCounts;

    private boolean endOfYear;

    private boolean holdGeneratedOriginEntries = false;
    private List<OriginEntryFull> generatedOriginEntries;

    public final String CARRY_FORWARD_OBJECT_CODE;
    public final String DEFAULT_FINANCIAL_DOCUMENT_TYPE_CODE;
    public final String DEFAULT_FINANCIAL_SYSTEM_ORIGINATION_CODE;
    public final String DEFAULT_FINANCIAL_BALANCE_TYPE_CODE;
    public final String DEFAULT_FINANCIAL_BALANCE_TYPE_CODE_YEAR_END;
    public final String DEFAULT_DOCUMENT_NUMBER_PREFIX;

    private final String CASH_REVERTED_TO_MESSAGE;
    private final String FUND_BALANCE_REVERTED_TO_MESSAGE;
    private final String CASH_REVERTED_FROM_MESSAGE;
    private final String FUND_BALANCE_REVERTED_FROM_MESSAGE;
    private final String FUND_CARRIED_MESSAGE;
    private final String FUND_REVERTED_TO_MESSAGE;
    private final String FUND_REVERTED_FROM_MESSAGE;

    public OrganizationReversionProcess() {
        super();

        ParameterService parameterService = SpringContext.getBean(ParameterService.class);
        this.CARRY_FORWARD_OBJECT_CODE = parameterService.getParameterValue(OrganizationReversion.class, GLConstants.OrganizationReversionProcess.CARRY_FORWARD_OBJECT_CODE);
        this.DEFAULT_FINANCIAL_DOCUMENT_TYPE_CODE = parameterService.getParameterValue(ParameterConstants.GENERAL_LEDGER_BATCH.class, GLConstants.ANNUAL_CLOSING_DOCUMENT_TYPE);
        this.DEFAULT_FINANCIAL_SYSTEM_ORIGINATION_CODE = parameterService.getParameterValue(OrganizationReversion.class, GLConstants.OrganizationReversionProcess.DEFAULT_FINANCIAL_SYSTEM_ORIGINATION_CODE);
        this.DEFAULT_FINANCIAL_BALANCE_TYPE_CODE = parameterService.getParameterValue(OrganizationReversion.class, GLConstants.OrganizationReversionProcess.DEFAULT_FINANCIAL_BALANCE_TYPE_CODE);
        this.DEFAULT_FINANCIAL_BALANCE_TYPE_CODE_YEAR_END = parameterService.getParameterValue(OrganizationReversion.class, GLConstants.OrganizationReversionProcess.DEFAULT_FINANCIAL_BALANCE_TYPE_CODE_YEAR_END);
        this.DEFAULT_DOCUMENT_NUMBER_PREFIX = parameterService.getParameterValue(OrganizationReversion.class, GLConstants.OrganizationReversionProcess.DEFAULT_DOCUMENT_NUMBER_PREFIX);
        KualiConfigurationService configurationService = SpringContext.getBean(KualiConfigurationService.class);
        this.CASH_REVERTED_TO_MESSAGE = configurationService.getPropertyString(KFSKeyConstants.OrganizationReversionProcess.CASH_REVERTED_TO);
        this.FUND_BALANCE_REVERTED_TO_MESSAGE = configurationService.getPropertyString(KFSKeyConstants.OrganizationReversionProcess.FUND_BALANCE_REVERTED_TO);
        this.CASH_REVERTED_FROM_MESSAGE = configurationService.getPropertyString(KFSKeyConstants.OrganizationReversionProcess.CASH_REVERTED_FROM);
        this.FUND_BALANCE_REVERTED_FROM_MESSAGE = configurationService.getPropertyString(KFSKeyConstants.OrganizationReversionProcess.FUND_BALANCE_REVERTED_FROM);
        this.FUND_CARRIED_MESSAGE = configurationService.getPropertyString(KFSKeyConstants.OrganizationReversionProcess.FUND_CARRIED);
        this.FUND_REVERTED_TO_MESSAGE = configurationService.getPropertyString(KFSKeyConstants.OrganizationReversionProcess.FUND_REVERTED_TO);
        this.FUND_REVERTED_FROM_MESSAGE = configurationService.getPropertyString(KFSKeyConstants.OrganizationReversionProcess.FUND_REVERTED_FROM);
    }

    public OrganizationReversionProcess(OriginEntryGroup outputGroup, boolean endOfYear, OrganizationReversionService ors, BalanceService bs, OriginEntryGroupService oegs, OriginEntryService oes, PersistenceService ps, DateTimeService dts, OrganizationReversionCategoryLogic corc, PriorYearAccountService pyas, OrgReversionUnitOfWorkService oruows, Map jobParameters, Map<String, Integer> organizationReversionCounts) {
        this();

        this.outputGroup = outputGroup;
        this.endOfYear = endOfYear;
        balanceService = bs;
        organizationReversionService = ors;
        originEntryGroupService = oegs;
        originEntryService = oes;
        persistenceService = ps;
        dateTimeService = dts;
        cashOrganizationReversionCategoryLogic = corc;
        priorYearAccountService = pyas;
        orgReversionUnitOfWorkService = oruows;
        this.jobParameters = jobParameters;
        this.organizationReversionCounts = organizationReversionCounts;
    }

    public void organizationReversionProcess() {
        LOG.debug("organizationReversionProcess() started");

        initializeProcess();

        Iterator<Balance> balances = balanceService.findOrganizationReversionBalancesForFiscalYear((Integer) jobParameters.get(KFSConstants.UNIV_FISCAL_YR), endOfYear);
        processBalances(balances);

    }

    public void processBalances(Iterator<Balance> balances) {
        boolean skipToNextUnitOfWork = false;
        unitOfWork = new OrgReversionUnitOfWork();
        unitOfWork.setCategories(categoryList);

        while (balances.hasNext()) {
            Balance bal = balances.next();
            incrementCount("balancesSelected");
            if (LOG.isDebugEnabled()) {
                LOG.debug("BALANCE SELECTED: "+bal.getUniversityFiscalYear()+bal.getChartOfAccountsCode()+bal.getAccountNumber()+bal.getSubAccountNumber()+bal.getObjectCode()+bal.getSubObjectCode()+bal.getBalanceTypeCode()+bal.getObjectTypeCode());
            }
            // TODO james, kill the code below
            if (bal.getObjectCode().equals("8000")) {
                LOG.info("CASH BALANCE SELECTED: "+bal.getUniversityFiscalYear()+bal.getChartOfAccountsCode()+bal.getAccountNumber()+bal.getSubAccountNumber()+bal.getObjectCode()+bal.getSubObjectCode()+bal.getBalanceTypeCode()+bal.getObjectTypeCode()+": "+bal.getBeginningBalanceLineAmount().add(bal.getAccountLineAnnualBalanceAmount()));
            }

            try {
                if (!unitOfWork.isInitialized()) {
{'fraction_non_alphanumeric': 0.07072359740328284, 'fraction_numerical': 0.0015833641209690187, 'mean_word_length': 5.611304954640614, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 17, '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': '6541080', 'n_tokens_mistral': 5403, 'n_tokens_neox': 5265, 'n_words': 868}