← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19370: Implemented producing ADXPeriod format from period

 

------------------------------------------------------------
revno: 19370
committer: Bob Jolliffe <bobjolliffe@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-06-11 20:53:44 +0100
message:
  Implemented producing ADXPeriod format from period
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/adx/ADXPeriod.java
  dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/adx/ADXPeriodTest.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/adx/ADXPeriod.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/adx/ADXPeriod.java	2015-06-11 14:38:10 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/adx/ADXPeriod.java	2015-06-11 19:53:44 +0000
@@ -74,6 +74,8 @@
         P6M, // 6monthly (including 6monthlyApril)
         P1Y  // yearly, financialApril, financialJuly, financialOctober
     }
+    
+    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 
     public static Period parse(String periodString) throws ADXPeriodException {
         String[] tokens = periodString.split("/");
@@ -84,7 +86,7 @@
         try {
             Period period = null;
             PeriodType periodType = null;
-            Date startDate = new SimpleDateFormat("yyyy-MM-dd").parse(tokens[0]);
+            Date startDate = dateFormat.parse(tokens[0]);
             Calendar cal = Calendar.getInstance();
             cal.setTime(startDate);
             Duration duration = Duration.valueOf(tokens[1]);
@@ -149,4 +151,10 @@
             throw new ADXPeriodException(tokens[1] + " is not a supported duration type");
         }
     }
+    
+    public static String serialize(Period period)
+    {
+        return dateFormat.format(period.getStartDate()) + "/"
+                + period.getPeriodType().getIso8601Duration();
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/adx/ADXPeriodTest.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/adx/ADXPeriodTest.java	2015-06-11 14:38:10 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/adx/ADXPeriodTest.java	2015-06-11 19:53:44 +0000
@@ -29,6 +29,7 @@
  */
 
 import org.hisp.dhis.period.Period;
+import org.hisp.dhis.period.PeriodType;
 import org.junit.Test;
 import static org.junit.Assert.*;
 
@@ -96,4 +97,22 @@
             assertEquals(ADXPeriod.ADXPeriodException.class,ex.getClass());
         }
     }
+    
+    @Test
+    public void testSerialize() {
+        p = PeriodType.getPeriodFromIsoString("2015");
+        assertEquals("2015-01-01/P1Y", ADXPeriod.serialize(p));
+        p = PeriodType.getPeriodFromIsoString("201503");
+        assertEquals("2015-03-01/P1M", ADXPeriod.serialize(p));
+        p = PeriodType.getPeriodFromIsoString("2015W1");
+        assertEquals("2014-12-29/P7D", ADXPeriod.serialize(p));
+        p = PeriodType.getPeriodFromIsoString("2015Q2");
+        assertEquals("2015-04-01/P1Q", ADXPeriod.serialize(p));
+        p = PeriodType.getPeriodFromIsoString("2015April");
+        assertEquals("2015-04-01/P1Y", ADXPeriod.serialize(p));
+        p = PeriodType.getPeriodFromIsoString("2015S2");
+        assertEquals("2015-07-01/P6M", ADXPeriod.serialize(p));
+        p = PeriodType.getPeriodFromIsoString("2015AprilS2");
+        assertEquals("2015-10-01/P6M", ADXPeriod.serialize(p));
+    }
 }