Skip to content

Commit

Permalink
Adds value type extension for Questionnaire items.
Browse files Browse the repository at this point in the history
  • Loading branch information
volsch committed Aug 4, 2019
1 parent 46299ce commit 054de1f
Show file tree
Hide file tree
Showing 14 changed files with 463 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<kind value="complex-type" />
<abstract value="false" />
<context>
<type value="element" />
<type value="fhirpath" />
<expression value="DomainResource" />
</context>
<type value="Extension" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,26 @@
<path value="Questionnaire.code" />
<max value="0" />
</element>
<element id="Questionnaire.item.extension">
<path value="Questionnaire.item.extension" />
<slicing>
<discriminator>
<type value="value" />
<path value="url" />
</discriminator>
<rules value="open" />
</slicing>
</element>
<element id="Questionnaire.item.extension:valueType">
<path value="Questionnaire.item.extension" />
<sliceName value="valueType" />
<min value="0" />
<max value="1" />
<type>
<code value="Extension" />
<profile value="http://www.dhis2.org/dhis2-fhir-adapter/fhir/extensions/value-type" />
</type>
</element>
<element id="Questionnaire.item.definition">
<path value="Questionnaire.item.definition" />
<max value="0" />
Expand Down
57 changes: 57 additions & 0 deletions docs/fhir/profiles/ValueTypeExtension.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/value-type" />
<name value="ValueTypeExtension" />
<status value="active" />
<experimental value="false" />
<fhirVersion value="4.0.0" />
<kind value="complex-type" />
<abstract value="false" />
<context>
<type value="fhirpath" />
<expression value="Questionnaire.item" />
</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="DHIS2 Value Type" />
<definition value="Must be a valid DHIS2 value type (like INTEGER_POSITIVE)." />
<min value="1" />
<type>
<code value="string" />
</type>
</element>
</differential>
</StructureDefinition>
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
* 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.ProgramStage;
import org.dhis2.fhir.adapter.fhir.data.repository.FhirDhisAssignmentRepository;
import org.dhis2.fhir.adapter.fhir.extension.ValueTypeExtensionUtils;
import org.dhis2.fhir.adapter.fhir.metadata.model.FhirClient;
import org.dhis2.fhir.adapter.fhir.metadata.model.ProgramStageMetadataRule;
import org.dhis2.fhir.adapter.fhir.metadata.model.RuleInfo;
Expand All @@ -49,12 +51,14 @@
import org.hl7.fhir.r4.model.Questionnaire;
import org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent;
import org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemType;
import org.hl7.fhir.r4.model.ResourceFactory;
import org.springframework.stereotype.Component;

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

import static org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemType.*;

