← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11800: (Event API) support getting events by program (single, no reg only), /api/events?program=UID

 

------------------------------------------------------------
revno: 11800
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-08-26 13:46:08 +0200
message:
  (Event API) support getting events by program (single, no reg only), /api/events?program=UID
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/BaseEventService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/EventService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/Events.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/CrudControllerAdvice.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.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/event/BaseEventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/BaseEventService.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/BaseEventService.java	2013-08-26 11:46:08 +0000
@@ -53,9 +53,11 @@
 import org.hisp.dhis.user.CurrentUserService;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 
 /**
@@ -340,38 +342,33 @@
     }
 
     @Override
+    public Events getEvents( Program program )
+    {
+        Events events = new Events();
+
+        ProgramStage programStage = program.getProgramStageByStage( 1 );
+
+        List<ProgramStageInstance> programStageInstances =
+            new ArrayList<ProgramStageInstance>( programStageInstanceService.getProgramStageInstances( programStage ) );
+
+        List<Event> convertedEvents = convertProgramStageInstances( programStageInstances );
+
+        events.setEvents( convertedEvents );
+
+        return events;
+    }
+
+    @Override
     public Event getEvent( String uid )
     {
         ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance( uid );
-
-        if ( programStageInstance == null )
-        {
-            return null;
-        }
-
-        Event event = new Event();
-
-        event.setCompleted( programStageInstance.isCompleted() );
-        event.setEvent( uid );
-        event.setEventDate( programStageInstance.getExecutionDate().toString() );
-        event.setOrgUnit( programStageInstance.getOrganisationUnit().getUid() );
-        event.setProgram( programStageInstance.getProgramInstance().getProgram().getUid() );
-        event.setProgramStage( programStageInstance.getProgramStage().getUid() );
-        event.setStoredBy( programStageInstance.getCompletedUser() );
-
-        Collection<PatientDataValue> patientDataValues = patientDataValueService.getPatientDataValues( programStageInstance );
-
-        for ( PatientDataValue patientDataValue : patientDataValues )
-        {
-            DataValue value = new DataValue();
-            value.setDataElement( patientDataValue.getDataElement().getUid() );
-            value.setValue( patientDataValue.getValue() );
-            value.setProvidedElsewhere( patientDataValue.getProvidedElsewhere() );
-
-            event.getDataValues().add( value );
-        }
-
-        return event;
+        return convertProgramStageInstance( programStageInstance );
+    }
+
+    @Override
+    public Event getEvent( ProgramStageInstance programStageInstance )
+    {
+        return convertProgramStageInstance( programStageInstance );
     }
 
     @Override
@@ -442,4 +439,48 @@
             programStageInstanceService.deleteProgramStageInstance( programStageInstance );
         }
     }
+
+    private List<Event> convertProgramStageInstances( List<ProgramStageInstance> programStageInstances )
+    {
+        List<Event> events = new ArrayList<Event>();
+
+        for ( ProgramStageInstance programStageInstance : programStageInstances )
+        {
+            events.add( convertProgramStageInstance( programStageInstance ) );
+        }
+
+        return events;
+    }
+
+    private Event convertProgramStageInstance( ProgramStageInstance programStageInstance )
+    {
+        if ( programStageInstance == null )
+        {
+            return null;
+        }
+
+        Event event = new Event();
+
+        event.setCompleted( programStageInstance.isCompleted() );
+        event.setEvent( programStageInstance.getUid() );
+        event.setEventDate( programStageInstance.getExecutionDate().toString() );
+        event.setOrgUnit( programStageInstance.getOrganisationUnit().getUid() );
+        event.setProgram( programStageInstance.getProgramInstance().getProgram().getUid() );
+        event.setProgramStage( programStageInstance.getProgramStage().getUid() );
+        event.setStoredBy( programStageInstance.getCompletedUser() );
+
+        Collection<PatientDataValue> patientDataValues = patientDataValueService.getPatientDataValues( programStageInstance );
+
+        for ( PatientDataValue patientDataValue : patientDataValues )
+        {
+            DataValue value = new DataValue();
+            value.setDataElement( patientDataValue.getDataElement().getUid() );
+            value.setValue( patientDataValue.getValue() );
+            value.setProvidedElsewhere( patientDataValue.getProvidedElsewhere() );
+
+            event.getDataValues().add( value );
+        }
+
+        return event;
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/EventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/EventService.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/EventService.java	2013-08-26 11:46:08 +0000
@@ -30,6 +30,8 @@
 
 import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
+import org.hisp.dhis.program.Program;
+import org.hisp.dhis.program.ProgramStageInstance;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -47,8 +49,12 @@
 
     ImportSummaries saveEventsJson( InputStream inputStream ) throws IOException;
 
+    Events getEvents( Program program );
+
     Event getEvent( String uid );
 
+    Event getEvent( ProgramStageInstance programStageInstance );
+
     void updateEvent( Event event );
 
     void deleteEvent( Event event );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/Events.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/Events.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/Events.java	2013-08-26 11:46:08 +0000
@@ -54,7 +54,7 @@
     }
 
     @JsonProperty
-    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0, isAttribute = true )
+    @JacksonXmlProperty( isAttribute = true )
     public String getProgram()
     {
         return program;
@@ -66,7 +66,7 @@
     }
 
     @JsonProperty
-    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0, isAttribute = true )
+    @JacksonXmlProperty( isAttribute = true )
     public String getProgramInstance()
     {
         return programInstance;

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/CrudControllerAdvice.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/CrudControllerAdvice.java	2013-08-23 16:00:30 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/CrudControllerAdvice.java	2013-08-26 11:46:08 +0000
@@ -37,6 +37,9 @@
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.web.client.HttpServerErrorException;
+import org.springframework.web.client.HttpStatusCodeException;
 
 import java.io.IOException;
 
@@ -55,7 +58,7 @@
         return new ResponseEntity<String>( ex.getMessage(), headers, HttpStatus.UNAUTHORIZED );
     }
 
-    @ExceptionHandler( { NotFoundException.class, NotFoundForQueryException.class } )
+    @ExceptionHandler({ NotFoundException.class, NotFoundForQueryException.class })
     public ResponseEntity<String> notFoundExceptionHandler( Exception ex ) throws IOException
     {
         HttpHeaders headers = new HttpHeaders();
@@ -63,4 +66,13 @@
 
         return new ResponseEntity<String>( ex.getMessage(), headers, HttpStatus.NOT_FOUND );
     }
+
+    @ExceptionHandler( { HttpClientErrorException.class, HttpServerErrorException.class } )
+    public ResponseEntity<String> httpClient( HttpStatusCodeException ex )
+    {
+        HttpHeaders headers = new HttpHeaders();
+        headers.add( "Content-Type", MediaType.TEXT_PLAIN_VALUE );
+
+        return new ResponseEntity<String>( ex.getStatusText(), headers, ex.getStatusCode() );
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.java	2013-08-23 16:00:30 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.java	2013-08-26 11:46:08 +0000
@@ -29,12 +29,16 @@
  */
 
 import org.hisp.dhis.api.controller.WebOptions;
