← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20062: Web API. Impl support for importing meta-data from a remote URL / JSOn document.

 

------------------------------------------------------------
revno: 20062
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-09-09 14:28:08 +0200
message:
  Web API. Impl support for importing meta-data from a remote URL / JSOn document.
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/synch/DataSynchronizationTask.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/synch/DefaultSynchronizationManager.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/synch/SynchronizationManager.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SynchronizationController.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/synch/DataSynchronizationTask.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/synch/DataSynchronizationTask.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/synch/DataSynchronizationTask.java	2015-09-09 12:28:08 +0000
@@ -60,7 +60,7 @@
     {
         try
         {
-            synchronizationManager.executeDataSynch();
+            synchronizationManager.executeDataPush();
         }
         catch ( RuntimeException ex )
         {

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/synch/DefaultSynchronizationManager.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/synch/DefaultSynchronizationManager.java	2015-06-15 13:44:20 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/synch/DefaultSynchronizationManager.java	2015-09-09 12:28:08 +0000
@@ -36,9 +36,14 @@
 import org.hisp.dhis.dxf2.datavalueset.DataValueSetService;
 import org.hisp.dhis.dxf2.importsummary.ImportStatus;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
+import org.hisp.dhis.dxf2.metadata.ImportService;
+import org.hisp.dhis.dxf2.metadata.MetaData;
 import org.hisp.dhis.dxf2.common.IdSchemes;
 import org.hisp.dhis.dxf2.common.ImportSummaryResponseExtractor;
+import org.hisp.dhis.dxf2.common.JacksonUtils;
 import org.hisp.dhis.setting.SystemSettingManager;
+import org.hisp.dhis.user.CurrentUserService;
+import org.hisp.dhis.user.User;
 import org.hisp.dhis.commons.util.CodecUtils;
 import org.joda.time.DateTime;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -84,6 +89,12 @@
     private ConfigurationService configurationService;
 
     @Autowired
+    private ImportService importService;
+
+    @Autowired
+    private CurrentUserService currentUserService;
+
+    @Autowired
     private SystemSettingManager systemSettingManager;
 
     @Autowired
@@ -166,8 +177,7 @@
     }
 
     @Override
-    public ImportSummary executeDataSynch()
-        throws HttpServerErrorException
+    public ImportSummary executeDataPush()
     {
         AvailabilityStatus availability = isRemoteServerAvailable();
 
@@ -203,7 +213,6 @@
 
         final RequestCallback requestCallback = new RequestCallback()
         {
-
             @Override
             public void doWithRequest( ClientHttpRequest request ) throws IOException
             {
@@ -234,6 +243,33 @@
         return (Date) systemSettingManager.getSystemSetting( KEY_LAST_SUCCESSFUL_DATA_SYNC );
     }
 
+    @Override
+    public org.hisp.dhis.dxf2.metadata.ImportSummary executeMetadataPull( String url )
+    {
+        User user = currentUserService.getCurrentUser();
+        
+        String userUid = user != null ? user.getUid() : null;
+        
+        log.info( "Metadata pull, url: " + url + ", user: " + userUid );
+        
+        String json = restTemplate.getForObject( url, String.class );
+        
+        MetaData metaData = null;
+        
+        try
+        {
+            metaData = JacksonUtils.fromJson( json, MetaData.class );
+        }
+        catch ( IOException ex )
+        {
+            throw new RuntimeException( "Failed to parse remote JSON document", ex );
+        }
+        
+        org.hisp.dhis.dxf2.metadata.ImportSummary summary = importService.importMetaData( userUid, metaData );
+        
+        return summary;
+    }
+    
     // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/synch/SynchronizationManager.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/synch/SynchronizationManager.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/synch/SynchronizationManager.java	2015-09-09 12:28:08 +0000
@@ -37,9 +37,11 @@
  */
 public interface SynchronizationManager
 {
-    AvailabilityStatus isRemoteServerAvailable();
-    
-    ImportSummary executeDataSynch();
+    ImportSummary executeDataPush();
     
     Date getLastSynchSuccess();
+    
+    org.hisp.dhis.dxf2.metadata.ImportSummary executeMetadataPull( String url );
+    
+    AvailabilityStatus isRemoteServerAvailable();    
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SynchronizationController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SynchronizationController.java	2015-07-08 05:42:56 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SynchronizationController.java	2015-09-09 12:28:08 +0000
@@ -33,12 +33,15 @@
 import org.hisp.dhis.dxf2.synch.AvailabilityStatus;
 import org.hisp.dhis.dxf2.synch.SynchronizationManager;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseBody;
 
 import javax.servlet.http.HttpServletResponse;
+
 import java.io.IOException;
 
 import static org.hisp.dhis.webapi.utils.ContextUtils.CONTENT_TYPE_JSON;
@@ -51,23 +54,35 @@
 public class SynchronizationController
 {
     public static final String RESOURCE_PATH = "/synchronization";
-
+    
     @Autowired
     private SynchronizationManager synchronizationManager;
 
-    @RequestMapping( method = RequestMethod.POST )
+    @PreAuthorize( "hasRole('ALL') or hasRole('F_EXPORT_DATA')" )
+    @RequestMapping( value = "/dataPush", method = RequestMethod.POST )
     public void execute( HttpServletResponse response )
         throws IOException
     {
-        ImportSummary summary = synchronizationManager.executeDataSynch();
+        ImportSummary summary = synchronizationManager.executeDataPush();
 
         response.setContentType( CONTENT_TYPE_JSON );
         JacksonUtils.toJson( response.getOutputStream(), summary );
     }
+    
+    @PreAuthorize( "hasRole('ALL')" )
+    @RequestMapping( value = "/metadataPull", method = RequestMethod.POST )
+    public void importMetaData( @RequestParam String url, HttpServletResponse response )
+        throws IOException
+    {
+        org.hisp.dhis.dxf2.metadata.ImportSummary summary = synchronizationManager.executeMetadataPull( url );
+        
+        response.setContentType( CONTENT_TYPE_JSON );
+        JacksonUtils.toJson( response.getOutputStream(), summary );        
+    }
 
     @RequestMapping( value = "/availability", method = RequestMethod.GET, produces = "application/json" )
     public @ResponseBody AvailabilityStatus isRemoteServerAvailable()
     {
         return synchronizationManager.isRemoteServerAvailable();
-    }
+    }    
 }