From 3e968e188ba8cf7f4ef96fedb1bb30a0ce363a70 Mon Sep 17 00:00:00 2001 From: David Katuscak Date: Fri, 12 Jul 2019 09:08:16 +0200 Subject: [PATCH] Adds DHIS2 DataValueSet resource type --- .../adapter/dhis/aggregate/DataValueSet.java | 232 ++++++++++++++++++ .../dhis/aggregate/DataValueSetService.java | 40 +++ .../impl/DataValueSetServiceImpl.java | 91 +++++++ .../adapter/dhis/model/DhisResourceType.java | 7 +- .../fhir/metadata/model/AbstractRule.java | 3 +- .../metadata/model/TransformDataType.java | 4 +- ...2_0_0__Add_New_Resource_Type_Into_Enum.sql | 30 +++ 7 files changed, 404 insertions(+), 3 deletions(-) create mode 100644 dhis/src/main/java/org/dhis2/fhir/adapter/dhis/aggregate/DataValueSet.java create mode 100644 dhis/src/main/java/org/dhis2/fhir/adapter/dhis/aggregate/DataValueSetService.java create mode 100644 dhis/src/main/java/org/dhis2/fhir/adapter/dhis/aggregate/impl/DataValueSetServiceImpl.java create mode 100644 fhir/src/main/resources/db/migration/production/V1.1.0.42_0_0__Add_New_Resource_Type_Into_Enum.sql diff --git a/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/aggregate/DataValueSet.java b/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/aggregate/DataValueSet.java new file mode 100644 index 00000000..b24e152e --- /dev/null +++ b/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/aggregate/DataValueSet.java @@ -0,0 +1,232 @@ +package org.dhis2.fhir.adapter.dhis.aggregate; + +/* + * 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 com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import org.dhis2.fhir.adapter.dhis.model.DhisResource; +import org.dhis2.fhir.adapter.dhis.model.DhisResourceId; +import org.dhis2.fhir.adapter.dhis.model.DhisResourceType; +import org.dhis2.fhir.adapter.dhis.model.WritableDataValue; + +import javax.annotation.Nonnull; +import java.io.Serializable; +import java.time.ZonedDateTime; +import java.util.List; + +/** + * @author David Katuscak + */ +public class DataValueSet implements DhisResource, Serializable +{ +// reporter > reference -> Organisation unit +// period > start/end -> Period +// group > code > text -> Data element +// group > measurescore -> Data value +// +// orgUnit +// period +// dataElement +// dataValue + + private static final long serialVersionUID = 6347075531883647618L; + + @JsonProperty( access = JsonProperty.Access.WRITE_ONLY ) + private boolean deleted; + + @JsonIgnore + private boolean modified; + + @JsonIgnore + private boolean newResource; + + @JsonIgnore + private boolean local; + + @JsonProperty + @JsonInclude( JsonInclude.Include.NON_NULL ) + private ZonedDateTime lastUpdated; + + @JsonProperty( "dataValueSet" ) + @JsonInclude( JsonInclude.Include.NON_NULL ) + private String id; + + @JsonProperty( "dataSet" ) + private String dataSetId; + + @JsonProperty( "orgUnit" ) + private String orgUnitId; + + @JsonProperty + private String period; + + private List dataValues; + + public DataValueSet() + { + super(); + } + + public DataValueSet( @Nonnull String id ) + { + this.id = id; + } + + public DataValueSet( boolean newResource ) + { + this.newResource = newResource; + this.modified = newResource; + this.local = newResource; + } + + @Override + @JsonIgnore + public String getId() + { + return id; + } + + @Override + public void setId( String id ) + { + this.id = id; + } + + @Override + public DhisResourceId getResourceId() + { + return (getId() == null) ? null : new DhisResourceId( DhisResourceType.DATA_VALUE_SET, getId() ); + } + + @Override + public String getOrgUnitId() + { + return orgUnitId; + } + + public void setOrgUnitId( String orgUnitId ) + { + this.orgUnitId = orgUnitId; + } + + public String getPeriod() + { + return period; + } + + public void setPeriod( String period ) + { + this.period = period; + } + + public String getDataSetId() + { + return dataSetId; + } + + public void setDataSetId( String dataSetId ) + { + this.dataSetId = dataSetId; + } + + @JsonIgnore + @Nonnull + @Override + public DhisResourceType getResourceType() + { + return DhisResourceType.DATA_VALUE_SET; + } + + public List getDataValues() + { + return dataValues; + } + + public void setDataValues( List dataValues ) + { + this.dataValues = dataValues; + } + + @Override + public boolean isNewResource() + { + return newResource; + } + + @Override + public void resetNewResource() + { + this.newResource = false; + this.modified = false; + + if ( lastUpdated == null ) + { + lastUpdated = ZonedDateTime.now(); + } + } + + @Override + public boolean isLocal() + { + return local; + } + + public void setLocal( boolean local ) + { + this.local = local; + } + + public boolean isModified() + { + return modified; + } + + public void setModified( boolean modified ) + { + this.modified = modified; + } + + @Override + public boolean isDeleted() + { + return deleted; + } + + @Override + public ZonedDateTime getLastUpdated() + { + return lastUpdated; + } + + public void setLastUpdated( ZonedDateTime lastUpdated ) + { + this.lastUpdated = lastUpdated; + } +} diff --git a/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/aggregate/DataValueSetService.java b/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/aggregate/DataValueSetService.java new file mode 100644 index 00000000..30c32d3b --- /dev/null +++ b/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/aggregate/DataValueSetService.java @@ -0,0 +1,40 @@ +package org.dhis2.fhir.adapter.dhis.aggregate; + +/* + * 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 javax.annotation.Nonnull; + +/** + * @author David Katuscak + */ +public interface DataValueSetService +{ + @Nonnull + DataValueSet createOrUpdate( @Nonnull DataValueSet enrollment ); +} diff --git a/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/aggregate/impl/DataValueSetServiceImpl.java b/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/aggregate/impl/DataValueSetServiceImpl.java new file mode 100644 index 00000000..f7c3d02b --- /dev/null +++ b/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/aggregate/impl/DataValueSetServiceImpl.java @@ -0,0 +1,91 @@ +package org.dhis2.fhir.adapter.dhis.aggregate.impl; + +/* + * 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 org.dhis2.fhir.adapter.dhis.DhisConflictException; +import org.dhis2.fhir.adapter.dhis.DhisImportUnsuccessfulException; +import org.dhis2.fhir.adapter.dhis.aggregate.DataValueSet; +import org.dhis2.fhir.adapter.dhis.aggregate.DataValueSetService; +import org.dhis2.fhir.adapter.dhis.model.ImportSummaryWebMessage; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestTemplate; + +import javax.annotation.Nonnull; +import java.util.Objects; + +/** + * @author David Katuscak + */ +@Service +public class DataValueSetServiceImpl implements DataValueSetService +{ + private static final String DATA_VALUE_SETS_URI = "/dataValueSets.json"; + + private final RestTemplate restTemplate; + + @Autowired + public DataValueSetServiceImpl( @Nonnull @Qualifier( "userDhis2RestTemplate" ) RestTemplate restTemplate ) + { + this.restTemplate = restTemplate; + } + + @Nonnull + @Override + public DataValueSet createOrUpdate( @Nonnull final DataValueSet dataValueSet ) + { + final ResponseEntity response; + + try + { + response = restTemplate.postForEntity( DATA_VALUE_SETS_URI, dataValueSet, ImportSummaryWebMessage.class ); + } + catch ( HttpClientErrorException e ) + { + if ( HttpStatus.CONFLICT.equals( e.getStatusCode() ) ) + { + throw new DhisConflictException( "DataValueSet could not be created: " + e.getResponseBodyAsString(), e ); + } + throw e; + } + + final ImportSummaryWebMessage result = Objects.requireNonNull( response.getBody() ); + + if ( result.isNotSuccessful() ) + { + throw new DhisImportUnsuccessfulException( "Response indicates an unsuccessful DataValueSet import." ); + } + + return dataValueSet; + } +} diff --git a/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/model/DhisResourceType.java b/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/model/DhisResourceType.java index 654008ad..94d96c78 100644 --- a/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/model/DhisResourceType.java +++ b/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/model/DhisResourceType.java @@ -74,7 +74,12 @@ public enum DhisResourceType /** * Resource is a organisation unit. */ - ORGANIZATION_UNIT( "organisationUnits", "ou" ); + ORGANIZATION_UNIT( "organisationUnits", "ou" ), + + /** + * Resource is a Data Value Set. + */ + DATA_VALUE_SET( "dataValueSets", "dvs" ); private static final Map byTypeName = Arrays.stream( values() ).collect( Collectors.toMap( DhisResourceType::getTypeName, v -> v ) ); diff --git a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/model/AbstractRule.java b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/model/AbstractRule.java index b5bf83f7..25be8065 100644 --- a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/model/AbstractRule.java +++ b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/model/AbstractRule.java @@ -108,7 +108,8 @@ @JsonSubTypes.Type( value = OrganizationUnitRule.class, name = "ORGANIZATION_UNIT" ), @JsonSubTypes.Type( value = EnrollmentRule.class, name = "ENROLLMENT" ), @JsonSubTypes.Type( value = ProgramMetadataRule.class, name = "PROGRAM_METADATA" ), - @JsonSubTypes.Type( value = ProgramStageMetadataRule.class, name = "PROGRAM_STAGE_METADATA" ) + @JsonSubTypes.Type( value = ProgramStageMetadataRule.class, name = "PROGRAM_STAGE_METADATA" ), + @JsonSubTypes.Type( value = DataValueSetRule.class, name = "DATA_VALUE_SET" ) } ) @JsonFilter( value = AdapterBeanPropertyFilter.FILTER_NAME ) public abstract class AbstractRule extends VersionedBaseMetadata implements Serializable, Comparable, NamedMetadata diff --git a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/model/TransformDataType.java b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/model/TransformDataType.java index 0ae3ab47..460f6195 100644 --- a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/model/TransformDataType.java +++ b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/model/TransformDataType.java @@ -42,6 +42,7 @@ public enum TransformDataType DHIS_TRACKED_ENTITY_INSTANCE( null ), DHIS_ENROLLMENT( null ), DHIS_EVENT( null ), + DHIS_DATA_VALUE_SET( null ), FHIR_ENCOUNTER( FhirResourceType.ENCOUNTER ), FHIR_LOCATION( FhirResourceType.LOCATION ), FHIR_ORGANIZATION( FhirResourceType.ORGANIZATION ), @@ -56,7 +57,8 @@ public enum TransformDataType FHIR_PLAN_DEFINITION( FhirResourceType.PLAN_DEFINITION ), FHIR_QUESTIONNAIRE( FhirResourceType.QUESTIONNAIRE ), FHIR_CARE_PLAN( FhirResourceType.CARE_PLAN ), - FHIR_QUESTIONNAIRE_RESPONSE( FhirResourceType.QUESTIONNAIRE_RESPONSE ); + FHIR_QUESTIONNAIRE_RESPONSE( FhirResourceType.QUESTIONNAIRE_RESPONSE ), + FHIR_MEASURE_REPORT( FhirResourceType.MEASURE_REPORT ); private final FhirResourceType fhirResourceType; diff --git a/fhir/src/main/resources/db/migration/production/V1.1.0.42_0_0__Add_New_Resource_Type_Into_Enum.sql b/fhir/src/main/resources/db/migration/production/V1.1.0.42_0_0__Add_New_Resource_Type_Into_Enum.sql new file mode 100644 index 00000000..067659e3 --- /dev/null +++ b/fhir/src/main/resources/db/migration/production/V1.1.0.42_0_0__Add_New_Resource_Type_Into_Enum.sql @@ -0,0 +1,30 @@ +/* + * 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 PROGRAM_STAGE_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. + */ + +-- @formatter:off +INSERT INTO fhir_resource_type_enum VALUES ('MEASURE_REPORT'); \ No newline at end of file