Search in sources :

Example 11 with QuarterlyPeriodType

use of org.hisp.dhis.period.QuarterlyPeriodType 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 12 with QuarterlyPeriodType

use of org.hisp.dhis.period.QuarterlyPeriodType 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));
        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;
        switch(periodName.substring(0, periodName.indexOf(" "))) {
            case "Jan":
                month = 1;
                break;
            case "Apr":
                month = 4;
                break;
            case "Jul":
                month = 6;
                break;
            case "Oct":
                month = 10;
                break;
        }
        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)

Example 13 with QuarterlyPeriodType

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

the class AdxPeriod method parse.

public static Period parse(String periodString) throws AdxException {
    String[] tokens = periodString.split("/");
    if (tokens.length != 2) {
        throw new AdxException(periodString + " not in valid <date>/<duration> format");
    }
    try {
        Period period;
        PeriodType periodType = null;
        Date startDate = DateUtils.getMediumDate(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:
                switch(cal.get(Calendar.DAY_OF_WEEK)) {
                    case MONDAY:
                        periodType = new WeeklyPeriodType();
                        break;
                    case WEDNESDAY:
                        periodType = new WeeklyWednesdayPeriodType();
                        break;
                    case THURSDAY:
                        periodType = new WeeklyThursdayPeriodType();
                        break;
                    case SATURDAY:
                        periodType = new WeeklySaturdayPeriodType();
                        break;
                    case SUNDAY:
                        periodType = new WeeklySundayPeriodType();
                        break;
                    default:
                        throw new AdxException(periodString + " is invalid weekly type");
                }
                break;
            case P14D:
                periodType = new BiWeeklyPeriodType();
                break;
            case P1M:
                periodType = new MonthlyPeriodType();
                break;
            case P2M:
                periodType = new BiMonthlyPeriodType();
                break;
            case P3M:
                periodType = new QuarterlyPeriodType();
                break;
            case P6M:
                switch(cal.get(Calendar.MONTH)) {
                    case JANUARY:
                    case JULY:
                        periodType = new SixMonthlyPeriodType();
                        break;
                    case APRIL:
                    case OCTOBER:
                        periodType = new SixMonthlyAprilPeriodType();
                        break;
                    case NOVEMBER:
                    case MAY:
                        periodType = new SixMonthlyNovemberPeriodType();
                        break;
                    default:
                        throw new AdxException(periodString + " is invalid sixmonthly type");
                }
                break;
            case P1Y:
                switch(cal.get(Calendar.MONTH)) {
                    case JANUARY:
                        periodType = new YearlyPeriodType();
                        break;
                    case APRIL:
                        periodType = new FinancialAprilPeriodType();
                        break;
                    case JULY:
                        periodType = new FinancialJulyPeriodType();
                        break;
                    case OCTOBER:
                        periodType = new FinancialOctoberPeriodType();
                        break;
                    case NOVEMBER:
                        periodType = new FinancialNovemberPeriodType();
                        break;
                    default:
                        throw new AdxException(periodString + " is invalid yearly type");
                }
                break;
        }
        if (periodType != null) {
            period = periodType.createPeriod(startDate);
        } else {
            throw new AdxException("Failed to create period type from " + duration);
        }
        return period;
    } catch (IllegalArgumentException ex) {
        throw new AdxException(tokens[1] + " is not a supported duration type");
    }
}
Also used : SixMonthlyNovemberPeriodType(org.hisp.dhis.period.SixMonthlyNovemberPeriodType) WeeklyWednesdayPeriodType(org.hisp.dhis.period.WeeklyWednesdayPeriodType) FinancialNovemberPeriodType(org.hisp.dhis.period.FinancialNovemberPeriodType) YearlyPeriodType(org.hisp.dhis.period.YearlyPeriodType) BiMonthlyPeriodType(org.hisp.dhis.period.BiMonthlyPeriodType) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) WeeklyThursdayPeriodType(org.hisp.dhis.period.WeeklyThursdayPeriodType) QuarterlyPeriodType(org.hisp.dhis.period.QuarterlyPeriodType) FinancialAprilPeriodType(org.hisp.dhis.period.FinancialAprilPeriodType) WeeklyPeriodType(org.hisp.dhis.period.WeeklyPeriodType) FinancialJulyPeriodType(org.hisp.dhis.period.FinancialJulyPeriodType) WeeklySundayPeriodType(org.hisp.dhis.period.WeeklySundayPeriodType) FinancialOctoberPeriodType(org.hisp.dhis.period.FinancialOctoberPeriodType) WeeklySaturdayPeriodType(org.hisp.dhis.period.WeeklySaturdayPeriodType) SixMonthlyAprilPeriodType(org.hisp.dhis.period.SixMonthlyAprilPeriodType) DailyPeriodType(org.hisp.dhis.period.DailyPeriodType) PeriodType(org.hisp.dhis.period.PeriodType) BiWeeklyPeriodType(org.hisp.dhis.period.BiWeeklyPeriodType) SixMonthlyPeriodType(org.hisp.dhis.period.SixMonthlyPeriodType) BiMonthlyPeriodType(org.hisp.dhis.period.BiMonthlyPeriodType) WeeklySaturdayPeriodType(org.hisp.dhis.period.WeeklySaturdayPeriodType) FinancialJulyPeriodType(org.hisp.dhis.period.FinancialJulyPeriodType) SixMonthlyAprilPeriodType(org.hisp.dhis.period.SixMonthlyAprilPeriodType) YearlyPeriodType(org.hisp.dhis.period.YearlyPeriodType) FinancialAprilPeriodType(org.hisp.dhis.period.FinancialAprilPeriodType) WeeklyThursdayPeriodType(org.hisp.dhis.period.WeeklyThursdayPeriodType) DailyPeriodType(org.hisp.dhis.period.DailyPeriodType) SixMonthlyNovemberPeriodType(org.hisp.dhis.period.SixMonthlyNovemberPeriodType) QuarterlyPeriodType(org.hisp.dhis.period.QuarterlyPeriodType) Calendar(java.util.Calendar) Period(org.hisp.dhis.period.Period) WeeklySundayPeriodType(org.hisp.dhis.period.WeeklySundayPeriodType) Date(java.util.Date) WeeklyPeriodType(org.hisp.dhis.period.WeeklyPeriodType) BiWeeklyPeriodType(org.hisp.dhis.period.BiWeeklyPeriodType) FinancialNovemberPeriodType(org.hisp.dhis.period.FinancialNovemberPeriodType) WeeklyWednesdayPeriodType(org.hisp.dhis.period.WeeklyWednesdayPeriodType) BiMonthlyPeriodType(org.hisp.dhis.period.BiMonthlyPeriodType) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) SixMonthlyPeriodType(org.hisp.dhis.period.SixMonthlyPeriodType) FinancialOctoberPeriodType(org.hisp.dhis.period.FinancialOctoberPeriodType) BiWeeklyPeriodType(org.hisp.dhis.period.BiWeeklyPeriodType) SixMonthlyPeriodType(org.hisp.dhis.period.SixMonthlyPeriodType)

