Search in sources :

Example 31 with Calendar

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

the class YearlyPeriodType method getRewindedDate.

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

Example 32 with Calendar

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

the class YearlyPeriodType method generateLast5Years.

/**
     * Generates the last 5 years where the last one is the year which the given
     * date is inside.
     */
@Override
public List<Period> generateLast5Years(Date date) {
    Calendar calendar = getCalendar();
    DateTimeUnit dateTimeUnit = createLocalDateUnitInstance(date);
    dateTimeUnit = calendar.minusYears(dateTimeUnit, 4);
    dateTimeUnit.setDay(1);
    dateTimeUnit.setMonth(1);
    List<Period> periods = Lists.newArrayList();
    for (int i = 0; i < 5; ++i) {
        periods.add(createPeriod(dateTimeUnit, calendar));
        dateTimeUnit = calendar.plusYears(dateTimeUnit, 1);
    }
    return periods;
}
Also used : Calendar(org.hisp.dhis.calendar.Calendar) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit)

Example 33 with Calendar

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

the class AnalyticsUtils method getDimensionItemNameMap.

/**
     * Returns a mapping between identifiers and names for the given query.
     *
     * @param params the data query parameters.
     * @return a mapping between identifiers and names.
     */
public static Map<String, String> getDimensionItemNameMap(DataQueryParams params) {
    List<DimensionalObject> dimensions = params.getDimensionsAndFilters();
    Map<String, String> map = new HashMap<>();
    Calendar calendar = PeriodType.getCalendar();
    for (DimensionalObject dimension : dimensions) {
        for (DimensionalItemObject item : dimension.getItems()) {
            if (DimensionType.PERIOD.equals(dimension.getDimensionType()) && !calendar.isIso8601()) {
                Period period = (Period) item;
                DateTimeUnit dateTimeUnit = calendar.fromIso(period.getStartDate());
                map.put(period.getPeriodType().getIsoDate(dateTimeUnit), period.getDisplayName());
            } else {
                map.put(item.getDimensionItem(), item.getDisplayProperty(params.getDisplayProperty()));
            }
            if (DimensionType.ORGANISATION_UNIT.equals(dimension.getDimensionType()) && params.isHierarchyMeta()) {
                OrganisationUnit unit = (OrganisationUnit) item;
                map.putAll(NameableObjectUtils.getUidDisplayPropertyMap(unit.getAncestors(), params.getDisplayProperty()));
            }
        }
        map.put(dimension.getDimension(), dimension.getDisplayProperty(params.getDisplayProperty()));
    }
    return map;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Calendar(org.hisp.dhis.calendar.Calendar) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) Period(org.hisp.dhis.period.Period) DateUtils.getMediumDateString(org.hisp.dhis.system.util.DateUtils.getMediumDateString) DimensionalObject(org.hisp.dhis.common.DimensionalObject)

Example 34 with Calendar

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

the class PeriodResourceTable method getPopulateTempTableContent.

@Override
public Optional<List<Object[]>> getPopulateTempTableContent() {
    Calendar calendar = PeriodType.getCalendar();
    List<Object[]> batchArgs = new ArrayList<>();
    Set<String> uniqueIsoDates = new HashSet<>();
    for (Period period : objects) {
        if (period != null && period.isValid()) {
            final PeriodType rowType = period.getPeriodType();
            final String isoDate = period.getIsoDate();
            if (!uniqueIsoDates.add(isoDate)) {
                log.warn("Duplicate ISO date for period, ignoring: " + period + ", ISO date: " + isoDate);
                continue;
            }
            List<Object> values = new ArrayList<>();
            values.add(period.getId());
            values.add(isoDate);
            values.add(period.getDaysInPeriod());
            values.add(period.getStartDate());
            values.add(period.getEndDate());
            for (PeriodType periodType : PeriodType.PERIOD_TYPES) {
                if (rowType.getFrequencyOrder() < periodType.getFrequencyOrder() || rowType.equals(periodType)) {
                    Period targetPeriod = IdentifiableObjectUtils.getPeriodByPeriodType(period, periodType, calendar);
                    values.add(IdentifiableObjectUtils.getLocalPeriodIdentifier(targetPeriod, calendar));
                } else {
                    values.add(null);
                }
            }
            batchArgs.add(values.toArray());
        }
    }
    return Optional.of(batchArgs);
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) Calendar(org.hisp.dhis.calendar.Calendar) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) HashSet(java.util.HashSet)

