dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #13796
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 4521: Determine periodtype from an iso string formated period.
------------------------------------------------------------
revno: 4521
committer: Bob Jolliffe bobjolliffe@xxxxxxxxx
branch nick: dhis2
timestamp: Mon 2011-09-05 22:35:18 +0100
message:
Determine periodtype from an iso string formated period.
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java
dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/PeriodTypeTest.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/PeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java 2011-09-02 10:40:22 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java 2011-09-05 21:35:18 +0000
@@ -37,6 +37,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.regex.Pattern;
/**
* The superclass of all PeriodTypes.
@@ -254,7 +255,27 @@
throw new RuntimeException( "Failed to parse medium date", ex );
}
}
-
+
+ /**
+ * Returns a PeriodType corresponding to the provided string
+ * The test is quite rudimentary, testing for string format rather than invalid periods.
+ * Currently only recognizes the basic subset of common period types.
+ *
+ * @param isoPeriod String formatted period (2011, 201101, 2011W34, 2011Q1 etc
+ * @return the PeriodType or null if unrecognised
+ */
+ public static PeriodType getPeriodTypeFromIsoString(String isoPeriod)
+ {
+ if (isoPeriod.matches("\\b\\d{4}\\b")) return new YearlyPeriodType();
+ if (isoPeriod.matches("\\b\\d{6}\\b")) return new MonthlyPeriodType();
+ if (isoPeriod.matches("\\b\\d{4}W\\d[\\d]?\\b")) return new WeeklyPeriodType();
+ if (isoPeriod.matches("\\b\\d{8}\\b")) return new DailyPeriodType();
+ if (isoPeriod.matches("\\b\\d{4}Q\\d\\b")) return new QuarterlyPeriodType();
+ if (isoPeriod.matches("\\b\\d{4}S\\d\\b")) return new SixMonthlyPeriodType();
+
+ return null;
+ }
+
/**
* Returns an iso8601 formatted string representation of the period
*
=== modified file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/PeriodTypeTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/PeriodTypeTest.java 2011-09-02 10:40:22 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/PeriodTypeTest.java 2011-09-05 21:35:18 +0000
@@ -44,4 +44,23 @@
}
+ @Test
+ public void testGetPeriodTypeFromIsoString()
+ {
+ assertEquals( PeriodType.getPeriodTypeFromIsoString( "2011").getName(),"Yearly");
+ assertEquals( PeriodType.getPeriodTypeFromIsoString( "201101").getName(),"Monthly");
+ assertEquals( PeriodType.getPeriodTypeFromIsoString( "2011W1").getName(),"Weekly");
+ assertEquals( PeriodType.getPeriodTypeFromIsoString( "2011W32").getName(),"Weekly");
+ assertEquals( PeriodType.getPeriodTypeFromIsoString( "20110101").getName(),"Daily");
+ assertEquals( PeriodType.getPeriodTypeFromIsoString( "2011Q3").getName(),"Quarterly");
+
+ assertNull(PeriodType.getPeriodTypeFromIsoString( "201"));
+ assertNull(PeriodType.getPeriodTypeFromIsoString( "20111"));
+ assertNull(PeriodType.getPeriodTypeFromIsoString( "201W2"));
+ assertNull(PeriodType.getPeriodTypeFromIsoString( "2011Q12"));
+ assertNull(PeriodType.getPeriodTypeFromIsoString( "2011W234"));
+ assertNull(PeriodType.getPeriodTypeFromIsoString( "201er2345566"));
+ assertNull(PeriodType.getPeriodTypeFromIsoString( "2011Q10"));
+ }
+
}