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.service.impl;
017
018 import java.io.BufferedReader;
019 import java.io.File;
020 import java.io.FileNotFoundException;
021 import java.io.FileReader;
022 import java.io.IOException;
023 import java.io.PrintStream;
024 import java.sql.Date;
025 import java.util.ArrayList;
026 import java.util.Collection;
027 import java.util.List;
028
029 import org.apache.commons.lang.StringUtils;
030 import org.apache.log4j.Logger;
031 import org.kuali.kfs.gl.GeneralLedgerConstants;
032 import org.kuali.kfs.gl.batch.service.CollectorHelperService;
033 import org.kuali.kfs.gl.batch.service.CollectorScrubberService;
034 import org.kuali.kfs.gl.batch.service.CollectorService;
035 import org.kuali.kfs.gl.batch.service.RunDateService;
036 import org.kuali.kfs.gl.businessobject.LedgerEntryHolder;
037 import org.kuali.kfs.gl.businessobject.OriginEntryGroup;
038 import org.kuali.kfs.gl.businessobject.OriginEntrySource;
039 import org.kuali.kfs.gl.report.CollectorReportData;
040 import org.kuali.kfs.gl.service.OriginEntryGroupService;
041 import org.kuali.kfs.gl.service.OriginEntryService;
042 import org.kuali.kfs.gl.service.impl.CollectorScrubberStatus;
043 import org.kuali.kfs.sys.batch.BatchInputFileType;
044 import org.kuali.kfs.sys.batch.service.BatchInputFileService;
045 import org.kuali.kfs.sys.service.ReportWriterService;
046 import org.kuali.rice.kns.service.DateTimeService;
047 import org.springframework.transaction.annotation.Transactional;
048
049 /**
050 * The base implementation of the Collector service
051 */
052 @Transactional
053 public class CollectorServiceImpl implements CollectorService {
054 private static Logger LOG = Logger.getLogger(CollectorServiceImpl.class);
055
056 private CollectorHelperService collectorHelperService;
057 private BatchInputFileService batchInputFileService;
058 private List<BatchInputFileType> collectorInputFileTypes;
059 private OriginEntryGroupService originEntryGroupService;
060 private DateTimeService dateTimeService;
061 private CollectorScrubberService collectorScrubberService;
062 private RunDateService runDateService;
063 private String batchFileDirectoryName;
064
065 private ReportWriterService collectorReportWriterService;
066
067 /**
068 * performs collection
069 *
070 * @return status information related to the collection execution
071 */
072 public CollectorReportData performCollection() {
073 List<String> processedFiles = new ArrayList<String>();
074 Date executionDate = dateTimeService.getCurrentSqlDate();
075
076 Date runDate = new Date(runDateService.calculateRunDate(executionDate).getTime());
077 CollectorReportData collectorReportData = new CollectorReportData();
078 List<CollectorScrubberStatus> collectorScrubberStatuses = new ArrayList<CollectorScrubberStatus>();
079
080 String collectorFinalOutputFileName = batchFileDirectoryName + File.separator + GeneralLedgerConstants.BatchFileSystem.COLLECTOR_OUTPUT + GeneralLedgerConstants.BatchFileSystem.EXTENSION;
081
082 PrintStream collectorFinalOutputFilePs = null;
083 BufferedReader inputFileReader = null;
084 try {
085 collectorFinalOutputFilePs = new PrintStream(collectorFinalOutputFileName);
086
087 } catch (FileNotFoundException e) {
088 throw new RuntimeException("writing all collector result files to output file process Stopped: " + e.getMessage(), e);
089 }
090
091 for (BatchInputFileType collectorInputFileType : collectorInputFileTypes) {
092 List<String> fileNamesToLoad = batchInputFileService.listInputFileNamesWithDoneFile(collectorInputFileType);
093 for (String inputFileName : fileNamesToLoad) {
094 boolean processSuccess = false;
095
096 LOG.info("Collecting file: " + inputFileName);
097 processSuccess = collectorHelperService.loadCollectorFile(inputFileName, collectorReportData, collectorScrubberStatuses, collectorInputFileType, collectorFinalOutputFilePs);
098 processedFiles.add(inputFileName);
099 if (processSuccess) {
100 renameCollectorScrubberFiles();
101 }
102 collectorReportData.getLoadedfileNames().add(inputFileName);
103
104 }
105 updateCollectorReportDataWithExecutionStatistics(collectorReportData, collectorScrubberStatuses);
106 }
107
108 collectorScrubberService.removeTempGroups(collectorScrubberStatuses);
109 collectorFinalOutputFilePs.close();
110
111 return collectorReportData;
112 }
113
114 /**
115 * Clears out associated .done files for the processed data files.
116 * @param dataFileNames the name of files with done files to remove
117 */
118 protected void removeDoneFiles(List<String> dataFileNames) {
119 for (String dataFileName : dataFileNames) {
120 File doneFile = new File(StringUtils.substringBeforeLast(dataFileName, ".") + ".done");
121 if (doneFile.exists()) {
122 doneFile.delete();
123 }
124 }
125 }
126
127 protected void renameCollectorScrubberFiles() {
128 String filePath = batchFileDirectoryName + File.separator;
129 List<String> fileNameList = new ArrayList<String>();
130 fileNameList.add(GeneralLedgerConstants.BatchFileSystem.COLLECTOR_BACKUP_FILE);
131 fileNameList.add(GeneralLedgerConstants.BatchFileSystem.COLLECTOR_SCRUBBER_INPUT_FILE);
132 fileNameList.add(GeneralLedgerConstants.BatchFileSystem.COLLECTOR_SCRUBBER_VALID_OUTPUT_FILE);
133 fileNameList.add(GeneralLedgerConstants.BatchFileSystem.COLLECTOR_SCRUBBER_ERROR_OUTPUT_FILE);
134 fileNameList.add(GeneralLedgerConstants.BatchFileSystem.COLLECTOR_SCRUBBER_EXPIRED_OUTPUT_FILE);
135 fileNameList.add(GeneralLedgerConstants.BatchFileSystem.COLLECTOR_SCRUBBER_ERROR_SORTED_FILE);
136 fileNameList.add(GeneralLedgerConstants.BatchFileSystem.COLLECTOR_DEMERGER_VAILD_OUTPUT_FILE);
137 fileNameList.add(GeneralLedgerConstants.BatchFileSystem.COLLECTOR_DEMERGER_ERROR_OUTPUT_FILE);
138
139 for (String fileName : fileNameList){
140 File file = new File(filePath + fileName + GeneralLedgerConstants.BatchFileSystem.EXTENSION);
141 if (file.exists()) {
142 String changedFileName = filePath + fileName + "." + getDateTimeService().toDateTimeStringForFilename(dateTimeService.getCurrentDate());
143 file.renameTo(new File(changedFileName + GeneralLedgerConstants.BatchFileSystem.EXTENSION));
144 }
145 }
146 }
147
148 public void finalizeCollector(CollectorReportData collectorReportData){
149 // remove all done files for processed files
150 removeDoneFiles( (List) collectorReportData.getLoadedfileNames());
151
152 // create a done file for collector gl output
153 String collectorFinalOutputDoneFileName = batchFileDirectoryName + File.separator + GeneralLedgerConstants.BatchFileSystem.COLLECTOR_OUTPUT + GeneralLedgerConstants.BatchFileSystem.DONE_FILE_EXTENSION;
154 File doneFile = new File (collectorFinalOutputDoneFileName);
155 if (!doneFile.exists()){
156 try {
157 doneFile.createNewFile();
158 } catch (IOException e) {
159 throw new RuntimeException("Error creating collector done file", e);
160 }
161 }
162 }
163
164
165 public void setCollectorHelperService(CollectorHelperService collectorHelperService) {
166 this.collectorHelperService = collectorHelperService;
167 }
168
169 public void setBatchInputFileService(BatchInputFileService batchInputFileService) {
170 this.batchInputFileService = batchInputFileService;
171 }
172
173 public void setCollectorInputFileTypes(List<BatchInputFileType> collectorInputFileTypes) {
174 this.collectorInputFileTypes = collectorInputFileTypes;
175 }
176
177 /**
178 * Gets the originEntryGroupService attribute.
179 *
180 * @return Returns the originEntryGroupService.
181 */
182 public OriginEntryGroupService getOriginEntryGroupService() {
183 return originEntryGroupService;
184 }
185
186 /**
187 * Sets the originEntryGroupService attribute value.
188 *
189 * @param originEntryGroupService The originEntryGroupService to set.
190 */
191 public void setOriginEntryGroupService(OriginEntryGroupService originEntryGroupService) {
192 this.originEntryGroupService = originEntryGroupService;
193 }
194
195 /**
196 * Gets the dateTimeService attribute.
197 *
198 * @return Returns the dateTimeService.
199 */
200 public DateTimeService getDateTimeService() {
201 return dateTimeService;
202 }
203
204 /**
205 * Sets the dateTimeService attribute value.
206 *
207 * @param dateTimeService The dateTimeService to set.
208 */
209 public void setDateTimeService(DateTimeService dateTimeService) {
210 this.dateTimeService = dateTimeService;
211 }
212
213 /**
214 * Gets the collectorScrubberService attribute.
215 *
216 * @return Returns the collectorScrubberService.
217 */
218 public CollectorScrubberService getCollectorScrubberService() {
219 return collectorScrubberService;
220 }
221
222 /**
223 * Sets the collectorScrubberService attribute value.
224 *
225 * @param collectorScrubberService The collectorScrubberService to set.
226 */
227 public void setCollectorScrubberService(CollectorScrubberService collectorScrubberService) {
228 this.collectorScrubberService = collectorScrubberService;
229 }
230
231 /**
232 * Adds execution statistics to the Collector run
233 *
234 * @param collectorReportData data gathered from the run of the Collector
235 * @param collectorScrubberStatuses a List of CollectorScrubberStatus records
236 */
237 protected void updateCollectorReportDataWithExecutionStatistics(CollectorReportData collectorReportData, List<CollectorScrubberStatus> collectorScrubberStatuses) {
238 }
239
240 public RunDateService getRunDateService() {
241 return runDateService;
242 }
243
244 public void setRunDateService(RunDateService runDateService) {
245 this.runDateService = runDateService;
246 }
247
248 public void setBatchFileDirectoryName(String batchFileDirectoryName) {
249 this.batchFileDirectoryName = batchFileDirectoryName;
250 }
251
252 /**
253 * Sets the collectorReportWriterService attribute value.
254 * @param collectorReportWriterService The collectorReportWriterService to set.
255 */
256 public void setCollectorReportWriterService(ReportWriterService collectorReportWriterService) {
257 this.collectorReportWriterService = collectorReportWriterService;
258 }
259 }