Search in sources :

Example 1 with StructureMap

use of org.hl7.fhir.r5.model.StructureMap in project Gravity-SDOH-Exchange-RI by FHIR.

the class TaskPollingService method demoRunQRThroughStructureMap.

/*
  A TEST polling service, implemented during January 2022 Connectathon, that automatically runs questionnaireResponse
  resources through a StructureMap. If any exception occurs - just ignore it.
   */
public void demoRunQRThroughStructureMap() {
    log.info("Looking for TOP 3 Patient QuestionnaireResponse resources without derived Observations...");
    // TODO use repository instead
    Bundle tasksBundle = openEhrClient.search().forResource(QuestionnaireResponse.class).where(new StringClientParam(Constants.PARAM_PROFILE).matches().value(SDOHProfiles.QUESTIONNAIRE_RESPONSE)).revInclude(Observation.INCLUDE_DERIVED_FROM.setRecurse(false)).sort().descending(QuestionnaireResponse.AUTHORED).count(3).returnBundle(Bundle.class).execute();
    List<QuestionnaireResponse> responses = FhirUtil.getFromBundle(tasksBundle, QuestionnaireResponse.class, Bundle.SearchEntryMode.MATCH);
    Set<String> derivedFrom = FhirUtil.getFromBundle(tasksBundle, Observation.class, Bundle.SearchEntryMode.INCLUDE).stream().map(o -> o.getDerivedFromFirstRep().getReferenceElement().getIdPart()).collect(Collectors.toSet());
    if (responses.size() != derivedFrom.size()) {
        log.info("Found " + (responses.size() - derivedFrom.size()) + " QuestionnaireResponse resources without observations.");
        List<QuestionnaireResponse> newResponses = responses.stream().filter(qr -> !derivedFrom.contains(qr.getIdElement().getIdPart())).collect(Collectors.toList());
        for (QuestionnaireResponse nr : newResponses) {
            log.info("Converting QuestionnaireResponse with id: " + nr.getIdElement().getIdPart());
            try {
                // TODO move parser logic within the convert service. Use resources instead
                JSONParser parser = new JSONParser(JSONParser.MODE_JSON_SIMPLE);
                Map<String, Object> result = convertService.convert((JSONObject) parser.parse(fhirContext.newJsonParser().encodeResourceToString(nr)));
                Bundle bundle = (Bundle) fhirContext.newJsonParser().parseResource(new JSONObject(result).toJSONString());
                bundle.getEntry().forEach(e -> e.setRequest(new Bundle.BundleEntryRequestComponent().setMethod(Bundle.HTTPVerb.POST).setUrl(e.getResource().getClass().getSimpleName())));
                openEhrClient.transaction().withBundle(bundle).execute();
            } catch (Exception exc) {
                // Just ignore this specific resource and go to the next one
                log.warn(exc.getMessage(), exc);
            }
        }
    }
    log.info("QuestionnaireResponse update process finished.");
}
Also used : Date(java.util.Date) TaskOutputComponent(org.hl7.fhir.r4.model.Task.TaskOutputComponent) Constants(ca.uhn.fhir.rest.api.Constants) RequiredArgsConstructor(lombok.RequiredArgsConstructor) LocalDateTime(java.time.LocalDateTime) CodeableConcept(org.hl7.fhir.r4.model.CodeableConcept) Autowired(org.springframework.beans.factory.annotation.Autowired) Endpoint(org.hl7.fhir.r4.model.Endpoint) Scheduled(org.springframework.scheduling.annotation.Scheduled) Reference(org.hl7.fhir.r4.model.Reference) TaskStatus(org.hl7.fhir.r4.model.Task.TaskStatus) Function(java.util.function.Function) ArrayList(java.util.ArrayList) JSONParser(net.minidev.json.parser.JSONParser) TasksPollingInfo(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TasksPollingBundleExtractor.TasksPollingInfo) Procedure(org.hl7.fhir.r4.model.Procedure) Task(org.hl7.fhir.r4.model.Task) FhirContext(ca.uhn.fhir.context.FhirContext) TaskInfoHolder(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor.TaskInfoHolder) Lists(com.google.common.collect.Lists) Service(org.springframework.stereotype.Service) Map(java.util.Map) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) StringClientParam(ca.uhn.fhir.rest.gclient.StringClientParam) SearchModifierCode(org.hl7.fhir.r4.model.codesystems.SearchModifierCode) Observation(org.hl7.fhir.r4.model.Observation) ServiceRequest(org.hl7.fhir.r4.model.ServiceRequest) SDOHProfiles(org.hl7.gravity.refimpl.sdohexchange.fhir.SDOHProfiles) TokenClientParam(ca.uhn.fhir.rest.gclient.TokenClientParam) Set(java.util.Set) TaskFailBundleFactory(org.hl7.gravity.refimpl.sdohexchange.fhir.factory.TaskFailBundleFactory) CpClientException(org.hl7.gravity.refimpl.sdohexchange.service.CpService.CpClientException) Collectors(java.util.stream.Collectors) IdType(org.hl7.fhir.r4.model.IdType) ZoneId(java.time.ZoneId) Organization(org.hl7.fhir.r4.model.Organization) Slf4j(lombok.extern.slf4j.Slf4j) QuestionnaireResponse(org.hl7.fhir.r4.model.QuestionnaireResponse) TaskPollingUpdateException(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TasksPollingBundleExtractor.TaskPollingUpdateException) List(java.util.List) TasksPollingBundleExtractor(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TasksPollingBundleExtractor) JSONObject(net.minidev.json.JSONObject) Bundle(org.hl7.fhir.r4.model.Bundle) OrganizationTypeCode(org.hl7.gravity.refimpl.sdohexchange.codes.OrganizationTypeCode) FhirUtil(org.hl7.gravity.refimpl.sdohexchange.util.FhirUtil) StringClientParam(ca.uhn.fhir.rest.gclient.StringClientParam) Bundle(org.hl7.fhir.r4.model.Bundle) QuestionnaireResponse(org.hl7.fhir.r4.model.QuestionnaireResponse) CpClientException(org.hl7.gravity.refimpl.sdohexchange.service.CpService.CpClientException) TaskPollingUpdateException(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TasksPollingBundleExtractor.TaskPollingUpdateException) JSONObject(net.minidev.json.JSONObject) Observation(org.hl7.fhir.r4.model.Observation) JSONParser(net.minidev.json.parser.JSONParser) JSONObject(net.minidev.json.JSONObject)

