← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 8460: Allowed alternative ISO format for monthly period types "YYYY-mm" which is commonly used, for exa...

 

------------------------------------------------------------
revno: 8460
committer: Bob Jolliffe <bobjolliffe@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2012-10-10 11:12:33 +0100
message:
  Allowed alternative ISO format for monthly period types "YYYY-mm" which is commonly used, for example, with xml xs:gYearMonth type.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java
  dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/MonthlyPeriodTypeTest.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/period/MonthlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java	2012-07-23 16:26:34 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java	2012-10-10 10:12:33 +0000
@@ -52,6 +52,8 @@
 
     private static final String ISO_FORMAT = "yyyyMM";
 
+    private static final String ALTERNATIVE_ISO_FORMAT = "yyyy-MM";
+
     /**
      * The name of the MonthlyPeriodType, which is "Monthly".
      */
@@ -170,14 +172,24 @@
     @Override
     public Period createPeriod( String isoDate )
     {
+        Date date = null;
         try
         {
-            Date date = new SimpleDateFormat( ISO_FORMAT ).parse( isoDate );
+            date = new SimpleDateFormat( ISO_FORMAT ).parse( isoDate );
             return createPeriod( date );
         }
-        catch ( ParseException ex )
+        catch ( ParseException ex1 )
         {
-            throw new RuntimeException( ex );
+            // if at first you don't succeed ... try again
+            try
+            {
+                date = new SimpleDateFormat( ALTERNATIVE_ISO_FORMAT ).parse( isoDate );
+                return createPeriod( date );
+            }
+            catch ( ParseException ex2 )
+            {
+                throw new RuntimeException( ex2 );
+            }       
         }
     }
 

=== modified file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/MonthlyPeriodTypeTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/MonthlyPeriodTypeTest.java	2012-03-16 12:05:01 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/MonthlyPeriodTypeTest.java	2012-10-10 10:12:33 +0000
@@ -27,11 +27,10 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import static org.junit.Assert.assertEquals;
-
+import java.util.Calendar;
 import java.util.List;
-
-import org.hisp.dhis.period.Cal;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertEquals;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -79,6 +78,25 @@
     }
 
     @Test
+    public void testCreatePeriodFromISOString()
+    {
+        String isoPeriod = "201001";
+        String alternativeIsoPeriod = "201001";
+        
+        Period period1 = periodType.createPeriod( isoPeriod );
+        Period period2 = periodType.createPeriod( alternativeIsoPeriod );
+        
+        testCal.set( period1.getStartDate());
+        assertEquals( 2010, testCal.get( Calendar.YEAR) );
+        assertEquals( 0, testCal.get( Calendar.MONTH) );
+        
+        testCal.set( period2.getStartDate());
+        assertEquals( 2010, testCal.get( Calendar.YEAR) );
+        assertEquals( 0, testCal.get( Calendar.MONTH) );
+        
+    }
+        
+    @Test
     public void testGetNextPeriod()
     {
         testCal.set( 2009, 8, 15 );