Skip to content

Commit

Permalink
KOP-682: Changed the way how Tasks refer to ActivityDefinitions as de…
Browse files Browse the repository at this point in the history
…cided and used in kt2 simplifier v0.11.0
  • Loading branch information
JorisHeadease committed Nov 3, 2023
1 parent 087c596 commit 5919154
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>
<groupId>nl.koppeltaal</groupId>
<artifactId>spring-boot-starter-smart-service</artifactId>
<version>1.1.57-SNAPSHOT</version>
<version>1.1.58-SNAPSHOT</version>
<name>Koppeltaal-2.0-Spring-SMART-Service</name>
<description>spring-boot-starter-smart-service project to connect to a FHIR Store that works according to
the SMART Backend Service flow (https://hl7.org/fhir/uv/bulkdata/authorization/index.html)</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class FhirConstant {
}};

public static final String KT2_EXTENSION__CARE_TEAM__OBSERVER = "http://koppeltaal.nl/fhir/StructureDefinition/KT2ObservationTeam";
public static final String KT2_EXTENSION__TASK__INSTANTIATES = "http://vzvz.nl/fhir/StructureDefinition/instantiates";
public final static String KT2_EXTENSION__ENDPOINT = "http://koppeltaal.nl/fhir/StructureDefinition/KT2EndpointExtension";
public final static String KT2_EXTENSION__PUBLISHER_IDENTIFIER = "http://koppeltaal.nl/fhir/StructureDefinition/KT2PublisherId";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@

package nl.koppeltaal.spring.boot.starter.smartservice.dto;

import static nl.koppeltaal.spring.boot.starter.smartservice.constants.FhirConstant.KT2_EXTENSION__CARE_TEAM__OBSERVER;
import nl.koppeltaal.spring.boot.starter.smartservice.utils.ExtensionUtils;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.r4.model.*;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.r4.model.Extension;
import org.hl7.fhir.r4.model.Identifier;
import org.hl7.fhir.r4.model.Reference;
import org.hl7.fhir.r4.model.ResourceType;
import org.hl7.fhir.r4.model.Task;
import org.springframework.stereotype.Component;

import static nl.koppeltaal.spring.boot.starter.smartservice.constants.FhirConstant.KT2_EXTENSION__CARE_TEAM__OBSERVER;
import static nl.koppeltaal.spring.boot.starter.smartservice.constants.FhirConstant.KT2_EXTENSION__TASK__INSTANTIATES;

/**
*
Expand All @@ -33,7 +32,7 @@ public void applyDto(Task task, TaskDto taskDto) {
Collections.singletonList(createIdentifier(taskDto.getIdentifierSystem(), taskDto.getIdentifierValue())));
task.setRequester(new Reference(taskDto.getPractitioner()));
task.setOwner(new Reference(taskDto.getPatient()));
task.setInstantiatesCanonical(taskDto.getActivityDefinition());
addInstantiatesExtension(task, taskDto.getActivityDefinition());
task.setStatus(Task.TaskStatus.fromCode(taskDto.getStatus()));

// remove all "old" observer values
Expand All @@ -60,6 +59,18 @@ public static void addObserverExtension(Task task, String observerReference) {
task.addExtension(observerExtension);
}

public static void addInstantiatesExtension(Task task, String activityDefinitionReference) {
final Reference instantiatesReference = new Reference();
instantiatesReference.setReference(activityDefinitionReference);
instantiatesReference.setType(ResourceType.ActivityDefinition.name());

final Extension instantiatesExtension = new Extension();
instantiatesExtension.setValue(instantiatesReference);
instantiatesExtension.setUrl(KT2_EXTENSION__TASK__INSTANTIATES);

task.addExtension(instantiatesExtension);
}

public void applyResource(TaskDto taskDto, Task task) {
taskDto.setReference(getRelativeReference(task.getIdElement()));

Expand All @@ -71,13 +82,15 @@ public void applyResource(TaskDto taskDto, Task task) {

taskDto.setPatient(task.getOwner().getReference());
taskDto.setPractitioner(task.getRequester().getReference());
taskDto.setActivityDefinition(task.getInstantiatesCanonical());
if (task.getStatus() != null)
taskDto.setStatus(task.getStatus().toCode());

task.getExtensionsByUrl(KT2_EXTENSION__CARE_TEAM__OBSERVER).forEach(extension ->
taskDto.getObserverReferences().add(((Reference) extension.getValue()).getReference())
);

ExtensionUtils.getReferenceValue(task, KT2_EXTENSION__TASK__INSTANTIATES)
.ifPresent(taskDto::setActivityDefinition);
}

public TaskDto convert(Task task) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import nl.koppeltaal.spring.boot.starter.smartservice.constants.FhirConstant;
import nl.koppeltaal.spring.boot.starter.smartservice.dto.TaskDto;
import nl.koppeltaal.spring.boot.starter.smartservice.dto.TaskDtoConverter;
import nl.koppeltaal.spring.boot.starter.smartservice.utils.ExtensionUtils;
import nl.koppeltaal.spring.boot.starter.smartservice.utils.ResourceUtils;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IIdType;
Expand Down Expand Up @@ -134,9 +135,13 @@ private List<Task> getTasksForOwnerAndDefinition(Patient fhirPatient, ActivityDe
List<Task> rv = new ArrayList<>();
List<Task> resourcesByOwner = getResourcesByOwner(ResourceUtils.getReference(fhirPatient));
for (Task task : resourcesByOwner) {
if (StringUtils.equals(task.getInstantiatesCanonical(), ResourceUtils.getCanonicalReference(fhirDefinition))) {
rv.add(task);
}

ExtensionUtils.getReferenceValue(task, FhirConstant.KT2_EXTENSION__TASK__INSTANTIATES)
.ifPresent(instantiates -> {
if (StringUtils.equals(instantiates, ResourceUtils.getReference(fhirDefinition))) {
rv.add(task);
}
});
}
return rv;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package nl.koppeltaal.spring.boot.starter.smartservice.utils;

import org.hl7.fhir.r4.model.DomainResource;
import org.hl7.fhir.r4.model.Extension;
import org.hl7.fhir.r4.model.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;

public class ExtensionUtils {
private static final Logger LOG = LoggerFactory.getLogger(ExtensionUtils.class);

public static Optional<String> getReferenceValue(DomainResource resource, String extensionSystem) {
Optional<Extension> instantiatesExtension = resource.getExtensionsByUrl(extensionSystem).stream().findFirst();

if(instantiatesExtension.isPresent()) {
return Optional.of(((Reference) instantiatesExtension.get().getValue()).getReference());
}

LOG.warn("No extension with system [{}] found on [{}]", extensionSystem, ResourceUtils.getReference(resource));
return Optional.empty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,4 @@ public static String getReference(IAnyResource resource) {
}
return null;
}

public static String getCanonicalReference(IAnyResource resource) {
IIdType idElement = resource.getIdElement();
return (idElement != null) ? idElement.getValue() : null;
}
}

0 comments on commit 5919154

Please sign in to comment.