← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17933: strategy support for event imports

 

------------------------------------------------------------
revno: 17933
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2015-01-10 19:53:00 +0700
message:
  strategy support for event imports
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/event/AbstractEventService.java
  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/JacksonEventService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/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/events/enrollment/AbstractEnrollmentService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java	2015-01-10 11:48:14 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java	2015-01-10 12:53:00 +0000
@@ -245,7 +245,7 @@
         return getEnrollments(
             programInstanceService.getProgramInstances( program, IdentifiableObjectUtils.getIdentifiers( organisationUnits ), startDate, endDate, 0, Integer.MAX_VALUE ) );
     }
-    
+
     @Override
     public Enrollments getEnrollments( Program program, TrackedEntityInstance trackedEntityInstance )
     {

=== 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	2014-12-08 00:52:23 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java	2015-01-10 12:53:00 +0000
@@ -28,19 +28,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import static org.hisp.dhis.system.notification.NotificationLevel.ERROR;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hibernate.SessionFactory;
@@ -83,8 +72,18 @@
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.Assert;
 
-import com.fasterxml.jackson.core.type.TypeReference;
-import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.hisp.dhis.system.notification.NotificationLevel.ERROR;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -180,12 +179,12 @@
         try
         {
             ImportSummaries importSummaries = addEvents( events, importOptions );
-    
+
             if ( taskId != null )
             {
                 notifier.notify( taskId, NotificationLevel.INFO, "Import done", true ).addTaskSummary( taskId, importSummaries );
             }
-            
+
             return importSummaries;
         }
         catch ( RuntimeException ex )
@@ -194,7 +193,7 @@
             notifier.notify( taskId, ERROR, "Process failed: " + ex.getMessage(), true );
             return new ImportSummaries().addImportSummary( new ImportSummary( ImportStatus.ERROR, "The import process failed: " + ex.getMessage() ) );
         }
-        
+
     }
 
     @Override
@@ -405,6 +404,15 @@
     // -------------------------------------------------------------------------
 
     @Override
+    public void updateEvents( List<Event> events, boolean singleValue )
+    {
+        for ( Event event : events )
+        {
+            updateEvent( event, singleValue );
+        }
+    }
+
+    @Override
     public void updateEvent( Event event, boolean singleValue )
     {
         updateEvent( event, singleValue, null );

=== 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	2014-10-08 06:12:13 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventService.java	2015-01-10 12:53:00 +0000
@@ -75,22 +75,10 @@
 
     ImportSummaries addEvents( List<Event> events, ImportOptions importOptions, TaskId taskId );
 
-    ImportSummary addEventXml( InputStream inputStream ) throws IOException;
-
-    ImportSummary addEventXml( InputStream inputStream, ImportOptions importOptions ) throws IOException;
-
-    ImportSummaries addEventsXml( InputStream inputStream ) throws IOException;
-
     ImportSummaries addEventsXml( InputStream inputStream, ImportOptions importOptions ) throws IOException;
 
     ImportSummaries addEventsXml( InputStream inputStream, TaskId taskId, ImportOptions importOptions ) throws IOException;
 
-    ImportSummary addEventJson( InputStream inputStream ) throws IOException;
-
-    ImportSummary addEventJson( InputStream inputStream, ImportOptions importOptions ) throws IOException;
-
-    ImportSummaries addEventsJson( InputStream inputStream ) throws IOException;
-
     ImportSummaries addEventsJson( InputStream inputStream, ImportOptions importOptions ) throws IOException;
 
     ImportSummaries addEventsJson( InputStream inputStream, TaskId taskId, ImportOptions importOptions ) throws IOException;
@@ -101,6 +89,8 @@
 
     void updateEvent( Event event, boolean singleValue );
 
+    void updateEvents( List<Event> events, boolean singleValue );
+
     void updateEvent( Event event, boolean singleValue, ImportOptions importOptions );
 
     void updateEventForNote( Event event );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JacksonEventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JacksonEventService.java	2015-01-03 10:15:57 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JacksonEventService.java	2015-01-10 12:53:00 +0000
@@ -34,7 +34,6 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
-import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.metadata.ImportOptions;
 import org.hisp.dhis.scheduling.TaskId;
 import org.hisp.dhis.system.notification.NotificationLevel;
@@ -42,10 +41,13 @@
 import org.hisp.dhis.system.timer.Timer;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.StreamUtils;
+import org.springframework.util.StringUtils;
 
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * Implementation of EventService that uses Jackson for serialization and deserialization.
@@ -100,12 +102,6 @@
     }
 
     @Override
