← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19579: switched to using WebMessage for created/notFound/conflict responses in Chart/Report/ReportTable ...

 

------------------------------------------------------------
revno: 19579
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-07-08 14:18:13 +0700
message:
  switched to using WebMessage for created/notFound/conflict responses in Chart/Report/ReportTable controllers
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/ChartController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/ReportController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/ReportTableController.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-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/ChartController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/ChartController.java	2015-07-08 05:42:56 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/ChartController.java	2015-07-08 07:18:13 +0000
@@ -38,7 +38,7 @@
 import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.dxf2.common.ImportOptions;
 import org.hisp.dhis.dxf2.common.JacksonUtils;
-import org.hisp.dhis.dxf2.webmessage.WebMessage;
+import org.hisp.dhis.dxf2.webmessage.WebMessageException;
 import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.i18n.I18nManager;
 import org.hisp.dhis.indicator.Indicator;
@@ -119,7 +119,8 @@
 
         chartService.addChart( chart );
 
-        ContextUtils.createdResponse( response, "Chart created", ChartSchemaDescriptor.API_ENDPOINT + "/" + chart.getUid() );
+        response.addHeader( "Location", ChartSchemaDescriptor.API_ENDPOINT + "/" + chart.getUid() );
+        webMessageService.send( WebMessageUtils.created( "Chart created" ), response, request );
     }
 
     @Override
@@ -130,8 +131,7 @@
 
         if ( chart == null )
         {
-            ContextUtils.notFoundResponse( response, "Chart does not exist: " + uid );
-            return;
+            throw new WebMessageException( WebMessageUtils.notFound( "Chart does not exist: " + uid ) );
         }
 
         Chart newChart = JacksonUtils.fromJson( request.getInputStream(), Chart.class );
@@ -151,8 +151,7 @@
 
         if ( chart == null )
         {
-            ContextUtils.notFoundResponse( response, "Chart does not exist: " + uid );
-            return;
+            throw new WebMessageException( WebMessageUtils.notFound( "Chart does not exist: " + uid ) );
         }
 
         chartService.deleteChart( chart );
@@ -170,14 +169,13 @@
         @RequestParam( value = "width", defaultValue = "800", required = false ) int width,
         @RequestParam( value = "height", defaultValue = "500", required = false ) int height,
         @RequestParam( value = "attachment", required = false ) boolean attachment,
-        HttpServletResponse response ) throws IOException
+        HttpServletResponse response ) throws IOException, WebMessageException
     {
         Chart chart = chartService.getChartNoAcl( uid );
 
         if ( chart == null )
         {
-            ContextUtils.notFoundResponse( response, "Chart does not exist: " + uid );
-            return;
+            throw new WebMessageException( WebMessageUtils.notFound( "Chart does not exist: " + uid ) );
         }
 
         OrganisationUnit unit = ou != null ? organisationUnitService.getOrganisationUnit( ou ) : null;
@@ -229,38 +227,34 @@
         @RequestParam String ou,
         @RequestParam( defaultValue = "525", required = false ) int width,
         @RequestParam( defaultValue = "300", required = false ) int height,
-        HttpServletResponse response ) throws IOException
+        HttpServletResponse response ) throws IOException, WebMessageException
     {
         DataElement dataElement = dataElementService.getDataElement( de );
 
         if ( dataElement == null )
         {
-            ContextUtils.conflictResponse( response, "Data element does not exist: " + de );
-            return;
+            throw new WebMessageException( WebMessageUtils.conflict( "Data element does not exist: " + de ) );
         }
 
         DataElementCategoryOptionCombo categoryOptionCombo = categoryService.getDataElementCategoryOptionCombo( co );
 
         if ( categoryOptionCombo == null )
         {
-            ContextUtils.conflictResponse( response, "Category option combo does not exist: " + co );
-            return;
+            throw new WebMessageException( WebMessageUtils.conflict( "Category option combo does not exist: " + co ) );
         }
 
         Period period = PeriodType.getPeriodFromIsoString( pe );
 
         if ( period == null )
         {
-            ContextUtils.conflictResponse( response, "Period does not exist: " + pe );
-            return;
+            throw new WebMessageException( WebMessageUtils.conflict( "Period does not exist: " + pe ) );
         }
 
         OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( ou );
 
         if ( organisationUnit == null )
         {
-            ContextUtils.conflictResponse( response, "Organisation unit does not exist: " + ou );
-            return;
+            throw new WebMessageException( WebMessageUtils.conflict( "Organisation unit does not exist: " + ou ) );
         }
 
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_PNG, CacheStrategy.RESPECT_SYSTEM_SETTING, "chart.png", false );

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/ReportController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/ReportController.java	2015-07-08 05:42:56 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/ReportController.java	2015-07-08 07:18:13 +0000
@@ -32,6 +32,7 @@
 import net.sf.jasperreports.j2ee.servlets.BaseHttpServlet;
 import net.sf.jasperreports.j2ee.servlets.ImageServlet;
 import org.hisp.dhis.commons.util.CodecUtils;
