Search in sources :

Example 1 with ParseException

use of org.kuali.kfs.sys.exception.ParseException in project cu-kfs by CU-CommunityApps.

the class ProcurementCardFlatInputFileType method parse.

public Object parse(byte[] fileByteContent) throws ParseException {
    BufferedReader bufferedFileReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(fileByteContent)));
    String fileLine;
    defaultChart = parameterService.getParameterValueAsString(KfsParameterConstants.FINANCIAL_SYSTEM_DOCUMENT.class, CUKFSParameterKeyConstants.DEFAULT_CHART_CODE);
    errorMessages = new ArrayList<String>();
    ArrayList<ProcurementCardTransaction> transactions = new ArrayList<ProcurementCardTransaction>();
    LOG.info("Beginning parse of file");
    try {
        initialize();
        while ((fileLine = bufferedFileReader.readLine()) != null) {
            ProcurementCardTransaction theTransaction = generateProcurementCardTransaction(fileLine);
            if (theTransaction != null) {
                if (ObjectUtils.isNotNull(theTransaction.getExtension())) {
                    lineCount = ((ProcurementCardTransactionExtendedAttribute) theTransaction.getExtension()).addAddendumLines(bufferedFileReader, lineCount);
                }
                transactions.add(theTransaction);
            }
            lineCount++;
        }
        if (totalDebits.compareTo(fileFooterDebits.abs()) != 0) {
            StringBuffer sb = new StringBuffer();
            sb.append("The sum of all debits does not match the value given in the file footer.");
            sb.append(" Sum of debits generated during ingestion of PCard file: ");
            sb.append(totalDebits);
            sb.append(" Total of debits given in file footer: ");
            sb.append(fileFooterDebits);
            throw new Exception(sb.toString());
        }
        if (totalCredits.compareTo(fileFooterCredits.abs()) != 0) {
            StringBuffer sb = new StringBuffer();
            sb.append("The sum of all credits does not match the value given in the file footer.");
            sb.append(" Sum of credits generated during ingestion of PCard file: ");
            sb.append(totalCredits);
            sb.append(" Total of credits given in file footer: ");
            sb.append(fileFooterCredits);
            throw new Exception(sb.toString());
        }
        if ((headerTransactionCount != footerTransactionCount) || (headerTransactionCount != transactionCount) || (footerTransactionCount != transactionCount)) {
            StringBuffer sb = new StringBuffer();
            sb.append("There is a discrepancy between the number of transactions counted during the ingestion process.");
            sb.append(" Transactions in header: ");
            sb.append(headerTransactionCount);
            sb.append(" Transactions in footer: ");
            sb.append(footerTransactionCount);
            sb.append(" Transactions counted while parsing file: ");
            sb.append(transactionCount);
            throw new Exception(sb.toString());
        }
    } catch (IOException e) {
        LOG.error("Error encountered reading from file content", e);
        throw new ParseException("Error encountered reading from file content", e);
    } catch (Exception e) {
        e.printStackTrace();
        errorMessages.add(e.getMessage());
        errorMessages.add("\r\n");
        errorMessages.add("Parsing of file stopped on line " + lineCount);
        procurementCardErrorEmailService.sendErrorEmail(errorMessages);
        throw new ParseException(e.getMessage());
    }
    return transactions;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) ParseException(org.kuali.kfs.sys.exception.ParseException) ProcurementCardTransaction(org.kuali.kfs.fp.businessobject.ProcurementCardTransaction) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) ParseException(org.kuali.kfs.sys.exception.ParseException)

Example 2 with ParseException

use of org.kuali.kfs.sys.exception.ParseException in project cu-kfs by CU-CommunityApps.

the class IWantDocumentFeedServiceImpl method parseInputFile.

/**
 * Parses the input file.
 *
 * @param incomingFileName
 * @return an IWantDocumentBatchFeed containing input data
 */
