← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20845: minor cleanup in enrollment service/controller

 

------------------------------------------------------------
revno: 20845
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-10-22 11:57:26 +0700
message:
  minor cleanup in enrollment service/controller
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/main/java/org/hisp/dhis/dxf2/events/enrollment/EnrollmentService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EnrollmentController.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	2015-10-20 19:03:27 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java	2015-10-22 04:57:26 +0000
@@ -62,7 +62,6 @@
 import org.hisp.dhis.user.CurrentUserService;
 import org.hisp.dhis.user.UserService;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.util.Assert;
 
 import java.util.ArrayList;
 import java.util.Date;
@@ -409,30 +408,51 @@
     // -------------------------------------------------------------------------
 
     @Override
-    public void deleteEnrollment( Enrollment enrollment )
-    {
-        ProgramInstance programInstance = programInstanceService.getProgramInstance( enrollment.getEnrollment() );
-        Assert.notNull( programInstance );
-
-        programInstanceService.deleteProgramInstance( programInstance );
-    }
-
-    @Override
-    public void cancelEnrollment( Enrollment enrollment )
-    {
-        ProgramInstance programInstance = programInstanceService.getProgramInstance( enrollment.getEnrollment() );
-        Assert.notNull( programInstance );
-
+    public ImportSummary deleteEnrollment( String uid )
+    {
+        ProgramInstance programInstance = programInstanceService.getProgramInstance( uid );
+
+        if ( programInstance != null )
+        {
+            programInstanceService.deleteProgramInstance( programInstance );
+            return new ImportSummary( ImportStatus.SUCCESS, "Deletion of enrollment " + uid + " was successful." );
+        }
+
+        return new ImportSummary( ImportStatus.ERROR, "ID " + uid + " does not point to a valid enrollment" );
+    }
+
+    @Override
+    public ImportSummaries deleteEnrollments( List<String> uids )
+    {
+        ImportSummaries importSummaries = new ImportSummaries();
+        int counter = 0;
+
+        for ( String uid : uids )
+        {
+            importSummaries.addImportSummary( deleteEnrollment( uid ) );
+
+            if ( counter % FLUSH_FREQUENCY == 0 )
+            {
+                dbmsManager.clearSession();
+            }
+
+            counter++;
+        }
+
+        return importSummaries;
+    }
+
+    @Override
+    public void cancelEnrollment( String uid )
+    {
+        ProgramInstance programInstance = programInstanceService.getProgramInstance( uid );
         programInstanceService.cancelProgramInstanceStatus( programInstance );
     }
 
     @Override
-    public void completeEnrollment( Enrollment enrollment )
+    public void completeEnrollment( String uid )
     {
-
-        ProgramInstance programInstance = programInstanceService.getProgramInstance( enrollment.getEnrollment() );
-        Assert.notNull( programInstance );
-
+        ProgramInstance programInstance = programInstanceService.getProgramInstance( uid );
         programInstanceService.completeProgramInstanceStatus( programInstance );
     }
 

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/EnrollmentService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/EnrollmentService.java	2015-07-08 03:04:42 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/EnrollmentService.java	2015-10-22 04:57:26 +0000
@@ -82,9 +82,11 @@
     // DELETE
     // -------------------------------------------------------------------------
 
-    void deleteEnrollment( Enrollment enrollment );
-
-    void cancelEnrollment( Enrollment enrollment );
-
-    void completeEnrollment( Enrollment enrollment );
+    ImportSummary deleteEnrollment( String uid );
+
+    ImportSummaries deleteEnrollments( List<String> uids );
+
+    void cancelEnrollment( String uid );
+
+    void completeEnrollment( String uid );
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EnrollmentController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EnrollmentController.java	2015-10-20 04:15:40 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EnrollmentController.java	2015-10-22 04:57:26 +0000
@@ -33,11 +33,11 @@
 import org.hisp.dhis.commons.util.TextUtils;
 import org.hisp.dhis.dxf2.events.enrollment.Enrollment;
 import org.hisp.dhis.dxf2.events.enrollment.EnrollmentService;
-import org.hisp.dhis.dxf2.events.enrollment.EnrollmentStatus;
 import org.hisp.dhis.dxf2.importsummary.ImportStatus;
 import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.render.RenderService;
+import org.hisp.dhis.dxf2.webmessage.WebMessageException;
 import org.hisp.dhis.fieldfilter.FieldFilterService;
 import org.hisp.dhis.importexport.ImportStrategy;
 import org.hisp.dhis.node.NodeUtils;
@@ -233,23 +233,27 @@
     @RequestMapping( value = "/{id}/cancelled", method = RequestMethod.PUT )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_PROGRAM_UNENROLLMENT')" )
     @ResponseStatus( HttpStatus.NO_CONTENT )
-    public void cancelEnrollment( @PathVariable String id ) throws NotFoundException
+    public void cancelEnrollment( @PathVariable String id ) throws NotFoundException, WebMessageException
     {
-        Enrollment enrollment = getEnrollment( id );
-        enrollment.setStatus( EnrollmentStatus.CANCELLED );
+        if ( !programInstanceService.programInstanceExists( id ) )
+        {
+            throw new WebMessageException( WebMessageUtils.notFound( "Enrollment not found for ID " + id ) );
+        }
 
-        enrollmentService.cancelEnrollment( enrollment );
+        enrollmentService.cancelEnrollment( id );
     }
 
     @RequestMapping( value = "/{id}/completed", method = RequestMethod.PUT )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_PROGRAM_UNENROLLMENT')" )
     @ResponseStatus( HttpStatus.NO_CONTENT )
-    public void completedEnrollment( @PathVariable String id ) throws NotFoundException
+    public void completedEnrollment( @PathVariable String id ) throws NotFoundException, WebMessageException
     {
-        Enrollment enrollment = getEnrollment( id );
-        enrollment.setStatus( EnrollmentStatus.COMPLETED );
+        if ( !programInstanceService.programInstanceExists( id ) )
+        {
+            throw new WebMessageException( WebMessageUtils.notFound( "Enrollment not found for ID " + id ) );
+        }
 
-        enrollmentService.completeEnrollment( enrollment );
+        enrollmentService.completeEnrollment( id );
     }
 
     // -------------------------------------------------------------------------
@@ -259,10 +263,16 @@
     @RequestMapping( value = "/{id}", method = RequestMethod.DELETE )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_PROGRAM_UNENROLLMENT')" )
     @ResponseStatus( HttpStatus.NO_CONTENT )
-    public void deleteEnrollment( @PathVariable String id ) throws NotFoundException
+    public void deleteEnrollment( @PathVariable String id, HttpServletRequest request, HttpServletResponse response ) throws WebMessageException
     {
-        Enrollment enrollment = getEnrollment( id );
-        enrollmentService.deleteEnrollment( enrollment );
+        if ( !programInstanceService.programInstanceExists( id ) )
+        {
+            throw new WebMessageException( WebMessageUtils.notFound( "Enrollment not found for ID " + id ) );
+        }
+
+        response.setStatus( HttpServletResponse.SC_OK );
+        ImportSummary importSummary = enrollmentService.deleteEnrollment( id );
+        webMessageService.send( WebMessageUtils.importSummary( importSummary ), response, request );
     }
 
     // -------------------------------------------------------------------------