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.module.ld.batch;
017    
018    import java.io.File;
019    import java.io.IOException;
020    
021    import org.apache.commons.io.FileUtils;
022    import org.apache.commons.io.LineIterator;
023    import org.kuali.kfs.gl.GeneralLedgerConstants;
024    import org.kuali.kfs.gl.report.PreScrubberReport;
025    import org.kuali.kfs.gl.report.PreScrubberReportData;
026    import org.kuali.kfs.gl.service.PreScrubberService;
027    import org.kuali.kfs.module.ld.LaborConstants;
028    import org.kuali.kfs.sys.batch.AbstractWrappedBatchStep;
029    import org.kuali.kfs.sys.batch.service.WrappingBatchService;
030    import org.kuali.kfs.sys.batch.service.WrappedBatchExecutorService.CustomBatchExecutor;
031    import org.kuali.kfs.sys.service.ReportWriterService;
032    import org.springframework.util.StopWatch;
033    
034    public class LaborPreScrubberStep extends AbstractWrappedBatchStep {
035        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LaborPreScrubberStep.class);
036        private String batchFileDirectoryName;
037        private PreScrubberService laborPreScrubberService;
038        private ReportWriterService laborPreScrubberReportWriterService;
039        /**
040         * @see org.kuali.kfs.sys.batch.AbstractWrappedBatchStep#getCustomBatchExecutor()
041         */
042        @Override
043        protected CustomBatchExecutor getCustomBatchExecutor() {
044            return new CustomBatchExecutor() {
045    
046                /**
047                 * @see org.kuali.kfs.sys.batch.service.WrappedBatchExecutorService.CustomBatchExecutor#execute()
048                 */
049                public boolean execute() {
050                    StopWatch stopWatch = new StopWatch();
051                    stopWatch.start();
052    
053                    String inputFile = batchFileDirectoryName + File.separator + LaborConstants.BatchFileSystem.BACKUP_FILE + GeneralLedgerConstants.BatchFileSystem.EXTENSION;
054                    String outputFile = batchFileDirectoryName + File.separator + LaborConstants.BatchFileSystem.PRE_SCRUBBER_FILE + GeneralLedgerConstants.BatchFileSystem.EXTENSION;
055                    
056                    PreScrubberReportData preScrubberReportData = null;
057                    LineIterator oeIterator = null;
058                    try {
059                        oeIterator = FileUtils.lineIterator(new File(inputFile));
060                        preScrubberReportData = laborPreScrubberService.preprocessOriginEntries(oeIterator, outputFile);
061                    }
062                    catch (IOException e) {
063                        LOG.error("IO exception occurred during pre scrubbing.", e);
064                        throw new RuntimeException("IO exception occurred during pre scrubbing.", e);
065                    }
066                    finally {
067                        LineIterator.closeQuietly(oeIterator);
068                    }
069                    if (preScrubberReportData != null) {
070                        ((WrappingBatchService) laborPreScrubberReportWriterService).initialize();
071                        new PreScrubberReport().generateReport(preScrubberReportData, laborPreScrubberReportWriterService);
072                        ((WrappingBatchService) laborPreScrubberReportWriterService).destroy();
073                    }
074                    
075                    stopWatch.stop();
076                    if (LOG.isDebugEnabled()) {
077                        LOG.debug("labor pre-scrubber scrubber step took " + (stopWatch.getTotalTimeSeconds() / 60.0) + " minutes to complete");
078                    }
079                    return true;
080                }
081                
082            };
083        }
084        
085        public void setBatchFileDirectoryName(String batchFileDirectoryName) {
086            this.batchFileDirectoryName = batchFileDirectoryName;
087        }
088    
089        public PreScrubberService getLaborPreScrubberService() {
090            return laborPreScrubberService;
091        }
092    
093        public void setLaborPreScrubberService(PreScrubberService preScrubberService) {
094            this.laborPreScrubberService = preScrubberService;
095        }
096    
097        /**
098         * Sets the laborPreScrubberReportWriterService attribute value.
099         * @param laborPreScrubberReportWriterService The laborPreScrubberReportWriterService to set.
100         */
101        public void setLaborPreScrubberReportWriterService(ReportWriterService laborPreScrubberReportWriterService) {
102            this.laborPreScrubberReportWriterService = laborPreScrubberReportWriterService;
103        }
104    }