← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5460: Web api: Moved rendering code from MapViewGenerationView to MapController. A less elegant solutio...

 

Merge authors:
  Lars Helge Øverland (larshelge)
------------------------------------------------------------
revno: 5460 [merge]
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2011-12-16 19:16:22 +0100
message:
  Web api: Moved rendering code from MapViewGenerationView to MapController. A less elegant solution but it was too hard to combine the meta-data view and data view of the Map when rendering.
removed:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/MapGenerationView.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/MapView.java
  dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/GeoToolsMapGenerationService.java
  dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/IntervalSet.java
  dhis-2/dhis-services/dhis-service-mapping/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MapController.java
  dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml


--
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/MapView.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/MapView.java	2011-12-09 20:53:07 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/MapView.java	2011-12-16 17:24:40 +0000
@@ -173,6 +173,13 @@
 
         return name.equals( other.name );
     }
+    
+    @Override
+    public String toString()
+    {
+        return "[Name: " + name + ", indicator: " + indicator + ", org unit: " + 
+            parentOrganisationUnit + ", period: " + period + ", value type: " + mapValueType + "]";
+    }
 
     // -------------------------------------------------------------------------
     // Logic

=== modified file 'dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/GeoToolsMapGenerationService.java'
--- dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/GeoToolsMapGenerationService.java	2011-12-15 17:02:41 +0000
+++ dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/GeoToolsMapGenerationService.java	2011-12-16 18:09:38 +0000
@@ -183,7 +183,7 @@
             mapValues = mappingService.getDataElementMapValues( mapView.getDataElement().getId(), mapView.getPeriod()
                 .getId(), mapView.getParentOrganisationUnit().getId(), mapView.getOrganisationUnitLevel().getLevel() );
         }
-
+        
         // Build and set the internal GeoTools map objects for the layer
         buildGeoToolsMapObjectsForMapLayer( mapLayer, mapValues );
 