-    public ImportSummaries addEventsXml( InputStream inputStream ) throws IOException
-    {
-        return addEventsXml( inputStream, null, null );
-    }
-
-    @Override
     public ImportSummaries addEventsXml( InputStream inputStream, ImportOptions importOptions ) throws IOException
     {
         return addEventsXml( inputStream, null, importOptions );
@@ -114,60 +110,21 @@
     @Override
     public ImportSummaries addEventsXml( InputStream inputStream, TaskId taskId, ImportOptions importOptions ) throws IOException
     {
-        ImportSummaries importSummaries = new ImportSummaries();
-
         String input = StreamUtils.copyToString( inputStream, Charset.forName( "UTF-8" ) );
-
-        notifier.clear( taskId ).notify( taskId, "Importing events" );
-
-        Timer timer = new SystemTimer().start();
-        Events events = new Events();
+        List<Event> events = new ArrayList<>();
 
         try
         {
             Events fromXml = fromXml( input, Events.class );
-            events.getEvents().addAll( fromXml.getEvents() );
+            events.addAll( fromXml.getEvents() );
         }
         catch ( Exception ex )
         {
-            Event event = fromXml( input, Event.class );
-            events.getEvents().add( event );
-        }
-
-        importSummaries = addEvents( events.getEvents(), importOptions );
-
-        timer.stop();
-
-        if ( taskId != null )
-        {
-            notifier.notify( taskId, NotificationLevel.INFO, "Import done. Completed in " + timer.toString() + ".", true ).
-                addTaskSummary( taskId, importSummaries );
-        }
-        else
-        {
-            log.info( "Import done. Completed in " + timer.toString() + "." );
-        }
-
-        return importSummaries;
-    }
-
-    @Override
-    public ImportSummary addEventXml( InputStream inputStream ) throws IOException
-    {
-        return addEventXml( inputStream, null );
-    }
-
-    @Override
-    public ImportSummary addEventXml( InputStream inputStream, ImportOptions importOptions ) throws IOException
-    {
-        Event event = fromXml( inputStream, Event.class );
-        return addEvent( event, importOptions );
-    }
-
-    @Override
-    public ImportSummaries addEventsJson( InputStream inputStream ) throws IOException
-    {
-        return addEventsJson( inputStream, null, null );
+            Event fromXml = fromXml( input, Event.class );
+            events.add( fromXml );
+        }
+
+        return addEvents( events, taskId, importOptions );
     }
 
     @Override
@@ -179,29 +136,61 @@
     @Override
     public ImportSummaries addEventsJson( InputStream inputStream, TaskId taskId, ImportOptions importOptions ) throws IOException
     {
-        ImportSummaries importSummaries = new ImportSummaries();
-
         String input = StreamUtils.copyToString( inputStream, Charset.forName( "UTF-8" ) );
-
-        notifier.clear( taskId ).notify( taskId, "Importing events" );
-
-        Timer timer = new SystemTimer().start();
-        Events events = new Events();
+        List<Event> events = new ArrayList<>();
 
         try
         {
             Events fromJson = fromJson( input, Events.class );
-            events.getEvents().addAll( fromJson.getEvents() );
+            events.addAll( fromJson.getEvents() );
         }
         catch ( Exception ex )
         {
-            Event event = fromJson( input, Event.class );
-            events.getEvents().add( event );
-        }
-
-        importSummaries = addEvents( events.getEvents(), importOptions );
-
-        timer.stop();
+            Event fromJson = fromJson( input, Event.class );
+            events.add( fromJson );
+        }
+
+        return addEvents( events, taskId, importOptions );
+    }
+
+    private ImportSummaries addEvents( List<Event> events, TaskId taskId, ImportOptions importOptions )
+    {
+        ImportSummaries importSummaries;
+
+        notifier.clear( taskId ).notify( taskId, "Importing events" );
+        Timer timer = new SystemTimer().start();
+
+        List<Event> create = new ArrayList<>();
+        List<Event> update = new ArrayList<>();
+
+        if ( importOptions.getImportStrategy().isCreate() )
+        {
+            create.addAll( events );
+        }
+        else if ( importOptions.getImportStrategy().isCreateAndUpdate() )
+        {
+            for ( Event event : events )
+            {
+                if ( StringUtils.isEmpty( event.getEvent() ) )
+                {
+                    create.add( event );
+                }
+                else
+                {
+                    if ( programStageInstanceService.getProgramStageInstance( event.getEvent() ) == null )
+                    {
+                        create.add( event );
+                    }
+                    else
+                    {
+                        update.add( event );
+                    }
+                }
+            }
+        }
+
+        importSummaries = addEvents( create, importOptions );
+        updateEvents( update, false );
 
         if ( taskId != null )
         {
@@ -215,17 +204,4 @@
 
         return importSummaries;
     }
-
-    @Override
-    public ImportSummary addEventJson( InputStream inputStream ) throws IOException
-    {
-        return addEventJson( inputStream, null );
-    }
-
-    @Override
-    public ImportSummary addEventJson( InputStream inputStream, ImportOptions importOptions ) throws IOException
-    {
-        Event event = fromJson( inputStream, Event.class );
-        return addEvent( event, importOptions );
-    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java	2015-01-10 11:02:17 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java	2015-01-10 12:53:00 +0000
@@ -430,6 +430,7 @@
     @PreAuthorize( "hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_ADD')" )
     public void postXmlEvent( @RequestParam( defaultValue = "CREATE" ) ImportStrategy strategy, HttpServletResponse response, HttpServletRequest request, ImportOptions importOptions ) throws Exception
     {
+        importOptions.setImportStrategy( strategy.name() );
         InputStream inputStream = StreamUtils.wrapAndCheckCompressionFormat( request.getInputStream() );
 
         if ( !importOptions.isAsync() )
@@ -475,6 +476,7 @@
     @PreAuthorize( "hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_ADD')" )
     public void postJsonEvent( @RequestParam( defaultValue = "CREATE" ) ImportStrategy strategy, HttpServletResponse response, HttpServletRequest request, ImportOptions importOptions ) throws Exception
     {
+        importOptions.setImportStrategy( strategy.name() );
         InputStream inputStream = StreamUtils.wrapAndCheckCompressionFormat( request.getInputStream() );
 
         if ( !importOptions.isAsync() )