+import org.hisp.dhis.dxf2.webmessage.WebMessageException;
 import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.i18n.I18nManager;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
@@ -43,6 +44,7 @@
 import org.hisp.dhis.schema.descriptors.ReportSchemaDescriptor;
 import org.hisp.dhis.webapi.utils.ContextUtils;
 import org.hisp.dhis.webapi.utils.ContextUtils.CacheStrategy;
+import org.hisp.dhis.webapi.utils.WebMessageUtils;
 import org.joda.time.DateTime;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
@@ -94,8 +96,7 @@
 
         if ( report == null )
         {
-            ContextUtils.notFoundResponse( response, "Report not found for identifier: " + uid );
-            return;
+            throw new WebMessageException( WebMessageUtils.notFound( "Report not found for identifier: " + uid ) );
         }
 
         report.setDesignContent( designContent );
@@ -109,14 +110,12 @@
 
         if ( report == null )
         {
-            ContextUtils.notFoundResponse( response, "Report not found for identifier: " + uid );
-            return;
+            throw new WebMessageException( WebMessageUtils.notFound( "Report not found for identifier: " + uid ) );
         }
 
         if ( report.getDesignContent() == null )
         {
-            ContextUtils.conflictResponse( response, "Report has no design content: " + uid );
-            return;
+            throw new WebMessageException( WebMessageUtils.conflict( "Report has no design content: " + uid ) );
         }
 
         if ( Report.TYPE_HTML.equals( report.getType() ) )
@@ -194,8 +193,7 @@
 
         if ( report == null )
         {
-            ContextUtils.notFoundResponse( response, "Report does not exist: " + uid );
-            return;
+            throw new WebMessageException( WebMessageUtils.notFound( "Report not found for identifier: " + uid ) );
         }
 
         if ( organisationUnitUid == null && report.hasReportTable() && report.getReportTable().hasReportParams()

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/ReportTableController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/ReportTableController.java	2015-07-04 16:43:29 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/ReportTableController.java	2015-07-08 07:18:13 +0000
@@ -33,10 +33,10 @@
 import org.hisp.dhis.common.MergeStrategy;
 import org.hisp.dhis.dxf2.common.ImportOptions;
 import org.hisp.dhis.dxf2.common.JacksonUtils;
+import org.hisp.dhis.dxf2.webmessage.WebMessageException;
 import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.i18n.I18nManager;
 import org.hisp.dhis.legend.LegendService;
-import org.hisp.dhis.mapping.MappingService;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.period.Period;
@@ -46,6 +46,7 @@
 import org.hisp.dhis.system.grid.GridUtils;
 import org.hisp.dhis.webapi.utils.ContextUtils;
 import org.hisp.dhis.webapi.utils.ContextUtils.CacheStrategy;
+import org.hisp.dhis.webapi.utils.WebMessageUtils;
 import org.joda.time.DateTime;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
@@ -82,9 +83,6 @@
     private DimensionService dimensionService;
 
     @Autowired
-    private MappingService mappingService;
-
-    @Autowired
     private LegendService legendService;
 
     @Autowired
@@ -107,7 +105,8 @@
 
         reportTableService.saveReportTable( reportTable );
 
-        ContextUtils.createdResponse( response, "Report table created", ReportTableSchemaDescriptor.API_ENDPOINT + "/" + reportTable.getUid() );
+        response.addHeader( "Location", ReportTableSchemaDescriptor.API_ENDPOINT + "/" + reportTable.getUid() );
+        webMessageService.send( WebMessageUtils.created( "Report table created" ), response, request );
     }
 
     @Override
@@ -118,8 +117,7 @@
 
         if ( reportTable == null )
         {
-            ContextUtils.notFoundResponse( response, "Report table does not exist: " + uid );
-            return;
+            throw new WebMessageException( WebMessageUtils.notFound( "Report table does not exist: " + uid ) );
         }
 
         ReportTable newReportTable = JacksonUtils.fromJson( request.getInputStream(), ReportTable.class );
@@ -139,8 +137,7 @@
 
         if ( reportTable == null )
         {
-            ContextUtils.notFoundResponse( response, "Report table does not exist: " + uid );
-            return;
+            throw new WebMessageException( WebMessageUtils.notFound( "Report table does not exist: " + uid ) );
         }
 
         reportTableService.deleteReportTable( reportTable );