← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12659: implemented support for updating enrollments

 

------------------------------------------------------------
revno: 12659
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2013-10-15 11:06:28 +0200
message:
  implemented support for updating enrollments
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/EnrollmentServiceTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java	2013-10-15 07:47:05 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java	2013-10-15 09:06:28 +0000
@@ -36,7 +36,6 @@
 import org.hisp.dhis.i18n.I18nManager;
 import org.hisp.dhis.i18n.I18nManagerException;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
-import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.patient.Patient;
 import org.hisp.dhis.patient.PatientService;
 import org.hisp.dhis.program.Program;
@@ -287,7 +286,59 @@
     @Override
     public ImportSummary updateEnrollment( Enrollment enrollment )
     {
-        return null;
+        if ( enrollment.getEnrollment() == null )
+        {
+            ImportSummary importSummary = new ImportSummary( ImportStatus.ERROR, "No enrollment ID was supplied" );
+            importSummary.getImportCount().incrementIgnored();
+
+            return importSummary;
+        }
+
+        ProgramInstance programInstance = programInstanceService.getProgramInstance( enrollment.getEnrollment() );
+
+        if ( programInstance == null )
+        {
+            ImportSummary importSummary = new ImportSummary( ImportStatus.ERROR, "Enrollment ID was not valid." );
+            importSummary.getImportCount().incrementIgnored();
+
+            return importSummary;
+        }
+
+        Patient patient = getPatient( enrollment.getPerson() );
+        Program program = getProgram( enrollment.getProgram() );
+
+        programInstance.setProgram( program );
+        programInstance.setPatient( patient );
+        programInstance.setDateOfIncident( enrollment.getDateOfIncident() );
+        programInstance.setEnrollmentDate( enrollment.getDateOfEnrollment() );
+
+        if ( programInstance.getStatus() != enrollment.getStatus().getValue() )
+        {
+            if ( enrollment.getStatus().equals( EnrollmentStatus.CANCELLED ) )
+            {
+                programInstanceService.cancelProgramInstanceStatus( programInstance );
+            }
+            else if ( enrollment.getStatus().equals( EnrollmentStatus.COMPLETED ) )
+            {
+                programInstanceService.completeProgramInstanceStatus( programInstance, getFormat() );
+            }
+            else
+            {
+                ImportSummary importSummary = new ImportSummary( ImportStatus.ERROR, "Re-enrollment is not allowed, please create a new enrollment." );
+                importSummary.getImportCount().incrementIgnored();
+
+                return importSummary;
+            }
+        }
+
+        programInstanceService.updateProgramInstance( programInstance );
+
+        ImportSummary importSummary = new ImportSummary( ImportStatus.SUCCESS );
+        importSummary.setReference( enrollment.getEnrollment() );
+        importSummary.setDataValueCount( null );
+        importSummary.getImportCount().incrementImported();
+
+        return importSummary;
     }
 
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/EnrollmentServiceTest.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/EnrollmentServiceTest.java	2013-09-23 08:43:52 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/EnrollmentServiceTest.java	2013-10-15 09:06:28 +0000
@@ -48,6 +48,7 @@
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import java.util.Date;
 import java.util.HashSet;
 import java.util.List;
 
@@ -201,6 +202,36 @@
     }
 
     @Test
+    public void testUpdateEnrollment()
+    {
+        Enrollment enrollment = new Enrollment();
+        enrollment.setPerson( maleA.getUid() );
+        enrollment.setProgram( programA.getUid() );
+        enrollment.setDateOfIncident( new Date() );
+        enrollment.setDateOfEnrollment( new Date() );
+
+        ImportSummary importSummary = enrollmentService.saveEnrollment( enrollment );
+        assertEquals( ImportStatus.SUCCESS, importSummary.getStatus() );
+
+        List<Enrollment> enrollments = enrollmentService.getEnrollments( maleA ).getEnrollments();
+
+        assertEquals( 1, enrollments.size() );
+        enrollment = enrollments.get( 0 );
+
+        assertEquals( maleA.getUid(), enrollment.getPerson() );
+        assertEquals( programA.getUid(), enrollment.getProgram() );
+
+        Date MARCH_20_81 = new Date( 81, 2, 20 );
+
+        enrollment.setDateOfEnrollment( MARCH_20_81 );
+        enrollmentService.updateEnrollment( enrollment );
+
+        enrollments = enrollmentService.getEnrollments( maleA ).getEnrollments();
+        assertEquals( 1, enrollments.size() );
+        assertEquals( MARCH_20_81, enrollments.get( 0 ).getDateOfEnrollment() );
+    }
+
+    @Test
     public void testMultipleEnrollmentsShouldFail()
     {
         Enrollment enrollment = new Enrollment();