Search in sources :

Example 6 with WeeklyPeriodType

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

the class PeriodUtil method getPeriod.

public static Period getPeriod(String periodName, PeriodType periodType) throws InvalidIdentifierReferenceException {
    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 InvalidIdentifierReferenceException("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) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException) YearlyPeriodType(org.hisp.dhis.period.YearlyPeriodType)

Example 7 with WeeklyPeriodType

use of org.hisp.dhis.period.WeeklyPeriodType 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

WeeklyPeriodType (org.hisp.dhis.period.WeeklyPeriodType)7 MonthlyPeriodType (org.hisp.dhis.period.MonthlyPeriodType)6 YearlyPeriodType (org.hisp.dhis.period.YearlyPeriodType)6 QuarterlyPeriodType (org.hisp.dhis.period.QuarterlyPeriodType)5 DailyPeriodType (org.hisp.dhis.period.DailyPeriodType)4 Calendar (java.util.Calendar)3 Date (java.util.Date)2 Period (org.hisp.dhis.period.Period)2 PeriodType (org.hisp.dhis.period.PeriodType)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 DataSet (org.hisp.dhis.api.mobile.model.DataSet)1 Calendar (org.hisp.dhis.calendar.Calendar)1 Iso8601Calendar (org.hisp.dhis.calendar.impl.Iso8601Calendar)1 InvalidIdentifierReferenceException (org.hisp.dhis.common.exception.InvalidIdentifierReferenceException)1 Expression (org.hisp.dhis.expression.Expression)1 BiMonthlyPeriodType (org.hisp.dhis.period.BiMonthlyPeriodType)1 Cal (org.hisp.dhis.period.Cal)1 FinancialAprilPeriodType (org.hisp.dhis.period.FinancialAprilPeriodType)1