Search in sources :

Example 11 with YearlyPeriodType

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

the class AnalyticsUtilsTest method testIsPeriodOverApprovalThreshold.

@Test
public void testIsPeriodOverApprovalThreshold() {
    java.util.Calendar calendar = java.util.Calendar.getInstance();
    int currentYear = calendar.get(Calendar.YEAR);
    YearlyPeriodType pt = new YearlyPeriodType();
    calendar.set(currentYear, 1, 1);
    Period thisYear = pt.createPeriod(Date.from(calendar.toInstant()));
    calendar.set(currentYear - 1, 1, 1);
    Period oneYearAgo = pt.createPeriod(Date.from(calendar.toInstant()));
    calendar.set(currentYear - 2, 1, 1);
    Period twoYearAgo = pt.createPeriod(Date.from(calendar.toInstant()));
    calendar.set(currentYear - 3, 1, 1);
    Period threeYearAgo = pt.createPeriod(Date.from(calendar.toInstant()));
    // maxYears = 0 should always return false
    assertTrue(!AnalyticsUtils.periodIsOutsideApprovalMaxYears(thisYear, 0));
    assertTrue(!AnalyticsUtils.periodIsOutsideApprovalMaxYears(oneYearAgo, 0));
    assertTrue(!AnalyticsUtils.periodIsOutsideApprovalMaxYears(twoYearAgo, 0));
    assertTrue(!AnalyticsUtils.periodIsOutsideApprovalMaxYears(threeYearAgo, 0));
    // maxYears = 1 should only return true for years other than thisYear
    assertTrue(!AnalyticsUtils.periodIsOutsideApprovalMaxYears(thisYear, 1));
    assertTrue(AnalyticsUtils.periodIsOutsideApprovalMaxYears(oneYearAgo, 1));
    assertTrue(AnalyticsUtils.periodIsOutsideApprovalMaxYears(twoYearAgo, 1));
    assertTrue(AnalyticsUtils.periodIsOutsideApprovalMaxYears(threeYearAgo, 1));
    // maxYears = 4 should only return false for all three years defined
    assertTrue(!AnalyticsUtils.periodIsOutsideApprovalMaxYears(thisYear, 5));
    assertTrue(!AnalyticsUtils.periodIsOutsideApprovalMaxYears(oneYearAgo, 5));
    assertTrue(!AnalyticsUtils.periodIsOutsideApprovalMaxYears(twoYearAgo, 5));
    assertTrue(!AnalyticsUtils.periodIsOutsideApprovalMaxYears(threeYearAgo, 5));
}
Also used : Calendar(java.util.Calendar) Period(org.hisp.dhis.period.Period) YearlyPeriodType(org.hisp.dhis.period.YearlyPeriodType) Test(org.junit.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 12 with YearlyPeriodType

use of org.hisp.dhis.period.YearlyPeriodType 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 = null;
        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:
                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 AdxException(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 AdxException(periodString + "is invalid yearly type");
                }
        }
        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 : DailyPeriodType(org.hisp.dhis.period.DailyPeriodType) FinancialJulyPeriodType(org.hisp.dhis.period.FinancialJulyPeriodType) FinancialOctoberPeriodType(org.hisp.dhis.period.FinancialOctoberPeriodType) YearlyPeriodType(org.hisp.dhis.period.YearlyPeriodType) BiMonthlyPeriodType(org.hisp.dhis.period.BiMonthlyPeriodType) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) QuarterlyPeriodType(org.hisp.dhis.period.QuarterlyPeriodType) SixMonthlyAprilPeriodType(org.hisp.dhis.period.SixMonthlyAprilPeriodType) DailyPeriodType(org.hisp.dhis.period.DailyPeriodType) FinancialAprilPeriodType(org.hisp.dhis.period.FinancialAprilPeriodType) PeriodType(org.hisp.dhis.period.PeriodType) WeeklyPeriodType(org.hisp.dhis.period.WeeklyPeriodType) SixMonthlyPeriodType(org.hisp.dhis.period.SixMonthlyPeriodType) BiMonthlyPeriodType(org.hisp.dhis.period.BiMonthlyPeriodType) QuarterlyPeriodType(org.hisp.dhis.period.QuarterlyPeriodType) FinancialJulyPeriodType(org.hisp.dhis.period.FinancialJulyPeriodType) Calendar(java.util.Calendar) Period(org.hisp.dhis.period.Period) SixMonthlyAprilPeriodType(org.hisp.dhis.period.SixMonthlyAprilPeriodType) YearlyPeriodType(org.hisp.dhis.period.YearlyPeriodType) Date(java.util.Date) WeeklyPeriodType(org.hisp.dhis.period.WeeklyPeriodType) BiMonthlyPeriodType(org.hisp.dhis.period.BiMonthlyPeriodType) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) SixMonthlyPeriodType(org.hisp.dhis.period.SixMonthlyPeriodType) FinancialOctoberPeriodType(org.hisp.dhis.period.FinancialOctoberPeriodType) SixMonthlyPeriodType(org.hisp.dhis.period.SixMonthlyPeriodType) FinancialAprilPeriodType(org.hisp.dhis.period.FinancialAprilPeriodType)

