Search in sources :

Example 16 with Calendar

use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.

the class PartitionUtils method getEarliestDate.

public static Date getEarliestDate(Integer lastYears) {
    Date earliest = null;
    if (lastYears != null) {
        Calendar calendar = PeriodType.getCalendar();
        DateTimeUnit dateTimeUnit = calendar.today();
        dateTimeUnit = calendar.minusYears(dateTimeUnit, lastYears - 1);
        dateTimeUnit.setMonth(1);
        dateTimeUnit.setDay(1);
        earliest = dateTimeUnit.toJdkDate();
    }
    return earliest;
}
Also used : Calendar(org.hisp.dhis.calendar.Calendar) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) Date(java.util.Date)

Example 17 with Calendar

use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.

the class DefaultDataQueryService method getDimension.

// TODO optimize so that org unit levels + boundary are used in query instead of fetching all org units one by one
@Override
public DimensionalObject getDimension(String dimension, List<String> items, Date relativePeriodDate, List<OrganisationUnit> userOrgUnits, I18nFormat format, boolean allowNull, boolean allowAllPeriodItems, IdScheme inputIdScheme) {
    final boolean allItems = items.isEmpty();
    if (DATA_X_DIM_ID.equals(dimension)) {
        List<DimensionalItemObject> dataDimensionItems = new ArrayList<>();
        for (String uid : items) {
            if (uid.startsWith(KEY_DE_GROUP)) {
                String groupUid = DimensionalObjectUtils.getUidFromGroupParam(uid);
                DataElementGroup group = idObjectManager.getObject(DataElementGroup.class, inputIdScheme, groupUid);
                if (group != null) {
                    dataDimensionItems.addAll(group.getMembers());
                }
            } else if (uid.startsWith(KEY_IN_GROUP)) {
                String groupUid = DimensionalObjectUtils.getUidFromGroupParam(uid);
                IndicatorGroup group = idObjectManager.getObject(IndicatorGroup.class, inputIdScheme, groupUid);
                if (group != null) {
                    dataDimensionItems.addAll(group.getMembers());
                }
            } else {
                DimensionalItemObject dimItemObject = dimensionService.getDataDimensionalItemObject(inputIdScheme, uid);
                if (dimItemObject != null) {
                    dataDimensionItems.add(dimItemObject);
                }
            }
        }
        if (dataDimensionItems.isEmpty()) {
            throw new IllegalQueryException("Dimension dx is present in query without any valid dimension options");
        }
        DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.DATA_X, null, DISPLAY_NAME_DATA_X, dataDimensionItems);
        return object;
    } else if (CATEGORYOPTIONCOMBO_DIM_ID.equals(dimension)) {
        List<DimensionalItemObject> cocs = new ArrayList<>();
        for (String uid : items) {
            DataElementCategoryOptionCombo coc = idObjectManager.getObject(DataElementCategoryOptionCombo.class, inputIdScheme, uid);
            if (coc != null) {
                cocs.add(coc);
            }
        }
        DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.CATEGORY_OPTION_COMBO, null, DISPLAY_NAME_CATEGORYOPTIONCOMBO, cocs);
        return object;
    } else if (ATTRIBUTEOPTIONCOMBO_DIM_ID.equals(dimension)) {
        List<DimensionalItemObject> aocs = new ArrayList<>();
        for (String uid : items) {
            DataElementCategoryOptionCombo aoc = idObjectManager.getObject(DataElementCategoryOptionCombo.class, inputIdScheme, uid);
            if (aoc != null) {
                aocs.add(aoc);
            }
        }
        DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.ATTRIBUTE_OPTION_COMBO, null, DISPLAY_NAME_ATTRIBUTEOPTIONCOMBO, aocs);
        return object;
    } else if (PERIOD_DIM_ID.equals(dimension)) {
        Calendar calendar = PeriodType.getCalendar();
        List<Period> periods = new ArrayList<>();
        for (String isoPeriod : items) {
            if (RelativePeriodEnum.contains(isoPeriod)) {
                RelativePeriodEnum relativePeriod = RelativePeriodEnum.valueOf(isoPeriod);
                List<Period> relativePeriods = RelativePeriods.getRelativePeriodsFromEnum(relativePeriod, relativePeriodDate, format, true);
                periods.addAll(relativePeriods);
            } else {
                Period period = PeriodType.getPeriodFromIsoString(isoPeriod);
                if (period != null) {
                    periods.add(period);
                }
            }
        }
        // Remove duplicates
        periods = periods.stream().distinct().collect(Collectors.toList());
        if (periods.isEmpty() && !allowAllPeriodItems) {
            throw new IllegalQueryException("Dimension pe is present in query without any valid dimension options");
        }
        for (Period period : periods) {
            String name = format != null ? format.formatPeriod(period) : null;
            period.setName(name);
            period.setShortName(name);
            if (!calendar.isIso8601()) {
                period.setUid(getLocalPeriodIdentifier(period, calendar));
            }
        }
        DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.PERIOD, null, DISPLAY_NAME_PERIOD, asList(periods));
        return object;
    } else if (ORGUNIT_DIM_ID.equals(dimension)) {
        List<DimensionalItemObject> ous = new ArrayList<>();
        List<Integer> levels = new ArrayList<>();
        List<OrganisationUnitGroup> groups = new ArrayList<>();
        for (String ou : items) {
            if (KEY_USER_ORGUNIT.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
                ous.addAll(userOrgUnits);
            } else if (KEY_USER_ORGUNIT_CHILDREN.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
                ous.addAll(OrganisationUnit.getSortedChildren(userOrgUnits));
            } else if (KEY_USER_ORGUNIT_GRANDCHILDREN.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
                ous.addAll(OrganisationUnit.getSortedGrandChildren(userOrgUnits));
            } else if (ou != null && ou.startsWith(KEY_LEVEL)) {
                int level = DimensionalObjectUtils.getLevelFromLevelParam(ou);
                if (level > 0) {
                    levels.add(level);
                }
            } else if (ou != null && ou.startsWith(KEY_ORGUNIT_GROUP)) {
                String uid = DimensionalObjectUtils.getUidFromGroupParam(ou);
                OrganisationUnitGroup group = idObjectManager.getObject(OrganisationUnitGroup.class, inputIdScheme, uid);
                if (group != null) {
                    groups.add(group);
                }
            } else if (!inputIdScheme.is(IdentifiableProperty.UID) || CodeGenerator.isValidUid(ou)) {
                OrganisationUnit unit = idObjectManager.getObject(OrganisationUnit.class, inputIdScheme, ou);
                if (unit != null) {
                    ous.add(unit);
                }
            }
        }
        // Remove duplicates
        ous = ous.stream().distinct().collect(Collectors.toList());
        List<DimensionalItemObject> orgUnits = new ArrayList<>();
        List<OrganisationUnit> ousList = asTypedList(ous);
        if (!levels.isEmpty()) {
            orgUnits.addAll(sort(organisationUnitService.getOrganisationUnitsAtLevels(levels, ousList)));
        }
        if (!groups.isEmpty()) {
            orgUnits.addAll(sort(organisationUnitService.getOrganisationUnits(groups, ousList)));
        }
        if (levels.isEmpty() && groups.isEmpty()) {
            orgUnits.addAll(ous);
        }
        if (orgUnits.isEmpty()) {
            throw new IllegalQueryException("Dimension ou is present in query without any valid dimension options");
        }
        // Remove duplicates
        orgUnits = orgUnits.stream().distinct().collect(Collectors.toList());
        DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.ORGANISATION_UNIT, null, DISPLAY_NAME_ORGUNIT, orgUnits);
        return object;
    } else if (LONGITUDE_DIM_ID.contains(dimension)) {
        DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.STATIC, null, DISPLAY_NAME_LONGITUDE, new ArrayList<>());
        return object;
    } else if (LATITUDE_DIM_ID.contains(dimension)) {
        DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.STATIC, null, DISPLAY_NAME_LATITUDE, new ArrayList<>());
        return object;
    } else {
        DimensionalObject dimObject = idObjectManager.get(DataQueryParams.DYNAMIC_DIM_CLASSES, inputIdScheme, dimension);
        if (dimObject != null && dimObject.isDataDimension()) {
            Class<?> dimClass = ReflectionUtils.getRealClass(dimObject.getClass());
            Class<? extends DimensionalItemObject> itemClass = DimensionalObject.DIMENSION_CLASS_ITEM_CLASS_MAP.get(dimClass);
            List<DimensionalItemObject> dimItems = !allItems ? asList(idObjectManager.getByUidOrdered(itemClass, items)) : dimObject.getItems();
            DimensionalObject object = new BaseDimensionalObject(dimension, dimObject.getDimensionType(), null, dimObject.getName(), dimItems, allItems);
            return object;
        }
    }
    if (allowNull) {
        return null;
    }
    throw new IllegalQueryException("Dimension identifier does not reference any dimension: " + dimension);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) IndicatorGroup(org.hisp.dhis.indicator.IndicatorGroup) Calendar(org.hisp.dhis.calendar.Calendar) Period(org.hisp.dhis.period.Period) DimensionalObject(org.hisp.dhis.common.DimensionalObject) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) RelativePeriodEnum(org.hisp.dhis.period.RelativePeriodEnum) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 18 with Calendar