Example 2 with StructureMap

use of org.hl7.fhir.r5.model.StructureMap in project Gravity-SDOH-Exchange-RI by FHIR.

the class ConvertService method loadMapIg.

private void loadMapIg(String mapUri) throws IOException {
    Path mapIg = Files.createTempDirectory("mapIg");
    Optional<StructureMap> foundStructureMap = structureMapRepository.findByUrl(mapUri);
    if (!foundStructureMap.isPresent()) {
        throw new ResourceNotFoundException(String.format("StructureMap was not found by URL '%s'.", mapUri));
    }
    StructureMap structureMap = foundStructureMap.get();
    File jsonMap = new File(mapIg.toString() + "/map.json");
    jsonMap.createNewFile();
    try (FileWriter fileWriter = new FileWriter(jsonMap)) {
        fileWriter.write(fhirContext.newJsonParser().encodeResourceToString(structureMap));
        fileWriter.flush();
    }
    validationEngine.loadIg(mapIg.toString(), false);
    Files.delete(jsonMap.toPath());
    Files.delete(mapIg);
}
Also used : Path(java.nio.file.Path) StructureMap(org.hl7.fhir.r4.model.StructureMap) FileWriter(java.io.FileWriter) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) File(java.io.File)

Example 3 with StructureMap

use of org.hl7.fhir.r5.model.StructureMap in project org.hl7.fhir.core by hapifhir.

