← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19364: Added class for parsing ISO periods in the form "<date>/<duration>"

 

------------------------------------------------------------
revno: 19364
committer: Bob Jolliffe <bobjolliffe@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-06-11 15:38:10 +0100
message:
  Added class for parsing ISO periods in the form "<date>/<duration>"
added:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/adx/
  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/
  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
=== added directory 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/adx'
=== added 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	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/adx/ADXPeriod.java	2015-06-11 14:38:10 +0000
@@ -0,0 +1,152 @@
+package org.hisp.dhis.dxf2.adx;
+
+/*
+ * Copyright (c) 2004-2015, 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 java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import org.hisp.dhis.period.BiMonthlyPeriodType;
+import org.hisp.dhis.period.DailyPeriodType;
+import org.hisp.dhis.period.FinancialAprilPeriodType;
+import org.hisp.dhis.period.FinancialJulyPeriodType;
+import org.hisp.dhis.period.FinancialOctoberPeriodType;
+import org.hisp.dhis.period.MonthlyPeriodType;
+import org.hisp.dhis.period.Period;
+import org.hisp.dhis.period.PeriodType;
+import org.hisp.dhis.period.QuarterlyPeriodType;
+import org.hisp.dhis.period.SixMonthlyAprilPeriodType;
+import org.hisp.dhis.period.SixMonthlyPeriodType;
+import org.hisp.dhis.period.WeeklyPeriodType;
+import org.hisp.dhis.period.YearlyPeriodType;
+import org.hisp.dhis.system.util.DateUtils;
+
+/**
+ * ADXPeriod
+ *
+ * A simple wrapper class for parsing ISO 8601 <date>/<duration> period types
+ *
+ * @author bobj
+ */
+public class ADXPeriod {
+
+    public static class ADXPeriodException extends RuntimeException {
+
+        ADXPeriodException(String message) {
+            super(message);
+        }
+    }
+
+    public static enum Duration {
+
+        P1D, // daily
+        P7D, // weekly
+        P1M, // monthly
+        P2M, // bi-monthly
+
+        P1Q, // quarterly
+        P6M, // 6monthly (including 6monthlyApril)
+        P1Y  // yearly, financialApril, financialJuly, financialOctober
+    }
+
+    public static Period parse(String periodString) throws ADXPeriodException {
+        String[] tokens = periodString.split("/");
+        if (tokens.length != 2) {
+            throw new ADXPeriodException(periodString + " not in valid <date>/<duration> format");
+        }
+
+        try {
+            Period period = null;
+            PeriodType periodType = null;
+            Date startDate = new SimpleDateFormat("yyyy-MM-dd").parse(tokens[0]);
+            Calendar cal = Calendar.getInstance();
+            cal.setTime(startDate);
+            Duration duration = Duration.valueOf(tokens[1]);
+
+            switch (duration) {
+                case P1D:
+                    periodType = new DailyPeriodType();
+                    break;
+                case P7D:
+                    periodType = new WeeklyPeriodType();
+                    break;
+                case P1M:
+                    periodType = new MonthlyPeriodType();
+                    break;
+                case P2M:
+                    periodType = new BiMonthlyPeriodType();
+                    break;
+                case P1Q:
+                    periodType = new QuarterlyPeriodType();
+                    break;
+                case P6M:
+                    switch (cal.get(Calendar.MONTH)) {
+                        case 0:
+                            periodType = new SixMonthlyPeriodType();
+                            break;
+                        case 6:
+                            periodType = new SixMonthlyAprilPeriodType();
+                            break;
+                        default:
+                            throw new ADXPeriodException(periodString + "is invalid sixmonthly type");
+                    }
+                case P1Y:
+                    switch (cal.get(Calendar.MONTH)) {
+                        case 0:
+                            periodType = new YearlyPeriodType();
+                            break;
+                        case 3:
+                            periodType = new FinancialAprilPeriodType();
+                            break;
+                        case 6:
+                            periodType = new FinancialJulyPeriodType();
+                            break;
+                        case 9:
+                            periodType = new FinancialOctoberPeriodType();
+                            break;
+                        default:
+                            throw new ADXPeriodException(periodString + "is invalid yearly type");
+                    }
+            }
+
+            if (periodType != null) {
+                period = periodType.createPeriod(startDate);
+            } else {
+                throw new ADXPeriodException("Failed to create period type from " + duration);
+            }
+
+            return period;
+
+        } catch (ParseException ex) {
+            throw new ADXPeriodException(tokens[0] + "is not a valid date in YYYY-MM-dd format");
+        } catch (IllegalArgumentException ex) {
+            throw new ADXPeriodException(tokens[1] + " is not a supported duration type");
+        }
+    }
+}

=== added directory 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/adx'
=== added 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	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/adx/ADXPeriodTest.java	2015-06-11 14:38:10 +0000
@@ -0,0 +1,99 @@
+package org.hisp.dhis.dxf2.adx;
+
+/*
+ * Copyright (c) 2004-2015, 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.period.Period;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author bobj
+ */
+public class ADXPeriodTest {
+    
+    private Period p;
+    
+    public ADXPeriodTest() {
+    }
+    
+    @Test
+    public void testParser() {
+        p = ADXPeriod.parse("2015-01-01/P1Y");
+        assertEquals("2015", p.getIsoDate());
+        p = ADXPeriod.parse("2015-01-01/P1M");
+        assertEquals("201501", p.getIsoDate());
+        p = ADXPeriod.parse("2015-01-01/P1D");
+        assertEquals("20150101", p.getIsoDate());
+        p = ADXPeriod.parse("2015-01-01/P1Q");
+        assertEquals("2015Q1", p.getIsoDate());
+        p = ADXPeriod.parse("2015-04-01/P1Q");
+        assertEquals("2015Q2", p.getIsoDate());
+        p = ADXPeriod.parse("2015-01-01/P7D");
+        assertEquals("2015W1", p.getIsoDate());
+        p = ADXPeriod.parse("2015-01-05/P7D");
+        assertEquals("2015W2", p.getIsoDate());
+    }
+
+    @Test
+    public void testBadDuration() {
+        try {
+            p = ADXPeriod.parse("2014-01-01/P1");
+            fail("Should have thrown exception parsing 2015-01-01/P1");
+        }
+        catch (Exception ex)
+        {
+            assertEquals(ADXPeriod.ADXPeriodException.class,ex.getClass());
+        }
+    }
+
+    @Test
+    public void testFinancialTypes() {
+        p = ADXPeriod.parse("2015-01-01/P1Y");
+        assertEquals("2015", p.getIsoDate());
+        p = ADXPeriod.parse("2015-04-01/P1Y");
+        assertEquals("2015April", p.getIsoDate());
+        p = ADXPeriod.parse("2015-07-01/P1Y");
+        assertEquals("2015July", p.getIsoDate());
+        p = ADXPeriod.parse("2015-10-01/P1Y");
+        assertEquals("2015Oct", p.getIsoDate());
+    }
+ 
+    @Test
+    public void testFailFinancialType() {
+        try {
+            p = ADXPeriod.parse("2014-02-01/P1Y");
+            fail("Should have thrown exception parsing 2014-02-01/P1Y");
+        }
+        catch (Exception ex)
+        {
+            assertEquals(ADXPeriod.ADXPeriodException.class,ex.getClass());
+        }
+    }
+}