View Javadoc
1   /*
2    * Copyright 2005 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.gl.service.impl;
17  
18  import java.io.BufferedReader;
19  import java.io.File;
20  import java.io.FileNotFoundException;
21  import java.io.FileReader;
22  import java.io.FilenameFilter;
23  import java.io.IOException;
24  import java.io.PrintStream;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.apache.commons.lang.StringUtils;
29  import org.kuali.ole.gl.GeneralLedgerConstants;
30  import org.kuali.ole.gl.service.OriginEntryGroupService;
31  import org.kuali.rice.core.api.datetime.DateTimeService;
32  import org.kuali.rice.krad.service.KualiModuleService;
33  import org.springframework.transaction.annotation.Transactional;
34  
35  /**
36   * @see org.kuali.ole.gl.service.OriginEntryGroupService
37   */
38  @Transactional
39  public class OriginEntryGroupServiceImpl implements OriginEntryGroupService {
40      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OriginEntryGroupServiceImpl.class);
41  
42      protected DateTimeService dateTimeService;
43      protected String batchFileDirectoryName;
44      protected KualiModuleService kualiModuleService;
45      protected String nightlyOutFileName;
46      protected String backupFileName;
47  
48      /**
49       * @see org.kuali.ole.gl.service.OriginEntryGroupService#getNewestScrubberErrorFileName()
50       */
51      public String getNewestScrubberErrorFileName() {
52          File newestFile = null;
53  
54          File[] files = null;
55          // can add filter here: listFiles(filter); -- check out originEntryTestBase from Jeff
56          if (new File(batchFileDirectoryName) == null) {
57              return null;
58          }
59          files = new File(batchFileDirectoryName).listFiles(new ScrubberErrorFilenameFilter());
60          List<File> fileList = Arrays.asList(files);
61          if (fileList.size() > 0) {
62              for (File eachFile : fileList) {
63                  if (newestFile == null) {
64                      newestFile = eachFile;
65                  }
66                  else {
67                      if (newestFile.lastModified() < eachFile.lastModified()) {
68                          newestFile = eachFile;
69                      }
70                  }
71              }
72          }
73          else {
74              return null;
75          }
76  
77          return newestFile.getName();
78      }
79  
80  
81      /**
82       * Retrieves all groups to be created today, and creates backup group versions of them
83       * 
84       * @see org.kuali.ole.gl.service.OriginEntryGroupService#createBackupGroup()
85       */
86      public void createBackupGroup() {
87          LOG.debug("createBackupGroup() started");
88          // check file from nightly out
89          File nightlyOutFile = new File(batchFileDirectoryName + File.separator + nightlyOutFileName);
90          if (!nightlyOutFile.exists()) {
91              LOG.warn("nightlyOutFile doesn't exist :" + nightlyOutFileName);
92          }
93  
94          String backupFile = batchFileDirectoryName + File.separator + backupFileName + GeneralLedgerConstants.BatchFileSystem.EXTENSION;
95          PrintStream backupPs = null;
96          try {
97              backupPs = new PrintStream(backupFile);
98          }
99          catch (FileNotFoundException e) {
100             throw new RuntimeException("backupFile doesn't exist " + backupFile);
101         }
102 
103         // get all done files from originEntry Directory
104         File[] doneFileList = new File(batchFileDirectoryName).listFiles(new DoneFileFilter());
105         // build output file with doneFileList and print stream
106         buildBackupFileOutput(doneFileList, backupPs);
107         backupPs.close();
108     }
109 
110 
111     /*
112      * buildBackupFileOuput with doneFileList and PrintStream
113      */
114     protected void buildBackupFileOutput(File[] doneFileList, PrintStream ps) {
115         BufferedReader inputFileReader = null;
116 
117         for (File doneFile : doneFileList) {
118             // get data file with done file
119             File dataFile = getDataFile(doneFile);
120             if (dataFile != null) {
121                 try {
122                     inputFileReader = new BufferedReader(new FileReader(dataFile.getPath()));
123                     String line = null;
124                     while ((line = inputFileReader.readLine()) != null) {
125                         try {
126                             ps.printf("%s\n", line);
127                         }
128                         catch (Exception e) {
129                             throw new IOException(e.toString());
130                         }
131                     }
132                     inputFileReader.close();
133                     inputFileReader = null;
134 
135                 }
136                 catch (Exception e) {
137                     throw new RuntimeException(e.toString());
138                 }
139 
140                 doneFile.delete();
141                 postProcessDataFile(dataFile);
142                 
143             }
144         }
145     }
146     
147     protected void postProcessDataFile( File dataFile )
148     {
149         // do nothing.  A hook for institution extension.
150     }
151 
152 
153     /**
154      * @see org.kuali.ole.gl.service.OriginEntryGroupService#createGroup(java.lang.String)
155      */
156     public File createGroup(String fileName) {
157         return new File(batchFileDirectoryName + File.separator + fileName);
158     }
159 
160     /**
161      * @see org.kuali.ole.gl.service.OriginEntryGroupService#getGroupExists(java.lang.String)
162      */
163     public boolean getGroupExists(String groupId) {
164 
165         File file = new File(batchFileDirectoryName + File.separator + groupId);
166         if (file == null) {
167             return false;
168         }
169         else {
170             return true;
171         }
172     }
173 
174     /**
175      * @see org.kuali.ole.gl.service.OriginEntryGroupService#getAllFileInBatchDirectory()
176      */
177     public File[] getAllFileInBatchDirectory() {
178         File[] returnFiles = null;
179         if (new File(batchFileDirectoryName) != null) {
180             returnFiles = new File(batchFileDirectoryName).listFiles(new DateAndDoneFileFilter());
181         }
182         return returnFiles;
183     }
184 
185 
186     /**
187      * @see org.kuali.ole.gl.service.OriginEntryGroupService#deleteFile(java.lang.String)
188      */
189     public void deleteFile(String fileNameWithPath) {
190         File file = new File(fileNameWithPath);
191         if (file.exists()) {
192             file.delete();
193         }
194     }
195 
196     /**
197      * @see org.kuali.ole.gl.service.OriginEntryGroupService#getLaborFileWithFileName(java.lang.String)
198      */
199     public File getFileWithFileName(String fileName) {
200         return new File(batchFileDirectoryName + File.separator + fileName);
201     }
202 
203     protected class ScrubberErrorFilenameFilter implements FilenameFilter {
204         /**
205          * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
206          */
207         public boolean accept(File dir, String name) {
208             return name.contains(GeneralLedgerConstants.BatchFileSystem.SCRUBBER_ERROR_PREFIX);
209         }
210     }
211 
212     protected class DateAndDoneFileFilter implements FilenameFilter {
213         /**
214          * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
215          */
216         public boolean accept(File dir, String name) {
217             return name.contains(GeneralLedgerConstants.BatchFileSystem.DONE_FILE_EXTENSION) || name.contains(GeneralLedgerConstants.BatchFileSystem.EXTENSION);
218         }
219     }
220 
221     protected class DoneFileFilter implements FilenameFilter {
222         /**
223          * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
224          */
225         public boolean accept(File dir, String name) {
226             return name.contains(GeneralLedgerConstants.BatchFileSystem.DONE_FILE_EXTENSION);
227         }
228     }
229 
230     protected File getDataFile(File doneFile) {
231         String doneFileAbsPath = doneFile.getAbsolutePath();
232         if (!doneFileAbsPath.endsWith(GeneralLedgerConstants.BatchFileSystem.DONE_FILE_EXTENSION)) {
233             throw new IllegalArgumentException("Done file name must end with " + GeneralLedgerConstants.BatchFileSystem.DONE_FILE_EXTENSION);
234         }
235         String dataFileAbsPath = StringUtils.removeEnd(doneFileAbsPath, GeneralLedgerConstants.BatchFileSystem.DONE_FILE_EXTENSION) + GeneralLedgerConstants.BatchFileSystem.EXTENSION;
236         File dataFile = new File(dataFileAbsPath);
237         if (!dataFile.exists() || !dataFile.canRead()) {
238             LOG.error("Cannot find/read data file " + dataFileAbsPath);
239             return null;
240         }
241         return dataFile;
242     }
243     
244     public void setBatchFileDirectoryName(String batchFileDirectoryName) {
245         this.batchFileDirectoryName = batchFileDirectoryName;
246     }
247 
248 
249     public void setKualiModuleService(KualiModuleService kualiModuleService) {
250         this.kualiModuleService = kualiModuleService;
251     }
252     
253     public void setDateTimeService(DateTimeService dts) {
254         dateTimeService = dts;
255     }
256 
257     public void setNightlyOutFileName(String nightlyOutFileName) {
258         this.nightlyOutFileName = nightlyOutFileName;
259     }
260 
261     public void setBackupFileName(String backupFileName) {
262         this.backupFileName = backupFileName;
263     }
264 
265     protected DateTimeService getDateTimeService() {
266         return dateTimeService;
267     }
268 
269     protected String getBatchFileDirectoryName() {
270         return batchFileDirectoryName;
271     }
272 
273     protected KualiModuleService getKualiModuleService() {
274         return kualiModuleService;
275     }
276     
277     
278 }