001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.gl.service.impl;
017:
018: import java.io.File;
019: import java.sql.Date;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.List;
023:
024: import org.apache.commons.lang.StringUtils;
025: import org.apache.log4j.Logger;
026: import org.kuali.core.service.DateTimeService;
027: import org.kuali.kfs.batch.BatchInputFileType;
028: import org.kuali.kfs.service.BatchInputFileService;
029: import org.kuali.module.gl.bo.OriginEntryGroup;
030: import org.kuali.module.gl.bo.OriginEntrySource;
031: import org.kuali.module.gl.service.CollectorHelperService;
032: import org.kuali.module.gl.service.CollectorScrubberService;
033: import org.kuali.module.gl.service.CollectorService;
034: import org.kuali.module.gl.service.OriginEntryGroupService;
035: import org.kuali.module.gl.service.OriginEntryService;
036: import org.kuali.module.gl.service.RunDateService;
037: import org.kuali.module.gl.util.CollectorReportData;
038: import org.kuali.module.gl.util.CollectorScrubberStatus;
039: import org.kuali.module.gl.util.LedgerEntryHolder;
040: import org.springframework.transaction.annotation.Transactional;
041:
042: /**
043: * The base implementation of the Collector service
044: */
045: @Transactional
046: public class CollectorServiceImpl implements CollectorService {
047: private static Logger LOG = Logger
048: .getLogger(CollectorServiceImpl.class);
049:
050: private CollectorHelperService collectorHelperService;
051: private BatchInputFileService batchInputFileService;
052: private BatchInputFileType collectorInputFileType;
053: private OriginEntryGroupService originEntryGroupService;
054: private DateTimeService dateTimeService;
055: private CollectorScrubberService collectorScrubberService;
056: private RunDateService runDateService;
057:
058: /**
059: * performs collection
060: *
061: * @return status information related to the collection execution
062: */
063: public CollectorReportData performCollection() {
064: List<String> fileNamesToLoad = batchInputFileService
065: .listInputFileNamesWithDoneFile(collectorInputFileType);
066: List<String> processedFiles = new ArrayList();
067:
068: Date executionDate = dateTimeService.getCurrentSqlDate();
069:
070: Date runDate = new Date(runDateService.calculateRunDate(
071: executionDate).getTime());
072: OriginEntryGroup group = originEntryGroupService
073: .createGroup(runDate, OriginEntrySource.COLLECTOR,
074: true, false, true);
075: CollectorReportData collectorReportData = new CollectorReportData();
076: List<CollectorScrubberStatus> collectorScrubberStatuses = new ArrayList<CollectorScrubberStatus>();
077:
078: try {
079: for (String inputFileName : fileNamesToLoad) {
080: boolean processSuccess = false;
081: try {
082: LOG.info("Collecting file: " + inputFileName);
083: processSuccess = collectorHelperService
084: .loadCollectorFile(inputFileName, group,
085: collectorReportData,
086: collectorScrubberStatuses);
087: } catch (RuntimeException e) {
088: LOG.error(
089: "Caught exception trying to load collector file: "
090: + inputFileName, e);
091: }
092: processedFiles.add(inputFileName);
093: }
094: updateCollectorReportDataWithExecutionStatistics(
095: collectorReportData, collectorScrubberStatuses);
096: } finally {
097: collectorScrubberService
098: .removeTempGroups(collectorScrubberStatuses);
099: group.setProcess(true);
100: originEntryGroupService.save(group);
101: removeDoneFiles(processedFiles);
102: }
103: return collectorReportData;
104: }
105:
106: /**
107: * Clears out associated .done files for the processed data files.
108: * @param dataFileNames the name of files with done files to remove
109: */
110: private void removeDoneFiles(List<String> dataFileNames) {
111: for (String dataFileName : dataFileNames) {
112: File doneFile = new File(StringUtils.substringBeforeLast(
113: dataFileName, ".")
114: + ".done");
115: if (doneFile.exists()) {
116: doneFile.delete();
117: }
118: }
119: }
120:
121: public void setCollectorHelperService(
122: CollectorHelperService collectorHelperService) {
123: this .collectorHelperService = collectorHelperService;
124: }
125:
126: public void setBatchInputFileService(
127: BatchInputFileService batchInputFileService) {
128: this .batchInputFileService = batchInputFileService;
129: }
130:
131: public void setCollectorInputFileType(
132: BatchInputFileType collectorInputFileType) {
133: this .collectorInputFileType = collectorInputFileType;
134: }
135:
136: /**
137: * Gets the originEntryGroupService attribute.
138: *
139: * @return Returns the originEntryGroupService.
140: */
141: public OriginEntryGroupService getOriginEntryGroupService() {
142: return originEntryGroupService;
143: }
144:
145: /**
146: * Sets the originEntryGroupService attribute value.
147: *
148: * @param originEntryGroupService The originEntryGroupService to set.
149: */
150: public void setOriginEntryGroupService(
151: OriginEntryGroupService originEntryGroupService) {
152: this .originEntryGroupService = originEntryGroupService;
153: }
154:
155: /**
156: * Gets the dateTimeService attribute.
157: *
158: * @return Returns the dateTimeService.
159: */
160: public DateTimeService getDateTimeService() {
161: return dateTimeService;
162: }
163:
164: /**
165: * Sets the dateTimeService attribute value.
166: *
167: * @param dateTimeService The dateTimeService to set.
168: */
169: public void setDateTimeService(DateTimeService dateTimeService) {
170: this .dateTimeService = dateTimeService;
171: }
172:
173: /**
174: * Gets the collectorScrubberService attribute.
175: *
176: * @return Returns the collectorScrubberService.
177: */
178: public CollectorScrubberService getCollectorScrubberService() {
179: return collectorScrubberService;
180: }
181:
182: /**
183: * Sets the collectorScrubberService attribute value.
184: *
185: * @param collectorScrubberService The collectorScrubberService to set.
186: */
187: public void setCollectorScrubberService(
188: CollectorScrubberService collectorScrubberService) {
189: this .collectorScrubberService = collectorScrubberService;
190: }
191:
192: /**
193: * Adds execution statistics to the Collector run
194: *
195: * @param collectorReportData data gathered from the run of the Collector
196: * @param collectorScrubberStatuses a List of CollectorScrubberStatus records
197: */
198: protected void updateCollectorReportDataWithExecutionStatistics(
199: CollectorReportData collectorReportData,
200: List<CollectorScrubberStatus> collectorScrubberStatuses) {
201: Collection<OriginEntryGroup> inputGroups = new ArrayList<OriginEntryGroup>();
202:
203: // NOTE: this implementation does not support the use of multiple origin entry group service/origin entry services
204: for (CollectorScrubberStatus collectorScrubberStatus : collectorScrubberStatuses) {
205: inputGroups.add(collectorScrubberStatus.getValidGroup());
206: }
207:
208: if (inputGroups.size() > 0
209: && collectorScrubberStatuses.size() > 0) {
210: OriginEntryService collectorScrubberOriginEntryService = collectorScrubberStatuses
211: .get(0).getOriginEntryService();
212: LedgerEntryHolder ledgerEntryHolder = collectorScrubberOriginEntryService
213: .getSummaryByGroupId(inputGroups);
214: collectorReportData.setLedgerEntryHolder(ledgerEntryHolder);
215: }
216: }
217:
218: public RunDateService getRunDateService() {
219: return runDateService;
220: }
221:
222: public void setRunDateService(RunDateService runDateService) {
223: this.runDateService = runDateService;
224: }
225: }
|