← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13018: support includeChildren/includeDescendants in event-api, remove requirement for program/programSt...

 

------------------------------------------------------------
revno: 13018
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-11-25 15:54:19 +0100
message:
  support includeChildren/includeDescendants in event-api, remove requirement for program/programStage, only orgUnit is now required
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectUtils.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventService.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-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectUtils.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectUtils.java	2013-10-04 10:59:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectUtils.java	2013-11-25 14:54:19 +0000
@@ -257,24 +257,27 @@
         {
             for ( IdentifiableObject identifiableObject : identifiableObjects )
             {
-                integers.add( identifiableObject.getId() );
+                if ( identifiableObject != null )
+                {
+                    integers.add( identifiableObject.getId() );
+                }
             }
         }
 
         return integers;
     }
-    
+
     /**
      * Returns a mapping between the uid and the name of the given identifiable
      * objects.
-     * 
+     *
      * @param objects the identifiable objects.
      * @return mapping between the uid and the name of the given objects.
      */
     public static Map<String, String> getUidNameMap( Collection<? extends IdentifiableObject> objects )
     {
         Map<String, String> map = new HashMap<String, String>();
-        
+
         if ( objects != null )
         {
             for ( IdentifiableObject object : objects )
@@ -282,7 +285,7 @@
                 map.put( object.getUid(), object.getDisplayName() );
             }
         }
-        
+
         return map;
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java	2013-11-25 06:30:47 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java	2013-11-25 14:54:19 +0000
@@ -420,6 +420,16 @@
     }
 
     @Override
+    public Events getEvents( List<Program> programs, List<ProgramStage> programStages, List<OrganisationUnit> organisationUnits, Person person, Date startDate, Date endDate )
+    {
+        List<Event> eventList = eventStore.getAll( programs, programStages, organisationUnits, person, startDate, endDate );
+        Events events = new Events();
+        events.setEvents( eventList );
+
+        return events;
+    }
+
+    @Override
     public Event getEvent( String uid )
     {
         ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance( uid );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventService.java	2013-11-19 10:02:44 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventService.java	2013-11-25 14:54:19 +0000
@@ -77,6 +77,8 @@
 
     Events getEvents( List<Program> programs, List<ProgramStage> programStages, List<OrganisationUnit> organisationUnits, Date startDate, Date endDate );
 
+    Events getEvents( List<Program> programs, List<ProgramStage> programStages, List<OrganisationUnit> organisationUnits, Person person, Date startDate, Date endDate );
+
     Event getEvent( String uid );
 
     Event getEvent( ProgramStageInstance programStageInstance );

=== 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-11-25 10:48:06 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.java	2013-11-25 14:54:19 +0000
@@ -44,6 +44,7 @@
 import org.hisp.dhis.dxf2.metadata.ImportOptions;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.program.Program;
 import org.hisp.dhis.program.ProgramStage;
 import org.hisp.dhis.scheduling.TaskCategory;
@@ -62,13 +63,15 @@
 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;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Date;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -99,6 +102,9 @@
     @Autowired
     private PersonService personService;
 
+    @Autowired
+    private OrganisationUnitService organisationUnitService;
+
     // -------------------------------------------------------------------------
     // READ
     // -------------------------------------------------------------------------
@@ -110,22 +116,19 @@
         @RequestParam( value = "programStage", required = false ) String programStageUid,
         @RequestParam( value = "person", required = false ) String personUid,
         @RequestParam( value = "orgUnit" ) String orgUnitUid,
-        @RequestParam( required =  false ) @DateTimeFormat( pattern = "yyyy-MM-dd" ) Date startDate,
-        @RequestParam( required =  false ) @DateTimeFormat( pattern = "yyyy-MM-dd" ) Date endDate,
+        @RequestParam( value = "includeChildren", required = false, defaultValue = "false" ) boolean includeChildren,
+        @RequestParam( value = "includeDescendants", required = false, defaultValue = "false" ) boolean includeDescendants,
+        @RequestParam( required = false ) @DateTimeFormat( pattern = "yyyy-MM-dd" ) Date startDate,
+        @RequestParam( required = false ) @DateTimeFormat( pattern = "yyyy-MM-dd" ) Date endDate,
         @RequestParam Map<String, String> parameters, Model model, HttpServletRequest request ) throws NotFoundException
     {
         WebOptions options = new WebOptions( parameters );
         Program program = manager.get( Program.class, programUid );
         ProgramStage programStage = manager.get( ProgramStage.class, programStageUid );
-        OrganisationUnit organisationUnit;
+        List<OrganisationUnit> organisationUnits = new ArrayList<OrganisationUnit>();
+        OrganisationUnit rootOrganisationUnit;
         Person person = null;
 
-        if ( program == null && programStage == null )
-        {
-            throw new HttpClientErrorException( HttpStatus.BAD_REQUEST,
-                "Both program and programStage is invalid or missing, needs at least one." );
-        }
-
         if ( personUid != null )
         {
             person = personService.getPerson( personUid );
@@ -136,60 +139,40 @@
             }
         }
 
-        organisationUnit = manager.get( OrganisationUnit.class, orgUnitUid );
+        rootOrganisationUnit = manager.get( OrganisationUnit.class, orgUnitUid );
 
-        if ( organisationUnit == null )
+        if ( rootOrganisationUnit == null )
         {
             try
             {
-                organisationUnit = manager.get( OrganisationUnit.class, Integer.parseInt( orgUnitUid ) );
+                rootOrganisationUnit = manager.get( OrganisationUnit.class, Integer.parseInt( orgUnitUid ) );
             }
             catch ( NumberFormatException ignored )
             {
             }
         }
 
-        if ( organisationUnit == null )
+        if ( rootOrganisationUnit == null )
         {
             throw new NotFoundException( "OrganisationUnit", programUid );
         }
 
-        Events events;
-
-        if ( program != null && programStage != null )
+        if ( includeDescendants )
         {
-            if ( person != null )
-            {
-                events = eventService.getEvents( program, programStage, organisationUnit, person, startDate, endDate );
-            }
-            else
-            {
-                events = eventService.getEvents( program, programStage, organisationUnit, startDate, endDate );
-            }
+            organisationUnits.addAll( organisationUnitService.getOrganisationUnitsWithChildren( rootOrganisationUnit.getUid() ) );
         }
-        else if ( program != null )
+        else if ( includeChildren )
         {
-            if ( person != null )
-            {
-                events = eventService.getEvents( program, organisationUnit, person, startDate, endDate );
-            }
-            else
-            {
-                events = eventService.getEvents( program, organisationUnit, startDate, endDate );
-            }
+            organisationUnits.add( rootOrganisationUnit );
+            organisationUnits.addAll( rootOrganisationUnit.getChildren() );
         }
         else
         {
-            if ( person != null )
-            {
-                events = eventService.getEvents( programStage, organisationUnit, person, startDate, endDate );
-            }
-            else
-            {
-                events = eventService.getEvents( programStage, organisationUnit, startDate, endDate );
-            }
+            organisationUnits.add( rootOrganisationUnit );
         }
 
+        Events events = eventService.getEvents( Arrays.asList( program ), Arrays.asList( programStage ), organisationUnits, person, startDate, endDate );
+
         if ( options.hasLinks() )
         {
             for ( Event event : events.getEvents() )