001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.financial.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.core.service.BusinessObjectService;
027: import org.kuali.kfs.batch.BatchInputFileType;
028: import org.kuali.kfs.exceptions.XMLParseException;
029: import org.kuali.kfs.service.BatchInputFileService;
030: import org.kuali.module.financial.bo.ProcurementCardTransaction;
031: import org.kuali.module.financial.service.ProcurementCardLoadTransactionsService;
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.module.financial.service.ProcurementCardCreateDocumentService
038: */
039: public class ProcurementCardLoadTransactionsServiceImpl implements
040: ProcurementCardLoadTransactionsService {
041: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
042: .getLogger(ProcurementCardLoadTransactionsServiceImpl.class);
043:
044: private BusinessObjectService businessObjectService;
045: private BatchInputFileService batchInputFileService;
046: private BatchInputFileType procurementCardInputFileType;
047:
048: /**
049: * Validates and parses the given file, then stores transactions into a temp table.
050: *
051: * @param fileName The name of the file to be parsed.
052: * @return This method always returns true. An exception is thrown if a problem occurs while loading the file.
053: *
054: * @see org.kuali.module.financial.service.ProcurementCardCreateDocumentService#loadProcurementCardFile()
055: */
056: public boolean loadProcurementCardFile(String fileName) {
057: FileInputStream fileContents;
058: try {
059: fileContents = new FileInputStream(fileName);
060: } catch (FileNotFoundException e1) {
061: LOG.error("file to parse not found " + fileName, e1);
062: throw new RuntimeException(
063: "Cannot find the file requested to be parsed "
064: + fileName + " " + e1.getMessage(), e1);
065: }
066:
067: Collection pcardTransactions = null;
068: try {
069: byte[] fileByteContent = IOUtils.toByteArray(fileContents);
070: pcardTransactions = (Collection) batchInputFileService
071: .parse(procurementCardInputFileType,
072: fileByteContent);
073: } catch (IOException e) {
074: LOG.error("error while getting file bytes: "
075: + e.getMessage(), e);
076: throw new RuntimeException(
077: "Error encountered while attempting to get file bytes: "
078: + e.getMessage(), e);
079: } catch (XMLParseException e) {
080: LOG.error("Error parsing xml " + e.getMessage());
081: throw new RuntimeException("Error parsing xml "
082: + e.getMessage(), e);
083: }
084:
085: if (pcardTransactions == null || pcardTransactions.isEmpty()) {
086: LOG.warn("No PCard transactions in input file " + fileName);
087: }
088:
089: loadTransactions((List) pcardTransactions);
090:
091: LOG.info("Total transactions loaded: "
092: + Integer.toString(pcardTransactions.size()));
093: return true;
094: }
095:
096: /**
097: * Calls businessObjectService to remove all the procurement card transaction rows from the transaction load table.
098: */
099: public void cleanTransactionsTable() {
100: businessObjectService.deleteMatching(
101: 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: private void loadTransactions(List transactions) {
110: businessObjectService.save(transactions);
111: }
112:
113: /**
114: * Sets the businessObjectService attribute value.
115: * @param businessObjectService The businessObjectService to set.
116: */
117: public void setBusinessObjectService(
118: BusinessObjectService businessObjectService) {
119: this .businessObjectService = businessObjectService;
120: }
121:
122: /**
123: * Sets the batchInputFileService attribute value.
124: * @param batchInputFileService The batchInputFileService to set.
125: */
126: public void setBatchInputFileService(
127: BatchInputFileService batchInputFileService) {
128: this .batchInputFileService = batchInputFileService;
129: }
130:
131: /**
132: * Sets the procurementCardInputFileType attribute value.
133: * @param procurementCardInputFileType The procurementCardInputFileType to set.
134: */
135: public void setProcurementCardInputFileType(
136: BatchInputFileType procurementCardInputFileType) {
137: this.procurementCardInputFileType = procurementCardInputFileType;
138: }
139:
140: }
|