Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public ProcessPluginImpl(ProcessPluginDefinition processPluginDefinition, int pr

this.processPluginDefinition = processPluginDefinition;

variablesFactory = delegateExecution -> new VariablesImpl(delegateExecution, getObjectMapper());
variablesFactory = delegateExecution -> new VariablesImpl(delegateExecution, getObjectMapper(),
getProcessPluginApi().getDsfClientProvider().getLocalDsfClient());
pluginMdc = new PluginMdcImpl(processPluginApiVersion, processPluginDefinition.getName(),
processPluginDefinition.getVersion(), jarFile.toString(), serverBaseUrl, variablesFactory);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package dev.dsf.bpe.v2.service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;

import org.hl7.fhir.r4.model.Coding;
import org.hl7.fhir.r4.model.Task;
import org.hl7.fhir.r4.model.Task.TaskOutputComponent;
import org.hl7.fhir.r4.model.Type;

import dev.dsf.bpe.v2.client.dsf.DsfClient;

public class StartTaskUpdaterImpl implements StartTaskUpdater
{
private final DsfClient client;

private final Supplier<Task> getStartTask;
private final Consumer<Task> updateTask;

public StartTaskUpdaterImpl(DsfClient client, Supplier<Task> getStartTask, Consumer<Task> updateTask)
{
this.client = Objects.requireNonNull(client, "client");

this.getStartTask = Objects.requireNonNull(getStartTask, "getStartTask");
this.updateTask = Objects.requireNonNull(updateTask, "updateTask");
}

@Override
public void addOutput(Coding outputType, Type outputValue)
{
Task task = getStartTask.get();
task.addOutput().setValue(outputValue).getType().addCoding(outputType);

Task updated = client.update(task);
updateTask.accept(updated);
}

@Override
public Optional<TaskOutputComponent> getOutput(Coding outputType)
{
checkOutputType(outputType);

Task task = getStartTask.get();
return doGetOutput(task, outputType);
}

private Optional<TaskOutputComponent> doGetOutput(Task task, Coding outputType)
{
return task.getOutput().stream().filter(matchesSystemAndCodeOptionallyVersion(outputType)).findFirst();
}

private Predicate<TaskOutputComponent> matchesSystemAndCodeOptionallyVersion(Coding outputType)
{
return o -> o.getType().getCoding().stream()
.anyMatch(c -> Objects.equals(c.getSystem(), outputType.getSystem())
&& Objects.equals(c.getCode(), outputType.getCode()) && outputType.hasVersion()
? Objects.equals(c.getVersion(), outputType.getVersion())
: true);
}

@Override
public void modifyOutput(Coding outputType, Type outputValue)
{
checkOutputType(outputType);

Task task = getStartTask.get();

doGetOutput(task, outputType)
.orElseThrow(() -> new IllegalArgumentException("Output for type " + outputType.getSystem() + "|"
+ outputType.getCode()
+ (outputType.hasVersion() ? " (version: " + outputType.getVersion() + ") not found" : "")))
.setValue(outputValue);

Task updated = client.update(task);
updateTask.accept(updated);
}

@Override
public void removeOutput(Coding outputType)
{
checkOutputType(outputType);

Task task = getStartTask.get();

List<TaskOutputComponent> filtered = task.getOutput().stream()
.filter(matchesSystemAndCodeOptionallyVersion(outputType).negate()).toList();

if (task.getOutput().size() == filtered.size())
throw new IllegalArgumentException("Output for type " + outputType.getSystem() + "|" + outputType.getCode()
+ (outputType.hasVersion() ? " (version: " + outputType.getVersion() + ") not found" : ""));

task.setOutput(filtered);

Task updated = client.update(task);
updateTask.accept(updated);
}

private void checkOutputType(Coding outputType)
{
Objects.requireNonNull(outputType, "outputType");

Objects.requireNonNull(outputType.getSystem(), "outputType.system");
Objects.requireNonNull(outputType.getCode(), "outputType.code");
Objects.requireNonNull(outputType.getVersion(), "outputType.version");

if (outputType.getSystem().isBlank())
throw new IllegalArgumentException("outputType.system is blank");
if (outputType.getCode().isBlank())
throw new IllegalArgumentException("outputType.code is blank");
if (outputType.getVersion().isBlank())
throw new IllegalArgumentException("outputType.version is blank");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,9 @@ public ParameterComponent createInput(Type value, Coding coding)
return new ParameterComponent(new CodeableConcept(coding), value);
}

@Override
public ParameterComponent createInput(Type value, String system, String code)
{
return createInput(value, new Coding(system, code, null));
}

@Override
public TaskOutputComponent createOutput(Type value, Coding coding)
{
return new TaskOutputComponent(new CodeableConcept(coding), value);
}

@Override
public TaskOutputComponent createOutput(Type value, String system, String code)
{
return createOutput(value, new Coding(system, code, null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public JsonHolderSerializer jsonVariableSerializer()
@Bean
public Function<DelegateExecution, ListenerVariables> listenerVariablesFactory()
{
return execution -> new VariablesImpl(execution, objectMapper());
return execution -> new VariablesImpl(execution, objectMapper(), dsfClientProvider().getLocalDsfClient());
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
import com.fasterxml.jackson.databind.ObjectMapper;

import dev.dsf.bpe.api.Constants;
import dev.dsf.bpe.v2.client.dsf.DsfClient;
import dev.dsf.bpe.v2.constants.BpmnExecutionVariables;
import dev.dsf.bpe.v2.listener.ListenerVariables;
import dev.dsf.bpe.v2.service.StartTaskUpdater;
import dev.dsf.bpe.v2.service.StartTaskUpdaterImpl;
import dev.dsf.bpe.v2.variables.FhirResourceValues.FhirResourceValue;
import dev.dsf.bpe.v2.variables.FhirResourcesListValues.FhirResourcesListValue;
import dev.dsf.bpe.v2.variables.TargetValues.TargetValue;
Expand Down Expand Up @@ -69,16 +72,22 @@ public int hashCode()
private final DelegateExecution execution;
private final ObjectMapper objectMapper;

private final StartTaskUpdater startTaskUpdater;

/**
* @param execution
* not <code>null</code>
* @param objectMapper
* not <code>null</code>
* @param client
* not <code>null</code>
*/
public VariablesImpl(DelegateExecution execution, ObjectMapper objectMapper)
public VariablesImpl(DelegateExecution execution, ObjectMapper objectMapper, DsfClient client)
{
this.execution = Objects.requireNonNull(execution, "execution");
this.objectMapper = Objects.requireNonNull(objectMapper, "objectMapper");

startTaskUpdater = new StartTaskUpdaterImpl(client, this::getStartTask, this::updateTask);
}

private JsonHolder toJsonHolder(Object json)
Expand Down Expand Up @@ -290,6 +299,12 @@ public Task getStartTask()
return getFhirResource(START_TASK);
}

@Override
public StartTaskUpdater getStartTaskUpdater()
{
return startTaskUpdater;
}

@Override
public Task getLatestTask()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.hl7.fhir.r4.model.Reference;
import org.hl7.fhir.r4.model.ResourceType;
import org.hl7.fhir.r4.model.StringType;
import org.hl7.fhir.r4.model.Task;
import org.hl7.fhir.r4.model.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -88,6 +89,10 @@ Coding toCoding()
private final Set<KeyAndValue> practitionerRoles = new HashSet<>();
private final Set<KeyAndValue> practitioners = new HashSet<>();

private String taskOutputSystem;
private String taskOutputCode;
private String taskOutputVersion;

/**
* @param practitionerRole
* does nothing if <code>null</code> or blank
Expand Down Expand Up @@ -144,6 +149,48 @@ public void setPractitioners(List<String> practitioners)
}
}

/**
* If {@link #taskOutputSystem}, {@link #taskOutputCode} and {@link #taskOutputVersion} are set with not blank
* values, an output parameter is added to the start Task with a reference to the created
* {@link QuestionnaireResponse} resource.
*
* @param taskOutputSystem
* @deprecated only for field injection
*/
@Deprecated
public void setTaskOutputSystem(String taskOutputSystem)
{
this.taskOutputSystem = taskOutputSystem;
}

/**
* If {@link #taskOutputSystem}, {@link #taskOutputCode} and {@link #taskOutputVersion} are set with not blank
* values, an output parameter is added to the start Task with a reference to the created
* {@link QuestionnaireResponse} resource.
*
* @param taskOutputCode
* @deprecated only for field injection
*/
@Deprecated
public void setTaskOutputCode(String taskOutputCode)
{
this.taskOutputCode = taskOutputCode;
}

/**
* If {@link #taskOutputSystem}, {@link #taskOutputCode} and {@link #taskOutputVersion} are set with not blank
* values, an output parameter is added to the start Task with a reference to the created
* {@link QuestionnaireResponse} resource.
*
* @param taskOutputVersion
* @deprecated only for field injection
*/
@Deprecated
public void setTaskOutputVersion(String taskOutputVersion)
{
this.taskOutputVersion = taskOutputVersion;
}

@Override
public void notify(ProcessPluginApi api, Variables variables,
CreateQuestionnaireResponseValues createQuestionnaireResponseValues) throws Exception
Expand Down Expand Up @@ -286,7 +333,13 @@ protected void beforeQuestionnaireResponseCreate(ProcessPluginApi api, Variables

/**
* <i>Override this method to execute code after the {@link QuestionnaireResponse} resource has been created on the
* DSF FHIR server</i>
* DSF FHIR server</i><br>
* <br>
* Default implementation will add an output parameter to the start {@link Task} with a reference to the created
* {@link QuestionnaireResponse} resource if {@link #taskOutputSystem}, {@link #taskOutputCode} and
* {@link #taskOutputVersion} are set with not blank values.<br>
* <br>
* Use field inject in BPMN to set value.
*
* @param api
* not <code>null</code>
Expand All @@ -301,7 +354,12 @@ protected void beforeQuestionnaireResponseCreate(ProcessPluginApi api, Variables
protected void afterQuestionnaireResponseCreate(ProcessPluginApi api, Variables variables,
CreateQuestionnaireResponseValues createQuestionnaireResponseValues, QuestionnaireResponse afterCreate)
{
// Nothing to do in default behavior
if (taskOutputSystem != null && !taskOutputSystem.isBlank() && taskOutputCode != null
&& !taskOutputCode.isBlank() && taskOutputVersion != null && !taskOutputVersion.isBlank())
{
variables.getStartTaskUpdater().addOutput(taskOutputSystem, taskOutputCode, taskOutputVersion,
new Reference(afterCreate.getIdElement().toUnqualifiedVersionless()));
}
}

/**
Expand Down
Loading
Loading