+import org.hisp.dhis.api.controller.exception.NotFoundException;
 import org.hisp.dhis.api.utils.ContextUtils;
+import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.dxf2.event.Event;
 import org.hisp.dhis.dxf2.event.EventService;
+import org.hisp.dhis.dxf2.event.Events;
 import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
+import org.hisp.dhis.program.Program;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.security.access.prepost.PreAuthorize;
@@ -45,6 +49,7 @@
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.client.HttpClientErrorException;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -55,7 +60,7 @@
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 @Controller
-@RequestMapping( value = EventController.RESOURCE_PATH )
+@RequestMapping(value = EventController.RESOURCE_PATH)
 public class EventController
 {
     public static final String RESOURCE_PATH = "/events";
@@ -65,12 +70,48 @@
     //--------------------------------------------------------------------------
 
     @Autowired
+    private IdentifiableObjectManager manager;
+
+    @Autowired
     private EventService eventService;
 
     // -------------------------------------------------------------------------
     // Controller
     // -------------------------------------------------------------------------
 
+    @RequestMapping( value = "", method = RequestMethod.GET )
+    public String getEvents( @RequestParam( "program" ) String programUid, @RequestParam Map<String, String> parameters,
+        Model model, HttpServletRequest request, HttpServletResponse response ) throws Exception
+    {
+        WebOptions options = new WebOptions( parameters );
+        Program program = manager.get( Program.class, programUid );
+
+        if ( program == null )
+        {
+            throw new NotFoundException( programUid );
+        }
+
+        if ( program.isRegistration() || !program.isSingleEvent() )
+        {
+            throw new HttpClientErrorException( HttpStatus.BAD_REQUEST, "Only single event with no registration is currently supported." );
+        }
+
+        Events events = eventService.getEvents( program );
+
+        if ( options.hasLinks() )
+        {
+            for ( Event event : events.getEvents() )
+            {
+                event.setHref( ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + event.getEvent() );
+            }
+        }
+
+        model.addAttribute( "model", events );
+        model.addAttribute( "viewClass", options.getViewClass( "detailed" ) );
+
+        return "event";
+    }
+
     @RequestMapping( value = "/{uid}", method = RequestMethod.GET )
     public String getEvent( @PathVariable( "uid" ) String uid, @RequestParam Map<String, String> parameters,
         Model model, HttpServletRequest request, HttpServletResponse response ) throws Exception