View Javadoc
1   /*
2    * Copyright 2006 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.service.impl;
17  
18  import java.io.BufferedReader;
19  import java.io.File;
20  import java.io.FileNotFoundException;
21  import java.io.IOException;
22  import java.io.PrintStream;
23  import java.sql.Date;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.apache.log4j.Logger;
29  import org.kuali.ole.gl.GeneralLedgerConstants;
30  import org.kuali.ole.gl.batch.service.CollectorHelperService;
31  import org.kuali.ole.gl.batch.service.CollectorScrubberService;
32  import org.kuali.ole.gl.batch.service.CollectorService;
33  import org.kuali.ole.gl.batch.service.RunDateService;
34  import org.kuali.ole.gl.report.CollectorReportData;
35  import org.kuali.ole.gl.service.OriginEntryGroupService;
36  import org.kuali.ole.gl.service.impl.CollectorScrubberStatus;
37  import org.kuali.ole.sys.batch.BatchInputFileType;
38  import org.kuali.ole.sys.batch.InitiateDirectoryBase;
39  import org.kuali.ole.sys.batch.service.BatchInputFileService;
40  import org.kuali.ole.sys.service.ReportWriterService;
41  import org.kuali.rice.core.api.datetime.DateTimeService;
42  import org.springframework.transaction.annotation.Transactional;
43  
44  /**
45   * The base implementation of the Collector service
46   */
47  @Transactional
48  public class CollectorServiceImpl extends InitiateDirectoryBase implements CollectorService {
49      private static Logger LOG = Logger.getLogger(CollectorServiceImpl.class);
50  
51      private CollectorHelperService collectorHelperService;
52      private BatchInputFileService batchInputFileService;
53      private List<BatchInputFileType> collectorInputFileTypes;
54      private OriginEntryGroupService originEntryGroupService;
55      private DateTimeService dateTimeService;
56      private CollectorScrubberService collectorScrubberService;
57      private RunDateService runDateService;
58      private String batchFileDirectoryName;
59      
60      private ReportWriterService collectorReportWriterService;
61  
62      /**
63       * performs collection
64       * 
65       * @return status information related to the collection execution
66       */
67      public CollectorReportData performCollection() {
68          
69          //add a step to check for directory paths
70          prepareDirectories(getRequiredDirectoryNames());
71          
72          List<String> processedFiles = new ArrayList<String>();
73          Date executionDate = dateTimeService.getCurrentSqlDate();
74  
75          Date runDate = new Date(runDateService.calculateRunDate(executionDate).getTime());
76          CollectorReportData collectorReportData = new CollectorReportData();
77          List<CollectorScrubberStatus> collectorScrubberStatuses = new ArrayList<CollectorScrubberStatus>();
78  
79          String collectorFinalOutputFileName = batchFileDirectoryName + File.separator + GeneralLedgerConstants.BatchFileSystem.COLLECTOR_OUTPUT + GeneralLedgerConstants.BatchFileSystem.EXTENSION;
80        
81          PrintStream collectorFinalOutputFilePs = null;
82          BufferedReader inputFileReader = null;
83          try {
84              collectorFinalOutputFilePs = new PrintStream(collectorFinalOutputFileName);
85                  
86          } catch (FileNotFoundException e) {
87              throw new RuntimeException("writing all collector result files to output file process Stopped: " + e.getMessage(), e);
88          }
89  
90          for (BatchInputFileType collectorInputFileType : collectorInputFileTypes) {
91              List<String> fileNamesToLoad = batchInputFileService.listInputFileNamesWithDoneFile(collectorInputFileType);
92              for (String inputFileName : fileNamesToLoad) {
93                  boolean processSuccess = false;
94                  
95                  LOG.info("Collecting file: " + inputFileName);
96                  processSuccess = collectorHelperService.loadCollectorFile(inputFileName, collectorReportData, collectorScrubberStatuses, collectorInputFileType, collectorFinalOutputFilePs);
97                  processedFiles.add(inputFileName);
98                  if (processSuccess) {
99                      renameCollectorScrubberFiles();
100                 }
101                 collectorReportData.getLoadedfileNames().add(inputFileName);
102                 
103             }
104             updateCollectorReportDataWithExecutionStatistics(collectorReportData, collectorScrubberStatuses);
105         }
106 
107         collectorScrubberService.removeTempGroups(collectorScrubberStatuses);
108         collectorFinalOutputFilePs.close();
109             
110         return collectorReportData;
111     }
112 
113     /**
114      * Clears out associated .done files for the processed data files.
115      * @param dataFileNames the name of files with done files to remove
116      */
117     protected void removeDoneFiles(List<String> dataFileNames) {
118         for (String dataFileName : dataFileNames) {
119             File doneFile = new File(StringUtils.substringBeforeLast(dataFileName, ".") + ".done");
120             if (doneFile.exists()) {
121                 doneFile.delete();
122             }
123         }
124     }
125     
126     protected void renameCollectorScrubberFiles() {
127         String filePath = batchFileDirectoryName + File.separator;
128         List<String> fileNameList = new ArrayList<String>();
129         fileNameList.add(GeneralLedgerConstants.BatchFileSystem.COLLECTOR_BACKUP_FILE);
130         fileNameList.add(GeneralLedgerConstants.BatchFileSystem.COLLECTOR_SCRUBBER_INPUT_FILE);
131         fileNameList.add(GeneralLedgerConstants.BatchFileSystem.COLLECTOR_SCRUBBER_VALID_OUTPUT_FILE);
132         fileNameList.add(GeneralLedgerConstants.BatchFileSystem.COLLECTOR_SCRUBBER_ERROR_OUTPUT_FILE);
133         fileNameList.add(GeneralLedgerConstants.BatchFileSystem.COLLECTOR_SCRUBBER_EXPIRED_OUTPUT_FILE);
134         fileNameList.add(GeneralLedgerConstants.BatchFileSystem.COLLECTOR_SCRUBBER_ERROR_SORTED_FILE);
135         fileNameList.add(GeneralLedgerConstants.BatchFileSystem.COLLECTOR_DEMERGER_VAILD_OUTPUT_FILE);
136         fileNameList.add(GeneralLedgerConstants.BatchFileSystem.COLLECTOR_DEMERGER_ERROR_OUTPUT_FILE);
137         
138         for (String fileName : fileNameList){
139             File file = new File(filePath + fileName + GeneralLedgerConstants.BatchFileSystem.EXTENSION);
140             if (file.exists()) {
141                 String changedFileName = filePath + fileName + "." + getDateTimeService().toDateTimeStringForFilename(dateTimeService.getCurrentDate());
142                 file.renameTo(new File(changedFileName + GeneralLedgerConstants.BatchFileSystem.EXTENSION));
143             }
144         }
145     }
146     
147     public void finalizeCollector(CollectorReportData collectorReportData){
148         // remove all done files for processed files
149         removeDoneFiles( (List) collectorReportData.getLoadedfileNames());
150         
151         // create a done file for collector gl output
152         String collectorFinalOutputDoneFileName = batchFileDirectoryName + File.separator + GeneralLedgerConstants.BatchFileSystem.COLLECTOR_OUTPUT + GeneralLedgerConstants.BatchFileSystem.DONE_FILE_EXTENSION;
153         File doneFile = new File (collectorFinalOutputDoneFileName);
154         if (!doneFile.exists()){
155             try {
156                 doneFile.createNewFile();
157             } catch (IOException e) {
158                 throw new RuntimeException("Error creating collector done file", e);
159             }
160         }
161     }
162     
163 
164     public void setCollectorHelperService(CollectorHelperService collectorHelperService) {
165         this.collectorHelperService = collectorHelperService;
166     }
167 
168     public void setBatchInputFileService(BatchInputFileService batchInputFileService) {
169         this.batchInputFileService = batchInputFileService;
170     }
171 
172     public void setCollectorInputFileTypes(List<BatchInputFileType> collectorInputFileTypes) {
173         this.collectorInputFileTypes = collectorInputFileTypes;
174     }
175 
176     /**
177      * Gets the originEntryGroupService attribute.
178      * 
179      * @return Returns the originEntryGroupService.
180      */
181     public OriginEntryGroupService getOriginEntryGroupService() {
182         return originEntryGroupService;
183     }
184 
185     /**
186      * Sets the originEntryGroupService attribute value.
187      * 
188      * @param originEntryGroupService The originEntryGroupService to set.
189      */
190     public void setOriginEntryGroupService(OriginEntryGroupService originEntryGroupService) {
191         this.originEntryGroupService = originEntryGroupService;
192     }
193 
194     /**
195      * Gets the dateTimeService attribute.
196      * 
197      * @return Returns the dateTimeService.
198      */
199     public DateTimeService getDateTimeService() {
200         return dateTimeService;
201     }
202 
203     /**
204      * Sets the dateTimeService attribute value.
205      * 
206      * @param dateTimeService The dateTimeService to set.
207      */
208     public void setDateTimeService(DateTimeService dateTimeService) {
209         this.dateTimeService = dateTimeService;
210     }
211 
212     /**
213      * Gets the collectorScrubberService attribute.
214      * 
215      * @return Returns the collectorScrubberService.
216      */
217     public CollectorScrubberService getCollectorScrubberService() {
218         return collectorScrubberService;
219     }
220 
221     /**
222      * Sets the collectorScrubberService attribute value.
223      * 
224      * @param collectorScrubberService The collectorScrubberService to set.
225      */
226     public void setCollectorScrubberService(CollectorScrubberService collectorScrubberService) {
227         this.collectorScrubberService = collectorScrubberService;
228     }
229 
230     /**
231      * Adds execution statistics to the Collector run
232      * 
233      * @param collectorReportData data gathered from the run of the Collector
234      * @param collectorScrubberStatuses a List of CollectorScrubberStatus records
235      */
236     protected void updateCollectorReportDataWithExecutionStatistics(CollectorReportData collectorReportData, List<CollectorScrubberStatus> collectorScrubberStatuses) {
237     }
238 
239     public RunDateService getRunDateService() {
240         return runDateService;
241     }
242 
243     public void setRunDateService(RunDateService runDateService) {
244         this.runDateService = runDateService;
245     }
246 
247     public void setBatchFileDirectoryName(String batchFileDirectoryName) {
248         this.batchFileDirectoryName = batchFileDirectoryName;
249     }
250 
251     /**
252      * Sets the collectorReportWriterService attribute value.
253      * @param collectorReportWriterService The collectorReportWriterService to set.
254      */
255     public void setCollectorReportWriterService(ReportWriterService collectorReportWriterService) {
256         this.collectorReportWriterService = collectorReportWriterService;
257     }
258 
259     /**
260      * @see org.kuali.ole.sys.batch.service.impl.InitiateDirectoryImpl#getRequiredDirectoryNames()
261      */
262     @Override
263     public List<String> getRequiredDirectoryNames() {
264         List<String> requiredDirectoryList = new ArrayList<String>();
265         for (BatchInputFileType batchInputFile : collectorInputFileTypes){
266             requiredDirectoryList.add(batchInputFile.getDirectoryPath());
267         }
268         return requiredDirectoryList;
269     }
270 }