use of gov.cms.bfd.sharedutils.exceptions.UncheckedJaxbException in project beneficiary-fhir-data by CMSgov.
the class DataSetSubsetter method downloadDataSet.
/**
* @param options the {@link ExtractionOptions} to use
* @param dataSetS3KeyPrefix the S3 key prefix (i.e. directory) of the data set to download
* @param downloadDirectory the Path to the directory to download the RIF files locally to
* @return the {@link S3RifFile}s that comprise the full 1M beneficiary dummy data set
*/
private static List<RifFile> downloadDataSet(ExtractionOptions options, String dataSetS3KeyPrefix, Path downloadDirectory) {
AmazonS3 s3Client = S3Utilities.createS3Client(options);
TransferManager transferManager = TransferManagerBuilder.standard().withS3Client(s3Client).build();
String dataSetPrefix = "data-random/" + dataSetS3KeyPrefix;
String manifestSuffix = "1_manifest.xml";
Path manifestDownloadPath = downloadDirectory.resolve(manifestSuffix);
if (!Files.exists(manifestDownloadPath)) {
String manifestKey = String.format("%s/%s", dataSetPrefix, manifestSuffix);
Download manifestDownload = transferManager.download(options.getS3BucketName(), manifestKey, manifestDownloadPath.toFile());
try {
manifestDownload.waitForCompletion();
} catch (AmazonClientException | InterruptedException e) {
throw new RuntimeException(e);
}
}
LOGGER.info("Manifest downloaded.");
DataSetManifest dummyDataSetManifest;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(DataSetManifest.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
dummyDataSetManifest = (DataSetManifest) jaxbUnmarshaller.unmarshal(manifestDownloadPath.toFile());
} catch (JAXBException e) {
throw new UncheckedJaxbException(e);
}
List<RifFile> rifFiles = new ArrayList<>();
for (DataSetManifestEntry manifestEntry : dummyDataSetManifest.getEntries()) {
String dataSetFileKey = String.format("%s/%s", dataSetPrefix, manifestEntry.getName());
Path dataSetFileDownloadPath = downloadDirectory.resolve(manifestEntry.getName());
if (!Files.exists(dataSetFileDownloadPath)) {
LOGGER.info("Downloading RIF file: '{}'...", manifestEntry.getName());
Download dataSetFileDownload = transferManager.download(options.getS3BucketName(), dataSetFileKey, dataSetFileDownloadPath.toFile());
try {
dataSetFileDownload.waitForCompletion();
} catch (AmazonClientException | InterruptedException e) {
throw new RuntimeException(e);
}
}
RifFile dataSetFile = new LocalRifFile(dataSetFileDownloadPath, manifestEntry.getType());
rifFiles.add(dataSetFile);
}
transferManager.shutdownNow();
LOGGER.info("Original RIF files ready.");
return rifFiles;
}
use of gov.cms.bfd.sharedutils.exceptions.UncheckedJaxbException in project beneficiary-fhir-data by CMSgov.
the class CodebookVariableReader method unmarshallCodebookXml.
/**
* @param codebookXmlStream the {@link Codebook} XML {@link InputStream} to unmarshall
* @return the {@link Codebook} that was unmarshalled from the specified XML
*/
private static Codebook unmarshallCodebookXml(InputStream codebookXmlStream) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Codebook.class.getPackageName(), Codebook.class.getClassLoader());
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
InputStreamReader codebookXmlReader = new InputStreamReader(codebookXmlStream, StandardCharsets.UTF_8.name());
Codebook codebook = (Codebook) jaxbUnmarshaller.unmarshal(codebookXmlReader);
return codebook;
} catch (JAXBException e) {
throw new UncheckedJaxbException(e);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Null input stream", e);
} catch (UnsupportedEncodingException e) {
throw new UncheckedIOException(e);
}
}
use of gov.cms.bfd.sharedutils.exceptions.UncheckedJaxbException in project beneficiary-fhir-data by CMSgov.
the class CodebookPdfToXmlApp method writeCodebookXmlToFile.
/**
* @param codebook the {@link Codebook} to write out
* @param outputFile the {@link Path} of the file to write the {@link Codebook} out as XML to
* (which will be overwritten if it already exists)
*/
private static void writeCodebookXmlToFile(Codebook codebook, Path outputFile) {
try (FileWriter outputWriter = new FileWriter(outputFile.toFile())) {
JAXBContext jaxbContext = JAXBContext.newInstance(Codebook.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.name());
jaxbMarshaller.marshal(codebook, outputWriter);
} catch (JAXBException e) {
throw new UncheckedJaxbException(e);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
Aggregations