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.pdp.batch.service.impl;
017    
018    import java.io.File;
019    import java.io.FileNotFoundException;
020    import java.io.IOException;
021    import java.io.PrintStream;
022    import java.sql.Date;
023    import java.text.MessageFormat;
024    import java.util.Iterator;
025    
026    import org.kuali.kfs.gl.GeneralLedgerConstants;
027    import org.kuali.kfs.gl.report.LedgerSummaryReport;
028    import org.kuali.kfs.gl.service.OriginEntryGroupService;
029    import org.kuali.kfs.gl.service.OriginEntryService;
030    import org.kuali.kfs.pdp.PdpKeyConstants;
031    import org.kuali.kfs.pdp.batch.service.ExtractTransactionsService;
032    import org.kuali.kfs.pdp.businessobject.GlPendingTransaction;
033    import org.kuali.kfs.pdp.service.PendingTransactionService;
034    import org.kuali.kfs.sys.service.ReportWriterService;
035    import org.kuali.rice.kns.service.DateTimeService;
036    import org.kuali.rice.kns.service.KualiConfigurationService;
037    import org.springframework.transaction.annotation.Transactional;
038    
039    @Transactional
040    public class ExtractTransactionsServiceImpl implements ExtractTransactionsService {
041        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ExtractTransactionsServiceImpl.class);
042    
043        private PendingTransactionService glPendingTransactionService;
044        private OriginEntryGroupService originEntryGroupService;
045        private OriginEntryService originEntryService;
046        private DateTimeService dateTimeService;
047        private KualiConfigurationService kualiConfigurationService;
048        private String batchFileDirectoryName;
049        private ReportWriterService reportWriterService;
050    
051        /**
052         * @see org.kuali.kfs.pdp.batch.service.ExtractTransactionsService#extractGlTransactions()
053         */
054        public void extractGlTransactions() {
055            LOG.debug("extractGlTransactions() started");
056    
057            Date processDate = dateTimeService.getCurrentSqlDate();
058    
059            // add time info to filename - better to move in common place?
060            java.util.Date jobRunDate = dateTimeService.getCurrentDate();
061            String fileTimeInfo = "." + dateTimeService.toDateTimeStringForFilename(jobRunDate);
062    
063            String extractTGlTransactionFileName = GeneralLedgerConstants.BatchFileSystem.EXTRACT_TRANSACTION_FILE + fileTimeInfo + GeneralLedgerConstants.BatchFileSystem.EXTENSION;
064            File extractTGlTransactionFile = new File(batchFileDirectoryName + File.separator + extractTGlTransactionFileName);
065            PrintStream extractTGlTransactionPS = null;
066    
067            try {
068                extractTGlTransactionPS = new PrintStream(extractTGlTransactionFile);
069            }
070            catch (FileNotFoundException e) {
071                throw new RuntimeException("extract transaction file doesn't exist " + extractTGlTransactionFileName);
072            }
073    
074    
075            Iterator transactions = glPendingTransactionService.getUnextractedTransactions();
076            LedgerSummaryReport extractGlSummaryReport = new LedgerSummaryReport();
077            while (transactions.hasNext()) {
078                GlPendingTransaction tran = (GlPendingTransaction) transactions.next();
079                //write to file
080                extractTGlTransactionPS.printf("%s\n", tran.getOriginEntry().getLine());
081                
082                extractGlSummaryReport.summarizeEntry(tran.getOriginEntry());
083                
084                tran.setProcessInd(true);
085                glPendingTransactionService.save(tran);
086            }
087    
088            if (extractTGlTransactionPS != null) {
089                extractTGlTransactionPS.close();
090    
091                // create done file
092                String extractTGlTransactionDoneFileName = extractTGlTransactionFileName.replace(GeneralLedgerConstants.BatchFileSystem.EXTENSION, GeneralLedgerConstants.BatchFileSystem.DONE_FILE_EXTENSION);
093                File extractTGlTransactionDoneFile = new File(batchFileDirectoryName + File.separator + extractTGlTransactionDoneFileName);
094                if (!extractTGlTransactionDoneFile.exists()) {
095                    try {
096                        extractTGlTransactionDoneFile.createNewFile();
097                    }
098                    catch (IOException e) {
099                        throw new RuntimeException();
100                    }
101                }
102    
103                String reportTitle = this.kualiConfigurationService.getPropertyString(PdpKeyConstants.EXTRACT_TRANSACTION_SERVICE_REPORT_TITLE);
104                reportTitle = MessageFormat.format(reportTitle, new Object[] { null });
105    
106                String reportFilename = this.kualiConfigurationService.getPropertyString(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 setKualiConfigurationService(KualiConfigurationService 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    }