Skip to content

Commit

Permalink
search for exisiting DocumentReferences and update if exists
Browse files Browse the repository at this point in the history
  • Loading branch information
wetret committed Oct 18, 2024
1 parent e6db29d commit df91700
Showing 1 changed file with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import org.camunda.bpm.engine.delegate.BpmnError;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.DocumentReference;
import org.hl7.fhir.r4.model.IdType;
import org.hl7.fhir.r4.model.Organization;
import org.hl7.fhir.r4.model.Reference;
import org.hl7.fhir.r4.model.ResourceType;
import org.hl7.fhir.r4.model.Task;
Expand All @@ -23,6 +25,7 @@
import de.medizininformatik_initiative.processes.common.util.DataSetStatusGenerator;
import dev.dsf.bpe.v1.ProcessPluginApi;
import dev.dsf.bpe.v1.activity.AbstractServiceDelegate;
import dev.dsf.bpe.v1.constants.NamingSystems;
import dev.dsf.bpe.v1.variables.Variables;

public class InsertData extends AbstractServiceDelegate implements InitializingBean
Expand Down Expand Up @@ -66,7 +69,8 @@ protected void doExecute(DelegateExecution execution, Variables variables)

try
{
List<IdType> createdIds = storeData(variables, fhirClient, bundle, sendingOrganization, projectIdentifier);
List<IdType> createdIds = storeData(variables, fhirClient, bundle, sendingOrganization, projectIdentifier,
task);

task.addOutput(
statusGenerator.createDataSetStatusOutput(ConstantsBase.CODESYSTEM_DATA_SET_STATUS_VALUE_RECEIVE_OK,
Expand Down Expand Up @@ -100,9 +104,11 @@ protected void doExecute(DelegateExecution execution, Variables variables)
}

private List<IdType> storeData(Variables variables, FhirClient fhirClient, Bundle bundle,
String sendingOrganization, String projectIdentifier)
String sendingOrganization, String projectIdentifier, Task task)
{
Bundle stored = fhirClient.executeTransaction(bundle);
Bundle transactionBundle = checkAndAdaptBundleForExistingData(fhirClient, bundle, sendingOrganization,
projectIdentifier, task);
Bundle stored = fhirClient.executeTransaction(transactionBundle);

List<IdType> idsOfCreatedResources = stored.getEntry().stream().filter(Bundle.BundleEntryComponent::hasResponse)
.map(Bundle.BundleEntryComponent::getResponse).map(Bundle.BundleEntryResponseComponent::getLocation)
Expand All @@ -116,6 +122,48 @@ private List<IdType> storeData(Variables variables, FhirClient fhirClient, Bundl
return idsOfCreatedResources;
}

private Bundle checkAndAdaptBundleForExistingData(FhirClient fhirClient, Bundle bundle, String sendingOrganization,
String projectIdentifier, Task task)
{
Bundle searchResult = fhirClient.getGenericFhirClient().search().forResource(DocumentReference.class)
.where(DocumentReference.IDENTIFIER.exactly()
.systemAndCode(ConstantsBase.NAMINGSYSTEM_MII_PROJECT_IDENTIFIER, projectIdentifier))
.and(DocumentReference.AUTHOR.hasChainedProperty(Organization.IDENTIFIER.exactly()
.systemAndCode(NamingSystems.OrganizationIdentifier.SID, sendingOrganization)))
.returnBundle(Bundle.class).execute();

List<DocumentReference> existingDocumentReferences = searchResult.getEntry().stream()
.filter(Bundle.BundleEntryComponent::hasResource).map(Bundle.BundleEntryComponent::getResource)
.filter(r -> r instanceof DocumentReference).map(r -> (DocumentReference) r).toList();

if (existingDocumentReferences.size() < 1)
return bundle;

if (existingDocumentReferences.size() > 1)
logger.warn(
"Found more than one DocumentReference for project-identifier '{}' authored by '{}', using the first",
projectIdentifier, sendingOrganization);

DocumentReference existingDocumentReference = existingDocumentReferences.get(0);
String existingDocumentReferenceId = existingDocumentReference.getIdElement().getIdPart();

logger.info(
"DocumentReference for project-identifier '{}' authored by '{}' already exists, updating data-set on FHIR server with baseUrl '{}' in Task with id '{}'",
projectIdentifier, sendingOrganization, fhirClient.getFhirBaseUrl(), task.getId());

bundle.getEntry().stream().filter(Bundle.BundleEntryComponent::hasResource)
.filter(e -> e.getResource() instanceof DocumentReference)
.filter(Bundle.BundleEntryComponent::hasRequest).filter(Bundle.BundleEntryComponent::hasResource)
.forEach(e ->
{
e.getRequest().setMethod(Bundle.HTTPVerb.PUT)
.setUrl(ResourceType.DocumentReference.name() + "/" + existingDocumentReferenceId);
e.getResource().setId(existingDocumentReferenceId);
});

return bundle;
}

private void sendMail(Task task, List<IdType> createdIds, String sendingOrganization, String projectIdentifier)
{
String subject = "New data-set received in process '" + ConstantsDataTransfer.PROCESS_NAME_FULL_DATA_RECEIVE
Expand Down

0 comments on commit df91700

Please sign in to comment.