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  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
38  
39  /**
40   * This is the default implementation of the ProcurementCardLoadTransactionsService interface.
41   * Handles loading, parsing, and storing of incoming procurement card batch files.
42   * 
43   * @see org.kuali.ole.fp.batch.service.ProcurementCardCreateDocumentService
44   */
45  public class ProcurementCardLoadTransactionsServiceImpl extends InitiateDirectoryBase implements ProcurementCardLoadTransactionsService {
46      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProcurementCardLoadTransactionsServiceImpl.class);
47  
48      protected BusinessObjectService businessObjectService;
49      protected BatchInputFileService batchInputFileService;
50      protected BatchInputFileType procurementCardInputFileType;
51  
52      /**
53       * @see org.kuali.ole.fp.batch.service.ProcurementCardLoadTransactionsService#loadProcurementCardFile(java.lang.String, org.kuali.ole.sys.service.ReportWriterService)
54       */
55      public boolean loadProcurementCardFile(String fileName, ReportWriterService reportWriterService) {
56          
57          //add a step to check for directory paths
58          prepareDirectories(getRequiredDirectoryNames());
59          
60          FileInputStream fileContents;
61          try {
62              fileContents = new FileInputStream(fileName);
63          }
64          catch (FileNotFoundException e1) {
65              LOG.error("file to parse not found " + fileName, e1);
66              throw new RuntimeException("Cannot find the file requested to be parsed " + fileName + " " + e1.getMessage(), e1);
67          }
68  
69          Collection pcardTransactions = new ArrayList();
70          try {
71              byte[] fileByteContent = IOUtils.toByteArray(fileContents);
72              pcardTransactions = (Collection) batchInputFileService.parse(procurementCardInputFileType, fileByteContent);
73          }
74          catch (IOException e) {
75              LOG.error("Error while getting file bytes:  " + e.getMessage(), e);
76              reportWriterService.writeFormattedMessageLine("%s cannot be processed. \n\tFile byptes error: %s", fileName, e.getMessage());
77              return false;
78          }
79          catch (ParseException e) {
80              LOG.error("Error parsing xml " + e.getMessage());
81              reportWriterService.writeFormattedMessageLine("%s cannot be processed. \n\tXML parsing error: %s", fileName, e.getMessage());
82              return false;
83          }
84  
85          if (pcardTransactions.isEmpty()) {
86              LOG.warn("No PCard transactions in input file " + fileName);
87              reportWriterService.writeFormattedMessageLine("%s is processed. No PCard transactios in file. ", fileName);
88          }else{
89          loadTransactions((List) pcardTransactions);
90              LOG.info("Total transactions loaded: " + String.valueOf(pcardTransactions.size()));
91              reportWriterService.writeFormattedMessageLine("%s is processed. %d transaction(s) loaded. ", fileName, pcardTransactions.size());
92          }
93  
94          return true;
95      }
96  
97      /**
98       * Calls businessObjectService to remove all the procurement card transaction rows from the transaction load table.
99       */
100     public void cleanTransactionsTable() {
101         KRADServiceLocatorWeb.getLegacyDataAdapter().deleteMatching(ProcurementCardTransaction.class, new HashMap());
102     }
103 
104     /**
105      * Loads all the parsed XML transactions into the temp transaction table.
106      * 
107      * @param transactions List of ProcurementCardTransactions to load.
108      */
109     protected void loadTransactions(List transactions) {
110         KRADServiceLocatorWeb.getLegacyDataAdapter().save(transactions);
111     }
112 
113     /**
114      * Sets the businessObjectService attribute value.
115      * @param businessObjectService The businessObjectService to set.
116      */
117     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
118         this.businessObjectService = businessObjectService;
119     }
120 
121     /**
122      * Sets the batchInputFileService attribute value.
123      * @param batchInputFileService The batchInputFileService to set.
124      */
125     public void setBatchInputFileService(BatchInputFileService batchInputFileService) {
126         this.batchInputFileService = batchInputFileService;
127     }
128 
129     /**
130      * Sets the procurementCardInputFileType attribute value.
131      * @param procurementCardInputFileType The procurementCardInputFileType to set.
132      */
133     public void setProcurementCardInputFileType(BatchInputFileType procurementCardInputFileType) {
134         this.procurementCardInputFileType = procurementCardInputFileType;
135     }
136 
137     /**
138      * @see org.kuali.ole.sys.batch.service.impl.InitiateDirectoryImpl#getRequiredDirectoryNames()
139      */
140     @Override
141     public List<String> getRequiredDirectoryNames() {
142         return new ArrayList<String>() {{add(procurementCardInputFileType.getDirectoryPath()); }};
143     }
144 
145 }