← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 8714: Web api, impl PUT and DELETE of Map. We might want to move the conversion stuff to the importer l...

 

------------------------------------------------------------
revno: 8714
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2012-10-25 18:27:03 +0200
message:
  Web api, impl PUT and DELETE of Map. We might want to move the conversion stuff to the importer later but OK for now.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/Map.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java
  dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/mapping/hibernate/Map.hbm.xml
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapLegendSetController.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/mapping/Map.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/Map.java	2012-10-25 13:19:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/Map.java	2012-10-25 16:27:03 +0000
@@ -32,6 +32,7 @@
 
 import org.hisp.dhis.common.BaseIdentifiableObject;
 import org.hisp.dhis.common.Dxf2Namespace;
+import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.annotation.Scanned;
 import org.hisp.dhis.common.view.DetailedView;
 import org.hisp.dhis.common.view.ExportView;
@@ -149,4 +150,23 @@
     {
         this.views = views;
     }
+
+    @Override
+    public void mergeWith( IdentifiableObject other )
+    {
+        super.mergeWith( other );
+        
+        if ( other.getClass().isInstance( this ) )
+        {
+            Map map = (Map) other;
+            
+            user = map.getUser() == null ? user : map.getUser();
+            longitude = map.getLongitude() == null ? longitude : map.getLongitude();
+            latitude = map.getLatitude() == null ? latitude : map.getLatitude();
+            zoom = map.getZoom() == null ? zoom : map.getZoom();
+            
+            views.clear();
+            views.addAll( map.getViews() );
+        }            
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java	2012-10-25 14:58:43 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java	2012-10-25 16:27:03 +0000
@@ -421,7 +421,7 @@
     
     public Map getMap( String uid )
     {
-        return mapStore.getByCode( uid );
+        return mapStore.getByUid( uid );
     }
     
     public void deleteMap( Map map )

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/mapping/hibernate/Map.hbm.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/mapping/hibernate/Map.hbm.xml	2012-10-25 12:38:20 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/mapping/hibernate/Map.hbm.xml	2012-10-25 16:27:03 +0000
@@ -11,8 +11,13 @@
     <id name="id" column="mapid">
       <generator class="native" />
     </id>
-    &identifiableProperties;
     
+    <property name="name" column="name" not-null="true" length="230" />
+      
+    <property name="uid" column="uid" length="11" />
+      
+    <property name="lastUpdated" type="timestamp" />
+        
     <many-to-one name="user" class="org.hisp.dhis.user.User" 
         column="userid" foreign-key="fk_mapview_userid" />
     

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapController.java	2012-10-25 14:58:43 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapController.java	2012-10-25 16:27:03 +0000
@@ -31,6 +31,7 @@
 
 import java.awt.image.BufferedImage;
 import java.io.InputStream;
+import java.util.Iterator;
 
 import javax.imageio.ImageIO;
 import javax.servlet.http.HttpServletRequest;
@@ -52,6 +53,7 @@
 import org.hisp.dhis.period.PeriodService;
 import org.hisp.dhis.user.UserService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
@@ -59,6 +61,7 @@
 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.ResponseStatus;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -107,66 +110,83 @@
     public void postJsonObject( HttpServletResponse response, HttpServletRequest request, InputStream input ) throws Exception
     {
         Map map = JacksonUtils.fromJson( input, Map.class );
-        
-        if ( map.getUser() != null )
-        {
-            map.setUser( userService.getUser( map.getUser().getUid() ) );
-        }
+
+        mergeMap( map );
         
         for ( MapView view : map.getViews() )
         {
-            if ( view.getIndicatorGroup() != null )
-            {
-                view.setIndicatorGroup( indicatorService.getIndicatorGroup( view.getIndicatorGroup().getUid() ) );
-            }
-            
-            if ( view.getIndicator() != null )
-            {
-                view.setIndicator( indicatorService.getIndicator( view.getIndicator().getUid() ) );
-            }
-            
-            if ( view.getDataElementGroup() != null )
-            {
-                view.setDataElementGroup( dataElementService.getDataElementGroup( view.getDataElementGroup().getUid() ) );
-            }
-            
-            if ( view.getDataElement() != null )
-            {
-                view.setDataElement( dataElementService.getDataElement( view.getDataElement().getUid() ) );
-            }
-            
-            if ( view.getPeriod() != null )
-            {
-                view.setPeriod( periodService.reloadPeriod( getPeriodFromIsoString( view.getPeriod().getUid() ) ) );
-            }
-            
-            if ( view.getParentOrganisationUnit() != null )
-            {
-                view.setParentOrganisationUnit( organisationUnitService.getOrganisationUnit( view.getParentOrganisationUnit().getUid() ) );
-            }
-            
-            if ( view.getOrganisationUnitLevel() != null )
-            {
-                view.setOrganisationUnitLevel( organisationUnitService.getOrganisationUnitLevel( view.getOrganisationUnitLevel().getUid() ) );
-            }
-            
-            if ( view.getLegendSet() != null )
-            {
-                view.setLegendSet( mappingService.getMapLegendSet( view.getLegendSet().getUid() ) );
-            }
-            
-            if ( view.getOrganisationUnitGroupSet() != null )
-            {
-                view.setOrganisationUnitGroupSet( organisationUnitGroupService.getOrganisationUnitGroupSet( view.getOrganisationUnitGroupSet().getUid() ) );
-            }
+            mergeMapView( view );
             
             mappingService.addMapView( view );
         }
-        
+
         mappingService.addMap( map );
         
         ContextUtils.createdResponse( response, "Map created", RESOURCE_PATH + "/" + map.getUid() );
     }
+    
+    @RequestMapping( value = "/{uid}", method = RequestMethod.PUT, consumes = "application/json" )
+    @ResponseStatus( value = HttpStatus.NO_CONTENT )
+    @PreAuthorize( "hasRole('F_GIS_ADMIN') or hasRole('ALL')" )
+    public void putJsonObject( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" ) String uid, InputStream input ) throws Exception
+    {
+        Map map = mappingService.getMap( uid );
+        
+        if ( map == null )
+        {
+            ContextUtils.notFoundResponse( response, "Map does not exist: " + uid );
+            return;
+        }
+
+        Iterator<MapView> views = map.getViews().iterator();
+        
+        while ( views.hasNext() )
+        {
+            MapView view = views.next();
+            views.remove();
+            mappingService.deleteMapView( view );
+        }
+        
+        Map newMap = JacksonUtils.fromJson( input, Map.class );
+
+        mergeMap( newMap );
+
+        for ( MapView view : newMap.getViews() )
+        {
+            mergeMapView( view );
+            
+            mappingService.addMapView( view );
+        }
+
+        map.mergeWith( newMap );
+        
+        mappingService.updateMap( map );
+    }
+    
+    @RequestMapping( value = "/{uid}", method = RequestMethod.DELETE )
+    @ResponseStatus( value = HttpStatus.NO_CONTENT )
+    @PreAuthorize( "hasRole('F_GIS_ADMIN') or hasRole('ALL')" )
+    public void deleteObject( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" ) String uid ) throws Exception
+    {
+        Map map = mappingService.getMap( uid );
+        
+        if ( map == null )
+        {
+            ContextUtils.notFoundResponse( response, "Map does not exist: " + uid );
+            return;
+        }
+
+        Iterator<MapView> views = map.getViews().iterator();
+        
+        while ( views.hasNext() )
+        {
+            MapView view = views.next();
+            views.remove();
+            mappingService.deleteMapView( view );
+        }
+        
+        mappingService.deleteMap( map );
+    }
 
     //--------------------------------------------------------------------------
     // Data
@@ -220,4 +240,62 @@
             response.setStatus( HttpServletResponse.SC_NO_CONTENT );
         }
     }
+    
+    // TODO use the import service instead
+    
+    private void mergeMap( Map map )
+    {
+        if ( map.getUser() != null )
+        {
+            map.setUser( userService.getUser( map.getUser().getUid() ) );
+        }        
+    }
+    
+    private void mergeMapView( MapView view )
+    {
+        if ( view.getIndicatorGroup() != null )
+        {
+            view.setIndicatorGroup( indicatorService.getIndicatorGroup( view.getIndicatorGroup().getUid() ) );
+        }
+        
+        if ( view.getIndicator() != null )
+        {
+            view.setIndicator( indicatorService.getIndicator( view.getIndicator().getUid() ) );
+        }
+        
+        if ( view.getDataElementGroup() != null )
+        {
+            view.setDataElementGroup( dataElementService.getDataElementGroup( view.getDataElementGroup().getUid() ) );
+        }
+        
+        if ( view.getDataElement() != null )
+        {
+            view.setDataElement( dataElementService.getDataElement( view.getDataElement().getUid() ) );
+        }
+        
+        if ( view.getPeriod() != null )
+        {
+            view.setPeriod( periodService.reloadPeriod( getPeriodFromIsoString( view.getPeriod().getUid() ) ) );
+        }
+        
+        if ( view.getParentOrganisationUnit() != null )
+        {
+            view.setParentOrganisationUnit( organisationUnitService.getOrganisationUnit( view.getParentOrganisationUnit().getUid() ) );
+        }
+        
+        if ( view.getOrganisationUnitLevel() != null )
+        {
+            view.setOrganisationUnitLevel( organisationUnitService.getOrganisationUnitLevel( view.getOrganisationUnitLevel().getUid() ) );
+        }
+        
+        if ( view.getLegendSet() != null )
+        {
+            view.setLegendSet( mappingService.getMapLegendSet( view.getLegendSet().getUid() ) );
+        }
+        
+        if ( view.getOrganisationUnitGroupSet() != null )
+        {
+            view.setOrganisationUnitGroupSet( organisationUnitGroupService.getOrganisationUnitGroupSet( view.getOrganisationUnitGroupSet().getUid() ) );
+        }
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapLegendSetController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapLegendSetController.java	2012-10-24 10:22:27 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapLegendSetController.java	2012-10-25 16:27:03 +0000
@@ -80,7 +80,7 @@
 
     @RequestMapping( value = "/{uid}", method = RequestMethod.PUT, consumes = "application/json" )
     @ResponseStatus( value = HttpStatus.NO_CONTENT )
-    @PreAuthorize( "hasRole('ALL') or hasRole('ALL')" )
+    @PreAuthorize( "hasRole('F_GIS_ADMIN') or hasRole('ALL')" )
     public void putJsonObject( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" ) String uid, InputStream input ) throws Exception
     {
         MapLegendSet legendSet = mappingService.getMapLegendSet( uid );
@@ -114,7 +114,7 @@
 
     @RequestMapping( value = "/{uid}", method = RequestMethod.DELETE )
     @ResponseStatus( value = HttpStatus.NO_CONTENT )
-    @PreAuthorize( "hasRole('ALL') or hasRole('ALL')" )
+    @PreAuthorize( "hasRole('F_GIS_ADMIN') or hasRole('ALL')" )
     public void deleteObject( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" ) String uid ) throws Exception
     {
         MapLegendSet legendSet = mappingService.getMapLegendSet( uid );