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.gl.service.impl; 017 018 import java.io.BufferedReader; 019 import java.io.File; 020 import java.io.FileNotFoundException; 021 import java.io.FileReader; 022 import java.io.FilenameFilter; 023 import java.io.IOException; 024 import java.io.PrintStream; 025 import java.util.Arrays; 026 import java.util.List; 027 028 import org.apache.commons.lang.StringUtils; 029 import org.kuali.kfs.gl.GeneralLedgerConstants; 030 import org.kuali.kfs.gl.service.OriginEntryGroupService; 031 import org.kuali.rice.kns.service.DateTimeService; 032 import org.kuali.rice.kns.service.KualiModuleService; 033 import org.springframework.transaction.annotation.Transactional; 034 035 /** 036 * @see org.kuali.kfs.gl.service.OriginEntryGroupService 037 */ 038 @Transactional 039 public class OriginEntryGroupServiceImpl implements OriginEntryGroupService { 040 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OriginEntryGroupServiceImpl.class); 041 042 protected DateTimeService dateTimeService; 043 protected String batchFileDirectoryName; 044 protected KualiModuleService kualiModuleService; 045 protected String nightlyOutFileName; 046 protected String backupFileName; 047 048 /** 049 * @see org.kuali.kfs.gl.service.OriginEntryGroupService#getNewestScrubberErrorFileName() 050 */ 051 public String getNewestScrubberErrorFileName() { 052 File newestFile = null; 053 054 File[] files = null; 055 // can add filter here: listFiles(filter); -- check out originEntryTestBase from Jeff 056 if (new File(batchFileDirectoryName) == null) { 057 return null; 058 } 059 files = new File(batchFileDirectoryName).listFiles(new ScrubberErrorFilenameFilter()); 060 List<File> fileList = Arrays.asList(files); 061 if (fileList.size() > 0) { 062 for (File eachFile : fileList) { 063 if (newestFile == null) { 064 newestFile = eachFile; 065 } 066 else { 067 if (newestFile.lastModified() < eachFile.lastModified()) { 068 newestFile = eachFile; 069 } 070 } 071 } 072 } 073 else { 074 return null; 075 } 076 077 return newestFile.getName(); 078 } 079 080 081 /** 082 * Retrieves all groups to be created today, and creates backup group versions of them 083 * 084 * @see org.kuali.kfs.gl.service.OriginEntryGroupService#createBackupGroup() 085 */ 086 public void createBackupGroup() { 087 LOG.debug("createBackupGroup() started"); 088 // check file from nightly out 089 File nightlyOutFile = new File(batchFileDirectoryName + File.separator + nightlyOutFileName); 090 if (!nightlyOutFile.exists()) { 091 LOG.warn("nightlyOutFile doesn't exist :" + nightlyOutFileName); 092 } 093 094 String backupFile = batchFileDirectoryName + File.separator + backupFileName + GeneralLedgerConstants.BatchFileSystem.EXTENSION; 095 PrintStream backupPs = null; 096 try { 097 backupPs = new PrintStream(backupFile); 098 } 099 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.kfs.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.kfs.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.kfs.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.kfs.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.kfs.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 }