View Javadoc
1   /*
2    * Copyright 2005-2009 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.gl.batch;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.apache.commons.io.FileUtils;
24  import org.apache.commons.io.LineIterator;
25  import org.kuali.ole.gl.GeneralLedgerConstants;
26  import org.kuali.ole.gl.report.PreScrubberReport;
27  import org.kuali.ole.gl.report.PreScrubberReportData;
28  import org.kuali.ole.gl.service.PreScrubberService;
29  import org.kuali.ole.sys.batch.AbstractWrappedBatchStep;
30  import org.kuali.ole.sys.batch.service.WrappedBatchExecutorService.CustomBatchExecutor;
31  import org.kuali.ole.sys.service.ReportWriterService;
32  import org.springframework.util.StopWatch;
33  
34  /**
35   * A step to run the scrubber process.
36   */
37  public class PreScrubberStep extends AbstractWrappedBatchStep {
38      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PreScrubberStep.class);
39      private String batchFileDirectoryName;
40      private PreScrubberService preScrubberService;
41      private ReportWriterService preScrubberReportWriterService;
42      
43      /**
44       * @see org.kuali.ole.sys.batch.AbstractStep#getRequiredDirectoryNames()
45       */
46      @Override
47      public List<String> getRequiredDirectoryNames() {
48          return new ArrayList<String>(){{add(batchFileDirectoryName);}};
49      }
50  
51      @Override
52      protected CustomBatchExecutor getCustomBatchExecutor() {
53          return new CustomBatchExecutor() {
54              public boolean execute() {
55                  StopWatch stopWatch = new StopWatch();
56                  stopWatch.start();
57  
58                  String inputFile = batchFileDirectoryName + File.separator + GeneralLedgerConstants.BatchFileSystem.BACKUP_FILE + GeneralLedgerConstants.BatchFileSystem.EXTENSION;
59                  String outputFile = batchFileDirectoryName + File.separator + GeneralLedgerConstants.BatchFileSystem.PRE_SCRUBBER_FILE + GeneralLedgerConstants.BatchFileSystem.EXTENSION;
60                  
61                  PreScrubberReportData preScrubberReportData = null;
62                  LineIterator oeIterator = null;
63                  try {
64                      oeIterator = FileUtils.lineIterator(new File(inputFile));
65                      preScrubberReportData = preScrubberService.preprocessOriginEntries(oeIterator, outputFile);
66                  }
67                  catch (IOException e) {
68                      LOG.error("IO exception occurred during pre scrubbing.", e);
69                      throw new RuntimeException("IO exception occurred during pre scrubbing.", e);
70                  }
71                  finally {
72                      LineIterator.closeQuietly(oeIterator);
73                  }
74  
75                  if (preScrubberReportData != null) {
76                      new PreScrubberReport().generateReport(preScrubberReportData, preScrubberReportWriterService);
77                  }
78                  
79                  stopWatch.stop();
80                  if (LOG.isDebugEnabled()) {
81                      LOG.debug("scrubber step of took " + (stopWatch.getTotalTimeSeconds() / 60.0) + " minutes to complete");
82                  }
83                  return true;
84              }
85          };
86      }
87  
88      public void setBatchFileDirectoryName(String batchFileDirectoryName) {
89          this.batchFileDirectoryName = batchFileDirectoryName;
90      }
91  
92      public PreScrubberService getPreScrubberService() {
93          return preScrubberService;
94      }
95  
96      public void setPreScrubberService(PreScrubberService preScrubberService) {
97          this.preScrubberService = preScrubberService;
98      }
99      
100     public void setPreScrubberReportWriterService(ReportWriterService preScrubberReportWriterService) {
101         this.preScrubberReportWriterService = preScrubberReportWriterService;
102     }
103 }