Search in sources :

Example 11 with ParseException

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

the class PaymentFileServiceImpl method parsePaymentFile.

/**
 * Calls <code>BatchInputFileService</code> to validate XML against schema and parse.
 *
 * @param paymentInputFileType <code>BatchInputFileType</code> for payment files
 * @param incomingFileName name of the payment file to parse
 * @param errorMap any errors encountered while parsing are adding to
 * @return <code>PaymentFile</code> containing the parsed values
 */
protected PaymentFileLoad parsePaymentFile(BatchInputFileType paymentInputFileType, String incomingFileName, MessageMap errorMap) {
    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
    PaymentFileLoad paymentFile = null;
    try {
        byte[] fileByteContent = IOUtils.toByteArray(fileContents);
        paymentFile = (PaymentFileLoad) batchInputFileService.parse(paymentInputFileType, 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());
        errorMap.putError(KFSConstants.GLOBAL_ERRORS, KFSKeyConstants.ERROR_BATCH_UPLOAD_PARSING_XML, new String[] { e1.getMessage() });
    }
    return paymentFile;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ParseException(org.kuali.kfs.sys.exception.ParseException) FileInputStream(java.io.FileInputStream) PaymentFileLoad(org.kuali.kfs.pdp.businessobject.PaymentFileLoad)

Example 12 with ParseException

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

the class TransactionOverrideCsvBatchInputFileType method parse.

/**
 * Overridden to parse a tab-delimited file instead of a comma-delimited one (unlike similar subclasses),
 * and to use the convertParsedObjectToVO method to create the desired objects (like similar subclasses).
 * Some other tweaks have also been made to improve upon the existing superclass code.
 *
 * @see org.kuali.kfs.sys.batch.CsvBatchInputFileTypeBase#parse(byte[])
 */
@Override
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;
    CSVReader csvReader = null;
    try {
        // validate csv header
        ByteArrayInputStream validateFileContents = new ByteArrayInputStream(fileByteContent);
        validateCSVFileInput(headerList, validateFileContents);
        // use csv reader to parse tab-delimited content
        csvReader = new CSVReader(new InputStreamReader(new ByteArrayInputStream(fileByteContent)), '\t');
        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);
    } finally {
        IOUtils.closeQuietly(csvReader);
    }
    return convertParsedObjectToVO(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) TransactionOverride(edu.cornell.kfs.tax.businessobject.TransactionOverride)

Example 13 with ParseException

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

the class CommodityCodeInputFileType method parse.

/**
 * @see org.kuali.kfs.sys.batch.BatchInputFileType#parse(byte[])
 */
public Object parse(byte[] fileByteContent) throws ParseException {
    List<CommodityCode> batchList = new ArrayList<CommodityCode>();
    CommodityCode newCode = null;
    BufferedReader bufferedFileReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(fileByteContent)));
    String fileLine;
    String commodityCode;
    String commodityCodeDesc;
    int lineNumber = 0;
    try {
        while ((fileLine = bufferedFileReader.readLine()) != null) {
            lineNumber++;
            // Parse the tab delimited lines
            StringTokenizer st = new StringTokenizer(fileLine, "\t");
            commodityCode = st.nextToken();
            commodityCodeDesc = st.nextToken();
            if (st.hasMoreTokens()) {
            // Report too many values on the line
            }
            newCode = new CommodityCode();
            newCode.setPurchasingCommodityCode(commodityCode);
            newCode.setCommodityDescription(commodityCodeDesc);
            batchList.add(newCode);
        }
    } catch (IOException e) {
        // probably won't happen since we're reading from a byte array, but just in case
        LOG.error("Error encountered reading from file content", e);
        throw new ParseException("Error encountered reading from file content", e);
    }
    return batchList;
}
Also used : StringTokenizer(java.util.StringTokenizer) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) CommodityCode(org.kuali.kfs.vnd.businessobject.CommodityCode) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ParseException(org.kuali.kfs.sys.exception.ParseException)

Example 14 with ParseException

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

the class ReceiptProcessingServiceImpl method matchAndAttach.

/**
 * Performs match and attach. It will search for a match PCDO document and attempt to attach the receipt.
 *
 * @param receipts
 * @param attachmentsPath
 * @param mimeTypeCode
 */