use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.

the class BiMonthlyPeriodType method generateRollingPeriods.

/**
     * Generates the last 6 bi-months where the last one is the bi-month
     * which the given date is inside.
     */
@Override
public List<Period> generateRollingPeriods(DateTimeUnit dateTimeUnit) {
    Calendar cal = getCalendar();
    dateTimeUnit.setDay(1);
    dateTimeUnit = cal.minusMonths(dateTimeUnit, (dateTimeUnit.getMonth() % 2) + 10);
    List<Period> periods = Lists.newArrayList();
    for (int i = 0; i < 6; i++) {
        periods.add(createPeriod(dateTimeUnit, cal));
        dateTimeUnit = cal.plusMonths(dateTimeUnit, 2);
    }
    return periods;
}
Also used : Calendar(org.hisp.dhis.calendar.Calendar)

Example 19 with Calendar

use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.

the class CalendarPeriodType method generateLast5Years.

/**
     * Generates a list of Periods for the last 5 years. Must be overridden by
     * CalendarPeriodTypes which do not generate periods for the current year
     * only in their implementation of generatePeriods( Date ).
     *
     * @param date the date which touches the time span to generate Periods for.
     * @return a list of Periods for a defined time span.
     */
public List<Period> generateLast5Years(Date date) {
    DateTimeUnit dateTimeUnit = createLocalDateUnitInstance(date);
    dateTimeUnit = getCalendar().minusYears(dateTimeUnit, 4);
    List<Period> periods = Lists.newArrayList();
    Calendar calendar = getCalendar();
    for (int i = 0; i < 5; i++) {
        periods.addAll(generatePeriods(dateTimeUnit));
        dateTimeUnit = calendar.plusYears(dateTimeUnit, 1);
    }
    return periods;
}
Also used : Calendar(org.hisp.dhis.calendar.Calendar) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit)

