← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11913: async/sync event import, wip

 

------------------------------------------------------------
revno: 11913
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2013-09-04 13:58:22 +0200
message:
  async/sync event import, wip
added:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/tasks/
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/tasks/ImportEventTask.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/scheduling/TaskCategory.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/AbstractEventService.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/JacksonEventService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportOptions.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/scheduling/TaskCategory.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/scheduling/TaskCategory.java	2013-08-23 15:56:19 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/scheduling/TaskCategory.java	2013-09-04 11:58:22 +0000
@@ -36,6 +36,7 @@
     DATAMART,
     RESOURCETABLE_UPDATE,
     DATAVALUE_IMPORT,
+    EVENT_IMPORT,
     METADATA_IMPORT,
     METADATA_EXPORT,
     AGGREGATE_QUERY_BUILDER,

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/AbstractEventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/AbstractEventService.java	2013-09-04 11:32:31 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/AbstractEventService.java	2013-09-04 11:58:22 +0000
@@ -89,7 +89,7 @@
     private DataElementService dataElementService;
 
     @Autowired
-    private CurrentUserService currentUserService;
+    protected CurrentUserService currentUserService;
 
     @Autowired
     private PatientDataValueService patientDataValueService;

=== 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-09-03 09:40:41 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/EventService.java	2013-09-04 11:58:22 +0000
@@ -30,10 +30,12 @@
 
 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.organisationunit.OrganisationUnit;
 import org.hisp.dhis.program.Program;
 import org.hisp.dhis.program.ProgramStage;
 import org.hisp.dhis.program.ProgramStageInstance;
+import org.hisp.dhis.scheduling.TaskId;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -49,10 +51,14 @@
 
     ImportSummaries saveEventsXml( InputStream inputStream ) throws IOException;
 
+    ImportSummaries saveEventsXml( InputStream inputStream, TaskId taskId, ImportOptions importOptions ) throws IOException;
+
     ImportSummary saveEventJson( InputStream inputStream ) throws IOException;
 
     ImportSummaries saveEventsJson( InputStream inputStream ) throws IOException;
 
+    ImportSummaries saveEventsJson( InputStream inputStream, TaskId taskId, ImportOptions importOptions ) throws IOException;
+
     Events getEvents( Program program, OrganisationUnit organisationUnit );
 
     Events getEvents( Program program, OrganisationUnit organisationUnit, Date startDate, Date endDate );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/JacksonEventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/JacksonEventService.java	2013-09-04 08:15:14 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/JacksonEventService.java	2013-09-04 11:58:22 +0000
@@ -33,6 +33,11 @@
 import com.fasterxml.jackson.dataformat.xml.XmlMapper;
 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;
+import org.hisp.dhis.system.notification.Notifier;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.StreamUtils;
 
 import java.io.IOException;