protected void matchAndAttach(List<ReceiptProcessing> receipts, String attachmentsPath, String mimeTypeCode) {
    StringBuilder processResults = new StringBuilder();
    processResults.append(RESULT_FILE_HEADER_LINE);
    for (ReceiptProcessing receipt : receipts) {
        Note note = new Note();
        java.util.Date pdate = null;
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        try {
            pdate = (java.util.Date) df.parse(receipt.getPurchasedate());
        } catch (ParseException e) {
            processResults.append(receipt.badData(false));
            LOG.error("Bad date field on incoming csv");
            continue;
        } catch (java.text.ParseException e) {
            processResults.append(receipt.badData(false));
            LOG.error("Bad date field on incoming csv");
            continue;
        }
        Date pdateSQL = null;
        if (pdate != null) {
            pdateSQL = new Date(pdate.getTime());
        }
        List<ProcurementCardDocument> pcdoList = procurementCardDocumentDao.getDocumentByCarhdHolderAmountDateVendor(receipt.getCardHolder(), receipt.getAmount(), pdateSQL);
        ProcurementCardDocument pcdo = null;
        if (ObjectUtils.isNull(pcdoList) || pcdoList.isEmpty()) {
            processResults.append(receipt.noMatch(false));
            continue;
        }
        if (pcdoList.size() > 1) {
            processResults.append(receipt.multipleMatch(false));
            continue;
        }
        if (pcdoList.size() == 1) {
            pcdo = pcdoList.get(0);
        }
        String pdfFileName = attachmentsPath + "/" + receipt.getFilename();
        LOG.info("Start creating note and attaching pdf file " + pdfFileName + " to PCDO document #" + pcdo.getDocumentNumber());
        File f = null;
        FileInputStream fileInputStream = null;
        try {
            f = new File(pdfFileName);
            fileInputStream = new FileInputStream(pdfFileName);
        } catch (FileNotFoundException e) {
            LOG.error("File " + pdfFileName + " not found for Document " + pcdo.getDocumentNumber());
            processResults.append(receipt.badData(false));
            continue;
        } catch (IOException e) {
            LOG.error("generic Io exception for Document " + pcdo.getDocumentNumber());
            processResults.append(receipt.badData(false));
            continue;
        }
        long fileSizeLong = f.length();
        Integer fileSize = Integer.parseInt(Long.toString(fileSizeLong));
        String attachType = "";
        Attachment noteAttachment = null;
        try {
            noteAttachment = attachmentService.createAttachment(pcdo.getDocumentHeader(), pdfFileName, mimeTypeCode, fileSize, fileInputStream, attachType);
        } catch (IOException e) {
            LOG.error("Failed to attach file for Document " + pcdo.getDocumentNumber());
            processResults.append(receipt.noMatch(false));
            e.printStackTrace();
            continue;
        } catch (IllegalArgumentException e) {
            /*
			     * Our custom attachment service will throw an IllegalArgumentException if the virus scan fails.
			     * The virus scan could also end up failing if the file size is too large. In such cases,
			     * return an error code indicating such a problem (or a problem with invalid parameters).
			     */
            LOG.error("Failed to create attachment for Document " + pcdo.getDocumentNumber(), e);
            processResults.append(receipt.attachmentCreationError(false));
            continue;
        }
        if (noteAttachment != null) {
            note.setNoteText("Receipt Attached");
            note.addAttachment(noteAttachment);
            note.setRemoteObjectIdentifier(pcdo.getDocumentHeader().getObjectId());
            note.setAuthorUniversalIdentifier(getSystemUser().getPrincipalId());
            note.setNoteTypeCode(KFSConstants.NoteTypeEnum.BUSINESS_OBJECT_NOTE_TYPE.getCode());
            note.setNotePostedTimestampToCurrent();
            try {
                noteService.save(note);
            } catch (Exception e) {
                LOG.error("Failed to save note for Document " + pcdo.getDocumentNumber());
                processResults.append(receipt.noMatch(false));
                e.printStackTrace();
                continue;
            }
            LOG.info("Attached pdf " + pdfFileName + " for document " + pcdo.getDocumentNumber());
            processResults.append(receipt.match(pcdo.getDocumentNumber(), false));
        }
    }
    String outputCsv = processResults.toString();
    // this is CALS output folder and it has to stay unchanged
    String reportDropFolder = pdfDirectory + "/CIT-csv-archive/";
    getcsvWriter(outputCsv, reportDropFolder);
}
Also used : ReceiptProcessing(edu.cornell.kfs.module.receiptProcessing.businessobject.ReceiptProcessing) FileNotFoundException(java.io.FileNotFoundException) Attachment(org.kuali.kfs.krad.bo.Attachment) IOException(java.io.IOException) Date(java.sql.Date) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.kuali.kfs.sys.exception.ParseException) ProcurementCardDocument(org.kuali.kfs.fp.document.ProcurementCardDocument) Note(org.kuali.kfs.krad.bo.Note) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ParseException(org.kuali.kfs.sys.exception.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File)

