← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 7608: Modified period serializzation in order to include period names

 

------------------------------------------------------------
revno: 7608
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2012-07-14 12:40:48 +0200
message:
  Modified period serializzation in order to include period names
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonPeriodDeserializer.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonPeriodSerializer.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/Period.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/DateUtils.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartController.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/common/adapter/JacksonPeriodDeserializer.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonPeriodDeserializer.java	2012-04-18 20:24:12 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonPeriodDeserializer.java	2012-07-14 10:40:48 +0000
@@ -29,6 +29,7 @@
 
 import com.fasterxml.jackson.core.JsonParser;
 import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.JsonToken;
 import com.fasterxml.jackson.databind.DeserializationContext;
 import com.fasterxml.jackson.databind.JsonDeserializer;
 import org.hisp.dhis.period.Period;
@@ -43,10 +44,17 @@
     extends JsonDeserializer<Period>
 {
     @Override
-    public Period deserialize( JsonParser jp, DeserializationContext ctxt ) throws IOException, JsonProcessingException
+    public Period deserialize( JsonParser jp, DeserializationContext ctxt )
+        throws IOException, JsonProcessingException
     {
-        String periodString = jp.readValueAs( String.class );
-
-        return PeriodType.getPeriodFromIsoString( periodString );
+        while ( jp.nextToken() != JsonToken.END_OBJECT )
+        {
+            if ( "id".equals( jp.getCurrentName() ) )
+            {
+                return PeriodType.getPeriodFromIsoString( jp.getText() );
+            }
+        }
+        
+        return null;        
     }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonPeriodSerializer.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonPeriodSerializer.java	2012-04-18 20:24:12 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonPeriodSerializer.java	2012-07-14 10:40:48 +0000
@@ -46,7 +46,15 @@
     {
         if ( value.getIsoDate() != null )
         {
-            jgen.writeString( value.getIsoDate() );
+            jgen.writeStartObject();
+            jgen.writeStringField( "id", value.getIsoDate() );
+            
+            if ( value.getName() != null )
+            {
+                jgen.writeStringField( "name", value.getName() );
+            }
+            
+            jgen.writeEndObject();
         }
     }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/Period.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/Period.java	2012-04-18 20:24:12 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/Period.java	2012-07-14 10:40:48 +0000
@@ -220,7 +220,8 @@
             format.applyPattern( DEFAULT_DATE_FORMAT );
 
             return dateString != null ? format.parse( dateString ) : null;
-        } catch ( ParseException ex )
+        } 
+        catch ( ParseException ex )
         {
             throw new RuntimeException( "Failed to parse medium date", ex );
         }

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/DateUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/DateUtils.java	2012-05-29 21:23:47 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/DateUtils.java	2012-07-14 10:40:48 +0000
@@ -503,7 +503,10 @@
     {
         for ( Period period : periods )
         {
-            period.setName( format.formatPeriod( period ) );
+            if ( period != null )
+            {
+                period.setName( format.formatPeriod( period ) );
+            }
         }
 
         return periods;

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java	2012-07-13 20:45:06 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java	2012-07-14 10:40:48 +0000
@@ -106,6 +106,8 @@
             WebUtils.generateLinks( entity );
         }
 
+        postProcessEntity( entity );
+        
         model.addAttribute( "model", entity );
         model.addAttribute( "viewClass", options.getViewClass( "detailed" ) );
 
@@ -130,6 +132,8 @@
             WebUtils.generateLinks( entity );
         }
 
+        postProcessEntity( entity );
+        
         model.addAttribute( "model", entity );
         model.addAttribute( "viewClass", "detailed" );
 
@@ -187,6 +191,18 @@
     }
 
     //--------------------------------------------------------------------------
+    // Hooks
+    //--------------------------------------------------------------------------
+
+    /**
+     * Override to process a single entity after it has been retrieved from 
+     * storage and before it is returned to the view. Entity is null-safe.
+     */
+    public void postProcessEntity( T entity ) throws Exception
+    {
+    }
+    
+    //--------------------------------------------------------------------------
     // Helpers
     //--------------------------------------------------------------------------
 

=== 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	2012-05-29 12:04:07 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartController.java	2012-07-14 10:40:48 +0000
@@ -27,15 +27,23 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.io.IOException;
+import java.util.Date;
+
+import javax.servlet.http.HttpServletResponse;
+
 import org.hisp.dhis.api.utils.ContextUtils;
+import org.hisp.dhis.api.utils.ContextUtils.CacheStrategy;
 import org.hisp.dhis.chart.Chart;
 import org.hisp.dhis.chart.ChartService;
+import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.i18n.I18nManager;
 import org.hisp.dhis.i18n.I18nManagerException;
 import org.hisp.dhis.indicator.Indicator;
 import org.hisp.dhis.indicator.IndicatorService;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.period.Period;
 import org.hisp.dhis.system.util.CodecUtils;
 import org.jfree.chart.ChartUtilities;
 import org.jfree.chart.JFreeChart;
@@ -47,12 +55,6 @@
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-import java.util.Date;
-
-import static org.hisp.dhis.api.utils.ContextUtils.CacheStrategy;
-
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  * @author Lars Helge Overland
@@ -124,4 +126,18 @@
 
         ChartUtilities.writeChartAsPNG( response.getOutputStream(), chart, width, height );
     }
+    
+    @Override
+    public void postProcessEntity( Chart chart ) throws Exception
+    {
+        I18nFormat format = i18nManager.getI18nFormat();
+        
+        if ( chart.getPeriods() != null && !chart.getPeriods().isEmpty() )
+        {
+            for ( Period period : chart.getPeriods() )
+            {
+                period.setName( format.formatPeriod( period ) );
+            }
+        }
+    }
 }