protected IWantDocumentBatchFeed parseInputFile(String incomingFileName) {
    LOG.info("Parsing file: " + incomingFileName);
    FileInputStream fileContents;
    try {
        fileContents = new FileInputStream(incomingFileName);
    } catch (FileNotFoundException e1) {
        LOG.error("file to load not found " + incomingFileName, e1);
        throw new RuntimeException("Cannot find the file requested to be loaded " + incomingFileName, e1);
    }
    // do the parse
    Object parsed = null;
    try {
        byte[] fileByteContent = IOUtils.toByteArray(fileContents);
        parsed = batchInputFileService.parse(iWantDocumentInputFileType, fileByteContent);
    } catch (IOException e) {
        LOG.error("error while getting file bytes:  " + e.getMessage(), e);
        throw new RuntimeException("Error encountered while attempting to get file bytes: " + e.getMessage(), e);
    } catch (ParseException e1) {
        LOG.error("Error parsing xml " + e1.getMessage());
        throw new RuntimeException("Error parsing xml " + e1.getMessage(), e1);
    } finally {
        IOUtils.closeQuietly(fileContents);
    }
    return (IWantDocumentBatchFeed) parsed;
}
Also used : IWantDocumentBatchFeed(edu.cornell.kfs.module.purap.businessobject.IWantDocumentBatchFeed) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ParseException(org.kuali.kfs.sys.exception.ParseException) FileInputStream(java.io.FileInputStream)

Example 3 with ParseException

use of org.kuali.kfs.sys.exception.ParseException in project cu-kfs by CU-CommunityApps.

the class BatchFeedHelperServiceImpl method parseBatchFile.

/**
 * @see com.rsmart.kuali.kfs.sys.batch.service.BatchFeedHelperService#parseBatchFile(org.kuali.kfs.sys.batch.BatchInputFileType,
 *      java.lang.String, com.rsmart.kuali.kfs.sys.businessobject.BatchFeedStatusBase)
 */
public Object parseBatchFile(BatchInputFileType batchInputFileType, String incomingFileName, BatchFeedStatusBase batchStatus) {
    FileInputStream fileContents;
    try {
        fileContents = new FileInputStream(incomingFileName);
    } catch (FileNotFoundException e1) {
        LOG.error("file to load not found " + incomingFileName, e1);
        throw new RuntimeException("Cannot find the file requested to be loaded " + incomingFileName, e1);
    }
    // do the parse
    Object parsed = null;
    try {
        byte[] fileByteContent = IOUtils.toByteArray(fileContents);
        parsed = batchInputFileService.parse(batchInputFileType, fileByteContent);
    } catch (IOException e) {
        LOG.error("error while getting file bytes:  " + e.getMessage(), e);
        throw new RuntimeException("Error encountered while attempting to get file bytes: " + e.getMessage(), e);
    } catch (ParseException e1) {
        LOG.error("Error parsing xml " + e1.getMessage());
        batchStatus.setXmlParseExceptionMessage(e1.getMessage());
    }
    return parsed;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) PersistableBusinessObject(org.kuali.kfs.krad.bo.PersistableBusinessObject) IOException(java.io.IOException) ParseException(org.kuali.kfs.sys.exception.ParseException) FileInputStream(java.io.FileInputStream)

Example 4 with ParseException

use of org.kuali.kfs.sys.exception.ParseException in project cu-kfs by CU-CommunityApps.

the class ReceiptProcessingServiceImpl method attachFiles.

/**
 */
