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    import java.util.HashMap;
023    import java.util.Map;
024    
025    import org.kuali.kfs.gl.GeneralLedgerConstants;
026    import org.kuali.kfs.gl.batch.service.YearEndService;
027    import org.kuali.kfs.gl.service.OriginEntryGroupService;
028    import org.kuali.kfs.sys.batch.AbstractWrappedBatchStep;
029    import org.kuali.kfs.sys.batch.service.WrappedBatchExecutorService.CustomBatchExecutor;
030    import org.kuali.kfs.sys.service.impl.KfsParameterConstants;
031    import org.springframework.util.StopWatch;
032    
033    /**
034     * A step to run the year end process of forwarding encumbrances into the next fiscal year
035     */
036    public class EncumbranceForwardStep extends AbstractWrappedBatchStep {
037        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EncumbranceForwardStep.class);
038        private YearEndService yearEndService;
039    
040        public static final String TRANSACTION_DATE_FORMAT_STRING = "yyyy-MM-dd";
041    
042        /**
043         * @see org.kuali.kfs.sys.batch.AbstractWrappedBatchStep#getCustomBatchExecutor()
044         */
045        @Override
046        protected CustomBatchExecutor getCustomBatchExecutor() {
047            return new CustomBatchExecutor() {
048                /**
049                 * This step runs the forward encumbrance process, including retrieving the parameters needed to run the job, creating the
050                 * origin entry group where output origin entries will go, and having the job's reports generated.
051                 * 
052                 * @return true if the step completed successfully, false if otherwise
053                 * @see org.kuali.kfs.sys.batch.Step#performStep()
054                 */
055                public boolean execute() {
056                    StopWatch stopWatch = new StopWatch();
057                    stopWatch.start("EncumbranceForwardStep");
058    
059                    Map jobParameters = new HashMap();
060                    Integer varFiscalYear = null;
061                    Date varTransactionDate = null;
062    
063                    String FIELD_FISCAL_YEAR = GeneralLedgerConstants.ColumnNames.UNIVERSITY_FISCAL_YEAR;
064                    String FIELD_TRANSACTION_DATE = GeneralLedgerConstants.ColumnNames.TRANSACTION_DT;
065    
066                    // Get the current fiscal year.
067                    varFiscalYear = new Integer(getParameterService().getParameterValue(KfsParameterConstants.GENERAL_LEDGER_BATCH.class, GeneralLedgerConstants.ANNUAL_CLOSING_FISCAL_YEAR_PARM));
068    
069                    // Get the current date (transaction date).
070                    try {
071                        DateFormat transactionDateFormat = new SimpleDateFormat(TRANSACTION_DATE_FORMAT_STRING);
072                        varTransactionDate = new Date(transactionDateFormat.parse(getParameterService().getParameterValue(KfsParameterConstants.GENERAL_LEDGER_BATCH.class, GeneralLedgerConstants.ANNUAL_CLOSING_TRANSACTION_DATE_PARM)).getTime());
073                    }
074                    catch (ParseException pe) {
075                        LOG.error("Failed to parse TRANSACTION_DT from kualiConfigurationService");
076                        throw new RuntimeException("Unable to get transaction date from kualiConfigurationService", pe);
077                    }
078    
079                    jobParameters.put(GeneralLedgerConstants.ColumnNames.UNIVERSITY_FISCAL_YEAR, varFiscalYear);
080                    jobParameters.put(GeneralLedgerConstants.ColumnNames.UNIV_DT, varTransactionDate);
081    
082                    String encumbranceForwardFileName = GeneralLedgerConstants.BatchFileSystem.ENCUMBRANCE_FORWARD_FILE + GeneralLedgerConstants.BatchFileSystem.EXTENSION;
083                    Map<String, Integer> forwardEncumbranceCounts = new HashMap<String, Integer>();
084    
085                    yearEndService.forwardEncumbrances(encumbranceForwardFileName, jobParameters, forwardEncumbranceCounts);
086    
087                    stopWatch.stop();
088                    LOG.info("EncumbranceForwardStep took " + (stopWatch.getTotalTimeSeconds() / 60.0) + " minutes to complete");
089    
090                    return true;
091                }
092            };
093        }
094    
095        /**
096         * Sets the yearEndService attribute, allowing the injection of an implementation of that service
097         * 
098         * @param yearEndService the yearEndService to set
099         * @see org.kuali.module.gl.service.YearEndService
100         */
101        public void setYearEndService(YearEndService yearEndService) {
102            this.yearEndService = yearEndService;
103        }
104    }