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