View Javadoc
1   /*
2    * Copyright 2007 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.pdp.batch.service.impl;
17  
18  import java.io.File;
19  import java.io.FileNotFoundException;
20  import java.io.IOException;
21  import java.io.PrintStream;
22  import java.sql.Date;
23  import java.text.MessageFormat;
24  import java.util.Iterator;
25  
26  import org.kuali.ole.gl.GeneralLedgerConstants;
27  import org.kuali.ole.gl.report.LedgerSummaryReport;
28  import org.kuali.ole.gl.service.OriginEntryGroupService;
29  import org.kuali.ole.gl.service.OriginEntryService;
30  import org.kuali.ole.pdp.PdpKeyConstants;
31  import org.kuali.ole.pdp.batch.service.ExtractTransactionsService;
32  import org.kuali.ole.pdp.businessobject.GlPendingTransaction;
33  import org.kuali.ole.pdp.service.PendingTransactionService;
34  import org.kuali.ole.sys.service.ReportWriterService;
35  import org.kuali.rice.core.api.config.property.ConfigurationService;
36  import org.kuali.rice.core.api.datetime.DateTimeService;
37  import org.springframework.transaction.annotation.Transactional;
38  
39  @Transactional
40  public class ExtractTransactionsServiceImpl implements ExtractTransactionsService {
41      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ExtractTransactionsServiceImpl.class);
42  
43      private PendingTransactionService glPendingTransactionService;
44      private OriginEntryGroupService originEntryGroupService;
45      private OriginEntryService originEntryService;
46      private DateTimeService dateTimeService;
47      private ConfigurationService kualiConfigurationService;
48      private String batchFileDirectoryName;
49      private ReportWriterService reportWriterService;
50  
51      /**
52       * @see org.kuali.ole.pdp.batch.service.ExtractTransactionsService#extractGlTransactions()
53       */
54      public void extractGlTransactions() {
55          LOG.debug("extractGlTransactions() started");
56  
57          Date processDate = dateTimeService.getCurrentSqlDate();
58  
59          // add time info to filename - better to move in common place?
60          java.util.Date jobRunDate = dateTimeService.getCurrentDate();
61          String fileTimeInfo = "." + dateTimeService.toDateTimeStringForFilename(jobRunDate);
62  
63          String extractTGlTransactionFileName = GeneralLedgerConstants.BatchFileSystem.EXTRACT_TRANSACTION_FILE + fileTimeInfo + GeneralLedgerConstants.BatchFileSystem.EXTENSION;
64          File extractTGlTransactionFile = new File(batchFileDirectoryName + File.separator + extractTGlTransactionFileName);
65          PrintStream extractTGlTransactionPS = null;
66  
67          try {
68              extractTGlTransactionPS = new PrintStream(extractTGlTransactionFile);
69          }
70          catch (FileNotFoundException e) {
71              throw new RuntimeException("extract transaction file doesn't exist " + extractTGlTransactionFileName);
72          }
73  
74  
75          Iterator transactions = glPendingTransactionService.getUnextractedTransactions();
76          LedgerSummaryReport extractGlSummaryReport = new LedgerSummaryReport();
77          while (transactions.hasNext()) {
78              GlPendingTransaction tran = (GlPendingTransaction) transactions.next();
79              //write to file
80              extractTGlTransactionPS.printf("%s\n", tran.getOriginEntry().getLine());
81              
82              extractGlSummaryReport.summarizeEntry(tran.getOriginEntry());
83              
84              tran.setProcessInd(true);
85              glPendingTransactionService.save(tran);
86          }
87  
88          if (extractTGlTransactionPS != null) {
89              extractTGlTransactionPS.close();
90  
91              // create done file
92              String extractTGlTransactionDoneFileName = extractTGlTransactionFileName.replace(GeneralLedgerConstants.BatchFileSystem.EXTENSION, GeneralLedgerConstants.BatchFileSystem.DONE_FILE_EXTENSION);
93              File extractTGlTransactionDoneFile = new File(batchFileDirectoryName + File.separator + extractTGlTransactionDoneFileName);
94              if (!extractTGlTransactionDoneFile.exists()) {
95                  try {
96                      extractTGlTransactionDoneFile.createNewFile();
97                  }
98                  catch (IOException e) {
99                      throw new RuntimeException();
100                 }
101             }
102 
103             String reportTitle = this.kualiConfigurationService.getPropertyValueAsString(PdpKeyConstants.EXTRACT_TRANSACTION_SERVICE_REPORT_TITLE);
104             reportTitle = MessageFormat.format(reportTitle, new Object[] { null });
105 
106             String reportFilename = this.kualiConfigurationService.getPropertyValueAsString(PdpKeyConstants.EXTRACT_TRANSACTION_SERVICE_REPORT_FILENAME);
107             reportFilename = MessageFormat.format(reportFilename, new Object[] { null });
108 
109             // Run a report
110             extractGlSummaryReport.writeReport(reportWriterService);
111         }
112     }
113 
114     public void setDateTimeService(DateTimeService dateTimeService) {
115         this.dateTimeService = dateTimeService;
116     }
117 
118     public void setGlPendingTransactionService(PendingTransactionService glPendingTransactionService) {
119         this.glPendingTransactionService = glPendingTransactionService;
120     }
121 
122     public void setOriginEntryGroupService(OriginEntryGroupService originEntryGroupService) {
123         this.originEntryGroupService = originEntryGroupService;
124     }
125 
126     public void setOriginEntryService(OriginEntryService originEntryService) {
127         this.originEntryService = originEntryService;
128     }
129 
130     public void setConfigurationService(ConfigurationService kualiConfigurationService) {
131         this.kualiConfigurationService = kualiConfigurationService;
132     }
133 
134     public void setBatchFileDirectoryName(String batchFileDirectoryName) {
135         this.batchFileDirectoryName = batchFileDirectoryName;
136     }
137 
138     public void setReportWriterService(ReportWriterService reportWriterService) {
139         this.reportWriterService = reportWriterService;
140     }
141 
142     
143 }