Expand Down Expand Up @@ -104,6 +108,8 @@ protected boolean transformInternal( @Nonnull FhirClient fhirClient, @Nonnull Dh
itemComponent.setRequired( dataElement.isCompulsory() );
itemComponent.setType( type );

ValueTypeExtensionUtils.setValue( itemComponent, dataElement.getElement().getValueType(), getTypeFactory() );

if ( dataElement.getElement().isOptionSetValue() )
{
dataElement.getElement().getOptionSet().getOptions().forEach( option -> itemComponent.addAnswerOption().setValue(
Expand Down Expand Up @@ -155,4 +161,11 @@ protected QuestionnaireItemType convertValueType( @Nonnull ValueType valueType )
// unhandled data type
return null;
}

@Nonnull
@Override
protected Function<String, IElement> getTypeFactory()
{
return ResourceFactory::createType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.dhis2.fhir.adapter.dhis.tracker.program.WritableProgramStage;
import org.dhis2.fhir.adapter.dhis.tracker.program.WritableProgramStageDataElement;
import org.dhis2.fhir.adapter.fhir.data.repository.FhirDhisAssignmentRepository;
import org.dhis2.fhir.adapter.fhir.extension.ValueTypeExtensionUtils;
import org.dhis2.fhir.adapter.fhir.metadata.model.FhirClient;
import org.dhis2.fhir.adapter.fhir.metadata.model.ProgramStageMetadataRule;
import org.dhis2.fhir.adapter.fhir.metadata.model.RuleInfo;
Expand All @@ -49,6 +50,7 @@
import org.dhis2.fhir.adapter.model.ValueType;
import org.hl7.fhir.r4.model.Coding;
import org.hl7.fhir.r4.model.Questionnaire;
import org.hl7.fhir.r4.model.StringType;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -157,6 +159,11 @@ public void transformInternal()
Assert.assertEquals( 0, fhirQuestionnaire.getItem().get( 0 ).getAnswerOption().size() );
Assert.assertFalse( fhirQuestionnaire.getItem().get( 0 ).getRequired() );

Assert.assertEquals( 1, fhirQuestionnaire.getItem().get( 0 ).getExtension().size() );
Assert.assertEquals( ValueTypeExtensionUtils.URL, fhirQuestionnaire.getItem().get( 0 ).getExtension().get( 0 ).getUrl() );
Assert.assertTrue( fhirQuestionnaire.getItem().get( 0 ).getExtension().get( 0 ).getValue() instanceof StringType );
Assert.assertEquals( "TEXT", ( (StringType) fhirQuestionnaire.getItem().get( 0 ).getExtension().get( 0 ).getValue() ).getValue() );

Assert.assertEquals( "d1123456789", fhirQuestionnaire.getItem().get( 1 ).getLinkId() );
Assert.assertEquals( "Value 2", fhirQuestionnaire.getItem().get( 1 ).getText() );
Assert.assertEquals( 2, fhirQuestionnaire.getItem().get( 1 ).getAnswerOption().size() );
Expand All @@ -166,5 +173,10 @@ public void transformInternal()
Assert.assertEquals( "7", ( (Coding) fhirQuestionnaire.getItem().get( 1 ).getAnswerOption().get( 1 ).getValue() ).getCode() );
Assert.assertEquals( "Test Value 2", ( (Coding) fhirQuestionnaire.getItem().get( 1 ).getAnswerOption().get( 1 ).getValue() ).getDisplay() );
Assert.assertTrue( fhirQuestionnaire.getItem().get( 1 ).getRequired() );

Assert.assertEquals( 1, fhirQuestionnaire.getItem().get( 1 ).getExtension().size() );
Assert.assertEquals( ValueTypeExtensionUtils.URL, fhirQuestionnaire.getItem().get( 1 ).getExtension().get( 0 ).getUrl() );
Assert.assertTrue( fhirQuestionnaire.getItem().get( 1 ).getExtension().get( 0 ).getValue() instanceof StringType );
Assert.assertEquals( "INTEGER", ( (StringType) fhirQuestionnaire.getItem().get( 1 ).getExtension().get( 0 ).getValue() ).getValue() );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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.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 extensions.
*
* @author volsch
*/
abstract class BaseExtensionUtils
{
@SuppressWarnings( "unchecked" )
protected static void setStringValue( @Nonnull String url, @Nonnull IBaseHasExtensions resource, @Nullable String value, @Nonnull Function<String, IElement> typeFactory )
{
resource.getExtension().removeIf( e -> url.equals( e.getUrl() ) );

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

extension.setUrl( url );
extension.setValue( ( (IPrimitiveType<String>) typeFactory.apply( "string" ) ).setValue( value ) );
}
}

private BaseExtensionUtils()
{
super();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@

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;
Expand All @@ -47,18 +45,9 @@ 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() ) );
}
BaseExtensionUtils.setStringValue( URL, resource, fhirResourceType == null ? null : fhirResourceType.getResourceTypeName(), typeFactory );
}

private ResourceTypeExtensionUtils()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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.model.ValueType;
import org.hl7.fhir.instance.model.api.IBaseHasExtensions;

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

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

public static void setValue( @Nonnull IBaseHasExtensions resource, @Nonnull ValueType valueType, @Nonnull Function<String, IElement> typeFactory )
{
BaseExtensionUtils.setStringValue( URL, resource, valueType.name(), typeFactory );
}

private ValueTypeExtensionUtils()
{
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.orgunit.OrganizationUnitService;
import org.dhis2.fhir.adapter.fhir.data.repository.FhirDhisAssignmentRepository;
Expand All @@ -45,6 +46,7 @@
import org.slf4j.LoggerFactory;

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

/**
* Implementation of {@link DhisToFhirTransformer} for transforming DHIS2
Expand Down Expand Up @@ -90,4 +92,7 @@ public Class<ProgramStageMetadataRule> getRuleClass()
{
return ProgramStageMetadataRule.class;
}

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

0 comments on commit 054de1f

Please sign in to comment.