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.module.ld.batch.service.impl; 017 018 import java.io.File; 019 import java.io.IOException; 020 import java.io.PrintStream; 021 import java.sql.Date; 022 import java.util.ArrayList; 023 import java.util.Collection; 024 import java.util.Iterator; 025 026 import org.kuali.kfs.gl.GeneralLedgerConstants; 027 import org.kuali.kfs.gl.businessobject.OriginEntryFull; 028 import org.kuali.kfs.gl.businessobject.OriginEntryGroup; 029 import org.kuali.kfs.gl.report.LedgerSummaryReport; 030 import org.kuali.kfs.gl.service.OriginEntryGroupService; 031 import org.kuali.kfs.module.ld.LaborConstants; 032 import org.kuali.kfs.module.ld.batch.service.LaborNightlyOutService; 033 import org.kuali.kfs.module.ld.businessobject.LaborGeneralLedgerEntry; 034 import org.kuali.kfs.module.ld.businessobject.LaborLedgerPendingEntry; 035 import org.kuali.kfs.module.ld.businessobject.LaborOriginEntry; 036 import org.kuali.kfs.module.ld.dataaccess.LaborClearGeneralLedgerEntryDao; 037 import org.kuali.kfs.module.ld.service.LaborLedgerPendingEntryService; 038 import org.kuali.kfs.sys.KFSConstants; 039 import org.kuali.kfs.sys.ObjectUtil; 040 import org.kuali.kfs.sys.batch.service.WrappingBatchService; 041 import org.kuali.kfs.sys.service.ReportWriterService; 042 import org.kuali.rice.kns.service.BusinessObjectService; 043 import org.kuali.rice.kns.service.DateTimeService; 044 import org.kuali.rice.kns.util.ObjectUtils; 045 import org.springframework.transaction.annotation.Transactional; 046 047 /** 048 * The class implements loading and cleanup methods for nightly batch jobs 049 */ 050 @Transactional 051 public class LaborNightlyOutServiceImpl implements LaborNightlyOutService { 052 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LaborNightlyOutServiceImpl.class); 053 054 private LaborLedgerPendingEntryService laborLedgerPendingEntryService; 055 private OriginEntryGroupService originEntryGroupService; 056 057 private BusinessObjectService businessObjectService; 058 private DateTimeService dateTimeService; 059 private String batchFileDirectoryName; 060 private String batchGlFileDirectoryName; 061 062 private LaborClearGeneralLedgerEntryDao laborClearGeneralLedgerEntryDao; 063 064 private ReportWriterService laborPendingEntryLedgerReportWriterService; 065 private ReportWriterService laborGLEntryReportWriterService; 066 067 /** 068 * @see org.kuali.kfs.module.ld.batch.service.LaborNightlyOutService#deleteCopiedPendingLedgerEntries() 069 */ 070 public void deleteCopiedPendingLedgerEntries() { 071 laborLedgerPendingEntryService.deleteByFinancialDocumentApprovedCode(KFSConstants.PENDING_ENTRY_APPROVED_STATUS_CODE.PROCESSED); 072 } 073 074 /** 075 * @see org.kuali.kfs.module.ld.batch.service.LaborNightlyOutService#copyApprovedPendingLedgerEntries() 076 */ 077 public void copyApprovedPendingLedgerEntries() { 078 Date runDate = dateTimeService.getCurrentSqlDate(); 079 080 String outputFile = batchFileDirectoryName + File.separator + LaborConstants.BatchFileSystem.NIGHTLY_OUT_FILE + GeneralLedgerConstants.BatchFileSystem.EXTENSION; 081 PrintStream outputFilePs; 082 083 try { 084 outputFilePs = new PrintStream(outputFile); 085 } 086 catch (IOException e) { 087 // FIXME: do whatever is supposed to be done here 088 throw new RuntimeException(e); 089 } 090 091 //TODO:- might need to change this part to use file not collection 092 Collection<OriginEntryFull> group = new ArrayList(); 093 Iterator<LaborLedgerPendingEntry> pendingEntries = laborLedgerPendingEntryService.findApprovedPendingLedgerEntries(); 094 095 LedgerSummaryReport nightlyOutLedgerSummaryReport = new LedgerSummaryReport(); 096 while (pendingEntries != null && pendingEntries.hasNext()) { 097 LaborLedgerPendingEntry pendingEntry = pendingEntries.next(); 098 LaborOriginEntry entry = new LaborOriginEntry(pendingEntry); 099 // copy the pending entry to labor origin entry table 100 // TODO:- do we need it??? 101 // boolean isSaved = saveAsLaborOriginEntry(pendingEntry, group); 102 boolean isSaved = true; 103 104 try { 105 outputFilePs.printf("%s\n", entry.getLine()); 106 nightlyOutLedgerSummaryReport.summarizeEntry(entry); 107 } catch (Exception e) { 108 throw new RuntimeException(e); 109 } 110 111 if (isSaved) { 112 // update the pending entry to indicate it has been copied 113 pendingEntry.setFinancialDocumentApprovedCode(KFSConstants.PENDING_ENTRY_APPROVED_STATUS_CODE.PROCESSED); 114 pendingEntry.setTransactionDate(runDate); 115 businessObjectService.save(pendingEntry); 116 } 117 } 118 119 outputFilePs.close(); 120 121 //create done file 122 String doneFileName = outputFile.replace(GeneralLedgerConstants.BatchFileSystem.EXTENSION, GeneralLedgerConstants.BatchFileSystem.DONE_FILE_EXTENSION); 123 File doneFile = new File (doneFileName); 124 if (!doneFile.exists()){ 125 try { 126 doneFile.createNewFile(); 127 } catch (IOException e) { 128 throw new RuntimeException(); 129 } 130 } 131 132 try { 133 ((WrappingBatchService)laborPendingEntryLedgerReportWriterService).initialize(); 134 nightlyOutLedgerSummaryReport.writeReport(laborPendingEntryLedgerReportWriterService); 135 } finally { 136 ((WrappingBatchService)laborPendingEntryLedgerReportWriterService).destroy(); 137 } 138 } 139 140 /** 141 * @see org.kuali.kfs.module.ld.batch.service.LaborNightlyOutService#deleteCopiedLaborGenerealLedgerEntries() 142 */ 143 public void deleteCopiedLaborGenerealLedgerEntries() { 144 laborClearGeneralLedgerEntryDao.deleteCopiedLaborGenerealLedgerEntries(); 145 } 146 147 /** 148 * @see org.kuali.kfs.module.ld.batch.service.LaborNightlyOutService#copyLaborGenerealLedgerEntries() 149 */ 150 public void copyLaborGenerealLedgerEntries() { 151 String outputFile = batchGlFileDirectoryName + File.separator + LaborConstants.BatchFileSystem.LABOR_GL_ENTRY_FILE + GeneralLedgerConstants.BatchFileSystem.EXTENSION; 152 PrintStream outputFilePs; 153 154 try { 155 outputFilePs = new PrintStream(outputFile); 156 } 157 catch (IOException e) { 158 // FIXME: do whatever is supposed to be done here 159 throw new RuntimeException(e); 160 } 161 162 // copy the labor general ledger entry to origin entry table 163 Collection<LaborGeneralLedgerEntry> generalLedgerEntries = businessObjectService.findAll(LaborGeneralLedgerEntry.class); 164 int numberOfGLEntries = generalLedgerEntries.size(); 165 166 LedgerSummaryReport laborGLSummaryReport = new LedgerSummaryReport(); 167 for (LaborGeneralLedgerEntry entry : generalLedgerEntries) { 168 OriginEntryFull originEntry = new OriginEntryFull(); 169 ObjectUtil.buildObject(originEntry, entry); 170 171 //write to file 172 try { 173 outputFilePs.printf("%s\n", originEntry.getLine()); 174 laborGLSummaryReport.summarizeEntry(originEntry); 175 } catch (Exception e) { 176 throw new RuntimeException(e); 177 } 178 //boolean isSaved = saveAsGLOriginEntry(entry, group); 179 } 180 181 outputFilePs.close(); 182 183 //create done file 184 String doneFileName = outputFile.replace(GeneralLedgerConstants.BatchFileSystem.EXTENSION, GeneralLedgerConstants.BatchFileSystem.DONE_FILE_EXTENSION); 185 File doneFile = new File (doneFileName); 186 if (!doneFile.exists()){ 187 try { 188 doneFile.createNewFile(); 189 } catch (IOException e) { 190 throw new RuntimeException(); 191 } 192 } 193 194 try { 195 ((WrappingBatchService)laborGLEntryReportWriterService).initialize(); 196 laborGLSummaryReport.writeReport(laborGLEntryReportWriterService); 197 } finally { 198 ((WrappingBatchService)laborGLEntryReportWriterService).destroy(); 199 } 200 } 201 202 /* 203 * save the given pending ledger entry as a labor origin entry 204 */ 205 protected boolean saveAsLaborOriginEntry(LaborLedgerPendingEntry pendingEntry, OriginEntryGroup group) { 206 try { 207 LaborOriginEntry originEntry = new LaborOriginEntry(); 208 ObjectUtil.buildObject(originEntry, pendingEntry); 209 210 originEntry.setTransactionPostingDate(group.getDate()); 211 originEntry.setEntryGroupId(group.getId()); 212 213 businessObjectService.save(originEntry); 214 } 215 catch (Exception e) { 216 LOG.debug("Fail to copy the pending entry as origin entry" + e); 217 return false; 218 } 219 return true; 220 } 221 222 /* 223 * save the given pending ledger entry as a labor origin entry 224 */ 225 protected boolean saveAsLaborOriginEntry(LaborLedgerPendingEntry pendingEntry) { 226 try { 227 LaborOriginEntry originEntry = new LaborOriginEntry(); 228 ObjectUtil.buildObject(originEntry, pendingEntry); 229 230 //originEntry.setTransactionPostingDate(group.getDate()); 231 //originEntry.setEntryGroupId(group.getId()); 232 233 businessObjectService.save(originEntry); 234 } 235 catch (Exception e) { 236 LOG.debug("Fail to copy the pending entry as origin entry" + e); 237 return false; 238 } 239 return true; 240 } 241 242 /* 243 * save the given pending ledger entry as a labor origin entry 244 */ 245 protected boolean saveAsGLOriginEntry(LaborGeneralLedgerEntry entry, OriginEntryGroup group) { 246 try { 247 OriginEntryFull originEntry = new OriginEntryFull(); 248 ObjectUtil.buildObject(originEntry, entry); 249 250 originEntry.setEntryGroupId(group.getId()); 251 businessObjectService.save(originEntry); 252 } 253 catch (Exception e) { 254 LOG.debug("Fail to copy the labor GL entry as an origin entry" + e); 255 return false; 256 } 257 return true; 258 } 259 260 /** 261 * Sets the businessObjectService attribute value. 262 * 263 * @param businessObjectService The businessObjectService to set. 264 */ 265 public void setBusinessObjectService(BusinessObjectService businessObjectService) { 266 this.businessObjectService = businessObjectService; 267 } 268 269 /** 270 * Sets the dateTimeService attribute value. 271 * 272 * @param dateTimeService The dateTimeService to set. 273 */ 274 public void setDateTimeService(DateTimeService dateTimeService) { 275 this.dateTimeService = dateTimeService; 276 } 277 278 /** 279 * Sets the laborLedgerPendingEntryService attribute value. 280 * 281 * @param laborLedgerPendingEntryService The laborLedgerPendingEntryService to set. 282 */ 283 public void setLaborLedgerPendingEntryService(LaborLedgerPendingEntryService laborLedgerPendingEntryService) { 284 this.laborLedgerPendingEntryService = laborLedgerPendingEntryService; 285 } 286 287 /** 288 * Sets the originEntryGroupService attribute value. 289 * 290 * @param originEntryGroupService The originEntryGroupService to set. 291 */ 292 public void setOriginEntryGroupService(OriginEntryGroupService originEntryGroupService) { 293 this.originEntryGroupService = originEntryGroupService; 294 } 295 296 public void setBatchFileDirectoryName(String batchFileDirectoryName) { 297 this.batchFileDirectoryName = batchFileDirectoryName; 298 } 299 300 public void setBatchGlFileDirectoryName(String batchGlFileDirectoryName) { 301 this.batchGlFileDirectoryName = batchGlFileDirectoryName; 302 } 303 304 /** 305 * Sets the laborPendingEntryLedgerReportWriterService attribute value. 306 * @param laborPendingEntryLedgerReportWriterService The laborPendingEntryLedgerReportWriterService to set. 307 */ 308 public void setLaborPendingEntryLedgerReportWriterService(ReportWriterService laborPendingEntryLedgerReportWriterService) { 309 this.laborPendingEntryLedgerReportWriterService = laborPendingEntryLedgerReportWriterService; 310 } 311 312 /** 313 * Sets the laborGLEntryReportWriterService attribute value. 314 * @param laborGLEntryReportWriterService The laborGLEntryReportWriterService to set. 315 */ 316 public void setLaborGLEntryReportWriterService(ReportWriterService laborGLEntryReportWriterService) { 317 this.laborGLEntryReportWriterService = laborGLEntryReportWriterService; 318 } 319 320 /** 321 * Gets the laborClearGeneralLedgerEntryDao attribute. 322 * 323 * @return Returns the laborClearGeneralLedgerEntryDao. 324 */ 325 protected LaborClearGeneralLedgerEntryDao getLaborClearGeneralLedgerEntryDao() { 326 return laborClearGeneralLedgerEntryDao; 327 } 328 329 /** 330 * Sets the laborClearGeneralLedgerEntryDao attribute value. 331 * 332 * @param laborClearGeneralLedgerEntryDao The laborClearGeneralLedgerEntryDao to set. 333 */ 334 public void setLaborClearGeneralLedgerEntryDao(LaborClearGeneralLedgerEntryDao laborClearGeneralLedgerEntryDao) { 335 this.laborClearGeneralLedgerEntryDao = laborClearGeneralLedgerEntryDao; 336 } 337 338 339 }