001/*
002 * Copyright 2006 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 */
016package org.kuali.ole.gl.batch;
017
018import java.sql.Date;
019import java.text.DateFormat;
020import java.text.ParseException;
021import java.text.SimpleDateFormat;
022
023import org.kuali.ole.gl.GeneralLedgerConstants;
024import org.kuali.ole.gl.batch.service.YearEndService;
025import org.kuali.ole.sys.batch.AbstractWrappedBatchStep;
026import org.kuali.ole.sys.batch.service.WrappedBatchExecutorService.CustomBatchExecutor;
027import org.kuali.ole.sys.service.impl.OleParameterConstants;
028import org.springframework.util.StopWatch;
029
030/**
031 * This step runs the balance forward year end process.
032 */
033public class BalanceForwardStep extends AbstractWrappedBatchStep {
034    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BalanceForwardStep.class);
035
036    private YearEndService yearEndService;
037
038    public static final String TRANSACTION_DATE_FORMAT_STRING = "yyyy-MM-dd";
039
040    /**
041     * @see org.kuali.ole.sys.batch.AbstractWrappedBatchStep#getCustomBatchExecutor()
042     */
043    @Override
044    protected CustomBatchExecutor getCustomBatchExecutor() {
045        return new CustomBatchExecutor() {
046            /**
047             * This step runs the balance forward service, specifically finding the parameters the job needs, creating the origin entry
048             * groups for the output origin entries, and creating the process's reports.
049             * @return that the job finished successfully
050             * @see org.kuali.ole.sys.batch.Step#execute(String, java.util.Date)
051             */
052            public boolean execute() {
053                StopWatch stopWatch = new StopWatch();
054                stopWatch.start("Balance Forward Step");
055
056                Date varTransactionDate;
057                try {
058                    DateFormat transactionDateFormat = new SimpleDateFormat(TRANSACTION_DATE_FORMAT_STRING);
059                    varTransactionDate = new Date(transactionDateFormat.parse(getParameterService().getParameterValueAsString(OleParameterConstants.GENERAL_LEDGER_BATCH.class, GeneralLedgerConstants.ANNUAL_CLOSING_TRANSACTION_DATE_PARM)).getTime());
060                }
061                catch (ParseException e) {
062                    LOG.error("forwardBalances() Unable to parse transaction date", e);
063                    throw new IllegalArgumentException("Unable to parse transaction date");
064                }
065
066                Integer varFiscalYear = new Integer(getParameterService().getParameterValueAsString(OleParameterConstants.GENERAL_LEDGER_BATCH.class, GeneralLedgerConstants.ANNUAL_CLOSING_FISCAL_YEAR_PARM));
067
068                yearEndService.logAllMissingPriorYearAccounts(varFiscalYear);
069                yearEndService.logAllMissingSubFundGroups(varFiscalYear);
070
071                String balanceForwardsUnclosedFileName = GeneralLedgerConstants.BatchFileSystem.BALANCE_FORWARDS_FILE + GeneralLedgerConstants.BatchFileSystem.EXTENSION; 
072                String balanceForwardsclosedFileName = GeneralLedgerConstants.BatchFileSystem.BALANCE_FORWARDS_CLOSED_FILE + GeneralLedgerConstants.BatchFileSystem.EXTENSION;
073                
074                BalanceForwardRuleHelper balanceForwardRuleHelper = new BalanceForwardRuleHelper(varFiscalYear, varTransactionDate, balanceForwardsclosedFileName, balanceForwardsUnclosedFileName);
075
076                yearEndService.forwardBalances(balanceForwardsUnclosedFileName, balanceForwardsclosedFileName, balanceForwardRuleHelper);
077
078                stopWatch.stop();
079                LOG.info("Balance Forward Step took " + (stopWatch.getTotalTimeSeconds() / 60.0) + " minutes to complete");
080
081                return true;
082            }
083        };
084    }
085
086    /**
087     * Sets the yearEndService attribute, allowing injection of an implementation of the service
088     * 
089     * @param yearEndService an implementation of the yearEndService
090     * @see org.kuali.module.gl.service.yearEndService
091     */
092    public void setYearEndService(YearEndService yearEndService) {
093        this.yearEndService = yearEndService;
094    }
095}