Skip to content

Commit

Permalink
Adds support for tracked entity resource type in Plan Definitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
volsch committed Aug 4, 2019
1 parent bf65003 commit 46299ce
Show file tree
Hide file tree
Showing 10 changed files with 372 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import org.apache.commons.io.IOUtils;
import org.dhis2.fhir.adapter.AbstractAppTest;
import org.dhis2.fhir.adapter.fhir.extension.ResourceTypeExtensionUtils;
import org.dhis2.fhir.adapter.fhir.model.FhirVersion;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.PlanDefinition;
Expand Down Expand Up @@ -124,6 +125,8 @@ private void getPlanDefinition() throws Exception
client.registerInterceptor( new BasicAuthInterceptor( "fhir_client", "fhir_client_1" ) );
PlanDefinition planDefinition = client.read().resource( PlanDefinition.class ).withId( "EPDyQuoRnXk" ).execute();
Assert.assertEquals( "Child Programme", planDefinition.getTitle() );
Assert.assertNotNull( planDefinition.getExtensionByUrl( ResourceTypeExtensionUtils.URL ) );
Assert.assertEquals( "Patient", planDefinition.getExtensionByUrl( ResourceTypeExtensionUtils.URL ).getValue().primitiveValue() );

systemDhis2Server.verify();
userDhis2Server.verify();
Expand Down
17 changes: 1 addition & 16 deletions docs/fhir/profiles/LocationExtension.StructureDefinition.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
-->

<StructureDefinition xmlns="http://hl7.org/fhir">
<url value="http://www.dhis2.org/dhis2-fhir-adapter/extensions/location" />
<url value="http://www.dhis2.org/dhis2-fhir-adapter/fhir/extensions/location" />
<name value="LocationExtension" />
<status value="active" />
<fhirVersion value="4.0.0" />
Expand All @@ -47,21 +47,6 @@
<short value="Location reference" />
<definition value="Location reference." />
<max value="1" />
<base>
<path value="Extension" />
<max value="1" />
</base>
<constraint>
<key value="ext-1" />
<severity value="error" />
<human value="Must have either extensions or value[x], not both" />
<expression value="extension.exists() != value.exists()" />
<xpath value="exists(f:extension)!=exists(f:*[starts-with(local-name(.), 'value')])" />
</constraint>
<mapping>
<identity value="rim" />
<map value="N/A" />
</mapping>
</element>
<element id="Extension.value[x]">
<path value="Extension.value[x]" />
Expand Down
57 changes: 57 additions & 0 deletions docs/fhir/profiles/ResourceTypeExtension.StructureDefinition.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2004-2019, University of Oslo
~ All rights reserved.
~
~ Redistribution and use in source and binary forms, with or without
~ modification, are permitted provided that the following conditions are met:
~ Redistributions of source code must retain the above copyright notice, this
~ list of conditions and the following disclaimer.
~
~ Redistributions in binary form must reproduce the above copyright notice,
~ this list of conditions and the following disclaimer in the documentation
~ and/or other materials provided with the distribution.
~ Neither the name of the HISP project nor the names of its contributors may
~ be used to endorse or promote products derived from this software without
~ specific prior written permission.
~
~ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
~ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
~ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
~ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
~ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
~ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
~ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
~ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
~ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
~ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->

<StructureDefinition xmlns="http://hl7.org/fhir">
<url value="http://www.dhis2.org/dhis2-fhir-adapter/fhir/extensions/resource-type" />
<name value="ResourceTypeExtension" />
<status value="active" />
<experimental value="false" />
<fhirVersion value="4.0.0" />
<kind value="complex-type" />
<abstract value="false" />
<context>
<type value="element" />
<expression value="DomainResource" />
</context>
<type value="Extension" />
<baseDefinition value="http://hl7.org/fhir/StructureDefinition/Extension" />
<derivation value="constraint" />
<differential>
<element id="Extension.value[x]">
<path value="Extension.value[x]" />
<label value="FHIR Resource Type" />
<short value="FHIR Resource Type" />
<definition value="Must be a valid FHIR Resource Type like Patient." />
<min value="1" />
<type>
<code value="string" />
</type>
</element>
</differential>
</StructureDefinition>
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,27 @@
<path value="PlanDefinition.contained" />
<max value="0" />
</element>
<element id="PlanDefinition.extension">
<path value="PlanDefinition.extension" />
<slicing>
<discriminator>
<type value="value" />
<path value="url" />
</discriminator>
<rules value="open" />
</slicing>
</element>
<element id="PlanDefinition.extension:subjectResourceType">
<path value="PlanDefinition.extension" />
<sliceName value="subjectResourceType" />
<definition value="The FHIR resource type that is used by the tracked entity. If this is not specified, the associated program is an event program to which no tracked entity is associated." />
<min value="0" />
<max value="1" />
<type>
<code value="Extension" />
<profile value="http://www.dhis2.org/dhis2-fhir-adapter/fhir/extensions/resource-type" />
</type>
</element>
<element id="PlanDefinition.url">
<path value="PlanDefinition.url" />
<min value="1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import ca.uhn.fhir.model.api.IElement;
import org.dhis2.fhir.adapter.dhis.orgunit.OrganizationUnitService;
import org.dhis2.fhir.adapter.dhis.tracker.program.Program;
import org.dhis2.fhir.adapter.dhis.tracker.trackedentity.TrackedEntityMetadataService;
Expand All @@ -50,11 +51,13 @@
import org.hl7.fhir.r4.model.Enumerations;
import org.hl7.fhir.r4.model.PlanDefinition;
import org.hl7.fhir.r4.model.PlanDefinition.PlanDefinitionActionComponent;
import org.hl7.fhir.r4.model.ResourceFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

