Search in sources :

Example 61 with DomainResource

use of org.hl7.fhir.r4b.model.DomainResource in project redmatch by aehrc.

the class RedmatchApi method exportAll.

/**
 * Runs an operation on all the Redmatch rule documents found in the base folder.
 *
 * @param baseFolder The folder that contains the Redmatch rule documents , one or more schemas referenced by the
 *                   rules and a redmatch-config.yaml file with source server details.
 * @param progressReporter An object used to report progress. Can be null.
 * @param cancelToken Used to check if the user has cancelled the operation.
 * @return Map of diagnostic messages. Key is file where error happened.
 */
public List<Diagnostic> exportAll(@NotNull File baseFolder, ProgressReporter progressReporter, CancelChecker cancelToken) {
    if (!baseFolder.canRead() || !baseFolder.canWrite()) {
        return List.of(new Diagnostic(zeroZero, "Unable to read or write on the base folder.", DiagnosticSeverity.Error, "API"));
    }
    // Get .rdm files
    List<File> rdmFiles;
    try (Stream<Path> walk = Files.walk(baseFolder.toPath())) {
        rdmFiles = walk.filter(p -> !Files.isDirectory(p)).map(Path::toFile).filter(f -> f.getName().endsWith(".rdm")).collect(Collectors.toList());
    } catch (IOException e) {
        return List.of(new Diagnostic(zeroZero, "Unexpected I/O error: " + e.getLocalizedMessage(), DiagnosticSeverity.Error, "API"));
    }
    try {
        List<Diagnostic> diagnostics = new ArrayList<>();
        Map<String, DomainResource> resourcesMap = new HashMap<>();
        for (File rdmFile : rdmFiles) {
            Pair<Map<String, DomainResource>, List<Diagnostic>> data = transform(rdmFile, progressReporter, cancelToken);
            resourcesMap.putAll(data.getValue0());
            diagnostics.addAll(data.getValue1());
        }
        // Group resources by type
        final Map<String, List<DomainResource>> grouped = new HashMap<>();
        for (String key : resourcesMap.keySet()) {
            DomainResource dr = resourcesMap.get(key);
            String resourceType = dr.getResourceType().toString();
            List<DomainResource> list = grouped.computeIfAbsent(resourceType, k -> new ArrayList<>());
            list.add(dr);
        }
        Path outputFolder = createOutputFolder(baseFolder).toPath();
        save(grouped, outputFolder, progressReporter, cancelToken);
        return diagnostics;
    } catch (Exception e) {
        log.error(e);
        return List.of(new Diagnostic(zeroZero, "Could not complete transformation:" + e.getLocalizedMessage(), DiagnosticSeverity.Error, "API"));
    }
}
Also used : Path(java.nio.file.Path) DomainResource(org.hl7.fhir.r4.model.DomainResource) Diagnostic(org.eclipse.lsp4j.Diagnostic)

Example 62 with DomainResource

use of org.hl7.fhir.r4b.model.DomainResource in project redmatch by aehrc.

the class RedmatchApi method generateGraph.

/**
 * Exports a graph representation of the generated resources.
 *
 * @param resources A collection of FHIR resources.
 * @param progressReporter An object used to report progress.
 * @param cancelToken A token to check if the operation has been cancelled.
 * @return A graph representation of the FHIR resources.
 */
public D3Graph generateGraph(Collection<DomainResource> resources, ProgressReporter progressReporter, CancelChecker cancelToken) {
    try {
        if (progressReporter != null) {
            progressReporter.reportProgress(Progress.reportStart("Generating graph"));
        }
        log.info("Creating graph representation for visualisation.");
        D3Graph d3Graph = new D3Graph();
        if (cancelToken != null && cancelToken.isCanceled()) {
            return d3Graph;
        }
        // Add vertices
        for (Resource res : resources) {
            d3Graph.addNode(new D3Node(generateId(res)));
            if (cancelToken != null && cancelToken.isCanceled()) {
                return d3Graph;
            }
        }
        // Add edges
        for (Resource src : resources) {
            for (FhirUtils.Target tgt : FhirUtils.getReferencedResources(src)) {
                d3Graph.addLink(new D3Link(generateId(src), tgt.getResourceId(), tgt.getAttributeName()));
                if (cancelToken != null && cancelToken.isCanceled()) {
                    return d3Graph;
                }
            }
        }
        return d3Graph;
    } finally {
        if (progressReporter != null) {
            progressReporter.reportProgress(Progress.reportEnd());
        }
    }
}
Also used : Resource(org.hl7.fhir.r4.model.Resource) DomainResource(org.hl7.fhir.r4.model.DomainResource)

Example 63 with DomainResource

use of org.hl7.fhir.r4b.model.DomainResource in project redmatch by aehrc.

the class RedmatchApi method save.