@@ -46,6 +51,9 @@
  */
 public class JacksonEventService extends AbstractEventService
 {
+    @Autowired
+    private Notifier notifier;
+
     // -------------------------------------------------------------------------
     // EventService Impl
     // -------------------------------------------------------------------------
@@ -53,25 +61,25 @@
     private static ObjectMapper xmlMapper = new XmlMapper();
     private static ObjectMapper jsonMapper = new ObjectMapper();
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     private static <T> T fromXml( InputStream inputStream, Class<?> clazz ) throws IOException
     {
         return (T) xmlMapper.readValue( inputStream, clazz );
     }
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     private static <T> T fromXml( String input, Class<?> clazz ) throws IOException
     {
         return (T) xmlMapper.readValue( input, clazz );
     }
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     private static <T> T fromJson( InputStream inputStream, Class<?> clazz ) throws IOException
     {
         return (T) jsonMapper.readValue( inputStream, clazz );
     }
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     private static <T> T fromJson( String input, Class<?> clazz ) throws IOException
     {
         return (T) jsonMapper.readValue( input, clazz );
@@ -90,10 +98,18 @@
     @Override
     public ImportSummaries saveEventsXml( InputStream inputStream ) throws IOException
     {
+        return saveEventsXml( inputStream, null, ImportOptions.getDefaultImportOptions() );
+    }
+
+    @Override
+    public ImportSummaries saveEventsXml( 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" );
+
         try
         {
             Events events = fromXml( input, Events.class );
@@ -109,6 +125,9 @@
             importSummaries.getImportSummaries().add( saveEvent( event ) );
         }
 
+        notifier.notify( taskId, NotificationLevel.INFO, "Import done", true ).
+            addTaskSummary( taskId, importSummaries );
+
         return importSummaries;
     }
 
@@ -122,10 +141,18 @@
     @Override
     public ImportSummaries saveEventsJson( InputStream inputStream ) throws IOException
     {
+        return saveEventsJson( inputStream, null, ImportOptions.getDefaultImportOptions() );
+    }
+
+    @Override
+    public ImportSummaries saveEventsJson( 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" );
+
         try
         {
             Events events = fromJson( input, Events.class );
@@ -141,6 +168,9 @@
             importSummaries.getImportSummaries().add( saveEvent( event ) );
         }
 
+        notifier.notify( taskId, NotificationLevel.INFO, "Import done", true ).
+            addTaskSummary( taskId, importSummaries );
+
         return importSummaries;
     }
 

=== added directory 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/tasks'
=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/tasks/ImportEventTask.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/tasks/ImportEventTask.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/tasks/ImportEventTask.java	2013-09-04 11:58:22 +0000
@@ -0,0 +1,91 @@
+package org.hisp.dhis.dxf2.event.tasks;
+
+/*
+ * Copyright (c) 2004-2013, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import org.hisp.dhis.dxf2.event.EventService;
+import org.hisp.dhis.dxf2.metadata.ImportOptions;
+import org.hisp.dhis.scheduling.TaskId;
+import org.hisp.dhis.user.User;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class ImportEventTask
+    implements Runnable
+{
+    private final InputStream inputStream;
+
+    private final EventService eventService;
+
+    private final ImportOptions importOptions;
+
+    private final TaskId taskId;
+
+    private final User user;
+
+    private final boolean jsonInput;
+
+    public ImportEventTask( InputStream inputStream, EventService eventService, ImportOptions importOptions, TaskId taskId, User user, boolean jsonInput )
+    {
+        this.inputStream = inputStream;
+        this.eventService = eventService;
+        this.importOptions = importOptions;
+        this.taskId = taskId;
+        this.user = user;
+        this.jsonInput = jsonInput;
+    }
+
+    @Override
+    public void run()
+    {
+        if ( jsonInput )
+        {
+            try
+            {
+                eventService.saveEventsJson( inputStream, taskId, importOptions );
+            }
+            catch ( IOException ignored )
+            {
+            }
+        }
+        else
+        {
+            try
+            {
+                eventService.saveEventsXml( inputStream, taskId, importOptions );
+            }
+            catch ( IOException ignored )
+            {
+            }
+        }
+    }
+}

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportOptions.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportOptions.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportOptions.java	2013-09-04 11:58:22 +0000
@@ -42,7 +42,7 @@
 
     private boolean dryRun;
 
-    private boolean async = true;
+    private boolean async;
 
     private ImportStrategy importStrategy;
 

=== 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-09-04 08:20:58 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.java	2013-09-04 11:58:22 +0000
@@ -35,17 +35,25 @@
 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.event.tasks.ImportEventTask;
 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.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.program.Program;
 import org.hisp.dhis.program.ProgramStage;
+import org.hisp.dhis.scheduling.TaskCategory;
+import org.hisp.dhis.scheduling.TaskId;
+import org.hisp.dhis.system.scheduling.Scheduler;
 import org.hisp.dhis.system.util.StreamUtils;
+import org.hisp.dhis.user.CurrentUserService;
+import org.hisp.dhis.user.User;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.format.annotation.DateTimeFormat;
 import org.springframework.http.HttpStatus;
 import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.security.authentication.AuthenticationManager;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.PathVariable;
@@ -79,8 +87,17 @@
     private IdentifiableObjectManager manager;
 
     @Autowired
+    private CurrentUserService currentUserService;
+
+    @Autowired
+    private Scheduler scheduler;
+
+    @Autowired
     private EventService eventService;
 
+    @Autowired
+    private AuthenticationManager authenticationManager;
+
     // -------------------------------------------------------------------------
     // Controller
     // -------------------------------------------------------------------------
@@ -179,44 +196,69 @@
 
     @RequestMapping( method = RequestMethod.POST, consumes = "application/xml" )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_PATIENT_DATAVALUE_ADD')" )
-    public void postXmlEvent( HttpServletResponse response, HttpServletRequest request ) throws Exception
+    public void postXmlEvent( HttpServletResponse response, HttpServletRequest request, ImportOptions importOptions ) throws Exception
     {
         InputStream inputStream = StreamUtils.wrapAndCheckCompressionFormat( request.getInputStream() );
-        ImportSummaries importSummaries = eventService.saveEventsXml( inputStream );
-
-        for ( ImportSummary importSummary : importSummaries.getImportSummaries() )
-        {
-            importSummary.setHref( ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + importSummary.getReference() );
-        }
-
-        if ( importSummaries.getImportSummaries().size() == 1 )
-        {
-            ImportSummary importSummary = importSummaries.getImportSummaries().get( 0 );
-            response.setHeader( "Location", ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + importSummary.getReference() );
-        }
-
-        JacksonUtils.toXml( response.getOutputStream(), importSummaries );
+
+        if ( !importOptions.isAsync() )
+        {
+            ImportSummaries importSummaries = eventService.saveEventsXml( inputStream );
+
+            for ( ImportSummary importSummary : importSummaries.getImportSummaries() )
+            {
+                importSummary.setHref( ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + importSummary.getReference() );
+            }
+
+            if ( importSummaries.getImportSummaries().size() == 1 )
+            {
+                ImportSummary importSummary = importSummaries.getImportSummaries().get( 0 );
+                response.setHeader( "Location", ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + importSummary.getReference() );
+            }
+
+            JacksonUtils.toXml( response.getOutputStream(), importSummaries );
+        }
+        else
+        {
+            User currentUser = currentUserService.getCurrentUser();
+            TaskId taskId = new TaskId( TaskCategory.EVENT_IMPORT, currentUser );
+            scheduler.executeTask( new ImportEventTask( inputStream, eventService, importOptions, taskId, currentUser, false ) );
+            response.setHeader( "Location", ContextUtils.getRootPath( request ) + "/system/tasks/" + TaskCategory.EVENT_IMPORT );
+            response.setStatus( HttpServletResponse.SC_NO_CONTENT );
+        }
     }
 
     @RequestMapping( method = RequestMethod.POST, consumes = "application/json" )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_PATIENT_DATAVALUE_ADD')" )
-    public void postJsonEvent( HttpServletResponse response, HttpServletRequest request ) throws Exception
+    public void postJsonEvent( HttpServletResponse response, HttpServletRequest request, ImportOptions importOptions ) throws Exception
     {
         InputStream inputStream = StreamUtils.wrapAndCheckCompressionFormat( request.getInputStream() );
-        ImportSummaries importSummaries = eventService.saveEventsJson( inputStream );
-
-        for ( ImportSummary importSummary : importSummaries.getImportSummaries() )
-        {
-            importSummary.setHref( ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + importSummary.getReference() );
-        }
-
-        if ( importSummaries.getImportSummaries().size() == 1 )
-        {
-            ImportSummary importSummary = importSummaries.getImportSummaries().get( 0 );
-            response.setHeader( "Location", ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + importSummary.getReference() );
-        }
-
-        JacksonUtils.toJson( response.getOutputStream(), importSummaries );
+
+        if ( !importOptions.isAsync() )
+        {
+            ImportSummaries importSummaries = eventService.saveEventsJson( inputStream );
+
+            for ( ImportSummary importSummary : importSummaries.getImportSummaries() )
+            {
+                importSummary.setHref( ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + importSummary.getReference() );
+            }
+
+            if ( importSummaries.getImportSummaries().size() == 1 )
+            {
+                ImportSummary importSummary = importSummaries.getImportSummaries().get( 0 );
+                response.setHeader( "Location", ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + importSummary.getReference() );
+            }
+
+            JacksonUtils.toJson( response.getOutputStream(), importSummaries );
+        }
+        else
+        {
+            User currentUser = currentUserService.getCurrentUser();
+            TaskId taskId = new TaskId( TaskCategory.EVENT_IMPORT, currentUser );
+            scheduler.executeTask( new ImportEventTask( inputStream, eventService, importOptions, taskId, currentUser, true ) );
+            response.setHeader( "Location", ContextUtils.getRootPath( request ) + "/system/tasks/" + TaskCategory.EVENT_IMPORT );
+            response.setStatus( HttpServletResponse.SC_NO_CONTENT );
+        }
+
     }
 
     @RequestMapping( value = "/{uid}", method = RequestMethod.DELETE )