/**
* R4 specific version of DHIS2 Program Metadata to FHIR Plan Definition transformer.
Expand Down Expand Up @@ -86,7 +89,7 @@ protected boolean transformInternal( @Nonnull FhirClient fhirClient, @Nonnull Dh
final Program dhisProgram = (Program) input.getDhisResource();
final PlanDefinition fhirPlanDefinition = (PlanDefinition) output;

if ( !isApplicableProgram( dhisProgram ) )
if ( !addSubjectResourceType( dhisProgram, fhirPlanDefinition ) )
{
return false;
}
Expand All @@ -111,4 +114,11 @@ protected boolean transformInternal( @Nonnull FhirClient fhirClient, @Nonnull Dh

return true;
}

@Nonnull
@Override
protected Function<String, IElement> getTypeFactory()
{
return ResourceFactory::createType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@
import org.dhis2.fhir.adapter.dhis.tracker.trackedentity.TrackedEntityMetadataService;
import org.dhis2.fhir.adapter.dhis.tracker.trackedentity.WritableTrackedEntityType;
import org.dhis2.fhir.adapter.fhir.data.repository.FhirDhisAssignmentRepository;
import org.dhis2.fhir.adapter.fhir.extension.ResourceTypeExtensionUtils;
import org.dhis2.fhir.adapter.fhir.metadata.model.FhirClient;
import org.dhis2.fhir.adapter.fhir.metadata.model.FhirResourceType;
import org.dhis2.fhir.adapter.fhir.metadata.model.ProgramMetadataRule;
import org.dhis2.fhir.adapter.fhir.metadata.model.RuleInfo;
import org.dhis2.fhir.adapter.fhir.metadata.model.TrackedEntityRule;
import org.dhis2.fhir.adapter.fhir.metadata.repository.SystemRepository;
import org.dhis2.fhir.adapter.fhir.metadata.repository.TrackedEntityRuleRepository;
import org.dhis2.fhir.adapter.fhir.model.FhirVersion;
Expand Down Expand Up @@ -120,10 +123,13 @@ public void getFhirVersions()
@Test
public void transformInternal()
{
final TrackedEntityRule rule2 = new TrackedEntityRule();
rule2.setEvaluationOrder( 100 );
rule2.setFhirResourceType( FhirResourceType.PATIENT );

Mockito.doReturn( Optional.of( new WritableTrackedEntityType( "a1234567890", "Test", Collections.emptyList() ) ) )
.when( trackedEntityMetadataService ).findTypeByReference( Mockito.eq( new Reference( "a1234567890", ReferenceType.ID ) ) );
Mockito.doReturn( Collections.singletonList( new RuleInfo<>( new ProgramMetadataRule(), Collections.emptyList() ) ) )
.when( trackedEntityRuleRepository ).findByTypeRefs( Mockito.eq( new HashSet<>(
Mockito.doReturn( Collections.singletonList( rule2 ) ).when( trackedEntityRuleRepository ).findByTypeRefs( Mockito.eq( new HashSet<>(
Arrays.asList( new Reference( "a1234567890", ReferenceType.ID ), new Reference( "Test", ReferenceType.NAME ) ) ) ) );

final WritableProgram program = new WritableProgram();
Expand Down Expand Up @@ -156,6 +162,10 @@ public void transformInternal()
Assert.assertEquals( "Test Description", fhirPlanDefinition.getDescription() );
Assert.assertEquals( 2, fhirPlanDefinition.getAction().size() );

Assert.assertEquals( 1, fhirPlanDefinition.getExtension().size() );
Assert.assertEquals( ResourceTypeExtensionUtils.URL, fhirPlanDefinition.getExtension().get( 0 ).getUrl() );
Assert.assertEquals( "Patient", fhirPlanDefinition.getExtension().get( 0 ).getValue().toString() );

Assert.assertEquals( "b1234567890", fhirPlanDefinition.getAction().get( 0 ).getId() );
Assert.assertEquals( "Test Stage 1", fhirPlanDefinition.getAction().get( 0 ).getTitle() );
Assert.assertEquals( "Test Description 1", fhirPlanDefinition.getAction().get( 0 ).getDescription() );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.dhis2.fhir.adapter.fhir.extension;

/*
* Copyright (c) 2004-2019, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import ca.uhn.fhir.model.api.IElement;
import org.dhis2.fhir.adapter.fhir.metadata.model.FhirResourceType;
import org.hl7.fhir.instance.model.api.IBaseExtension;
import org.hl7.fhir.instance.model.api.IBaseHasExtensions;
import org.hl7.fhir.instance.model.api.IPrimitiveType;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.function.Function;

/**
* Utility class to process FHIR resource type extension.
*
* @author volsch
*/
public abstract class ResourceTypeExtensionUtils
{
public static final String URL = "http://www.dhis2.org/dhis2-fhir-adapter/fhir/extensions/resource-type";

@SuppressWarnings( "unchecked" )
public static void setValue( @Nonnull IBaseHasExtensions resource, @Nullable FhirResourceType fhirResourceType, @Nonnull Function<String, IElement> typeFactory )
{
resource.getExtension().removeIf( e -> URL.equals( e.getUrl() ) );

if ( fhirResourceType != null )
{
final IBaseExtension<?, ?> extension = resource.addExtension();

extension.setUrl( URL );
extension.setValue( ( (IPrimitiveType<String>) typeFactory.apply( "string" ) ).setValue( fhirResourceType.getResourceTypeName() ) );
}
}

private ResourceTypeExtensionUtils()
{
super();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import ca.uhn.fhir.model.api.IElement;
import org.dhis2.fhir.adapter.dhis.model.DhisResourceType;
import org.dhis2.fhir.adapter.dhis.model.Reference;
import org.dhis2.fhir.adapter.dhis.model.ReferenceType;
Expand All @@ -36,6 +37,8 @@
import org.dhis2.fhir.adapter.dhis.tracker.trackedentity.TrackedEntityMetadataService;
import org.dhis2.fhir.adapter.dhis.tracker.trackedentity.TrackedEntityType;
import org.dhis2.fhir.adapter.fhir.data.repository.FhirDhisAssignmentRepository;
import org.dhis2.fhir.adapter.fhir.extension.ResourceTypeExtensionUtils;
import org.dhis2.fhir.adapter.fhir.metadata.model.AbstractRule;
import org.dhis2.fhir.adapter.fhir.metadata.model.FhirResourceType;
import org.dhis2.fhir.adapter.fhir.metadata.model.ProgramMetadataRule;
import org.dhis2.fhir.adapter.fhir.metadata.repository.SystemRepository;
Expand All @@ -46,11 +49,13 @@
import org.dhis2.fhir.adapter.fhir.transform.dhis.impl.metadata.AbstractReadOnlyDhisMetadataToTypedFhirTransformer;
import org.dhis2.fhir.adapter.fhir.transform.scripted.AccessibleScriptedDhisMetadata;
import org.dhis2.fhir.adapter.lock.LockManager;
import org.hl7.fhir.instance.model.api.IBaseHasExtensions;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import java.util.function.Function;

/**
* Implementation of {@link DhisToFhirTransformer} for transforming DHIS2
Expand All @@ -63,6 +68,8 @@ public abstract class AbstractProgramMetadataToFhirPlanDefinitionTransformer<F e
{
private final Logger logger = LoggerFactory.getLogger( getClass() );

public static final FhirResourceType DEFAULT_SUBJECT_RESOURCE_TYPE = FhirResourceType.PATIENT;

private final TrackedEntityMetadataService trackedEntityMetadataService;

private final TrackedEntityRuleRepository trackedEntityRuleRepository;
Expand Down Expand Up @@ -104,20 +111,36 @@ public Class<ProgramMetadataRule> getRuleClass()
return ProgramMetadataRule.class;
}

protected boolean isApplicableProgram( @Nonnull Program program )
protected boolean addSubjectResourceType( @Nonnull Program program, @Nonnull IBaseHasExtensions resource )
{
if ( program.isWithoutRegistration() || program.getTrackedEntityTypeId() == null )
if ( program.getTrackedEntityTypeId() == null )
{
return false;
ResourceTypeExtensionUtils.setValue( resource, null, getTypeFactory() );

return true;
}

final TrackedEntityType trackedEntityType = trackedEntityMetadataService.findTypeByReference( new Reference( program.getTrackedEntityTypeId(), ReferenceType.ID ) ).orElse( null );
final TrackedEntityType trackedEntityType = trackedEntityMetadataService.findTypeByReference(
new Reference( program.getTrackedEntityTypeId(), ReferenceType.ID ) ).orElse( null );

if ( trackedEntityType == null )
{
return false;
}

return !trackedEntityRuleRepository.findByTypeRefs( trackedEntityType.getAllReferences() ).isEmpty();
final FhirResourceType fhirResourceType = trackedEntityRuleRepository.findByTypeRefs( trackedEntityType.getAllReferences() ).stream()
.sorted( ( o1, o2 ) -> o2.getEvaluationOrder() - o1.getEvaluationOrder() ).map( AbstractRule::getFhirResourceType ).findFirst().orElse( null );

if ( fhirResourceType == null )
{
return false;
}

ResourceTypeExtensionUtils.setValue( resource, fhirResourceType, getTypeFactory() );

return true;
}

@Nonnull
protected abstract Function<String, IElement> getTypeFactory();
}
Loading

0 comments on commit 46299ce

Please sign in to comment.