Search in sources :

Example 1 with WeeklyAbstractPeriodType

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

the class DateUnitPeriodTypeParser method parse.

@Override
public DateInterval parse(Calendar calendar, String period) {
    DateUnitType dateUnitType = DateUnitType.find(period);
    if (dateUnitType == null) {
        return null;
    }
    if (compileCache.get(dateUnitType.getName()) == null) {
        try {
            Pattern pattern = Pattern.compile(dateUnitType.getPattern());
            compileCache.put(dateUnitType.getName(), pattern);
        } catch (PatternSyntaxException ex) {
            return null;
        }
    }
    Pattern pattern = compileCache.get(dateUnitType.getName());
    Matcher matcher = pattern.matcher(period);
    boolean match = matcher.find();
    if (!match) {
        return null;
    }
    if (DateUnitType.DAILY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int month = Integer.parseInt(matcher.group(2));
        int day = Integer.parseInt(matcher.group(3));
        DateTimeUnit dateTimeUnit = new DateTimeUnit(year, month, day, calendar.isIso8601());
        dateTimeUnit.setDayOfWeek(calendar.weekday(dateTimeUnit));
        return new DateInterval(dateTimeUnit, dateTimeUnit);
    } else if (DateUnitType.WEEKLY == dateUnitType || DateUnitType.WEEKLY_WEDNESDAY == dateUnitType || DateUnitType.WEEKLY_THURSDAY == dateUnitType || DateUnitType.WEEKLY_SATURDAY == dateUnitType || DateUnitType.WEEKLY_SUNDAY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int week = Integer.parseInt(matcher.group(2));
        WeeklyAbstractPeriodType periodType = (WeeklyAbstractPeriodType) PeriodType.getByNameIgnoreCase(dateUnitType.getName());
        if (periodType == null || week < 1 || week > calendar.weeksInYear(year)) {
            return null;
        }
        DateTimeUnit start = new DateTimeUnit(year, 1, 1, calendar.isIso8601());
        start = periodType.adjustToStartOfWeek(start, calendar);
        // since we rewind to start of week, we might end up in the previous years weeks, so we check and forward if needed
        if (calendar.isoWeek(start) == calendar.weeksInYear(year)) {
            start = calendar.plusWeeks(start, 1);
        }
        start = calendar.plusWeeks(start, week - 1);
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusWeeks(end, 1);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.MONTHLY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int month = Integer.parseInt(matcher.group(2));
        DateTimeUnit start = new DateTimeUnit(year, month, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(year, month, calendar.daysInMonth(start.getYear(), start.getMonth()), calendar.isIso8601());
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.BI_MONTHLY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int month = Integer.parseInt(matcher.group(2));
        if (month < 1 || month > 6) {
            return null;
        }
        DateTimeUnit start = new DateTimeUnit(year, (month * 2) - 1, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusMonths(end, 2);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.QUARTERLY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int quarter = Integer.parseInt(matcher.group(2));
        // valid quarters are from 1 - 4
        if (quarter < 1 || quarter > 4) {
            return null;
        }
        DateTimeUnit start = new DateTimeUnit(year, ((quarter - 1) * 3) + 1, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusMonths(end, 3);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.SIX_MONTHLY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int semester = Integer.parseInt(matcher.group(2));
        // valid six-monthly are from 1 - 2
        if (semester < 1 || semester > 2) {
            return null;
        }
        DateTimeUnit start = new DateTimeUnit(year, semester == 1 ? 1 : 7, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusMonths(end, 6);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.SIX_MONTHLY_APRIL == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int semester = Integer.parseInt(matcher.group(2));
        // valid six-monthly are from 1 - 2
        if (semester < 1 || semester > 2) {
            return null;
        }
        DateTimeUnit start = new DateTimeUnit(year, semester == 1 ? 4 : 10, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusMonths(end, 6);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.YEARLY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        DateTimeUnit start = new DateTimeUnit(year, 1, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(year, calendar.monthsInYear(), calendar.daysInMonth(start.getYear(), calendar.monthsInYear()), calendar.isIso8601());
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.FINANCIAL_APRIL == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        DateTimeUnit start = new DateTimeUnit(year, 4, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusYears(end, 1);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.FINANCIAL_JULY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        DateTimeUnit start = new DateTimeUnit(year, 7, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusYears(end, 1);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.FINANCIAL_OCTOBER == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        DateTimeUnit start = new DateTimeUnit(year, 10, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusYears(end, 1);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) WeeklyAbstractPeriodType(org.hisp.dhis.period.WeeklyAbstractPeriodType) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 2 with WeeklyAbstractPeriodType

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

the class I18nFormat method formatPeriod.

/**
 * Formats a period. Returns null if value is null. Returns INVALID_DATE if
 * formatting string is invalid.
 *
 * @param period the value to format.
 */
public String formatPeriod(Period period) {
    if (period == null) {
        return null;
    }
    PeriodType periodType = period.getPeriodType();
    String typeName = periodType.getName();
    if (// Use ISO dates
    periodType instanceof WeeklyAbstractPeriodType) // due to
    // potential week
    // confusion
    {
        DateTime dateTime = new DateTime(period.getStartDate());
        LocalDate date = Instant.ofEpochMilli(period.getStartDate().getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
        WeekFields weekFields = WeekFields.of(PeriodType.MAP_WEEK_TYPE.get(periodType.getName()), 4);
        String year = String.valueOf(date.get(weekFields.weekBasedYear()));
        String week = String.valueOf(date.get(weekFields.weekOfWeekBasedYear()));
        if (periodType instanceof WeeklyPeriodType) {
            return String.format("W%s %s", week, year);
        }
        year += dateTime.dayOfWeek().getAsShortText() + " " + year;
        return String.format("W%s %s", week, year);
    } else if (periodType instanceof BiWeeklyAbstractPeriodType) {
        int year;
        int week;
        Calendar calendar = PeriodType.getCalendar();
        BiWeeklyAbstractPeriodType biWeeklyAbstractPeriodType = (BiWeeklyAbstractPeriodType) periodType;
        DateTimeUnit dateTimeUnit = DateTimeUnit.fromJdkDate(period.getStartDate());
        if (calendar.isIso8601()) {
            LocalDate date = LocalDate.of(dateTimeUnit.getYear(), dateTimeUnit.getMonth(), dateTimeUnit.getDay());
            WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY, 4);
            year = date.get(weekFields.weekBasedYear());
            week = (date.get(weekFields.weekOfWeekBasedYear()) / 2) + 1;
        } else {
            DateTimeUnit date = biWeeklyAbstractPeriodType.adjustToStartOfBiWeek(dateTimeUnit, calendar);
            week = calendar.week(date);
            if (week == 1 && date.getMonth() == calendar.monthsInYear()) {
                date.setYear(date.getYear() + 1);
            }
            year = date.getYear();
        }
        return String.format("BiW%s %s", week, year);
    }
    String keyStartDate = "format." + typeName + ".startDate";
    String keyEndDate = "format." + typeName + ".endDate";
    String startPattern = resourceBundle.getString(keyStartDate);
    String endPattern = resourceBundle.getString(keyEndDate);
    boolean dayPattern = startPattern.contains("dd") || endPattern.contains("dd");
    Date periodStartDate = period.getStartDate();
    Date periodEndDate = period.getEndDate();
    DateTimeUnit start = PeriodType.getCalendar().fromIso(periodStartDate);
    DateTimeUnit end = PeriodType.getCalendar().fromIso(periodEndDate);
    String startDate;
    String endDate;
    if (!dayPattern) {
        // Set day to first of month to not overflow when converting to JDK
        // date
        start.setDay(1);
        end.setDay(1);
        startDate = commonFormatting(new DateTimeUnit(start, true).toJdkDate(), startPattern);
        endDate = commonFormatting(new DateTimeUnit(end, true).toJdkDate(), endPattern);
    } else {
        startDate = PeriodType.getCalendar().formattedDate(startPattern, start);
        endDate = PeriodType.getCalendar().formattedDate(endPattern, end);
    }
    try {
        return Character.toUpperCase(startDate.charAt(0)) + startDate.substring(1) + endDate;
    } catch (IllegalArgumentException ex) {
        return INVALID_DATE;
    }
}
Also used : BiWeeklyAbstractPeriodType(org.hisp.dhis.period.BiWeeklyAbstractPeriodType) WeeklyAbstractPeriodType(org.hisp.dhis.period.WeeklyAbstractPeriodType) PeriodType(org.hisp.dhis.period.PeriodType) WeeklyPeriodType(org.hisp.dhis.period.WeeklyPeriodType) Calendar(org.hisp.dhis.calendar.Calendar) BiWeeklyAbstractPeriodType(org.hisp.dhis.period.BiWeeklyAbstractPeriodType) WeeklyAbstractPeriodType(org.hisp.dhis.period.WeeklyAbstractPeriodType) LocalDate(java.time.LocalDate) DateTime(org.joda.time.DateTime) BiWeeklyAbstractPeriodType(org.hisp.dhis.period.BiWeeklyAbstractPeriodType) Date(java.util.Date) LocalDate(java.time.LocalDate) WeeklyPeriodType(org.hisp.dhis.period.WeeklyPeriodType) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) WeekFields(java.time.temporal.WeekFields)

Example 3 with WeeklyAbstractPeriodType

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

the class DateUnitPeriodTypeParser method parseInternal.

private DateInterval parseInternal(Calendar calendar, String period, DateUnitType.DateUnitTypeWithPattern dateUnitTypeWithPattern) {
    Pattern pattern = dateUnitTypeWithPattern.getPattern();
    DateUnitType dateUnitType = dateUnitTypeWithPattern.getDateUnitType();
    Matcher matcher = pattern.matcher(period);
    boolean match = matcher.find();
    if (!match) {
        return null;
    }
    if (DateUnitType.DAILY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int month = Integer.parseInt(matcher.group(2));
        int day = Integer.parseInt(matcher.group(3));
        DateTimeUnit dateTimeUnit = new DateTimeUnit(year, month, day, calendar.isIso8601());
        dateTimeUnit.setDayOfWeek(calendar.weekday(dateTimeUnit));
        return new DateInterval(dateTimeUnit, dateTimeUnit);
    } else if (DateUnitType.WEEKLY == dateUnitType || DateUnitType.WEEKLY_WEDNESDAY == dateUnitType || DateUnitType.WEEKLY_THURSDAY == dateUnitType || DateUnitType.WEEKLY_SATURDAY == dateUnitType || DateUnitType.WEEKLY_SUNDAY == dateUnitType) {
        DateTimeUnit start;
        DateTimeUnit end;
        int year = Integer.parseInt(matcher.group(1));
        int week = Integer.parseInt(matcher.group(2));
        WeeklyAbstractPeriodType periodType = (WeeklyAbstractPeriodType) PeriodType.getByNameIgnoreCase(dateUnitType.getName());
        if (periodType == null || week < 1 || week > calendar.weeksInYear(year)) {
            return null;
        }
        start = getDateTimeFromWeek(year, week, calendar, PeriodType.MAP_WEEK_TYPE.get(periodType.getName()), new DateTimeUnit(year, 1, 1));
        end = calendar.plusWeeks(start, 1);
        end = calendar.minusDays(end, 1);
        return new DateInterval(start, end);
    } else if (DateUnitType.BI_WEEKLY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int week = Integer.parseInt(matcher.group(2)) * 2 - 1;
        BiWeeklyPeriodType periodType = (BiWeeklyPeriodType) PeriodType.getByNameIgnoreCase(dateUnitType.getName());
        if (periodType == null || week < 1 || week > calendar.weeksInYear(year)) {
            return null;
        }
        DateTimeUnit start = getDateTimeFromWeek(year, week, calendar, DayOfWeek.MONDAY, new DateTimeUnit(year, 1, 1));
        DateTimeUnit end = calendar.plusWeeks(start, 2);
        end = calendar.minusDays(end, 1);
        return new DateInterval(start, end);
    } else if (DateUnitType.MONTHLY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int month = Integer.parseInt(matcher.group(2));
        DateTimeUnit start = new DateTimeUnit(year, month, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(year, month, calendar.daysInMonth(start.getYear(), start.getMonth()), calendar.isIso8601());
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.BI_MONTHLY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int month = Integer.parseInt(matcher.group(2));
        if (month < 1 || month > 6) {
            return null;
        }
        DateTimeUnit start = new DateTimeUnit(year, (month * 2) - 1, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusMonths(end, 2);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.QUARTERLY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int quarter = Integer.parseInt(matcher.group(2));
        // valid quarters are from 1 - 4
        if (quarter < 1 || quarter > 4) {
            return null;
        }
        DateTimeUnit start = new DateTimeUnit(year, ((quarter - 1) * 3) + 1, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusMonths(end, 3);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.SIX_MONTHLY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int semester = Integer.parseInt(matcher.group(2));
        // valid six-monthly are from 1 - 2
        if (semester < 1 || semester > 2) {
            return null;
        }
        DateTimeUnit start = new DateTimeUnit(year, semester == 1 ? 1 : 7, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusMonths(end, 6);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.SIX_MONTHLY_APRIL == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int semester = Integer.parseInt(matcher.group(2));
        // valid six-monthly are from 1 - 2
        if (semester < 1 || semester > 2) {
            return null;
        }
        DateTimeUnit start = new DateTimeUnit(year, semester == 1 ? 4 : 10, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusMonths(end, 6);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.SIX_MONTHLY_NOVEMBER == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int semester = Integer.parseInt(matcher.group(2));
        // valid six-monthly are from 1 - 2
        if (semester < 1 || semester > 2) {
            return null;
        }
        DateTimeUnit start = new DateTimeUnit(semester == 1 ? year - 1 : year, semester == 1 ? 11 : 5, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusMonths(end, 6);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.YEARLY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        DateTimeUnit start = new DateTimeUnit(year, 1, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(year, calendar.monthsInYear(), calendar.daysInMonth(start.getYear(), calendar.monthsInYear()), calendar.isIso8601());
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.FINANCIAL_APRIL == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        DateTimeUnit start = new DateTimeUnit(year, 4, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusYears(end, 1);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.FINANCIAL_JULY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        DateTimeUnit start = new DateTimeUnit(year, 7, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusYears(end, 1);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.FINANCIAL_OCTOBER == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        DateTimeUnit start = new DateTimeUnit(year, 10, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusYears(end, 1);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.FINANCIAL_NOVEMBER == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        DateTimeUnit start = new DateTimeUnit(year - 1, 11, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusYears(end, 1);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) BiWeeklyPeriodType(org.hisp.dhis.period.BiWeeklyPeriodType) WeeklyAbstractPeriodType(org.hisp.dhis.period.WeeklyAbstractPeriodType)

Aggregations

WeeklyAbstractPeriodType (org.hisp.dhis.period.WeeklyAbstractPeriodType)3 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 LocalDate (java.time.LocalDate)1 WeekFields (java.time.temporal.WeekFields)1 Date (java.util.Date)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 Calendar (org.hisp.dhis.calendar.Calendar)1 DateTimeUnit (org.hisp.dhis.calendar.DateTimeUnit)1 BiWeeklyAbstractPeriodType (org.hisp.dhis.period.BiWeeklyAbstractPeriodType)1 BiWeeklyPeriodType (org.hisp.dhis.period.BiWeeklyPeriodType)1 PeriodType (org.hisp.dhis.period.PeriodType)1 WeeklyPeriodType (org.hisp.dhis.period.WeeklyPeriodType)1 DateTime (org.joda.time.DateTime)1