From b05c0eb2419876ef014cf1dd9ff7a8041fa9abc2 Mon Sep 17 00:00:00 2001 From: Volker Schmidt Date: Mon, 12 Aug 2019 17:05:36 +0200 Subject: [PATCH] Implemented read access to care plan. Test are missing and will be added later. --- .../impl/AbstractDhisMetadataServiceImpl.java | 3 +- .../tracker/program/EnrollmentService.java | 5 + .../program/impl/EnrollmentServiceImpl.java | 38 +++ .../program/impl/EventServiceImpl.java | 2 +- .../impl/TrackedEntityServiceImpl.java | 2 +- ...kerProgramCarePlan.StructureDefinition.xml | 9 +- ...u3ValueTypeFhirToDhisTransformerUtils.java | 20 ++ ...R4EnrollmentToCarePlanFhirTransformer.java | 129 +++++++++ .../r4/R4EnrollmentToFhirDataProvider.java | 75 ++++++ ...R4ValueTypeFhirToDhisTransformerUtils.java | 15 ++ .../fhir/extension/BaseExtensionUtils.java | 29 ++ .../extension/IncidentDateExtensionUtils.java | 57 ++++ .../extension/LocationExtensionUtils.java | 57 ++++ .../fhir/metadata/model/FhirResourceType.java | 6 + .../repository/impl/DhisRepositoryImpl.java | 3 +- .../impl/AbstractDhisToFhirTransformer.java | 51 ++++ ...ctEnrollmentToCarePlanFhirTransformer.java | 255 ++++++++++++++++++ .../AbstractEnrollmentToFhirDataProvider.java | 136 ++++++++++ .../EnrollmentToFhirRequestResolver.java | 146 ++++++++++ .../EnrollmentToFhirSearchState.java | 71 +++++ .../PreparedEnrollmentToFhirSearch.java | 54 ++++ ...tadataToFhirPlanDefinitionTransformer.java | 30 +-- .../ProgramTrackedEntityTypeUtils.java | 87 ++++++ .../ProgramStageToFhirTransformer.java | 43 +-- .../program/FhirToEnrollmentTransformer.java | 2 +- .../FhirToProgramStageTransformer.java | 8 +- .../scripted/ImmutableScriptedEnrollment.java | 91 ++----- .../scripted/ScriptedEnrollment.java | 7 +- .../scripted/WritableScriptedEnrollment.java | 73 +---- .../V1.1.0.45_0_0__Care_Resources_Read.sql | 30 +++ .../extension/BaseExtensionUtilsTest.java | 57 +++- .../IncidentDateExtensionUtilsTest.java | 68 +++++ .../extension/LocationExtensionUtilsTest.java | 65 +++++ .../adapter/fhir/extension/TestReference.java | 107 ++++++++ .../adapter/fhir/extension/TypeFactory.java | 13 +- .../ProgramTrackedEntityTypeUtilsTest.java | 122 +++++++++ 36 files changed, 1747 insertions(+), 219 deletions(-) create mode 100644 fhir-r4/src/main/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/enrollment/r4/R4EnrollmentToCarePlanFhirTransformer.java create mode 100644 fhir-r4/src/main/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/enrollment/r4/R4EnrollmentToFhirDataProvider.java create mode 100644 fhir/src/main/java/org/dhis2/fhir/adapter/fhir/extension/IncidentDateExtensionUtils.java create mode 100644 fhir/src/main/java/org/dhis2/fhir/adapter/fhir/extension/LocationExtensionUtils.java create mode 100644 fhir/src/main/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/enrollment/AbstractEnrollmentToCarePlanFhirTransformer.java create mode 100644 fhir/src/main/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/enrollment/AbstractEnrollmentToFhirDataProvider.java create mode 100644 fhir/src/main/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/enrollment/EnrollmentToFhirRequestResolver.java create mode 100644 fhir/src/main/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/enrollment/EnrollmentToFhirSearchState.java create mode 100644 fhir/src/main/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/enrollment/PreparedEnrollmentToFhirSearch.java create mode 100644 fhir/src/main/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/metadata/program/ProgramTrackedEntityTypeUtils.java create mode 100644 fhir/src/main/resources/db/migration/production/V1.1.0.45_0_0__Care_Resources_Read.sql create mode 100644 fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/IncidentDateExtensionUtilsTest.java create mode 100644 fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/LocationExtensionUtilsTest.java create mode 100644 fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/TestReference.java create mode 100644 fhir/src/test/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/metadata/program/ProgramTrackedEntityTypeUtilsTest.java diff --git a/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/service/impl/AbstractDhisMetadataServiceImpl.java b/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/service/impl/AbstractDhisMetadataServiceImpl.java index fc466c7b..102aab20 100644 --- a/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/service/impl/AbstractDhisMetadataServiceImpl.java +++ b/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/service/impl/AbstractDhisMetadataServiceImpl.java @@ -123,13 +123,12 @@ public Optional findMetadataRefreshedByReference( @Nonnull Reference referenc @HystrixCommand( ignoreExceptions = UnauthorizedException.class ) @Nonnull @Override - @CachePut( cacheResolver = "dhisMetadataCacheResolver", unless = "#result==null" ) public Optional findOneByReference( @Nonnull Reference reference ) { return findOneByReference( userRestTemplate, reference ); } - @HystrixCommand( ignoreExceptions = UnauthorizedException.class ) + @HystrixCommand( ignoreExceptions = { UnauthorizedException.class, DhisFindException.class } ) @Nonnull @Override public DhisResourceResult find( @Nonnull UriFilterApplier uriFilterApplier, int from, int max ) diff --git a/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/tracker/program/EnrollmentService.java b/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/tracker/program/EnrollmentService.java index fa3db68c..4c8f77fd 100644 --- a/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/tracker/program/EnrollmentService.java +++ b/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/tracker/program/EnrollmentService.java @@ -28,6 +28,8 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import org.dhis2.fhir.adapter.dhis.model.DhisResourceResult; +import org.dhis2.fhir.adapter.dhis.model.UriFilterApplier; import org.dhis2.fhir.adapter.dhis.service.DhisService; import javax.annotation.Nonnull; @@ -55,4 +57,7 @@ public interface EnrollmentService extends DhisService Enrollment createOrUpdate( @Nonnull Enrollment enrollment ); boolean delete( @Nonnull String enrollmentId ); + + @Nonnull + DhisResourceResult find( @Nonnull UriFilterApplier uriFilterApplier, int from, int max ); } diff --git a/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/tracker/program/impl/EnrollmentServiceImpl.java b/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/tracker/program/impl/EnrollmentServiceImpl.java index ba8b1e07..b0d47089 100644 --- a/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/tracker/program/impl/EnrollmentServiceImpl.java +++ b/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/tracker/program/impl/EnrollmentServiceImpl.java @@ -33,24 +33,29 @@ import org.dhis2.fhir.adapter.auth.UnauthorizedException; import org.dhis2.fhir.adapter.cache.RequestCacheService; import org.dhis2.fhir.adapter.dhis.DhisConflictException; +import org.dhis2.fhir.adapter.dhis.DhisFindException; import org.dhis2.fhir.adapter.dhis.DhisImportUnsuccessfulException; import org.dhis2.fhir.adapter.dhis.local.LocalDhisRepositoryPersistCallback; import org.dhis2.fhir.adapter.dhis.local.LocalDhisRepositoryPersistResult; import org.dhis2.fhir.adapter.dhis.local.LocalDhisRepositoryPersistStatus; import org.dhis2.fhir.adapter.dhis.local.LocalDhisResourceRepositoryTemplate; import org.dhis2.fhir.adapter.dhis.model.DhisResourceComparator; +import org.dhis2.fhir.adapter.dhis.model.DhisResourceResult; import org.dhis2.fhir.adapter.dhis.model.DhisResourceType; import org.dhis2.fhir.adapter.dhis.model.ImportStatus; import org.dhis2.fhir.adapter.dhis.model.ImportSummaries; import org.dhis2.fhir.adapter.dhis.model.ImportSummariesWebMessage; import org.dhis2.fhir.adapter.dhis.model.ImportSummary; import org.dhis2.fhir.adapter.dhis.model.Status; +import org.dhis2.fhir.adapter.dhis.model.UriFilterApplier; import org.dhis2.fhir.adapter.dhis.tracker.program.Enrollment; import org.dhis2.fhir.adapter.dhis.tracker.program.EnrollmentService; import org.dhis2.fhir.adapter.dhis.tracker.program.EnrollmentStatus; import org.dhis2.fhir.adapter.dhis.tracker.program.Event; import org.dhis2.fhir.adapter.dhis.tracker.program.EventService; import org.dhis2.fhir.adapter.dhis.util.CodeGenerator; +import org.dhis2.fhir.adapter.dhis.util.DhisPagingQuery; +import org.dhis2.fhir.adapter.dhis.util.DhisPagingUtils; import org.dhis2.fhir.adapter.rest.RestTemplateUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -64,9 +69,11 @@ import org.springframework.stereotype.Service; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; @@ -125,6 +132,37 @@ public DhisResourceType getDhisResourceType() return DhisResourceType.ENROLLMENT; } + @HystrixCommand( ignoreExceptions = { UnauthorizedException.class, DhisFindException.class } ) + @Nonnull + @Override + public DhisResourceResult find( @Nonnull UriFilterApplier uriFilterApplier, int from, int max ) + { + final DhisPagingQuery pagingQuery = DhisPagingUtils.createPagingQuery( from, max ); + final List variables = new ArrayList<>(); + final String uri = uriFilterApplier.add( UriComponentsBuilder.newInstance(), variables ).path( "/enrollments.json" ) + .queryParam( "skipPaging", "false" ).queryParam( "page", pagingQuery.getPage() ).queryParam( "pageSize", pagingQuery.getPageSize() ) + .queryParam( "ouMode", uriFilterApplier.containsQueryParam( "ou" ) ? "SELECTED" : "ACCESSIBLE" ) + .queryParam( "fields", ":all" ).build().toString(); + final ResponseEntity result; + try + { + result = restTemplate.getForEntity( uri, DhisEnrollments.class, variables.toArray() ); + } + catch ( HttpClientErrorException e ) + { + if ( e.getStatusCode() == HttpStatus.BAD_REQUEST || e.getStatusCode() == HttpStatus.CONFLICT ) + { + throw new DhisFindException( e.getMessage(), e ); + } + throw e; + } + final DhisEnrollments enrollments = Objects.requireNonNull( result.getBody() ); + + return new DhisResourceResult<>( ( enrollments.getEnrollments().size() > pagingQuery.getResultOffset() ) ? + enrollments.getEnrollments().subList( pagingQuery.getResultOffset(), enrollments.getEnrollments().size() ) : Collections.emptyList(), + ( enrollments.getEnrollments().size() >= pagingQuery.getPageSize() ) ); + } + @HystrixCommand( ignoreExceptions = UnauthorizedException.class ) @Nonnull @Override diff --git a/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/tracker/program/impl/EventServiceImpl.java b/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/tracker/program/impl/EventServiceImpl.java index 4cf20c3a..437e279a 100644 --- a/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/tracker/program/impl/EventServiceImpl.java +++ b/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/tracker/program/impl/EventServiceImpl.java @@ -447,7 +447,7 @@ protected Event update( @Nonnull Event event ) return event; } - @HystrixCommand( ignoreExceptions = UnauthorizedException.class ) + @HystrixCommand( ignoreExceptions = { UnauthorizedException.class, DhisFindException.class } ) @Nonnull @Override public DhisResourceResult find( @Nonnull String programId, @Nonnull String programStageId, @Nonnull UriFilterApplier uriFilterApplier, int from, int max ) diff --git a/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/tracker/trackedentity/impl/TrackedEntityServiceImpl.java b/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/tracker/trackedentity/impl/TrackedEntityServiceImpl.java index a955f56c..0da19f9a 100644 --- a/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/tracker/trackedentity/impl/TrackedEntityServiceImpl.java +++ b/dhis/src/main/java/org/dhis2/fhir/adapter/dhis/tracker/trackedentity/impl/TrackedEntityServiceImpl.java @@ -433,7 +433,7 @@ public boolean persistDeleteById( @Nonnull String id ) return _delete( id ); } - @HystrixCommand( ignoreExceptions = UnauthorizedException.class ) + @HystrixCommand( ignoreExceptions = { UnauthorizedException.class, DhisFindException.class } ) @Nonnull @Override public DhisResourceResult find( @Nonnull String trackedEntityTypeId, @Nonnull UriFilterApplier uriFilterApplier, int from, int max ) diff --git a/docs/fhir/profiles/TrackerProgramCarePlan.StructureDefinition.xml b/docs/fhir/profiles/TrackerProgramCarePlan.StructureDefinition.xml index 185560b2..c3f0c2ac 100644 --- a/docs/fhir/profiles/TrackerProgramCarePlan.StructureDefinition.xml +++ b/docs/fhir/profiles/TrackerProgramCarePlan.StructureDefinition.xml @@ -66,6 +66,7 @@