Skip to content

Commit

Permalink
fix requester not being copied over and referral in french broken
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebSLane committed Jul 30, 2021
1 parent ff79e74 commit 64496ab
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,13 @@ private void beginTaskImportResultsPath(String remoteStorePath) {
.forResource(Task.class)//
.returnBundle(Bundle.class)//
.include(Task.INCLUDE_BASED_ON) // serviceRequest
.include(ServiceRequest.INCLUDE_REQUESTER.asRecursive()) // serviceRequest
.where(Task.STATUS.exactly().code(TaskStatus.ACCEPTED.toCode()))//
.where(Task.RES_ID.exactly().identifier(referralTaskUuid.toString()));
Bundle originalTasksBundle = searchQuery.execute();
if (originalTasksBundle.hasEntry()) {
LogEvent.logDebug(this.getClass().getName(), "beginTaskImportResultsPath",
"received bundle with " + originalTasksBundle.getEntry().size() + " entries for Task ID "
+ referralTaskUuid);
LogEvent.logDebug(this.getClass().getName(), "beginTaskImportResultsPath", "received bundle with "
+ originalTasksBundle.getEntry().size() + " entries for Task ID " + referralTaskUuid);
} else {
LogEvent.logDebug(this.getClass().getName(), "beginTaskImportResultsPath",
"received bundle with 0 entries for Task ID " + referralTaskUuid);
Expand Down Expand Up @@ -279,8 +279,6 @@ private void beginTaskImportResultsPath(String remoteStorePath) {
}
}



} catch (RuntimeException e) {
LogEvent.logError(e);
LogEvent.logError("could not import result for referral with UUID: " + referralTaskUuid, e);
Expand All @@ -298,6 +296,14 @@ private void addOriginalReferralObject(BundleEntryComponent bundleEntry,
referralObjects.serviceRequests = Arrays.asList(originalServiceRequest);
originalReferralObjectsByServiceRequest.put(serviceRequestId, referralObjects);
}
if (bundleEntry.getResource().getResourceType().equals(ResourceType.Practitioner)) {
Practitioner originalPractitioner = (Practitioner) bundleEntry.getResource();
String practitionerId = originalPractitioner.getIdElement().getIdPart();
OriginalReferralObjects referralObjects = originalReferralObjectsByServiceRequest
.getOrDefault(practitionerId, new OriginalReferralObjects());
referralObjects.requestors = Arrays.asList(originalPractitioner);
originalReferralObjectsByServiceRequest.put(practitionerId, referralObjects);
}
if (bundleEntry.getResource().getResourceType().equals(ResourceType.Task)) {
Task referralTask = (Task) bundleEntry.getResource();
String serviceRequestId = referralTask.getBasedOnFirstRep().getReferenceElement().getIdPart();
Expand Down Expand Up @@ -476,6 +482,8 @@ private OriginalReferralObjects saveRemoteTaskAsLocalTask(IGenericClient sourceF
List<ServiceRequest> remoteServiceRequests = getBasedOnServiceRequestsFromServer(sourceFhirClient, remoteTask);
List<Specimen> remoteSpecimens = getSpecimenForServiceRequestsFromServer(sourceFhirClient,
remoteServiceRequests);
List<Practitioner> remoteRequesters = getRequestorsForServiceRequestsFromServer(sourceFhirClient,
remoteServiceRequests);
Patient remotePatientForTask = getForPatientFromServer(sourceFhirClient, remoteTask);
if (remotePatientForTask == null) {
remotePatientForTask = getForPatientFromServer(sourceFhirClient, remoteServiceRequests);
Expand Down Expand Up @@ -553,7 +561,7 @@ private OriginalReferralObjects saveRemoteTaskAsLocalTask(IGenericClient sourceF
serviceRequest.setSubject(fhirTransformService.createReferenceFor(objects.patient));
}

// Practitioner
// Task Practitioner
// Patient forPatient = getForPatientFromBundle(bundle, remoteTask);
if (remotePractitionerForTask != null) {
Optional<Practitioner> existingLocalPractitioner = getPractitionerWithSameServiceIdentifier(
Expand All @@ -571,6 +579,23 @@ private OriginalReferralObjects saveRemoteTaskAsLocalTask(IGenericClient sourceF
objects.task.setRequester(fhirTransformService.createReferenceFor(objects.practitioner));
}

// ServiceRequest Requester
objects.requestors = new ArrayList<>();
for (Practitioner remotePractitioner : remoteRequesters) {
Practitioner localPractitioner;
Optional<Practitioner> existingLocalRequester = getProviderWithSameIdentifier(remotePractitioner,
remoteStorePath);
if (existingLocalRequester.isEmpty()) {
localPractitioner = remotePractitioner
.addIdentifier(createIdentifierToRemoteResource(remotePractitioner, remoteStorePath));
// fhirOperations.createResources.add(localSpecimen);
fhirOperations.updateResources.put(localPractitioner.getIdElement().getIdPart(), localPractitioner);
} else {
localPractitioner = existingLocalRequester.get();
}
objects.requestors.add(localPractitioner);
}

LogEvent.logDebug(this.getClass().getName(), "",
"creating local copies of the remote fhir objects relating to Task: " + originalRemoteTaskId);
// Run the transaction
Expand All @@ -597,6 +622,25 @@ private Optional<Specimen> getSpecimenWithSameIdentifier(Specimen specimen, Stri
return Optional.empty();
}

private Optional<Practitioner> getProviderWithSameIdentifier(Practitioner provider, String remoteStorePath) {
IGenericClient localFhirClient = fhirUtil.getFhirClient(localFhirStorePath);
Bundle localBundle = localFhirClient.search()//
.forResource(Specimen.class)//
.where(Specimen.IDENTIFIER.exactly().systemAndIdentifier(remoteStorePath,
provider.getIdElement().getIdPart()))//
.returnBundle(Bundle.class).execute();
for (BundleEntryComponent entry : localBundle.getEntry()) {
if (entry.hasResource() && ResourceType.Practitioner.equals(entry.getResource().getResourceType())) {
LogEvent.logDebug(this.getClass().getName(), "",
"found provider with same identifier as " + provider.getIdElement().getIdPart());
return Optional.of((Practitioner) localBundle.getEntryFirstRep().getResource());
}
}
LogEvent.logDebug(this.getClass().getName(), "",
"no provider with same identifier " + provider.getIdElement().getIdPart());
return Optional.empty();
}

private List<Specimen> getSpecimenForServiceRequestsFromServer(IGenericClient fhirClient,
List<ServiceRequest> remoteServiceRequests) {
List<Specimen> specimens = new ArrayList<>();
Expand All @@ -609,6 +653,17 @@ private List<Specimen> getSpecimenForServiceRequestsFromServer(IGenericClient fh
return specimens;
}

private List<Practitioner> getRequestorsForServiceRequestsFromServer(IGenericClient fhirClient,
List<ServiceRequest> remoteServiceRequests) {
List<Practitioner> specimens = new ArrayList<>();
for (ServiceRequest serviceRequest : remoteServiceRequests) {
Reference requesterReference = serviceRequest.getRequester();
specimens.add(fhirClient.read().resource(Practitioner.class)
.withId(requesterReference.getReferenceElement().getIdPart()).execute());
}
return specimens;
}

private Patient getForPatientFromServer(IGenericClient fhirClient, List<ServiceRequest> serviceRequests) {
for (ServiceRequest serviceRequest : serviceRequests) {
if (serviceRequest.getSubject() != null && serviceRequest.getSubject().getReference() != null) {
Expand Down Expand Up @@ -751,6 +806,7 @@ public class OriginalReferralObjects {
public Practitioner practitioner;
public Task task;
public List<ServiceRequest> serviceRequests;
public List<Practitioner> requestors;
public List<Specimen> specimens;
public Patient patient;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/pages/result/resultListView.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ function createReferralOption(sampleNum, testNum, testId, testName, index) {
<c:forEach items="${form.referralReasons}" var="referralReason" varStatus="iter">
option = document.createElement('option');
option.value = '${referralReason.id}';
option.innerHTML = '${referralReason.value}';
option.innerHTML = '<c:out value="${referralReason.value}"/>';
referralReasonSelect.appendChild(option);
</c:forEach>
cell1.innerHTML = "Referral Reason ";
Expand Down

0 comments on commit 64496ab

Please sign in to comment.