Example 20 with Calendar

use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.

the class DailyPeriodType method getRewindedDate.

@Override
public Date getRewindedDate(Date date, Integer rewindedPeriods) {
    Calendar cal = getCalendar();
    date = date != null ? date : new Date();
    rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1;
    DateTimeUnit dateTimeUnit = createLocalDateUnitInstance(date, cal);
    dateTimeUnit = cal.minusDays(dateTimeUnit, rewindedPeriods);
    return cal.toIso(dateTimeUnit).toJdkDate();
}
Also used : Calendar(org.hisp.dhis.calendar.Calendar) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) Date(java.util.Date)

Aggregations

Calendar (org.hisp.dhis.calendar.Calendar)36 DateTimeUnit (org.hisp.dhis.calendar.DateTimeUnit)14 Date (java.util.Date)10 Period (org.hisp.dhis.period.Period)10 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)6 ArrayList (java.util.ArrayList)5 DimensionalObject (org.hisp.dhis.common.DimensionalObject)4 PeriodType (org.hisp.dhis.period.PeriodType)4 UniqueArrayList (org.hisp.dhis.commons.collection.UniqueArrayList)3 AnalyticsTable (org.hisp.dhis.analytics.AnalyticsTable)2 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)2 OrganisationUnitGroup (org.hisp.dhis.organisationunit.OrganisationUnitGroup)2 ReportTable (org.hisp.dhis.reporttable.ReportTable)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1