use of org.hl7.fhir.r5.model.Enumerations.FHIRVersion in project CRD by HL7-DaVinci.
the class FhirController method getFhirResourceById.
/**
* Retrieve a FHIR resource by id
* @param fhirVersion (converted to uppercase)
* @param resource (converted to lowercase)
* @param id (converted to lowercase)
* @return
* @throws IOException
*/
@GetMapping(path = "/fhir/{fhirVersion}/{resource}/{id}")
public ResponseEntity<Resource> getFhirResourceById(HttpServletRequest request, @PathVariable String fhirVersion, @PathVariable String resource, @PathVariable String id) throws IOException {
fhirVersion = fhirVersion.toUpperCase();
resource = resource.toLowerCase();
logger.info("GET /fhir/" + fhirVersion + "/" + resource + "/" + id);
String baseUrl = Utils.getApplicationBaseUrl(request).toString() + "/";
FileResource fileResource = fileStore.getFhirResourceById(fhirVersion, resource, id, baseUrl);
return processFileResource(fileResource);
}
use of org.hl7.fhir.r5.model.Enumerations.FHIRVersion in project CRD by HL7-DaVinci.
the class FhirController method submitFhirResource.
/**
* Store a FHIR resource.
*/
@PostMapping(path = "/fhir/{fhirVersion}/{resource}", consumes = { MediaType.APPLICATION_JSON_VALUE, "application/fhir+json" })
public ResponseEntity<String> submitFhirResource(HttpServletRequest request, HttpEntity<String> entity, @PathVariable String fhirVersion, @PathVariable String resource) {
fhirVersion = fhirVersion.toUpperCase();
resource = resource.toLowerCase();
logger.info("POST /fhir/" + fhirVersion + "/" + resource);
FhirResourceInfo fhirResourceInfo = new FhirResourceInfo();
// pull out the ID and name
if (fhirVersion.equalsIgnoreCase("R4")) {
fhirResourceInfo = org.hl7.davinci.r4.Utilities.getFhirResourceInfo(entity.getBody());
} else {
logger.warning("unsupported FHIR version: " + fhirVersion + ", not storing");
HttpStatus status = HttpStatus.BAD_REQUEST;
MediaType contentType = MediaType.TEXT_PLAIN;
return ResponseEntity.status(status).contentType(contentType).body("Bad Request");
}
if (!fhirResourceInfo.valid()) {
logger.warning("submitFhirResource: unsupported FHIR Resource of type: " + fhirResourceInfo.getType() + " (" + fhirVersion + "), not storing");
HttpStatus status = HttpStatus.BAD_REQUEST;
MediaType contentType = MediaType.TEXT_PLAIN;
return ResponseEntity.status(status).contentType(contentType).body("Bad Request");
}
// build the FhirResource and save to the database
FhirResource fhirResource = new FhirResource();
fhirResource.setFhirVersion(fhirVersion).setResourceType(fhirResourceInfo.getType()).setData((entity.getBody()));
if (fhirResourceInfo.getId() != null) {
fhirResource.setId(fhirResourceInfo.getId());
}
if (fhirResourceInfo.getName() != null) {
fhirResource.setName(fhirResourceInfo.getName());
}
FhirResource newFhirResource = fhirResourceRepository.save(fhirResource);
String newId = newFhirResource.getId();
String statusMsg = "successfully stored " + fhirResourceInfo.getType() + ": " + newId;
logger.info(statusMsg);
// build the response
HttpStatus status = HttpStatus.CREATED;
MediaType contentType = MediaType.TEXT_PLAIN;
String baseUrl = Utils.getApplicationBaseUrl(request).toString() + "/";
return ResponseEntity.status(status).contentType(contentType).header(HttpHeaders.LOCATION, baseUrl + "/fhir/" + fhirVersion + "/" + resource + "?identifier=" + newId).body(statusMsg);
}
use of org.hl7.fhir.r5.model.Enumerations.FHIRVersion in project CRD by HL7-DaVinci.
the class CdsConnectFileStore method processFhirFiles.
private void processFhirFiles(List<CdsConnectFile> files, String topic) {
// process the fhir resource files
// setup the proper FHIR Context for the version of FHIR we are dealing with
FhirContext r4ctx = new org.hl7.davinci.r4.FhirComponents().getFhirContext();
IParser r4parser = r4ctx.newJsonParser();
// suppress the unknown element warnings
r4parser.setParserErrorHandler(new SuppressParserErrorHandler());
// process all of the files found within the topic/artifact
for (CdsConnectFile file : files) {
String path = file.getPath();
String filename = file.getFilename();
if (filename.endsWith(".json")) {
logger.info(" process: FHIR Resource: " + filename);
String[] parts = filename.split("-");
if (parts.length > 2) {
// String resourceType = parts[0];
String fhirVersion = parts[1];
String name = parts[2];
IBaseResource baseResource = null;
byte[] fileContents = file.getCqlBundle();
if (fhirVersion.equalsIgnoreCase("R4")) {
baseResource = r4parser.parseResource(new ByteArrayInputStream(fileContents));
}
processFhirResource(baseResource, path, filename, fhirVersion, topic);
}
}
}
}
use of org.hl7.fhir.r5.model.Enumerations.FHIRVersion in project CRD by HL7-DaVinci.
the class GitHubFileStore method processFhirFolder.
private void processFhirFolder(String topic, String fhirVersion, String fhirPath) {
fhirVersion = fhirVersion.toUpperCase();
logger.info(" GitHubFileStore::processFhirFolder(): " + fhirVersion + ": " + fhirPath);
// setup the proper FHIR Context for the version of FHIR we are dealing with
FhirContext ctx = null;
if (fhirVersion.equalsIgnoreCase("R4")) {
ctx = new org.hl7.davinci.r4.FhirComponents().getFhirContext();
} else {
logger.warn("unsupported FHIR version: " + fhirVersion + ", skipping folder");
return;
}
IParser parser = ctx.newJsonParser();
// suppress the unknown element warnings
parser.setParserErrorHandler(new SuppressParserErrorHandler());
for (String folder : connection.getDirectory(fhirPath)) {
if (folder.equalsIgnoreCase("resources")) {
String fullFolderPath = fhirPath + "/" + folder;
for (String resource : connection.getDirectory(fullFolderPath)) {
String filename = resource;
String fullFilePath = fullFolderPath + "/" + filename;
logger.info(" process: FHIR Resource: " + filename);
String[] parts = filename.split("-");
if (parts.length > 2) {
// = parts[0];
String resourceType;
if (!parts[1].equalsIgnoreCase(fhirVersion)) {
logger.warn("GitHubFileStore::processFhirFolder() warning: FhirVersion doesn't match!");
continue;
}
InputStream inputStream = connection.getFile(fullFilePath);
if (inputStream != null) {
IBaseResource baseResource = parser.parseResource(inputStream);
processFhirResource(baseResource, filename, filename, fhirVersion, topic);
} else {
logger.warn("could not find file: " + fullFilePath);
continue;
}
}
}
}
}
}
use of org.hl7.fhir.r5.model.Enumerations.FHIRVersion in project CRD by HL7-DaVinci.
the class CqlRule method getFhirVersionFromCqlFile.
private static String getFhirVersionFromCqlFile(byte[] cql) {
String fhirVersion = "";
UsingDef usingDef = new UsingDef();
Pattern pattern = Pattern.compile("using (.*?) version '(.*?)'");
try {
Matcher matcher = pattern.matcher(new String(cql));
while (fhirVersion.isEmpty()) {
matcher.find();
if (matcher.groupCount() != 2) {
throw new RuntimeException("Encountered bad CQL file, could not detect library identifier.");
}
if (matcher.group(1).equalsIgnoreCase("FHIR")) {
fhirVersion = matcher.group(2);
}
}
} catch (Exception e) {
logger.warn("exception in CqlRule::getFhirVersionFromCqlFile(): " + e.getMessage());
}
return fhirVersion;
}
Aggregations