use of gov.cms.bfd.pipeline.ccw.rif.extract.CsvRecordGroupingIterator.CsvRecordGrouper in project beneficiary-fhir-data by CMSgov.
the class RifFilesProcessor method produceRecords.
/**
* @param rifFileEvent the {@link RifFileEvent} that is being processed
* @return a {@link RifFileRecords} with the {@link RifRecordEvent}s produced from the specified
* {@link RifFileEvent}
*/
public RifFileRecords produceRecords(RifFileEvent rifFileEvent) {
RifFile file = rifFileEvent.getFile();
/*
* Approach used here to parse CSV as a Java 8 Stream is courtesy of
* https://rumianom.pl/rumianom/entry/apache-commons-csv-with-java.
*/
CSVParser parser = RifParsingUtils.createCsvParser(file);
boolean isGrouped;
BiFunction<RifFileEvent, List<CSVRecord>, RifRecordEvent<?>> recordParser;
if (file.getFileType() == RifFileType.BENEFICIARY) {
isGrouped = false;
recordParser = RifFilesProcessor::buildBeneficiaryEvent;
} else if (file.getFileType() == RifFileType.BENEFICIARY_HISTORY) {
isGrouped = false;
recordParser = RifFilesProcessor::buildBeneficiaryHistoryEvent;
} else if (file.getFileType() == RifFileType.MEDICARE_BENEFICIARY_ID_HISTORY) {
isGrouped = false;
recordParser = RifFilesProcessor::buildMedicareBeneficiaryIdHistoryEvent;
} else if (file.getFileType() == RifFileType.PDE) {
isGrouped = false;
recordParser = RifFilesProcessor::buildPartDEvent;
} else if (file.getFileType() == RifFileType.CARRIER) {
isGrouped = true;
recordParser = RifFilesProcessor::buildCarrierClaimEvent;
} else if (file.getFileType() == RifFileType.INPATIENT) {
isGrouped = true;
recordParser = RifFilesProcessor::buildInpatientClaimEvent;
} else if (file.getFileType() == RifFileType.OUTPATIENT) {
isGrouped = true;
recordParser = RifFilesProcessor::buildOutpatientClaimEvent;
} else if (file.getFileType() == RifFileType.SNF) {
isGrouped = true;
recordParser = RifFilesProcessor::buildSNFClaimEvent;
} else if (file.getFileType() == RifFileType.HOSPICE) {
isGrouped = true;
recordParser = RifFilesProcessor::buildHospiceClaimEvent;
} else if (file.getFileType() == RifFileType.HHA) {
isGrouped = true;
recordParser = RifFilesProcessor::buildHHAClaimEvent;
} else if (file.getFileType() == RifFileType.DME) {
isGrouped = true;
recordParser = RifFilesProcessor::buildDMEClaimEvent;
} else {
throw new UnsupportedRifFileTypeException("Unsupported file type:" + file.getFileType());
}
/*
* Use the CSVParser to drive a Stream of grouped CSVRecords
* (specifically, group by claim ID/lines).
*/
CsvRecordGrouper grouper = new ColumnValueCsvRecordGrouper(isGrouped ? file.getFileType().getIdColumn() : null);
Iterator<List<CSVRecord>> csvIterator = new CsvRecordGroupingIterator(parser, grouper);
Spliterator<List<CSVRecord>> spliterator = Spliterators.spliteratorUnknownSize(csvIterator, Spliterator.ORDERED | Spliterator.NONNULL);
Stream<List<CSVRecord>> csvRecordStream = StreamSupport.stream(spliterator, false).onClose(() -> {
try {
/*
* This will also close the Reader and InputStream that the
* CSVParser was consuming.
*/
parser.close();
} catch (IOException e) {
LOGGER.warn("Unable to close CSVParser", e);
}
});
/* Map each record group to a single RifRecordEvent. */
Stream<RifRecordEvent<?>> rifRecordStream = csvRecordStream.map(csvRecordGroup -> {
try {
Timer.Context parsingTimer = rifFileEvent.getEventMetrics().timer(MetricRegistry.name(getClass().getSimpleName(), "recordParsing")).time();
RifRecordEvent<?> recordEvent = recordParser.apply(rifFileEvent, csvRecordGroup);
parsingTimer.close();
return recordEvent;
} catch (InvalidRifValueException e) {
LOGGER.warn("Parse error encountered near line number '{}'.", csvRecordGroup.get(0).getRecordNumber());
throw new InvalidRifValueException(e);
}
});
return new RifFileRecords(rifFileEvent, rifRecordStream);
}
Aggregations