001/*
002 * Copyright 2005-2009 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 */
016package org.kuali.ole.gl.batch;
017
018import java.io.File;
019import java.io.IOException;
020import java.util.ArrayList;
021import java.util.List;
022
023import org.apache.commons.io.FileUtils;
024import org.apache.commons.io.LineIterator;
025import org.kuali.ole.gl.GeneralLedgerConstants;
026import org.kuali.ole.gl.report.PreScrubberReport;
027import org.kuali.ole.gl.report.PreScrubberReportData;
028import org.kuali.ole.gl.service.PreScrubberService;
029import org.kuali.ole.sys.batch.AbstractWrappedBatchStep;
030import org.kuali.ole.sys.batch.service.WrappedBatchExecutorService.CustomBatchExecutor;
031import org.kuali.ole.sys.service.ReportWriterService;
032import org.springframework.util.StopWatch;
033
034/**
035 * A step to run the scrubber process.
036 */
037public class PreScrubberStep extends AbstractWrappedBatchStep {
038    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PreScrubberStep.class);
039    private String batchFileDirectoryName;
040    private PreScrubberService preScrubberService;
041    private ReportWriterService preScrubberReportWriterService;
042    
043    /**
044     * @see org.kuali.ole.sys.batch.AbstractStep#getRequiredDirectoryNames()
045     */
046    @Override
047    public List<String> getRequiredDirectoryNames() {
048        return new ArrayList<String>(){{add(batchFileDirectoryName);}};
049    }
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}