Example 15 with ParseException

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

the class ReceiptProcessingServiceImpl method matchOrAttachOnly.

/**
 * Performs match or attach on each incoming record. The method determines
 * for each incoming record if a match only or attach only should be
 * performed. If first card holder, amount and purchase date are not blank
 * then a match only is performed. If the source unique id and edoc number
 * are not blank then an attach only is performed.
 *
 * @param fileName
 * @param receipts
 * @param attachmentsPath
 * @param mimeTypeCode
 */
protected void matchOrAttachOnly(String fileName, BatchInputFileType batchInputFileType, List<ReceiptProcessing> receipts, String attachmentsPath, String mimeTypeCode) {
    StringBuilder processResults = new StringBuilder();
    processResults.append(RESULT_FILE_HEADER_LINE_WITH_EXTRA_FIELDS);
    String customerName = getCustomerNameFromFileName(fileName, batchInputFileType);
    for (ReceiptProcessing receipt : receipts) {
        boolean matchOnly = StringUtils.isNotBlank(receipt.getCardHolder()) && StringUtils.isNotBlank(receipt.getAmount()) && StringUtils.isNotBlank(receipt.getPurchasedate()) && StringUtils.isBlank(receipt.getFilePath()) && StringUtils.isBlank(receipt.getFilename());
        boolean attachOnly = StringUtils.isNotBlank(receipt.getSourceUniqueID()) && StringUtils.isNotBlank(receipt.getFilePath()) && StringUtils.isNotBlank(receipt.getFilename());
        if (matchOnly) {
            java.util.Date pdate = null;
            DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
            try {
                pdate = (java.util.Date) df.parse(receipt.getPurchasedate());
            } catch (ParseException e) {
                processResults.append(receipt.badData(true));
                LOG.error("Bad date field on incoming csv");
                continue;
            } catch (java.text.ParseException e) {
                processResults.append(receipt.badData(true));
                LOG.error("Bad date field on incoming csv");
                continue;
            }
            Date pdateSQL = null;
            if (pdate != null) {
                pdateSQL = new Date(pdate.getTime());
            }
            List<ProcurementCardDocument> pcdoList = procurementCardDocumentDao.getDocumentByCarhdHolderNameAmountDateCardHolderNetID(receipt.getAmount(), pdateSQL, receipt.getCardHolderNetID());
            ProcurementCardDocument pcdo = null;
            if (ObjectUtils.isNull(pcdoList) || pcdoList.isEmpty()) {
                processResults.append(receipt.noMatch(true));
                continue;
            }
            if (pcdoList.size() > 1) {
                processResults.append(receipt.multipleMatch(true));
                continue;
            }
            if (pcdoList.size() == 1) {
                pcdo = pcdoList.get(0);
                String eDocNumber = pcdo.getDocumentNumber();
                receipt.seteDocNumber(eDocNumber);
                processResults.append(receipt.match(eDocNumber, true));
            }
        }
        if (attachOnly) {
            Note note = new Note();
            List<ProcurementCardDocument> pcdoList = procurementCardDocumentDao.getDocumentByEdocNumber(receipt.geteDocNumber());
            ProcurementCardDocument pcdo = null;
            if (ObjectUtils.isNull(pcdoList) || pcdoList.isEmpty()) {
                processResults.append(receipt.attachOnlyError());
                continue;
            }
            if (pcdoList.size() > 1) {
                processResults.append(receipt.attachOnlyError());
                continue;
            }
            if (pcdoList.size() == 1) {
                pcdo = pcdoList.get(0);
            }
            if (StringUtils.isNotBlank(customerName)) {
                attachmentsPath = pdfDirectory + "/" + StringUtils.upperCase(customerName) + CUSTOMER_PDF_SUBFOLDER_SUFFIX;
            }
            String pdfFileName = attachmentsPath + "/" + receipt.getFilename();
            LOG.info("Start creating note and attaching pdf file " + pdfFileName + " to PCDO document #" + pcdo.getDocumentNumber());
            File f = null;
            FileInputStream fileInputStream = null;
            try {
                f = new File(pdfFileName);
                fileInputStream = new FileInputStream(pdfFileName);
            } catch (FileNotFoundException e) {
                LOG.error("File " + pdfFileName + " not found for Document " + pcdo.getDocumentNumber());
                processResults.append(receipt.attachOnlyError());
                continue;
            } catch (IOException e) {
                LOG.error("generic Io exception for Document " + pcdo.getDocumentNumber());
                processResults.append(receipt.attachOnlyError());
                continue;
            }
            long fileSizeLong = f.length();
            Integer fileSize = Integer.parseInt(Long.toString(fileSizeLong));
            String attachType = "";
            Attachment noteAttachment = null;
            try {
                noteAttachment = attachmentService.createAttachment(pcdo.getDocumentHeader(), pdfFileName, mimeTypeCode, fileSize, fileInputStream, attachType);
            } catch (IOException e) {
                LOG.error("Failed to attach file for Document " + pcdo.getDocumentNumber());
                processResults.append(receipt.attachOnlyError());
                e.printStackTrace();
                continue;
            } catch (IllegalArgumentException e) {
                /*
				     * Our custom attachment service will throw an IllegalArgumentException if the virus scan fails.
				     * The virus scan could also end up failing if the file size is too large. In such cases,
				     * return an error code indicating such a problem (or a problem with invalid parameters).
				     */
                LOG.error("Failed to create attachment for Document " + pcdo.getDocumentNumber(), e);
                processResults.append(receipt.attachmentCreationError(true));
                continue;
            }
            if (noteAttachment != null) {
                note.setNoteText("Receipt Attached");
                note.addAttachment(noteAttachment);
                note.setRemoteObjectIdentifier(pcdo.getDocumentHeader().getObjectId());
                note.setAuthorUniversalIdentifier(getSystemUser().getPrincipalId());
                note.setNoteTypeCode(KFSConstants.NoteTypeEnum.BUSINESS_OBJECT_NOTE_TYPE.getCode());
                note.setNotePostedTimestampToCurrent();
                try {
                    noteService.save(note);
                } catch (Exception e) {
                    LOG.error("Failed to save note for Document " + pcdo.getDocumentNumber());
                    processResults.append(receipt.attachOnlyError());
                    e.printStackTrace();
                    continue;
                }
                LOG.info("Attached pdf " + pdfFileName + " for document " + pcdo.getDocumentNumber());
                processResults.append(receipt.match("8", true));
            }
        }
        if (!matchOnly && !attachOnly) {
            LOG.info("Invalid input data does not meet either match only nor attach only conditions: " + receipt.returnBoLine(true));
        }
    }
    String outputCsv = processResults.toString();
    // each customer will have a separate output folder for easier processing of the results files
    String reportDropFolder = pdfDirectory + "/CIT-" + customerName + "-csv-archive/";
    try {
        /**
         * Create, if not there
         */
        FileUtils.forceMkdir(new File(reportDropFolder));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    getcsvWriter(outputCsv, reportDropFolder);
}
Also used : FileNotFoundException(java.io.FileNotFoundException) Attachment(org.kuali.kfs.krad.bo.Attachment) ProcurementCardDocument(org.kuali.kfs.fp.document.ProcurementCardDocument) ReceiptProcessing(edu.cornell.kfs.module.receiptProcessing.businessobject.ReceiptProcessing) IOException(java.io.IOException) Date(java.sql.Date) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.kuali.kfs.sys.exception.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Note(org.kuali.kfs.krad.bo.Note) ParseException(org.kuali.kfs.sys.exception.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File)

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