Example 13 with YearlyPeriodType

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

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

the class FormUtilsImpl method getPeriodsForDataSet.

@Override
public List<Period> getPeriodsForDataSet(Integer dataSetId, int first, int max) {
    Validate.notNull(dataSetId);
    DataSet dataSet = dataSetService.getDataSet(dataSetId);
    CalendarPeriodType periodType;
    if (dataSet.getPeriodType().getName().equalsIgnoreCase("Yearly")) {
        periodType = new YearlyPeriodType();
    } else {
        periodType = (CalendarPeriodType) dataSet.getPeriodType();
    }
    if (dataSet.getOpenFuturePeriods() > 0) {
        List<Period> periods = periodType.generatePeriods(new Date());
        Collections.reverse(periods);
        return periods;
    } else {
        List<Period> periods = periodType.generateLast5Years(new Date());
        FilterUtils.filter(periods, new PastAndCurrentPeriodFilter());
        Collections.reverse(periods);
        if (periods.size() > (first + max)) {
            periods = periods.subList(first, max);
        }
        return periods;
    }
}
Also used : DataSet(org.hisp.dhis.dataset.DataSet) Period(org.hisp.dhis.period.Period) CalendarPeriodType(org.hisp.dhis.period.CalendarPeriodType) YearlyPeriodType(org.hisp.dhis.period.YearlyPeriodType) PastAndCurrentPeriodFilter(org.hisp.dhis.system.filter.PastAndCurrentPeriodFilter)

Example 15 with YearlyPeriodType

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

YearlyPeriodType (org.hisp.dhis.period.YearlyPeriodType)15 MonthlyPeriodType (org.hisp.dhis.period.MonthlyPeriodType)9 Test (org.junit.Test)9 QuarterlyPeriodType (org.hisp.dhis.period.QuarterlyPeriodType)8 Period (org.hisp.dhis.period.Period)6 WeeklyPeriodType (org.hisp.dhis.period.WeeklyPeriodType)6 Calendar (java.util.Calendar)4 DataSet (org.hisp.dhis.dataset.DataSet)4 DailyPeriodType (org.hisp.dhis.period.DailyPeriodType)4 DhisSpringTest (org.hisp.dhis.DhisSpringTest)2 AnalyticsTable (org.hisp.dhis.analytics.AnalyticsTable)2 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)2 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)2 DataApprovalWorkflow (org.hisp.dhis.dataapproval.DataApprovalWorkflow)2 PeriodType (org.hisp.dhis.period.PeriodType)2 DateTime (org.joda.time.DateTime)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1