001/* 002 * Copyright 2011 The Kuali Foundation. 003 * 004 * Licensed under the Educational Community License, Version 1.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/ecl1.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 */ 016package org.kuali.ole.vnd.batch.service.impl; 017 018import java.io.File; 019import java.io.FileInputStream; 020import java.io.FileNotFoundException; 021import java.io.IOException; 022import java.util.ArrayList; 023import java.util.HashMap; 024import java.util.List; 025 026import org.apache.commons.io.IOUtils; 027import org.apache.commons.lang.StringUtils; 028import org.kuali.ole.sys.batch.BatchInputFileType; 029import org.kuali.ole.sys.batch.service.BatchInputFileService; 030import org.kuali.ole.vnd.batch.dataaccess.DebarredVendorDao; 031import org.kuali.ole.vnd.batch.dataaccess.DebarredVendorMatchDao; 032import org.kuali.ole.vnd.batch.service.VendorExcludeService; 033import org.kuali.ole.vnd.businessobject.DebarredVendorDetail; 034import org.kuali.ole.vnd.businessobject.DebarredVendorMatch; 035import org.kuali.ole.vnd.businessobject.VendorDetail; 036import org.kuali.rice.core.api.datetime.DateTimeService; 037import org.kuali.rice.krad.service.BusinessObjectService; 038import org.kuali.rice.krad.util.GlobalVariables; 039import org.springframework.transaction.annotation.Transactional; 040@Transactional 041public class VendorExcludeServiceImpl implements VendorExcludeService { 042 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(VendorExcludeServiceImpl.class); 043 044 private BatchInputFileService batchInputFileService; 045 private BusinessObjectService businessObjectService; 046 private DateTimeService dateTimeService; 047 private BatchInputFileType batchInputFileType; 048 private DebarredVendorDao debarredVendorDao; 049 private DebarredVendorMatchDao debarredVendorMatchDao; 050 051 /** 052 * @see org.kuali.ole.vnd.batch.service.VendorExcludeService.loadEplsFile() 053 */ 054 @Override 055 public boolean loadEplsFile() { 056 // create a list of the files to process 057 List<String> fileNames = batchInputFileService.listInputFileNamesWithDoneFile(batchInputFileType); 058 String fileName = null; 059 long lastModified = 0; 060 if(fileNames.size() == 0) { 061 return true; 062 } 063 File tempFile; 064 //Consider the latest file as the files are cumulative 065 for (String name : fileNames) { 066 tempFile = new File (name); 067 if(tempFile.lastModified() > lastModified) { 068 lastModified = tempFile.lastModified(); 069 fileName = name; 070 } 071 } 072 if (fileName == null) { 073 LOG.error("BatchInputFileService.listInputFileNamesWithDoneFile(" + 074 batchInputFileType.getFileTypeIdentifer() + ") returned NULL which should never happen."); 075 throw new RuntimeException("BatchInputFileService.listInputFileNamesWithDoneFile(" + 076 batchInputFileType.getFileTypeIdentifer() + ") returned NULL which should never happen."); 077 } 078 byte[] fileByteContent; 079 List debarredVendorList = new ArrayList<DebarredVendorDetail> (); 080 try { 081 fileByteContent = IOUtils.toByteArray(new FileInputStream(fileName)); 082 } 083 catch (FileNotFoundException ex) { 084 LOG.error("File not found [" + fileName + "]. " + ex.getMessage()); 085 throw new RuntimeException("File not found [" + fileName + "]. " + ex.getMessage()); 086 } 087 catch (IOException ex) { 088 LOG.error("IO Exception loading: [" + fileName + "]. " + ex.getMessage()); 089 throw new RuntimeException("IO Exception loading: [" + fileName + "]. " + ex.getMessage()); 090 } 091 Object parsedObject = batchInputFileService.parse(batchInputFileType, fileByteContent); 092 debarredVendorList = (List<DebarredVendorDetail>) parsedObject; 093 094 purgeOldVendorRecords(); 095 businessObjectService.save(debarredVendorList); 096 097 removeDoneFiles(fileNames); 098 return true; 099 } 100 101 /** 102 * Clears out associated .done files for the processed data files. 103 */ 104 protected void removeDoneFiles(List<String> dataFileNames) { 105 for (String dataFileName : dataFileNames) { 106 File doneFile = new File(StringUtils.substringBeforeLast(dataFileName, ".") + ".done"); 107 if (doneFile.exists()) { 108 doneFile.delete(); 109 } 110 } 111 } 112 113 /** 114 * @see org.kuali.ole.vnd.batch.service.VendorExcludeService.matchVendors() 115 */ 116 @Override 117 public boolean matchVendors() { 118 List<DebarredVendorMatch> matches = debarredVendorDao.match(); 119 businessObjectService.save(matches); 120 return true; 121 } 122 123 /** 124 * @see org.kuali.ole.vnd.batch.service.VendorExcludeService.purgeOldVendorRecords() 125 */ 126 @Override 127 public void purgeOldVendorRecords() { 128 businessObjectService.deleteMatching(DebarredVendorDetail.class, new HashMap()); 129 } 130 131 /** 132 * @see org.kuali.ole.vnd.batch.service.VendorExcludeService.getDebarredVendorsUnmatched() 133 */ 134 @Override 135 public List<VendorDetail> getDebarredVendorsUnmatched() { 136 return debarredVendorMatchDao.getDebarredVendorsUnmatched(); 137 } 138 139 /** 140 * @see org.kuali.ole.vnd.batch.service.VendorExcludeService.confirmDebarredVendor() 141 */ 142 @Override 143 public void confirmDebarredVendor(int debarredVendorId) { 144 DebarredVendorMatch match = debarredVendorMatchDao.getDebarredVendor(debarredVendorId); 145 match.setConfirmStatusCode("C"); 146 match.setLastUpdatedPrincipalName(GlobalVariables.getUserSession().getPerson().getPrincipalName()); 147 match.setLastUpdatedTimeStamp(dateTimeService.getCurrentTimestamp()); 148 businessObjectService.save(match); 149 } 150 151 /** 152 * @see org.kuali.ole.vnd.batch.service.VendorExcludeService.denyDebarredVendor() 153 */ 154 @Override 155 public void denyDebarredVendor(int debarredVendorId) { 156 DebarredVendorMatch match = debarredVendorMatchDao.getDebarredVendor(debarredVendorId); 157 match.setConfirmStatusCode("D"); 158 match.setLastUpdatedPrincipalName(GlobalVariables.getUserSession().getPerson().getPrincipalName()); 159 match.setLastUpdatedTimeStamp(dateTimeService.getCurrentTimestamp()); 160 businessObjectService.save(match); 161 } 162 163 /** 164 * Gets the batchInputFileService attribute. 165 * @return Returns the batchInputFileService. 166 */ 167 public BatchInputFileService getBatchInputFileService() { 168 return batchInputFileService; 169 } 170 171 /** 172 * Sets the batchInputFileService attribute value. 173 * @param batchInputFileService The batchInputFileService to set. 174 */ 175 public void setBatchInputFileService(BatchInputFileService batchInputFileService) { 176 this.batchInputFileService = batchInputFileService; 177 } 178 179 /** 180 * Gets the businessObjectService attribute. 181 * @return Returns the businessObjectService. 182 */ 183 public BusinessObjectService getBusinessObjectService() { 184 return businessObjectService; 185 } 186 187 /** 188 * Sets the businessObjectService attribute value. 189 * @param businessObjectService The businessObjectService to set. 190 */ 191 public void setBusinessObjectService(BusinessObjectService businessObjectService) { 192 this.businessObjectService = businessObjectService; 193 } 194 195 /** 196 * Gets the dateTimeService attribute. 197 * @return Returns the dateTimeService. 198 */ 199 public DateTimeService getDateTimeService() { 200 return dateTimeService; 201 } 202 203 /** 204 * Sets the dateTimeService attribute value. 205 * @param dateTimeService The dateTimeService to set. 206 */ 207 public void setDateTimeService(DateTimeService dateTimeService) { 208 this.dateTimeService = dateTimeService; 209 } 210 211 /** 212 * Gets the batchInputFileType attribute. 213 * @return Returns the batchInputFileType. 214 */ 215 public BatchInputFileType getBatchInputFileType() { 216 return batchInputFileType; 217 } 218 219 /** 220 * Sets the batchInputFileType attribute value. 221 * @param batchInputFileType The batchInputFileType to set. 222 */ 223 public void setBatchInputFileType(BatchInputFileType batchInputFileType) { 224 this.batchInputFileType = batchInputFileType; 225 } 226 227 /** 228 * Gets the vendorExcludeDao attribute. 229 * @return Returns the vendorExcludeDao. 230 */ 231 public DebarredVendorDao getDebarredVendorDao() { 232 return debarredVendorDao; 233 } 234 235 /** 236 * Sets the vendorExcludeDao attribute value. 237 * @param vendorExcludeDao The vendorExcludeDao to set. 238 */ 239 public void setDebarredVendorDao(DebarredVendorDao debarredVendorDao) { 240 this.debarredVendorDao = debarredVendorDao; 241 } 242 243 /** 244 * Gets the debarredVendorMatchDao attribute. 245 * @return Returns the debarredVendorMatchDao. 246 */ 247 public DebarredVendorMatchDao getDebarredVendorMatchDao() { 248 return debarredVendorMatchDao; 249 } 250 251 /** 252 * Sets the debarredVendorMatchDao attribute value. 253 * @param debarredVendorMatchDao The debarredVendorMatchDao to set. 254 */ 255 public void setDebarredVendorMatchDao(DebarredVendorMatchDao debarredVendorMatchDao) { 256 this.debarredVendorMatchDao = debarredVendorMatchDao; 257 } 258}