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