View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 1.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/ecl1.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.vnd.batch.service.impl;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.FileNotFoundException;
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  
26  import org.apache.commons.io.IOUtils;
27  import org.apache.commons.lang.StringUtils;
28  import org.kuali.ole.sys.batch.BatchInputFileType;
29  import org.kuali.ole.sys.batch.service.BatchInputFileService;
30  import org.kuali.ole.vnd.batch.dataaccess.DebarredVendorDao;
31  import org.kuali.ole.vnd.batch.dataaccess.DebarredVendorMatchDao;
32  import org.kuali.ole.vnd.batch.service.VendorExcludeService;
33  import org.kuali.ole.vnd.businessobject.DebarredVendorDetail;
34  import org.kuali.ole.vnd.businessobject.DebarredVendorMatch;
35  import org.kuali.ole.vnd.businessobject.VendorDetail;
36  import org.kuali.rice.core.api.datetime.DateTimeService;
37  import org.kuali.rice.krad.service.BusinessObjectService;
38  import org.kuali.rice.krad.util.GlobalVariables;
39  import org.springframework.transaction.annotation.Transactional;
40  @Transactional
41  public class VendorExcludeServiceImpl implements VendorExcludeService {
42      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(VendorExcludeServiceImpl.class);
43  
44      private BatchInputFileService batchInputFileService;
45      private BusinessObjectService businessObjectService;
46      private DateTimeService dateTimeService;
47      private BatchInputFileType batchInputFileType;
48      private DebarredVendorDao debarredVendorDao;
49      private DebarredVendorMatchDao debarredVendorMatchDao;
50  
51      /**
52       * @see org.kuali.ole.vnd.batch.service.VendorExcludeService.loadEplsFile()
53       */
54      @Override
55      public boolean loadEplsFile() {
56          //  create a list of the files to process
57          List<String> fileNames = batchInputFileService.listInputFileNamesWithDoneFile(batchInputFileType);
58          String fileName = null;
59          long lastModified = 0;
60          if(fileNames.size() == 0) {
61              return true;
62          }
63          File tempFile;
64          //Consider the latest file as the files are cumulative
65          for (String name : fileNames) {
66              tempFile = new File (name);
67              if(tempFile.lastModified() > lastModified) {
68                  lastModified = tempFile.lastModified();
69                  fileName = name;
70              }
71          }
72          if (fileName == null) {
73              LOG.error("BatchInputFileService.listInputFileNamesWithDoneFile(" +
74                      batchInputFileType.getFileTypeIdentifer() + ") returned NULL which should never happen.");
75              throw new RuntimeException("BatchInputFileService.listInputFileNamesWithDoneFile(" +
76                      batchInputFileType.getFileTypeIdentifer() + ") returned NULL which should never happen.");
77          }
78          byte[] fileByteContent;
79          List debarredVendorList = new ArrayList<DebarredVendorDetail> ();
80          try {
81              fileByteContent = IOUtils.toByteArray(new FileInputStream(fileName));
82          }
83          catch (FileNotFoundException ex) {
84              LOG.error("File not found [" + fileName + "]. " + ex.getMessage());
85              throw new RuntimeException("File not found [" + fileName + "]. " + ex.getMessage());
86          }
87          catch (IOException ex) {
88              LOG.error("IO Exception loading: [" + fileName + "]. " + ex.getMessage());
89              throw new RuntimeException("IO Exception loading: [" + fileName + "]. " + ex.getMessage());
90          }
91          Object parsedObject = batchInputFileService.parse(batchInputFileType, fileByteContent);
92          debarredVendorList = (List<DebarredVendorDetail>) parsedObject;
93  
94          purgeOldVendorRecords();
95          businessObjectService.save(debarredVendorList);
96  
97          removeDoneFiles(fileNames);
98          return true;
99      }
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 }