Search in sources :

Example 6 with DailyPeriodType

use of org.hisp.dhis.period.DailyPeriodType in project dhis2-core by dhis2.

the class J2MEDataValueSMSListener method getPeriod.

public Period getPeriod(String periodName, PeriodType periodType) throws IllegalArgumentException {
    if (periodType instanceof DailyPeriodType) {
        return periodType.createPeriod(DateUtils.getMediumDate(periodName));
    }
    if (periodType instanceof WeeklyPeriodType) {
        return periodType.createPeriod(DateUtils.getMediumDate(periodName));
    }
    if (periodType instanceof MonthlyPeriodType) {
        int dashIndex = periodName.indexOf('-');
        if (dashIndex < 0) {
            return null;
        }
        int month = Integer.parseInt(periodName.substring(0, dashIndex));
        int year = Integer.parseInt(periodName.substring(dashIndex + 1, periodName.length()));
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        return periodType.createPeriod(cal.getTime());
    }
    if (periodType instanceof YearlyPeriodType) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, Integer.parseInt(periodName));
        return periodType.createPeriod(cal.getTime());
    }
    if (periodType instanceof QuarterlyPeriodType) {
        Calendar cal = Calendar.getInstance();
        int month = 0;
        if (periodName.substring(0, periodName.indexOf(" ")).equals("Jan")) {
            month = 1;
        } else if (periodName.substring(0, periodName.indexOf(" ")).equals("Apr")) {
            month = 4;
        } else if (periodName.substring(0, periodName.indexOf(" ")).equals("Jul")) {
            month = 6;
        } else if (periodName.substring(0, periodName.indexOf(" ")).equals("Oct")) {
            month = 10;
        }
        int year = Integer.parseInt(periodName.substring(periodName.lastIndexOf(" ") + 1));
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.YEAR, year);
        if (month != 0) {
            return periodType.createPeriod(cal.getTime());
        }
    }
    throw new IllegalArgumentException("Couldn't make a period of type " + periodType.getName() + " and name " + periodName);
}
Also used : DailyPeriodType(org.hisp.dhis.period.DailyPeriodType) WeeklyPeriodType(org.hisp.dhis.period.WeeklyPeriodType) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) QuarterlyPeriodType(org.hisp.dhis.period.QuarterlyPeriodType) Calendar(java.util.Calendar) YearlyPeriodType(org.hisp.dhis.period.YearlyPeriodType)

Aggregations

DailyPeriodType (org.hisp.dhis.period.DailyPeriodType)6 MonthlyPeriodType (org.hisp.dhis.period.MonthlyPeriodType)4 QuarterlyPeriodType (org.hisp.dhis.period.QuarterlyPeriodType)4 WeeklyPeriodType (org.hisp.dhis.period.WeeklyPeriodType)4 YearlyPeriodType (org.hisp.dhis.period.YearlyPeriodType)4 Calendar (java.util.Calendar)3 Date (java.util.Date)3 Period (org.hisp.dhis.period.Period)3 PeriodType (org.hisp.dhis.period.PeriodType)3 ArrayList (java.util.ArrayList)2 Cal (org.hisp.dhis.period.Cal)2 Locale (java.util.Locale)1 DataSet (org.hisp.dhis.api.mobile.model.DataSet)1 Calendar (org.hisp.dhis.calendar.Calendar)1 InvalidIdentifierReferenceException (org.hisp.dhis.common.exception.InvalidIdentifierReferenceException)1 UniqueArrayList (org.hisp.dhis.commons.collection.UniqueArrayList)1 BiMonthlyPeriodType (org.hisp.dhis.period.BiMonthlyPeriodType)1 FinancialAprilPeriodType (org.hisp.dhis.period.FinancialAprilPeriodType)1 FinancialJulyPeriodType (org.hisp.dhis.period.FinancialJulyPeriodType)1 FinancialOctoberPeriodType (org.hisp.dhis.period.FinancialOctoberPeriodType)1