001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 */
016 package org.kuali.kfs.fp.batch.service.impl;
017
018 import java.io.FileInputStream;
019 import java.io.FileNotFoundException;
020 import java.io.IOException;
021 import java.util.Collection;
022 import java.util.HashMap;
023 import java.util.List;
024
025 import org.apache.commons.io.IOUtils;
026 import org.kuali.kfs.fp.batch.service.ProcurementCardLoadTransactionsService;
027 import org.kuali.kfs.fp.businessobject.ProcurementCardTransaction;
028 import org.kuali.kfs.sys.batch.BatchInputFileType;
029 import org.kuali.kfs.sys.batch.service.BatchInputFileService;
030 import org.kuali.kfs.sys.exception.ParseException;
031 import org.kuali.rice.kns.service.BusinessObjectService;
032
033 /**
034 * This is the default implementation of the ProcurementCardLoadTransactionsService interface.
035 * Handles loading, parsing, and storing of incoming procurement card batch files.
036 *
037 * @see org.kuali.kfs.fp.batch.service.ProcurementCardCreateDocumentService
038 */
039 public class ProcurementCardLoadTransactionsServiceImpl implements ProcurementCardLoadTransactionsService {
040 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProcurementCardLoadTransactionsServiceImpl.class);
041
042 private BusinessObjectService businessObjectService;
043 private BatchInputFileService batchInputFileService;
044 private BatchInputFileType procurementCardInputFileType;
045
046 /**
047 * Validates and parses the given file, then stores transactions into a temp table.
048 *
049 * @param fileName The name of the file to be parsed.
050 * @return This method always returns true. An exception is thrown if a problem occurs while loading the file.
051 *
052 * @see org.kuali.kfs.fp.batch.service.ProcurementCardCreateDocumentService#loadProcurementCardFile()
053 */
054 public boolean loadProcurementCardFile(String fileName) {
055 FileInputStream fileContents;
056 try {
057 fileContents = new FileInputStream(fileName);
058 }
059 catch (FileNotFoundException e1) {
060 LOG.error("file to parse not found " + fileName, e1);
061 throw new RuntimeException("Cannot find the file requested to be parsed " + fileName + " " + e1.getMessage(), e1);
062 }
063
064 Collection pcardTransactions = null;
065 try {
066 byte[] fileByteContent = IOUtils.toByteArray(fileContents);
067 pcardTransactions = (Collection) batchInputFileService.parse(procurementCardInputFileType, fileByteContent);
068 }
069 catch (IOException e) {
070 LOG.error("error while getting file bytes: " + e.getMessage(), e);
071 throw new RuntimeException("Error encountered while attempting to get file bytes: " + e.getMessage(), e);
072 }
073 catch (ParseException e) {
074 LOG.error("Error parsing xml " + e.getMessage());
075 throw new RuntimeException("Error parsing xml " + e.getMessage(), e);
076 }
077
078 if (pcardTransactions == null || pcardTransactions.isEmpty()) {
079 LOG.warn("No PCard transactions in input file " + fileName);
080 }
081
082 loadTransactions((List) pcardTransactions);
083
084 LOG.info("Total transactions loaded: " + Integer.toString(pcardTransactions.size()));
085 return true;
086 }
087
088 /**
089 * Calls businessObjectService to remove all the procurement card transaction rows from the transaction load table.
090 */
091 public void cleanTransactionsTable() {
092 businessObjectService.deleteMatching(ProcurementCardTransaction.class, new HashMap());
093 }
094
095 /**
096 * Loads all the parsed XML transactions into the temp transaction table.
097 *
098 * @param transactions List of ProcurementCardTransactions to load.
099 */
100 protected void loadTransactions(List transactions) {
101 businessObjectService.save(transactions);
102 }
103
104 /**
105 * Sets the businessObjectService attribute value.
106 * @param businessObjectService The businessObjectService to set.
107 */
108 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
109 this.businessObjectService = businessObjectService;
110 }
111
112 /**
113 * Sets the batchInputFileService attribute value.
114 * @param batchInputFileService The batchInputFileService to set.
115 */
116 public void setBatchInputFileService(BatchInputFileService batchInputFileService) {
117 this.batchInputFileService = batchInputFileService;
118 }
119
120 /**
121 * Sets the procurementCardInputFileType attribute value.
122 * @param procurementCardInputFileType The procurementCardInputFileType to set.
123 */
124 public void setProcurementCardInputFileType(BatchInputFileType procurementCardInputFileType) {
125 this.procurementCardInputFileType = procurementCardInputFileType;
126 }
127
128 }