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"));
}
}
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());
}
}
}
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());
}
}
}
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());
}
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);
}
Aggregations