1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
64
65
66
67 public CollectorReportData performCollection() {
68
69
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
115
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
149 removeDoneFiles( (List) collectorReportData.getLoadedfileNames());
150
151
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
178
179
180
181 public OriginEntryGroupService getOriginEntryGroupService() {
182 return originEntryGroupService;
183 }
184
185
186
187
188
189
190 public void setOriginEntryGroupService(OriginEntryGroupService originEntryGroupService) {
191 this.originEntryGroupService = originEntryGroupService;
192 }
193
194
195
196
197
198
199 public DateTimeService getDateTimeService() {
200 return dateTimeService;
201 }
202
203
204
205
206
207
208 public void setDateTimeService(DateTimeService dateTimeService) {
209 this.dateTimeService = dateTimeService;
210 }
211
212
213
214
215
216
217 public CollectorScrubberService getCollectorScrubberService() {
218 return collectorScrubberService;
219 }
220
221
222
223
224
225
226 public void setCollectorScrubberService(CollectorScrubberService collectorScrubberService) {
227 this.collectorScrubberService = collectorScrubberService;
228 }
229
230
231
232
233
234
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
253
254
255 public void setCollectorReportWriterService(ReportWriterService collectorReportWriterService) {
256 this.collectorReportWriterService = collectorReportWriterService;
257 }
258
259
260
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 }