View Javadoc
1   /*
2    * Copyright 2006 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.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   * This is the default implementation of the ProcurementCardLoadTransactionsService interface.
40   * Handles loading, parsing, and storing of incoming procurement card batch files.
41   * 
42   * @see org.kuali.ole.fp.batch.service.ProcurementCardCreateDocumentService
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       * @see org.kuali.ole.fp.batch.service.ProcurementCardLoadTransactionsService#loadProcurementCardFile(java.lang.String, org.kuali.ole.sys.service.ReportWriterService)
53       */
54      public boolean loadProcurementCardFile(String fileName, ReportWriterService reportWriterService) {
55          
56          //add a step to check for directory paths
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       * Calls businessObjectService to remove all the procurement card transaction rows from the transaction load table.
98       */
99      public void cleanTransactionsTable() {
100         businessObjectService.deleteMatching(ProcurementCardTransaction.class, new HashMap());
101     }
102 
103     /**
104      * Loads all the parsed XML transactions into the temp transaction table.
105      * 
106      * @param transactions List of ProcurementCardTransactions to load.
107      */
108     protected void loadTransactions(List transactions) {
109         businessObjectService.save(transactions);
110     }
111 
112     /**
113      * Sets the businessObjectService attribute value.
114      * @param businessObjectService The businessObjectService to set.
115      */
116     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
117         this.businessObjectService = businessObjectService;
118     }
119 
120     /**
121      * Sets the batchInputFileService attribute value.
122      * @param batchInputFileService The batchInputFileService to set.
123      */
124     public void setBatchInputFileService(BatchInputFileService batchInputFileService) {
125         this.batchInputFileService = batchInputFileService;
126     }
127 
128     /**
129      * Sets the procurementCardInputFileType attribute value.
130      * @param procurementCardInputFileType The procurementCardInputFileType to set.
131      */
132     public void setProcurementCardInputFileType(BatchInputFileType procurementCardInputFileType) {
133         this.procurementCardInputFileType = procurementCardInputFileType;
134     }
135 
136     /**
137      * @see org.kuali.ole.sys.batch.service.impl.InitiateDirectoryImpl#getRequiredDirectoryNames()
138      */
139     @Override
140     public List<String> getRequiredDirectoryNames() {
141         return new ArrayList<String>() {{add(procurementCardInputFileType.getDirectoryPath()); }};
142     }
143 
144 }