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     * The step that runs the year end nominal activity closing process.
035     */
036    public class NominalActivityClosingStep extends AbstractWrappedBatchStep {
037        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(NominalActivityClosingStep.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                 * Runs the nominal activity process, including retrieving system parameters for the process, creating the origin entry group
050                 * for output origin entries, and generating reports based on the run
051                 * @return true if the step completed successfully, false if otherwise
052                 * @see org.kuali.kfs.sys.batch.Step#performStep()
053                 */
054                public boolean execute() {
055                    StopWatch stopWatch = new StopWatch();
056                    stopWatch.start("NominalActivityClosingStep");
057    
058                    Date varTransactionDate;
059                    try {
060                        DateFormat transactionDateFormat = new SimpleDateFormat(TRANSACTION_DATE_FORMAT_STRING);
061                        varTransactionDate = new Date(transactionDateFormat.parse(getParameterService().getParameterValue(KfsParameterConstants.GENERAL_LEDGER_BATCH.class, GeneralLedgerConstants.ANNUAL_CLOSING_TRANSACTION_DATE_PARM)).getTime());
062                    }
063                    catch (ParseException e) {
064                        LOG.error("forwardBalances() Unable to parse transaction date", e);
065                        throw new IllegalArgumentException("Unable to parse transaction date");
066                    }
067                    Integer varFiscalYear = new Integer(getParameterService().getParameterValue(KfsParameterConstants.GENERAL_LEDGER_BATCH.class, GeneralLedgerConstants.ANNUAL_CLOSING_FISCAL_YEAR_PARM));
068                    String nominalClosingFileName = GeneralLedgerConstants.BatchFileSystem.CLOSE_NOMINAL_ACTIVITY_FILE + GeneralLedgerConstants.BatchFileSystem.EXTENSION;
069                    
070                    Map nominalClosingJobParameters = new HashMap();
071                    nominalClosingJobParameters.put(GeneralLedgerConstants.ColumnNames.UNIV_DT, varTransactionDate);
072                    nominalClosingJobParameters.put(GeneralLedgerConstants.ColumnNames.UNIVERSITY_FISCAL_YEAR, varFiscalYear);
073                    Map<String, Integer> nominalActivityClosingCounts = new HashMap<String, Integer>();
074    
075                    yearEndService.closeNominalActivity(nominalClosingFileName, nominalClosingJobParameters);
076                    stopWatch.stop();
077                    LOG.info("NominalActivityClosingStep took " + (stopWatch.getTotalTimeSeconds() / 60.0) + " minutes to complete");
078    
079                    return true;
080                }
081            };
082        }
083    
084        /**
085         * Sets the yearEndService attribute, allowing the injection of an implementation of the service
086         * 
087         * @param yearEndService the year end service to set
088         * @see org.kuali.module.gl.service.YearEndService
089         */
090        public void setYearEndService(YearEndService yearEndService) {
091            this.yearEndService = yearEndService;
092        }
093    
094        /**
095         * This method returns the YearEndService object associated with this step
096         * 
097         * @return the yearEndService this step is using to complete its process
098         * @see org.kuali.module.gl.service.YearEndSErvice
099         */
100        public YearEndService getYearEndService() {
101            return yearEndService;
102        }
103    }