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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations