← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11774: support sync/async import of meta-data through /api/metaData?async=true/false (true by default)

 

------------------------------------------------------------
revno: 11774
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-08-23 14:56:46 +0200
message:
  support sync/async import of meta-data through /api/metaData?async=true/false (true by default)
modified:
  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/MetaDataController.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/metadata/ImportOptions.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportOptions.java	2013-02-13 00:45:31 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportOptions.java	2013-08-23 12:56:46 +0000
@@ -39,7 +39,9 @@
 
     private IdentifiableProperty orgUnitIdScheme;
 
-    private boolean dryRun = false;
+    private boolean dryRun;
+
+    private boolean async = true;
 
     private ImportStrategy importStrategy;
 
@@ -118,6 +120,16 @@
         this.dryRun = dryRun;
     }
 
+    public boolean isAsync()
+    {
+        return async;
+    }
+
+    public void setAsync( boolean async )
+    {
+        this.async = async;
+    }
+
     public void setStrategy( String strategy )
     {
         this.importStrategy = strategy != null ? ImportStrategy.valueOf( strategy.toUpperCase() ) : null;
@@ -137,6 +149,6 @@
     public String toString()
     {
         return "[data element id scheme: " + dataElementIdScheme + ", org unit id scheme: " +
-            orgUnitIdScheme + ", dry run: " + dryRun + ", strategy: " + importStrategy + ", skip check: " + skipExistingCheck + "]";
+            orgUnitIdScheme + ", dry run: " + dryRun + ", async: " + async + ", strategy: " + importStrategy + ", skip check: " + skipExistingCheck + "]";
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MetaDataController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MetaDataController.java	2013-06-13 03:13:26 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MetaDataController.java	2013-08-23 12:56:46 +0000
@@ -27,23 +27,13 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.io.IOException;
-import java.util.Map;
-import java.util.zip.GZIPInputStream;
-import java.util.zip.GZIPOutputStream;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
-import java.util.zip.ZipOutputStream;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
 import org.hisp.dhis.api.utils.ContextUtils;
 import org.hisp.dhis.api.utils.ContextUtils.CacheStrategy;
 import org.hisp.dhis.common.view.ExportView;
 import org.hisp.dhis.dxf2.metadata.ExportService;
 import org.hisp.dhis.dxf2.metadata.ImportOptions;
 import org.hisp.dhis.dxf2.metadata.ImportService;
+import org.hisp.dhis.dxf2.metadata.ImportSummary;
 import org.hisp.dhis.dxf2.metadata.MetaData;
 import org.hisp.dhis.dxf2.metadata.tasks.ImportMetaDataTask;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
@@ -59,6 +49,16 @@
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.Map;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+import java.util.zip.ZipOutputStream;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -226,12 +226,14 @@
     {
         MetaData metaData = JacksonUtils.fromXml( request.getInputStream(), MetaData.class );
 
-        TaskId taskId = new TaskId( TaskCategory.METADATA_IMPORT, currentUserService.getCurrentUser() );
-
-        scheduler.executeTask( new ImportMetaDataTask( currentUserService.getCurrentUser().getUid(), importService, importOptions, taskId, metaData ) );
-
-        response.setHeader( "Location", ContextUtils.getRootPath( request ) + "/system/tasks/" + TaskCategory.METADATA_IMPORT );
-        response.setStatus( HttpServletResponse.SC_NO_CONTENT );
+        if ( importOptions.isAsync() )
+        {
+            startAsyncImport( importOptions, response, request, metaData );
+        }
+        else
+        {
+            startSyncImportXml( importOptions, response, metaData );
+        }
     }
 
     @RequestMapping( value = MetaDataController.RESOURCE_PATH, method = RequestMethod.POST, consumes = "application/json" )
@@ -240,12 +242,14 @@
     {
         MetaData metaData = JacksonUtils.fromJson( request.getInputStream(), MetaData.class );
 
-        TaskId taskId = new TaskId( TaskCategory.METADATA_IMPORT, currentUserService.getCurrentUser() );
-
-        scheduler.executeTask( new ImportMetaDataTask( currentUserService.getCurrentUser().getUid(), importService, importOptions, taskId, metaData ) );
-
-        response.setHeader( "Location", ContextUtils.getRootPath( request ) + "/system/tasks/" + TaskCategory.METADATA_IMPORT );
-        response.setStatus( HttpServletResponse.SC_NO_CONTENT );
+        if ( importOptions.isAsync() )
+        {
+            startAsyncImport( importOptions, response, request, metaData );
+        }
+        else
+        {
+            startSyncImportJson( importOptions, response, metaData );
+        }
     }
 
     @RequestMapping( value = { MetaDataController.RESOURCE_PATH + ".zip", MetaDataController.RESOURCE_PATH + ".xml.zip" }, method = RequestMethod.POST, consumes = { "application/xml", "text/*" } )
@@ -257,12 +261,14 @@
 
         MetaData metaData = JacksonUtils.fromXml( zip, MetaData.class );
 
-        TaskId taskId = new TaskId( TaskCategory.METADATA_IMPORT, currentUserService.getCurrentUser() );
-
-        scheduler.executeTask( new ImportMetaDataTask( currentUserService.getCurrentUser().getUid(), importService, importOptions, taskId, metaData ) );
-
-        response.setHeader( "Location", ContextUtils.getRootPath( request ) + "/system/tasks/" + TaskCategory.METADATA_IMPORT );
-        response.setStatus( HttpServletResponse.SC_NO_CONTENT );
+        if ( importOptions.isAsync() )
+        {
+            startAsyncImport( importOptions, response, request, metaData );
+        }
+        else
+        {
+            startSyncImportXml( importOptions, response, metaData );
+        }
     }
 
     @RequestMapping( value = { MetaDataController.RESOURCE_PATH + ".zip", MetaDataController.RESOURCE_PATH + ".json.zip" }, method = RequestMethod.POST, consumes = "application/json" )
@@ -274,12 +280,14 @@
 
         MetaData metaData = JacksonUtils.fromJson( zip, MetaData.class );
 
-        TaskId taskId = new TaskId( TaskCategory.METADATA_IMPORT, currentUserService.getCurrentUser() );
-
-        scheduler.executeTask( new ImportMetaDataTask( currentUserService.getCurrentUser().getUid(), importService, importOptions, taskId, metaData ) );
-
-        response.setHeader( "Location", ContextUtils.getRootPath( request ) + "/system/tasks/" + TaskCategory.METADATA_IMPORT );
-        response.setStatus( HttpServletResponse.SC_NO_CONTENT );
+        if ( importOptions.isAsync() )
+        {
+            startAsyncImport( importOptions, response, request, metaData );
+        }
+        else
+        {
+            startSyncImportJson( importOptions, response, metaData );
+        }
     }
 
     @RequestMapping( value = { MetaDataController.RESOURCE_PATH + ".gz", MetaDataController.RESOURCE_PATH + ".xml.gz" }, method = RequestMethod.POST, consumes = { "application/xml", "text/*" } )
@@ -289,12 +297,14 @@
         GZIPInputStream gzip = new GZIPInputStream( request.getInputStream() );
         MetaData metaData = JacksonUtils.fromXml( gzip, MetaData.class );
 
-        TaskId taskId = new TaskId( TaskCategory.METADATA_IMPORT, currentUserService.getCurrentUser() );
-
-        scheduler.executeTask( new ImportMetaDataTask( currentUserService.getCurrentUser().getUid(), importService, importOptions, taskId, metaData ) );
-
-        response.setHeader( "Location", ContextUtils.getRootPath( request ) + "/system/tasks/" + TaskCategory.METADATA_IMPORT );
-        response.setStatus( HttpServletResponse.SC_NO_CONTENT );
+        if ( importOptions.isAsync() )
+        {
+            startAsyncImport( importOptions, response, request, metaData );
+        }
+        else
+        {
+            startSyncImportXml( importOptions, response, metaData );
+        }
     }
 
     @RequestMapping( value = { MetaDataController.RESOURCE_PATH + ".gz", MetaDataController.RESOURCE_PATH + ".json.gz" }, method = RequestMethod.POST, consumes = "application/json" )
@@ -304,9 +314,47 @@
         GZIPInputStream gzip = new GZIPInputStream( request.getInputStream() );
         MetaData metaData = JacksonUtils.fromJson( gzip, MetaData.class );
 
+        if ( importOptions.isAsync() )
+        {
+            startAsyncImport( importOptions, response, request, metaData );
+        }
+        else
+        {
+            startSyncImportJson( importOptions, response, metaData );
+        }
+    }
+
+    //--------------------------------------------------------------------------
+    // Helpers
+    //--------------------------------------------------------------------------
+
+    private void startSyncImportXml( ImportOptions importOptions, HttpServletResponse response, MetaData metaData ) throws IOException
+    {
+        String userUid = currentUserService.getCurrentUser().getUid();
+
+        ImportSummary importSummary = importService.importMetaData( userUid, metaData, importOptions, null );
+        response.setStatus( HttpServletResponse.SC_OK );
+        response.setContentType( ContextUtils.CONTENT_TYPE_XML );
+        JacksonUtils.toXml( response.getOutputStream(), importSummary );
+    }
+
+    private void startSyncImportJson( ImportOptions importOptions, HttpServletResponse response, MetaData metaData ) throws IOException
+    {
+        String userUid = currentUserService.getCurrentUser().getUid();
+
+        ImportSummary importSummary = importService.importMetaData( userUid, metaData, importOptions, null );
+        response.setStatus( HttpServletResponse.SC_OK );
+        response.setContentType( ContextUtils.CONTENT_TYPE_JSON );
+        JacksonUtils.toJson( response.getOutputStream(), importSummary );
+    }
+
+    private void startAsyncImport( ImportOptions importOptions, HttpServletResponse response, HttpServletRequest request, MetaData metaData )
+    {
+        String userUid = currentUserService.getCurrentUser().getUid();
+
         TaskId taskId = new TaskId( TaskCategory.METADATA_IMPORT, currentUserService.getCurrentUser() );
 
-        scheduler.executeTask( new ImportMetaDataTask( currentUserService.getCurrentUser().getUid(), importService, importOptions, taskId, metaData ) );
+        scheduler.executeTask( new ImportMetaDataTask( userUid, importService, importOptions, taskId, metaData ) );
 
         response.setHeader( "Location", ContextUtils.getRootPath( request ) + "/system/tasks/" + TaskCategory.METADATA_IMPORT );
         response.setStatus( HttpServletResponse.SC_NO_CONTENT );