Example 14 with QuarterlyPeriodType

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

the class PersianCalendarTest method testGenerateQuarterlyPeriods.

@Test
void testGenerateQuarterlyPeriods() {
    Date startDate = new Cal(2017, 3, 21, true).time();
    Date endDate = new Cal(2017, 6, 21, true).time();
    List<Period> monthly = new QuarterlyPeriodType().generatePeriods(calendar, startDate, endDate);
    assertEquals(1, monthly.size());
}
Also used : QuarterlyPeriodType(org.hisp.dhis.period.QuarterlyPeriodType) Period(org.hisp.dhis.period.Period) Date(java.util.Date) Cal(org.hisp.dhis.period.Cal) Test(org.junit.jupiter.api.Test)

Example 15 with QuarterlyPeriodType

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

the class DataSetFrequencyComparatorTest method testA.

@Test
void testA() {
    DataSet dsA = new DataSet("DataSetA", new QuarterlyPeriodType());
    DataSet dsB = new DataSet("DataSetB", new YearlyPeriodType());
    DataSet dsC = new DataSet("DataSetC", new MonthlyPeriodType());
    DataSet dsD = new DataSet("DataSetD", new QuarterlyPeriodType());
    List<DataSet> list = Lists.newArrayList(dsA, dsC, dsB, dsD);
    Collections.sort(list, DataSetFrequencyComparator.INSTANCE);
    assertEquals(dsC, list.get(0));
    assertEquals(dsA, list.get(1));
    assertEquals(dsD, list.get(2));
    assertEquals(dsB, list.get(3));
}
Also used : DataSet(org.hisp.dhis.dataset.DataSet) QuarterlyPeriodType(org.hisp.dhis.period.QuarterlyPeriodType) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) YearlyPeriodType(org.hisp.dhis.period.YearlyPeriodType) Test(org.junit.jupiter.api.Test)

Aggregations

QuarterlyPeriodType (org.hisp.dhis.period.QuarterlyPeriodType)18 MonthlyPeriodType (org.hisp.dhis.period.MonthlyPeriodType)12 Test (org.junit.jupiter.api.Test)12 YearlyPeriodType (org.hisp.dhis.period.YearlyPeriodType)10 DataSet (org.hisp.dhis.dataset.DataSet)8 WeeklyPeriodType (org.hisp.dhis.period.WeeklyPeriodType)6 DataApprovalWorkflow (org.hisp.dhis.dataapproval.DataApprovalWorkflow)5 DailyPeriodType (org.hisp.dhis.period.DailyPeriodType)5 Calendar (java.util.Calendar)3 Date (java.util.Date)3 Period (org.hisp.dhis.period.Period)3 DataElement (org.hisp.dhis.dataelement.DataElement)2 BiMonthlyPeriodType (org.hisp.dhis.period.BiMonthlyPeriodType)2 BiWeeklyPeriodType (org.hisp.dhis.period.BiWeeklyPeriodType)2 Cal (org.hisp.dhis.period.Cal)2 FinancialAprilPeriodType (org.hisp.dhis.period.FinancialAprilPeriodType)2 FinancialJulyPeriodType (org.hisp.dhis.period.FinancialJulyPeriodType)2 FinancialNovemberPeriodType (org.hisp.dhis.period.FinancialNovemberPeriodType)2 FinancialOctoberPeriodType (org.hisp.dhis.period.FinancialOctoberPeriodType)2 PeriodType (org.hisp.dhis.period.PeriodType)2