the class R2R3ConversionManager method convertToR3.

private void convertToR3(InputStream source, OutputStream dest, FhirFormat format) throws FHIRException, IOException {
    org.hl7.fhir.dstu3.elementmodel.Element r2 = new org.hl7.fhir.dstu3.elementmodel.XmlParser(contextR2).parse(source);
    StructureMap map = library.get("http://hl7.org/fhir/StructureMap/" + r2.fhirType() + "2to3");
    if (map == null)
        throw new FHIRException("No Map Found from R2 to R3 for " + r2.fhirType());
    String tn = smu3.getTargetType(map).getType();
    Resource r3 = ResourceFactory.createResource(tn);
    smu3.transform(new TransformContextR2R3(contextR3, r2.getChildValue("id")), r2, map, r3);
    FormatUtilities.makeParser(format).setOutputStyle(style).compose(dest, r3);
}
Also used : FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 4 with StructureMap

use of org.hl7.fhir.r5.model.StructureMap in project org.hl7.fhir.core by hapifhir.

the class R2R3ConversionManager method setMappingLibrary.

public void setMappingLibrary(InputStream stream) throws IOException, FHIRException {
    needPrepare = true;
    Map<String, InputStream> files = readInputStream(stream);
    for (InputStream s : files.values()) {
        StructureMap sm = new StructureMapUtilities(contextR3).parse(TextFile.streamToString(s));
        library.put(sm.getUrl(), sm);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) StructureMapUtilities(org.hl7.fhir.dstu3.utils.StructureMapUtilities)

Example 5 with StructureMap

use of org.hl7.fhir.r5.model.StructureMap in project org.hl7.fhir.core by hapifhir.

the class PackageMaintainer method check.

private void check(String ver) throws IOException {
    System.out.println("Check " + ver);
    List<String> allIds = listResources(Utilities.path(PATH, "hl7.fhir." + ver + ".examples", "package"));
    List<String> coreIds = listResources(Utilities.path(PATH, "hl7.fhir." + ver + ".core", "package"));
    for (String s : coreIds) {
        if (!allIds.contains(s)) {
            System.out.println("Core contains " + s + " but allIds doesn't");
        }
    }
    for (String s : allIds) {
        if (!coreIds.contains(s)) {
            String c = s.substring(0, s.indexOf("-"));
            if (Utilities.existsInList(c, "CodeSystem", "ValueSet", "ConceptMap", "StructureDefinition", "StructureMap", "NamingSystem", "SearchParameter", "OperationDefinition", "CapabilityStatement", "Conformance"))
                System.out.println("Examples contains " + s + " but core doesn't");
        }
    }
    strip(new File(Utilities.path(PATH, "hl7.fhir." + ver + ".core", "package")));
    strip(new File(Utilities.path(PATH, "hl7.fhir." + ver + ".expansions", "package")));
    if (!ver.equals("r2b"))
        strip(new File(Utilities.path(PATH, "hl7.fhir." + ver + ".elements", "package")));
}
Also used : TextFile(org.hl7.fhir.utilities.TextFile) File(java.io.File)

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)69 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)23 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)17 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)16 StructureMap (org.hl7.fhir.r4b.model.StructureMap)13 StructureMap (org.hl7.fhir.r5.model.StructureMap)13 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 StructureMap (org.hl7.fhir.r4.model.StructureMap)11 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)11 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)10 Complex (org.hl7.fhir.dstu3.utils.formats.Turtle.Complex)9 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)9 StructureMap (org.hl7.fhir.dstu3.model.StructureMap)8 File (java.io.File)7 UriType (org.hl7.fhir.r4.model.UriType)7 Test (org.junit.jupiter.api.Test)7 Base (org.hl7.fhir.dstu3.model.Base)6 TextFile (org.hl7.fhir.utilities.TextFile)6 FileOutputStream (java.io.FileOutputStream)5