Skip to content

Commit

Permalink
Add ImportSummariesWebMessage class and adjusts code
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidCKen committed Jul 24, 2019
1 parent d77e7b6 commit fa3eac3
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.dhis2.fhir.adapter.dhis.model;

/*
* Copyright (c) 2004-2018, 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.
*/

/**
* The web message that contains (1 to many) import summaries for creating and updating
* DHIS2 resources.
*
* @author David Katuscak
*/
public class ImportSummariesWebMessage extends WebMessage
{
private static final long serialVersionUID = -7713823944527785249L;

private ImportSummaries response;

public ImportSummaries getResponse()
{
return response;
}

public void setResponse( ImportSummaries response )
{
this.response = response;
}

public boolean isNotSuccessful()
{
return (getStatus() != Status.OK) ||
(getResponse().getImportSummaries().size() != 1) ||
(getResponse().getImportSummaries().get( 0 ).getStatus() != ImportStatus.SUCCESS) ||
(getResponse().getImportSummaries().get( 0 ).getReference() == null);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.dhis2.fhir.adapter.dhis.model;

/*
* Copyright (c) 2004-2018, University of Oslo
* Copyright (c) 2004-2019, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -29,32 +29,29 @@
*/

/**
* The web message that contains import summaries for creating and updating
* The web message that contains (1) import summary for creating and updating
* DHIS2 resources.
*
* @author volsch
*/
public class ImportSummaryWebMessage extends WebMessage
public class ImportSummaryWebMessage extends WebMessage
{
private static final long serialVersionUID = -7713823944527785249L;

private ImportSummaries response;
private ImportSummary response;

public ImportSummaries getResponse()
public ImportSummary getResponse()
{
return response;
}

public void setResponse( ImportSummaries response )
public void setResponse( ImportSummary response )
{
this.response = response;
}

public boolean isNotSuccessful()
{
return (getStatus() != Status.OK) ||
(getResponse().getImportSummaries().size() != 1) ||
(getResponse().getImportSummaries().get( 0 ).getStatus() != ImportStatus.SUCCESS) ||
(getResponse().getImportSummaries().get( 0 ).getReference() == null);
return getStatus() != Status.OK;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
import org.dhis2.fhir.adapter.dhis.model.DhisResourceComparator;
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.ImportSummaryWebMessage;
import org.dhis2.fhir.adapter.dhis.model.Status;
import org.dhis2.fhir.adapter.dhis.tracker.program.Enrollment;
import org.dhis2.fhir.adapter.dhis.tracker.program.EnrollmentService;
Expand Down Expand Up @@ -207,7 +207,7 @@ public Enrollment createOrUpdate( @Nonnull Enrollment enrollment )
@Nonnull
protected Enrollment _create( @Nonnull Enrollment enrollment )
{
final ResponseEntity<ImportSummaryWebMessage> response;
final ResponseEntity<ImportSummariesWebMessage> response;

if ( enrollment.getId() == null )
{
Expand All @@ -230,7 +230,7 @@ protected Enrollment _create( @Nonnull Enrollment enrollment )
try
{
response = restTemplate.exchange( ENROLLMENT_CREATE_URI, HttpMethod.PUT, new HttpEntity<>( enrollment ),
ImportSummaryWebMessage.class, enrollment.getId() );
ImportSummariesWebMessage.class, enrollment.getId() );
}
catch ( HttpClientErrorException e )
{
Expand All @@ -242,7 +242,7 @@ protected Enrollment _create( @Nonnull Enrollment enrollment )
throw e;
}

final ImportSummaryWebMessage result = Objects.requireNonNull( response.getBody() );
final ImportSummariesWebMessage result = Objects.requireNonNull( response.getBody() );

if ( result.isNotSuccessful() )
{
Expand Down Expand Up @@ -283,11 +283,11 @@ protected Enrollment _update( @Nonnull Enrollment enrollment )
// update of included events is not supported
enrollment.setEvents( Collections.emptyList() );

final ResponseEntity<ImportSummaryWebMessage> response;
final ResponseEntity<ImportSummariesWebMessage> response;
try
{
response = restTemplate.exchange( ENROLLMENT_UPDATE_URI, HttpMethod.PUT, new HttpEntity<>( enrollment ),
ImportSummaryWebMessage.class, enrollment.getId() );
ImportSummariesWebMessage.class, enrollment.getId() );
}
catch ( HttpClientErrorException e )
{
Expand All @@ -299,7 +299,7 @@ protected Enrollment _update( @Nonnull Enrollment enrollment )
throw e;
}

final ImportSummaryWebMessage result = Objects.requireNonNull( response.getBody() );
final ImportSummariesWebMessage result = Objects.requireNonNull( response.getBody() );

if ( result.getStatus() != Status.OK )
{
Expand Down Expand Up @@ -327,10 +327,10 @@ public void persistSave( @Nonnull Collection<Enrollment> resources, boolean crea
final List<Enrollment> enrollments = resources.stream().sorted( DhisResourceComparator.INSTANCE ).collect( Collectors.toList() );
enrollments.forEach( e -> e.setEvents( Collections.emptyList() ) );

final ResponseEntity<ImportSummaryWebMessage> response =
restTemplate.postForEntity( create ? ENROLLMENT_CREATES_URI : ENROLLMENT_UPDATES_URI, new DhisEnrollments( enrollments ), ImportSummaryWebMessage.class );
final ResponseEntity<ImportSummariesWebMessage> response =
restTemplate.postForEntity( create ? ENROLLMENT_CREATES_URI : ENROLLMENT_UPDATES_URI, new DhisEnrollments( enrollments ), ImportSummariesWebMessage.class );

final ImportSummaryWebMessage result = Objects.requireNonNull( response.getBody() );
final ImportSummariesWebMessage result = Objects.requireNonNull( response.getBody() );
final int size = enrollments.size();

if ( result.getStatus() != Status.OK || result.getResponse() == null || result.getResponse().getImportSummaries().size() != size )
Expand Down Expand Up @@ -406,10 +406,10 @@ public void persistDeleteById( @Nonnull Collection<String> ids, @Nullable Consum
}

final List<Enrollment> Enrollments = ids.stream().map( Enrollment::new ).sorted( DhisResourceComparator.INSTANCE ).collect( Collectors.toList() );
final ResponseEntity<ImportSummaryWebMessage> response =
restTemplate.postForEntity( ENROLLMENT_DELETES_URI, new DhisEnrollments( Enrollments ), ImportSummaryWebMessage.class );
final ResponseEntity<ImportSummariesWebMessage> response =
restTemplate.postForEntity( ENROLLMENT_DELETES_URI, new DhisEnrollments( Enrollments ), ImportSummariesWebMessage.class );

final ImportSummaryWebMessage result = Objects.requireNonNull( response.getBody() );
final ImportSummariesWebMessage result = Objects.requireNonNull( response.getBody() );
final int size = Enrollments.size();

if ( result.getStatus() != Status.OK || result.getResponse() == null || result.getResponse().getImportSummaries().size() != size )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
import org.dhis2.fhir.adapter.dhis.model.DhisResourceComparator;
import org.dhis2.fhir.adapter.dhis.model.DhisResourceResult;
import org.dhis2.fhir.adapter.dhis.model.ImportStatus;
import org.dhis2.fhir.adapter.dhis.model.ImportSummariesWebMessage;
import org.dhis2.fhir.adapter.dhis.model.ImportSummary;
import org.dhis2.fhir.adapter.dhis.model.ImportSummaryWebMessage;
import org.dhis2.fhir.adapter.dhis.model.Status;
import org.dhis2.fhir.adapter.dhis.model.UriFilterApplier;
import org.dhis2.fhir.adapter.dhis.tracker.program.Event;
Expand Down Expand Up @@ -173,10 +173,10 @@ public void persistDeleteById( @Nonnull Collection<String> ids, @Nullable Consum
}

final List<Event> Events = ids.stream().map( Event::new ).sorted( DhisResourceComparator.INSTANCE ).collect( Collectors.toList() );
final ResponseEntity<ImportSummaryWebMessage> response =
restTemplate.postForEntity( DELETES_URI, new DhisEvents( Events ), ImportSummaryWebMessage.class );
final ResponseEntity<ImportSummariesWebMessage> response =
restTemplate.postForEntity( DELETES_URI, new DhisEvents( Events ), ImportSummariesWebMessage.class );

final ImportSummaryWebMessage result = Objects.requireNonNull( response.getBody() );
final ImportSummariesWebMessage result = Objects.requireNonNull( response.getBody() );
final int size = Events.size();

if ( result.getStatus() != Status.OK || result.getResponse() == null || result.getResponse().getImportSummaries().size() != size )
Expand Down Expand Up @@ -241,10 +241,10 @@ public void persistSave( @Nonnull Collection<Event> resources, boolean create, @
}

final List<Event> events = resources.stream().sorted( DhisResourceComparator.INSTANCE ).collect( Collectors.toList() );
final ResponseEntity<ImportSummaryWebMessage> response =
restTemplate.postForEntity( create ? CREATES_URI : UPDATES_URI, new DhisEvents( events ), ImportSummaryWebMessage.class );
final ResponseEntity<ImportSummariesWebMessage> response =
restTemplate.postForEntity( create ? CREATES_URI : UPDATES_URI, new DhisEvents( events ), ImportSummariesWebMessage.class );

final ImportSummaryWebMessage result = Objects.requireNonNull( response.getBody() );
final ImportSummariesWebMessage result = Objects.requireNonNull( response.getBody() );
final int size = events.size();

if ( result.getStatus() != Status.OK || result.getResponse() == null || result.getResponse().getImportSummaries().size() != size )
Expand Down Expand Up @@ -373,7 +373,7 @@ public Instant poll( @Nonnull DhisSyncGroup group, @Nonnull Instant lastUpdated,
@Nonnull
protected Event create( @Nonnull Event event )
{
final ResponseEntity<ImportSummaryWebMessage> response;
final ResponseEntity<ImportSummariesWebMessage> response;

if ( event.getId() == null )
{
Expand All @@ -383,7 +383,7 @@ protected Event create( @Nonnull Event event )
try
{
response = restTemplate.exchange( CREATE_URI, HttpMethod.PUT, new HttpEntity<>( event ),
ImportSummaryWebMessage.class, event.getId() );
ImportSummariesWebMessage.class, event.getId() );
}
catch ( HttpClientErrorException e )
{
Expand All @@ -395,7 +395,7 @@ protected Event create( @Nonnull Event event )
throw e;
}

final ImportSummaryWebMessage result = Objects.requireNonNull( response.getBody() );
final ImportSummariesWebMessage result = Objects.requireNonNull( response.getBody() );

if ( result.isNotSuccessful() )
{
Expand All @@ -412,12 +412,12 @@ protected Event create( @Nonnull Event event )
@Nonnull
protected Event update( @Nonnull Event event )
{
final ResponseEntity<ImportSummaryWebMessage> response;
final ResponseEntity<ImportSummariesWebMessage> response;

try
{
response = restTemplate.exchange( UPDATE_URI, HttpMethod.PUT, new HttpEntity<>( event ),
ImportSummaryWebMessage.class, event.getId() );
ImportSummariesWebMessage.class, event.getId() );
}
catch ( HttpClientErrorException e )
{
Expand All @@ -429,7 +429,7 @@ protected Event update( @Nonnull Event event )
throw e;
}

final ImportSummaryWebMessage result = Objects.requireNonNull( response.getBody() );
final ImportSummariesWebMessage result = Objects.requireNonNull( response.getBody() );

if ( result.getStatus() != Status.OK )
{
Expand Down Expand Up @@ -474,11 +474,11 @@ public DhisResourceResult<Event> find( @Nonnull String programId, @Nonnull Strin

protected void update( @Nonnull MinimalEvent event )
{
final ResponseEntity<ImportSummaryWebMessage> response;
final ResponseEntity<ImportSummariesWebMessage> response;
try
{
response = restTemplate.exchange( UPDATE_DATA_VALUE_URI, HttpMethod.PUT, new HttpEntity<>( event ),
ImportSummaryWebMessage.class, event.getId(), event.getDataElementId() );
ImportSummariesWebMessage.class, event.getId(), event.getDataElementId() );
}
catch ( HttpClientErrorException e )
{
Expand All @@ -488,7 +488,7 @@ protected void update( @Nonnull MinimalEvent event )
}
throw e;
}
final ImportSummaryWebMessage result = Objects.requireNonNull( response.getBody() );
final ImportSummariesWebMessage result = Objects.requireNonNull( response.getBody() );

if ( result.getStatus() != Status.OK )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
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.ImportSummariesWebMessage;
import org.dhis2.fhir.adapter.dhis.model.ImportSummary;
import org.dhis2.fhir.adapter.dhis.model.ImportSummaryWebMessage;
import org.dhis2.fhir.adapter.dhis.model.Status;
import org.dhis2.fhir.adapter.dhis.model.UriFilterApplier;
import org.dhis2.fhir.adapter.dhis.sync.DhisLastUpdated;
Expand Down Expand Up @@ -293,10 +293,10 @@ public void persistSave( @Nonnull Collection<TrackedEntityInstance> resources, b

trackedEntityInstances.forEach( this::clear );

final ResponseEntity<ImportSummaryWebMessage> response =
restTemplate.postForEntity( create ? CREATES_URI : UPDATES_URI, new TrackedEntityInstances( trackedEntityInstances ), ImportSummaryWebMessage.class );
final ResponseEntity<ImportSummariesWebMessage> response =
restTemplate.postForEntity( create ? CREATES_URI : UPDATES_URI, new TrackedEntityInstances( trackedEntityInstances ), ImportSummariesWebMessage.class );

final ImportSummaryWebMessage result = Objects.requireNonNull( response.getBody() );
final ImportSummariesWebMessage result = Objects.requireNonNull( response.getBody() );
final int size = trackedEntityInstances.size();

if ( result.getStatus() != Status.OK || result.getResponse() == null || result.getResponse().getImportSummaries().size() != size )
Expand Down Expand Up @@ -375,10 +375,10 @@ public void persistDeleteById( @Nonnull Collection<String> ids, @Nullable Consum
}

final List<TrackedEntityInstance> trackedEntityInstances = ids.stream().map( TrackedEntityInstance::new ).sorted( DhisResourceComparator.INSTANCE ).collect( Collectors.toList() );
final ResponseEntity<ImportSummaryWebMessage> response =
restTemplate.postForEntity( DELETES_URI, new TrackedEntityInstances( trackedEntityInstances ), ImportSummaryWebMessage.class );
final ResponseEntity<ImportSummariesWebMessage> response =
restTemplate.postForEntity( DELETES_URI, new TrackedEntityInstances( trackedEntityInstances ), ImportSummariesWebMessage.class );

final ImportSummaryWebMessage result = Objects.requireNonNull( response.getBody() );
final ImportSummariesWebMessage result = Objects.requireNonNull( response.getBody() );
final int size = trackedEntityInstances.size();

if ( result.getStatus() != Status.OK || result.getResponse() == null || result.getResponse().getImportSummaries().size() != size )
Expand Down Expand Up @@ -457,7 +457,7 @@ protected TrackedEntityInstance create( @Nonnull TrackedEntityInstance trackedEn
{
final DhisSyncGroup syncGroup = storedItemService.findSyncGroupById( DhisSyncGroup.DEFAULT_ID )
.orElseThrow( () -> new DhisResourceException( "Could not load default DHIS2 sync group." ) );
final ResponseEntity<ImportSummaryWebMessage> response;
final ResponseEntity<ImportSummariesWebMessage> response;

if ( trackedEntityInstance.getId() == null )
{
Expand All @@ -467,7 +467,7 @@ protected TrackedEntityInstance create( @Nonnull TrackedEntityInstance trackedEn
try
{
clear( trackedEntityInstance );
response = restTemplate.postForEntity( CREATE_URI, trackedEntityInstance, ImportSummaryWebMessage.class, trackedEntityInstance.getId() );
response = restTemplate.postForEntity( CREATE_URI, trackedEntityInstance, ImportSummariesWebMessage.class, trackedEntityInstance.getId() );
}
catch ( HttpClientErrorException e )
{
Expand All @@ -479,7 +479,7 @@ protected TrackedEntityInstance create( @Nonnull TrackedEntityInstance trackedEn
throw e;
}

final ImportSummaryWebMessage result = Objects.requireNonNull( response.getBody() );
final ImportSummariesWebMessage result = Objects.requireNonNull( response.getBody() );

if ( result.isNotSuccessful() )
{
Expand All @@ -500,12 +500,12 @@ protected TrackedEntityInstance update( @Nonnull TrackedEntityInstance trackedEn
final DhisSyncGroup syncGroup = storedItemService.findSyncGroupById( DhisSyncGroup.DEFAULT_ID )
.orElseThrow( () -> new DhisResourceException( "Could not load default DHIS2 sync group." ) );

final ResponseEntity<ImportSummaryWebMessage> response;
final ResponseEntity<ImportSummariesWebMessage> response;
try
{
clear( trackedEntityInstance );
response = restTemplate.exchange( UPDATE_URI, HttpMethod.PUT, new HttpEntity<>( trackedEntityInstance ),
ImportSummaryWebMessage.class, trackedEntityInstance.getId() );
ImportSummariesWebMessage.class, trackedEntityInstance.getId() );
}
catch ( HttpClientErrorException e )
{
Expand All @@ -515,7 +515,7 @@ protected TrackedEntityInstance update( @Nonnull TrackedEntityInstance trackedEn
}
throw e;
}
final ImportSummaryWebMessage result = Objects.requireNonNull( response.getBody() );
final ImportSummariesWebMessage result = Objects.requireNonNull( response.getBody() );
if ( result.getStatus() != Status.OK )
{
throw new DhisImportUnsuccessfulException( "Response indicates an unsuccessful import of tracked entity instance: " + result.getStatus() );
Expand Down

0 comments on commit fa3eac3

Please sign in to comment.