1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.fp.batch.service.impl;
17  
18  import java.io.FileInputStream;
19  import java.io.FileNotFoundException;
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.HashMap;
24  import java.util.List;
25  
26  import org.apache.commons.io.IOUtils;
27  import org.apache.commons.lang.NumberUtils;
28  import org.apache.commons.lang.ObjectUtils;
29  import org.kuali.ole.fp.batch.service.ProcurementCardLoadTransactionsService;
30  import org.kuali.ole.fp.businessobject.ProcurementCardTransaction;
31  import org.kuali.ole.sys.batch.BatchInputFileType;
32  import org.kuali.ole.sys.batch.InitiateDirectoryBase;
33  import org.kuali.ole.sys.batch.service.BatchInputFileService;
34  import org.kuali.ole.sys.exception.ParseException;
35  import org.kuali.ole.sys.service.ReportWriterService;
36  import org.kuali.rice.krad.service.BusinessObjectService;
37  
38  
39  
40  
41  
42  
43  
44  public class ProcurementCardLoadTransactionsServiceImpl extends InitiateDirectoryBase implements ProcurementCardLoadTransactionsService {
45      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProcurementCardLoadTransactionsServiceImpl.class);
46  
47      protected BusinessObjectService businessObjectService;
48      protected BatchInputFileService batchInputFileService;
49      protected BatchInputFileType procurementCardInputFileType;
50  
51      
52  
53  
54      public boolean loadProcurementCardFile(String fileName, ReportWriterService reportWriterService) {
55          
56          
57          prepareDirectories(getRequiredDirectoryNames());
58          
59          FileInputStream fileContents;
60          try {
61              fileContents = new FileInputStream(fileName);
62          }
63          catch (FileNotFoundException e1) {
64              LOG.error("file to parse not found " + fileName, e1);
65              throw new RuntimeException("Cannot find the file requested to be parsed " + fileName + " " + e1.getMessage(), e1);
66          }
67  
68          Collection pcardTransactions = new ArrayList();
69          try {
70              byte[] fileByteContent = IOUtils.toByteArray(fileContents);
71              pcardTransactions = (Collection) batchInputFileService.parse(procurementCardInputFileType, fileByteContent);
72          }
73          catch (IOException e) {
74              LOG.error("Error while getting file bytes:  " + e.getMessage(), e);
75              reportWriterService.writeFormattedMessageLine("%s cannot be processed. \n\tFile byptes error: %s", fileName, e.getMessage());
76              return false;
77          }
78          catch (ParseException e) {
79              LOG.error("Error parsing xml " + e.getMessage());
80              reportWriterService.writeFormattedMessageLine("%s cannot be processed. \n\tXML parsing error: %s", fileName, e.getMessage());
81              return false;
82          }
83  
84          if (pcardTransactions.isEmpty()) {
85              LOG.warn("No PCard transactions in input file " + fileName);
86              reportWriterService.writeFormattedMessageLine("%s is processed. No PCard transactios in file. ", fileName);
87          }else{
88          loadTransactions((List) pcardTransactions);
89              LOG.info("Total transactions loaded: " + String.valueOf(pcardTransactions.size()));
90              reportWriterService.writeFormattedMessageLine("%s is processed. %d transaction(s) loaded. ", fileName, pcardTransactions.size());
91          }
92  
93          return true;
94      }
95  
96      
97  
98  
99      public void cleanTransactionsTable() {
100         businessObjectService.deleteMatching(ProcurementCardTransaction.class, new HashMap());
101     }
102 
103     
104 
105 
106 
107 
108     protected void loadTransactions(List transactions) {
109         businessObjectService.save(transactions);
110     }
111 
112     
113 
114 
115 
116     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
117         this.businessObjectService = businessObjectService;
118     }
119 
120     
121 
122 
123 
124     public void setBatchInputFileService(BatchInputFileService batchInputFileService) {
125         this.batchInputFileService = batchInputFileService;
126     }
127 
128     
129 
130 
131 
132     public void setProcurementCardInputFileType(BatchInputFileType procurementCardInputFileType) {
133         this.procurementCardInputFileType = procurementCardInputFileType;
134     }
135 
136     
137 
138 
139     @Override
140     public List<String> getRequiredDirectoryNames() {
141         return new ArrayList<String>() {{add(procurementCardInputFileType.getDirectoryPath()); }};
142     }
143 
144 }