@@ -213,7 +213,7 @@
         {
             // Get the org unit for this map value
             OrganisationUnit orgUnit = organisationUnitService.getOrganisationUnit( mapValue.getOrganisationUnitId() );
-
+            
             if ( orgUnit != null && orgUnit.hasCoordinates() && orgUnit.hasFeatureType() )
             {
                 mapObjects.add( buildSingleGeoToolsMapObjectForMapLayer( mapLayer, mapValue, orgUnit ) );

=== modified file 'dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/IntervalSet.java'
--- dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/IntervalSet.java	2011-12-13 09:43:27 +0000
+++ dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/IntervalSet.java	2011-12-16 18:09:38 +0000
@@ -123,7 +123,6 @@
      */
     private static IntervalSet applyEqualRangeIntervalSetToMapLayer( InternalMapLayer mapLayer, int length )
     {
-
         Assert.isTrue( mapLayer != null );
         Assert.isTrue( length > 0 );
         Assert.isTrue( mapLayer.getAllMapObjects() != null );
@@ -139,9 +138,14 @@
         for ( InternalMapObject mapObject : mapLayer.getAllMapObjects() )
         {
             if ( set.objectLow == null || mapObject.getValue() < set.objectLow.getValue() )
+            {
                 set.objectLow = mapObject;
+            }
+            
             if ( set.objectHigh == null || mapObject.getValue() > set.objectHigh.getValue() )
+            {
                 set.objectHigh = mapObject;
+            }
         }
 
         // Determine and set the color for each of the intervals according to
@@ -183,7 +187,6 @@
      */
     private static IntervalSet applyEqualSizeIntervalSetToMapLayer( InternalMapLayer mapLayer, int length )
     {
-
         Assert.isTrue( mapLayer != null );
         Assert.isTrue( length > 0 );
         Assert.isTrue( mapLayer.getAllMapObjects() != null );

=== modified file 'dhis-2/dhis-services/dhis-service-mapping/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java'
--- dhis-2/dhis-services/dhis-service-mapping/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java	2011-12-15 17:53:48 +0000
+++ dhis-2/dhis-services/dhis-service-mapping/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java	2011-12-16 18:09:38 +0000
@@ -524,7 +524,7 @@
         MapView mapView = new MapView();
         
         Period period = periodService.reloadPeriod( 
-            new RelativePeriods().setLastYear( true ).getRelativePeriods().iterator().next() );
+            new RelativePeriods().setThisYear( true ).getRelativePeriods().iterator().next() );
         
         Indicator indicator = indicatorService.getIndicator( indicatorUid );
         OrganisationUnit unit = organisationUnitService.getOrganisationUnit( organisationUnitUid );

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartController.java	2011-12-15 18:48:17 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartController.java	2011-12-16 17:42:41 +0000
@@ -119,9 +119,9 @@
 
     @RequestMapping( value = {"/{uid}/data","/{uid}/data.png"}, method = RequestMethod.GET )
     public void getChart( @PathVariable( "uid" ) String uid,
-                             @RequestParam( value = "width", defaultValue = "700", required = false ) int width,
-                             @RequestParam( value = "height", defaultValue = "500", required = false ) int height,
-                             HttpServletResponse response ) throws IOException, I18nManagerException
+                          @RequestParam( value = "width", defaultValue = "700", required = false ) int width,
+                          @RequestParam( value = "height", defaultValue = "500", required = false ) int height,
+                          HttpServletResponse response ) throws IOException, I18nManagerException
     {
         JFreeChart chart = chartService.getJFreeChart( uid, i18nManager.getI18nFormat() );
 
@@ -129,14 +129,14 @@
         ChartUtilities.writeChartAsPNG( response.getOutputStream(), chart, width, height );
     }
 
-    @RequestMapping( value = "/data", method = RequestMethod.GET )
+    @RequestMapping( value = {"/data","/data.png"}, method = RequestMethod.GET )
     public void getChart( @RequestParam( value = "in" ) String indicatorUid, 
-        @RequestParam( value = "ou" ) String organisationUnitUid,
-        @RequestParam( value = "periods", required = false ) boolean periods,
-        @RequestParam( value = "width", defaultValue = "700", required = false ) int width,
-        @RequestParam( value = "height", defaultValue = "500", required = false ) int height,
-        @RequestParam( value = "skipTitle", required = false ) boolean skipTitle,
-        HttpServletResponse response ) throws Exception
+                          @RequestParam( value = "ou" ) String organisationUnitUid,
+                          @RequestParam( value = "periods", required = false ) boolean periods,
+                          @RequestParam( value = "width", defaultValue = "700", required = false ) int width,
+                          @RequestParam( value = "height", defaultValue = "500", required = false ) int height,
+                          @RequestParam( value = "skipTitle", required = false ) boolean skipTitle,
+                          HttpServletResponse response ) throws Exception
     {
         Indicator indicator = indicatorService.getIndicator( indicatorUid );
         OrganisationUnit unit = organisationUnitService.getOrganisationUnit( organisationUnitUid );

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MapController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MapController.java	2011-12-16 10:26:08 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MapController.java	2011-12-16 18:09:38 +0000
@@ -27,20 +27,24 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.awt.image.BufferedImage;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 
+import javax.imageio.ImageIO;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.hisp.dhis.api.utils.IdentifiableObjectParams;
 import org.hisp.dhis.api.utils.WebLinkPopulator;
+import org.hisp.dhis.mapgeneration.MapGenerationService;
 import org.hisp.dhis.mapping.MapView;
 import org.hisp.dhis.mapping.MappingService;
 import org.hisp.dhis.mapping.Maps;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.util.ContextUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.stereotype.Controller;
@@ -64,6 +68,9 @@
     @Autowired
     private OrganisationUnitService organisationUnitService;
 
+    @Autowired
+    private MapGenerationService mapGenerationService;
+
     //-------------------------------------------------------------------------------------------------------
     // GET
     //-------------------------------------------------------------------------------------------------------
@@ -101,32 +108,32 @@
         return "map";
     }
     
-    @RequestMapping( value = "/{uid}/data", method = RequestMethod.GET )
-    public void getMapPng( HttpServletRequest request, HttpServletResponse response ) throws Exception
+    @RequestMapping( value = {"/{uid}/data","/{uid}/data.png"}, method = RequestMethod.GET )
+    public void getMap( @PathVariable String uid, HttpServletResponse response ) throws Exception
     {
-        String url = request.getRequestURL().toString().replace( "/data", ".png" );
+        MapView mapView = mappingService.getMapView( uid );
         
-        response.sendRedirect( response.encodeRedirectURL( url ) );
+        renderMapViewPng( mapView, response );
     }
     
-    @RequestMapping( value = "/data", method = RequestMethod.GET )
-    public String getMap( Model model,
-        @RequestParam( value = "in" ) String indicatorUid, 
-        @RequestParam( value = "ou" ) String organisationUnitUid,
-        @RequestParam( value = "level", required = false ) Integer level )
+    @RequestMapping( value = {"/data","/data.png"}, method = RequestMethod.GET )
+    public void getMap( Model model,
+                        @RequestParam( value = "in" ) String indicatorUid, 
+                        @RequestParam( value = "ou" ) String organisationUnitUid,
+                        @RequestParam( value = "level", required = false ) Integer level,
+                        HttpServletResponse response ) throws Exception
     {
         if ( level == null )
         {
             OrganisationUnit unit = organisationUnitService.getOrganisationUnit( organisationUnitUid );
             
             level = organisationUnitService.getLevelOfOrganisationUnit( unit.getId() );
+            level++;
         }
         
         MapView mapView = mappingService.getIndicatorLastYearMapView( indicatorUid, organisationUnitUid, level );
-
-        model.addAttribute( "model", mapView );
-
-        return "map";
+        
+        renderMapViewPng( mapView, response );
     }
 
     //-------------------------------------------------------------------------------------------------------
@@ -175,4 +182,16 @@
     {
         throw new HttpRequestMethodNotSupportedException( RequestMethod.DELETE.toString() );
     }
+
+    //-------------------------------------------------------------------------------------------------------
+    // Supportive methods
+    //-------------------------------------------------------------------------------------------------------
+
+    private void renderMapViewPng( MapView mapView, HttpServletResponse response )
+        throws Exception
+    {
+        BufferedImage image = mapGenerationService.generateMapImage( mapView );
+        response.setContentType( ContextUtils.CONTENT_TYPE_PNG );
+        ImageIO.write( image, "PNG", response.getOutputStream() );
+    }
 }