Example 35 with Calendar

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

the class DefaultEventAnalyticsService method addMetadata.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
/**
     * Adds meta data values to the given grid based on the given data query
     * parameters.
     * 
     * @param params the data query parameters.
     * @param grid the grid.
     */
private void addMetadata(EventQueryParams params, Grid grid) {
    if (!params.isSkipMeta()) {
        Calendar calendar = PeriodType.getCalendar();
        List<String> periodUids = calendar.isIso8601() ? getDimensionalItemIds(params.getDimensionOrFilterItems(PERIOD_DIM_ID)) : getLocalPeriodIdentifiers(params.getDimensionOrFilterItems(PERIOD_DIM_ID), calendar);
        Map<String, Object> metaData = new HashMap<>();
        Map<String, String> uidNameMap = AnalyticsUtils.getUidNameMap(params);
        if (params.getApiVersion().ge(DhisApiVersion.V26)) {
            metaData.put(AnalyticsMetaDataKey.ITEMS.getKey(), uidNameMap.entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> new MetadataItem(e.getValue()))));
        } else {
            metaData.put(AnalyticsMetaDataKey.NAMES.getKey(), uidNameMap);
        }
        Map<String, Object> dimensionItems = new HashMap<>();
        dimensionItems.put(PERIOD_DIM_ID, periodUids);
        for (DimensionalObject dim : params.getDimensionsAndFilters()) {
            if (!metaData.keySet().contains(dim.getDimension())) {
                dimensionItems.put(dim.getDimension(), getDimensionalItemIds(dim.getItems()));
            }
        }
        for (QueryItem item : params.getItems()) {
            if (item.hasOptionSet()) {
                dimensionItems.put(item.getItemId(), item.getQueryFilterItems());
            } else if (item.hasLegendSet()) {
                dimensionItems.put(item.getItemId(), item.getLegendSetFilterItemsOrAll());
            } else {
                dimensionItems.put(item.getItemId(), Lists.newArrayList());
            }
        }
        for (QueryItem item : params.getItemFilters()) {
            if (item.hasOptionSet()) {
                dimensionItems.put(item.getItemId(), item.getQueryFilterItems());
            } else if (item.hasLegendSet()) {
                dimensionItems.put(item.getItemId(), item.getLegendSetFilterItemsOrAll());
            } else {
                dimensionItems.put(item.getItemId(), Lists.newArrayList(item.getFiltersAsString()));
            }
        }
        if (params.getApiVersion().ge(DhisApiVersion.V26)) {
            metaData.put(AnalyticsMetaDataKey.DIMENSIONS.getKey(), dimensionItems);
        } else {
            metaData.putAll(dimensionItems);
        }
        User user = securityManager.getCurrentUser(params);
        List<OrganisationUnit> organisationUnits = asTypedList(params.getDimensionOrFilterItems(ORGUNIT_DIM_ID));
        Collection<OrganisationUnit> roots = user != null ? user.getOrganisationUnits() : null;
        if (params.isHierarchyMeta()) {
            metaData.put(AnalyticsMetaDataKey.ORG_UNIT_HIERARCHY.getKey(), getParentGraphMap(organisationUnits, roots));
        }
        if (params.isShowHierarchy()) {
            metaData.put(AnalyticsMetaDataKey.ORG_UNIT_NAME_HIERARCHY.getKey(), getParentNameGraphMap(organisationUnits, roots, true));
        }
        grid.setMetaData(metaData);
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) Calendar(org.hisp.dhis.calendar.Calendar)

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