private void save(Map<String, List<DomainResource>> grouped, Path tgtDir, ProgressReporter progressReporter, CancelChecker cancelToken) throws IOException {
    try {
        log.info("Saving to output folder " + tgtDir);
        if (progressReporter != null) {
            progressReporter.reportProgress(Progress.reportStart("Saving files"));
        }
        IParser jsonParser = ctx.newJsonParser();
        double div = grouped.size() / 100.0;
        int i = 0;
        for (String key : grouped.keySet()) {
            File f = new File(tgtDir.toFile(), key + ".ndjson");
            try (BufferedWriter bw = new BufferedWriter(new FileWriter(f))) {
                for (DomainResource dr : grouped.get(key)) {
                    jsonParser.encodeResourceToWriter(dr, bw);
                    bw.newLine();
                }
            }
            i++;
            if (progressReporter != null) {
                progressReporter.reportProgress(Progress.reportProgress((int) Math.floor(i / div)));
            }
            if (cancelToken != null && cancelToken.isCanceled()) {
                return;
            }
        }
    } finally {
        if (progressReporter != null) {
            progressReporter.reportProgress(Progress.reportEnd());
        }
    }
}
Also used : DomainResource(org.hl7.fhir.r4.model.DomainResource) IParser(ca.uhn.fhir.parser.IParser)

Example 64 with DomainResource

use of org.hl7.fhir.r4b.model.DomainResource in project nia-patient-switching-standard-adaptor by NHSDigital.

the class BundleMapperServiceTest method setup.

@BeforeEach
public void setup() {
    when(bundleGenerator.generateBundle()).thenReturn(new Bundle());
    var agentResourceList = new ArrayList<DomainResource>();
    agentResourceList.add(new Organization());
    List mockedList = mock(List.class);
    Map<String, List<? extends DomainResource>> encounterResources = new HashMap<>();
    encounterResources.put(ENCOUNTER_KEY, new ArrayList<>());
    encounterResources.put(CONSULTATION_KEY, new ArrayList<>());
    encounterResources.put(TOPIC_KEY, new ArrayList<>());
    encounterResources.put(CATEGORY_KEY, new ArrayList<>());
    when(agentDirectoryMapper.mapAgentDirectory(any())).thenReturn(mockedList);
    when(mockedList.stream()).thenReturn(agentResourceList.stream());
    when(patientMapper.mapToPatient(any(RCMRMT030101UK04Patient.class), any(Organization.class))).thenReturn(new Patient());
    when(encounterMapper.mapEncounters(any(RCMRMT030101UK04EhrExtract.class), any(Patient.class), any(String.class))).thenReturn(encounterResources);
    when(organizationMapper.mapAuthorOrganization(anyString())).thenReturn(new Organization());
}
Also used : Organization(org.hl7.fhir.dstu3.model.Organization) DomainResource(org.hl7.fhir.dstu3.model.DomainResource) HashMap(java.util.HashMap) Bundle(org.hl7.fhir.dstu3.model.Bundle) ArrayList(java.util.ArrayList) RCMRMT030101UK04Patient(org.hl7.v3.RCMRMT030101UK04Patient) Patient(org.hl7.fhir.dstu3.model.Patient) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RCMRMT030101UK04Patient(org.hl7.v3.RCMRMT030101UK04Patient) RCMRMT030101UK04EhrExtract(org.hl7.v3.RCMRMT030101UK04EhrExtract) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) List(java.util.List) ArrayList(java.util.ArrayList) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 65 with DomainResource

use of org.hl7.fhir.r4b.model.DomainResource in project bunsen by cerner.

the class HapiContainedConverter method fromHapi.

@Override
public Object fromHapi(Object input) {
    List containedList = (List) input;
    Object[] values = new Object[containedList.size()];
    for (int valueIndex = 0; valueIndex < containedList.size(); ++valueIndex) {
        DomainResource composite = (DomainResource) containedList.get(valueIndex);
        StructureField<HapiConverter<T>> schemaEntry = contained.get(composite.fhirType());
        values[valueIndex] = schemaEntry.result().fromHapi(composite);
    }
    return createContained(values);
}
Also used : DomainResource(org.hl7.fhir.dstu3.model.DomainResource) List(java.util.List)

Aggregations

XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)24 FHIRException (org.hl7.fhir.exceptions.FHIRException)22 DomainResource (org.hl7.fhir.r4.model.DomainResource)21 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)16 IOException (java.io.IOException)15 DomainResource (org.hl7.fhir.dstu3.model.DomainResource)15 FileOutputStream (java.io.FileOutputStream)12 ArrayList (java.util.ArrayList)11 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)11 Test (org.junit.jupiter.api.Test)11 Resource (org.hl7.fhir.r4.model.Resource)10 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)9 FileNotFoundException (java.io.FileNotFoundException)8 List (java.util.List)8 NotImplementedException (org.apache.commons.lang3.NotImplementedException)8 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)8 SystemRequestDetails (ca.uhn.fhir.jpa.partition.SystemRequestDetails)7 File (java.io.File)7 ElementDefn (org.hl7.fhir.definitions.model.ElementDefn)7 RestIntegrationTest (org.opencds.cqf.ruler.test.RestIntegrationTest)7