=== removed file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/MapGenerationView.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/MapGenerationView.java	2011-12-15 16:57:22 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/MapGenerationView.java	1970-01-01 00:00:00 +0000
@@ -1,64 +0,0 @@
-package org.hisp.dhis.api.view;
-
-/*
- * Copyright (c) 2004-2011, 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.mapgeneration.MapGenerationService;
-import org.hisp.dhis.mapping.MapView;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.MediaType;
-import org.springframework.web.servlet.view.AbstractView;
-
-import javax.imageio.ImageIO;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.awt.image.BufferedImage;
-import java.util.Map;
-
-public class MapGenerationView
-    extends AbstractView
-{
-    @Autowired
-    private MapGenerationService mapGenerationService;
-
-    public MapGenerationView()
-    {
-        super();
-        setContentType( "image/png" );
-    }
-
-    @Override
-    protected void renderMergedOutputModel( Map<String, Object> model, HttpServletRequest request, HttpServletResponse response )
-        throws Exception
-    {
-        MapView mapView = (MapView) model.get( "model" );
-        BufferedImage image = mapGenerationService.generateMapImage( mapView );
-
-        response.setContentType( MediaType.IMAGE_PNG.toString() );
-        ImageIO.write( image, "PNG", response.getOutputStream() );
-    }
-}

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml'
--- dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml	2011-12-12 11:18:21 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml	2011-12-16 17:42:41 +0000
@@ -37,8 +37,6 @@
         </bean>
 
         <bean class="org.hisp.dhis.api.view.Jaxb2View" />
-
-        <bean name="mapGenerationView" class="org.hisp.dhis.api.view.MapGenerationView" />
       </list>
     </property>