public boolean attachFiles(String fileName, BatchInputFileType batchInputFileType, String customerName) {
    boolean result = true;
    // load up the file into a byte array
    byte[] fileByteContent = safelyLoadFileBytes(fileName);
    LOG.info("Attempting to parse the file");
    Object parsedObject = null;
    try {
        parsedObject = batchInputFileService.parse(batchInputFileType, fileByteContent);
    } catch (ParseException e) {
        String errorMessage = "Error parsing batch file: " + e.getMessage();
        LOG.error(errorMessage, e);
        throw new RuntimeException(errorMessage);
    }
    // make sure we got the type we expected, then cast it
    if (!(parsedObject instanceof List)) {
        String errorMessage = "Parsed file was not of the expected type.  Expected [" + List.class + "] but got [" + parsedObject.getClass() + "].";
        criticalError(errorMessage);
    }
    List<ReceiptProcessing> receipts = ((List<ReceiptProcessing>) parsedObject);
    final String attachmentsPath = pdfDirectory;
    String mimeTypeCode = "pdf";
    // determine in which mode we are: match& attach, match, attach
    // if first 5 fields ate not blank then it is a match and attach
    boolean matchAndAttach = false;
    // if any receipt get the first one
    if (ObjectUtils.isNotNull(receipts) && receipts.size() > 0) {
        ReceiptProcessing receipt = receipts.get(0);
        // match and attach only occurs for CALS; for CALS files the source unique ID is blank
        matchAndAttach = StringUtils.isBlank(receipt.getSourceUniqueID());
    }
    if (matchAndAttach) {
        matchAndAttach(receipts, attachmentsPath, mimeTypeCode);
    } else {
        matchOrAttachOnly(fileName, batchInputFileType, receipts, attachmentsPath, mimeTypeCode);
    }
    return result;
}
Also used : ReceiptProcessing(edu.cornell.kfs.module.receiptProcessing.businessobject.ReceiptProcessing) ArrayList(java.util.ArrayList) List(java.util.List) ParseException(org.kuali.kfs.sys.exception.ParseException)

Example 5 with ParseException

use of org.kuali.kfs.sys.exception.ParseException in project cu-kfs by CU-CommunityApps.

the class CsvBatchInputFileTypeBase method parse.

/**
 * @see org.kuali.kfs.sys.batch.BatchInputFileType#parse(byte[])
 *
 * @return parsed object in structure - List<Map<String, String>>
 */
public Object parse(byte[] fileByteContent) throws ParseException {
    // handle null objects and zero byte contents
    String errorMessage = fileByteContent == null ? "an invalid(null) argument was given" : fileByteContent.length == 0 ? "an invalid argument was given, empty input stream" : "";
    if (!errorMessage.isEmpty()) {
        LOG.error(errorMessage);
        throw new IllegalArgumentException(errorMessage);
    }
    List<String> headerList = getCsvHeaderList();
    Object parsedContents = null;
    try {
        // validate csv header
        ByteArrayInputStream validateFileContents = new ByteArrayInputStream(fileByteContent);
        validateCSVFileInput(headerList, validateFileContents);
        // use csv reader to parse the csv content
        CSVReader csvReader = new CSVReader(new InputStreamReader(new ByteArrayInputStream(fileByteContent)), ',', '"', '|');
        List<String[]> dataList = csvReader.readAll();
        // remove first header line
        dataList.remove(0);
        // parse and create List of Maps base on enum value names as map keys
        List<Map<String, String>> dataMapList = new ArrayList<Map<String, String>>();
        Map<String, String> rowMap;
        int index = 0;
        for (String[] row : dataList) {
            rowMap = new LinkedHashMap<String, String>();
            // reset index
            index = 0;
            for (String header : headerList) {
                rowMap.put(header, row[index++]);
            }
            dataMapList.add(rowMap);
        }
        parsedContents = dataMapList;
    } catch (IOException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new ParseException(ex.getMessage(), ex);
    }
    return parsedContents;
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(au.com.bytecode.opencsv.CSVReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseException(org.kuali.kfs.sys.exception.ParseException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

ParseException (org.kuali.kfs.sys.exception.ParseException)15 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)8 FileInputStream (java.io.FileInputStream)7 FileNotFoundException (java.io.FileNotFoundException)7 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStreamReader (java.io.InputStreamReader)4 List (java.util.List)4 ReceiptProcessing (edu.cornell.kfs.module.receiptProcessing.businessobject.ReceiptProcessing)3 CSVReader (au.com.bytecode.opencsv.CSVReader)2 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 Date (java.sql.Date)2 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ProcurementCardDocument (org.kuali.kfs.fp.document.ProcurementCardDocument)2 Attachment (org.kuali.kfs.krad.bo.Attachment)2 Note (org.kuali.kfs.krad.bo.Note)2