Search in sources :

Example 1 with PERIOD_DIM_ID

use of org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID in project dhis2-core by dhis2.

the class EventTimeFieldSqlRenderer method getSqlConditionForPeriods.

@Override
protected String getSqlConditionForPeriods(EventQueryParams params) {
    final List<DimensionalItemObject> periods = params.getDimensionOrFilterItems(PERIOD_DIM_ID);
    Optional<TimeField> timeField = getTimeField(params);
    StringBuilder sql = new StringBuilder();
    if (timeField.isPresent()) {
        sql.append(periods.stream().filter(dimensionalItemObject -> dimensionalItemObject instanceof Period).map(dimensionalItemObject -> (Period) dimensionalItemObject).map(period -> toSqlCondition(period, timeField.get())).collect(Collectors.joining(" or ", "(", ")")));
    } else {
        String alias = getPeriodAlias(params);
        sql.append(quote(alias, params.getPeriodType().toLowerCase())).append(OPEN_IN).append(getQuotedCommaDelimitedString(getUids(periods))).append(") ");
    }
    return sql.toString();
}
Also used : DATE_PERIOD_STRUCT_ALIAS(org.hisp.dhis.analytics.util.AnalyticsSqlUtils.DATE_PERIOD_STRUCT_ALIAS) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) Getter(lombok.Getter) TimeField(org.hisp.dhis.analytics.TimeField) AnalyticsSqlUtils.quote(org.hisp.dhis.analytics.util.AnalyticsSqlUtils.quote) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Collections.singleton(java.util.Collections.singleton) EVENT_DATE(org.hisp.dhis.analytics.TimeField.EVENT_DATE) ANALYTICS_TBL_ALIAS(org.hisp.dhis.analytics.util.AnalyticsSqlUtils.ANALYTICS_TBL_ALIAS) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString) Period(org.hisp.dhis.period.Period) EventQueryParams(org.hisp.dhis.analytics.event.EventQueryParams) PERIOD_DIM_ID(org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID) Collection(java.util.Collection) DateUtils.getMediumDateString(org.hisp.dhis.util.DateUtils.getMediumDateString) OPEN_IN(org.hisp.dhis.analytics.event.data.JdbcEventAnalyticsManager.OPEN_IN) StatementBuilder(org.hisp.dhis.jdbc.StatementBuilder) Collectors(java.util.stream.Collectors) List(java.util.List) Component(org.springframework.stereotype.Component) IdentifiableObjectUtils.getUids(org.hisp.dhis.common.IdentifiableObjectUtils.getUids) ENROLLMENT(org.hisp.dhis.analytics.EventOutputType.ENROLLMENT) AnalyticsSqlUtils.quoteAlias(org.hisp.dhis.analytics.util.AnalyticsSqlUtils.quoteAlias) Optional(java.util.Optional) DateUtils.plusOneDay(org.hisp.dhis.util.DateUtils.plusOneDay) EventOutputType(org.hisp.dhis.analytics.EventOutputType) TimeField(org.hisp.dhis.analytics.TimeField) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) Period(org.hisp.dhis.period.Period) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString) DateUtils.getMediumDateString(org.hisp.dhis.util.DateUtils.getMediumDateString)

Example 2 with PERIOD_DIM_ID

use of org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID in project dhis2-core by dhis2.

the class MetadataHandler method addMetaData.

/**
 * Adds meta data values to the given grid based on the given data query
 * parameters.
 *
 * @param params the {@link DataQueryParams}.
 * @param grid the grid.
 */
void addMetaData(DataQueryParams params, Grid grid) {
    if (!params.isSkipMeta()) {
        Map<String, Object> metaData = new HashMap<>();
        Map<String, Object> internalMetaData = new HashMap<>();
        // -----------------------------------------------------------------
        // Items / names element
        // -----------------------------------------------------------------
        Map<String, String> cocNameMap = getCocNameMap(params);
        metaData.put(ITEMS.getKey(), getDimensionMetadataItemMap(params));
        // -----------------------------------------------------------------
        // Item order elements
        // -----------------------------------------------------------------
        Map<String, Object> dimensionItems = new HashMap<>();
        Calendar calendar = PeriodType.getCalendar();
        List<String> periodUids = calendar.isIso8601() ? getUids(params.getDimensionOrFilterItems(PERIOD_DIM_ID)) : getLocalPeriodIdentifiers(params.getDimensionOrFilterItems(PERIOD_DIM_ID), calendar);
        dimensionItems.put(PERIOD_DIM_ID, periodUids);
        dimensionItems.put(CATEGORYOPTIONCOMBO_DIM_ID, Sets.newHashSet(cocNameMap.keySet()));
        for (DimensionalObject dim : params.getDimensionsAndFilters()) {
            if (!dimensionItems.containsKey(dim.getDimension())) {
                dimensionItems.put(dim.getDimension(), getDimensionalItemIds(dim.getItems()));
            }
        }
        metaData.put(DIMENSIONS.getKey(), dimensionItems);
        // -----------------------------------------------------------------
        // Organisation unit hierarchy
        // -----------------------------------------------------------------
        List<OrganisationUnit> organisationUnits = asTypedList(params.getDimensionOrFilterItems(ORGUNIT_DIM_ID));
        List<OrganisationUnit> roots = dataQueryService.getUserOrgUnits(params, null);
        if (params.isHierarchyMeta()) {
            metaData.put(ORG_UNIT_HIERARCHY.getKey(), getParentGraphMap(organisationUnits, roots));
        }
        if (params.isShowHierarchy()) {
            Map<Object, List<?>> ancestorMap = organisationUnits.stream().collect(toMap(OrganisationUnit::getUid, ou -> ou.getAncestorNames(roots, true)));
            internalMetaData.put(ORG_UNIT_ANCESTORS.getKey(), ancestorMap);
            metaData.put(ORG_UNIT_NAME_HIERARCHY.getKey(), getParentNameGraphMap(organisationUnits, roots, true));
        }
        grid.setMetaData(copyOf(metaData));
        grid.setInternalMetaData(copyOf(internalMetaData));
    }
}
Also used : DIMENSIONS(org.hisp.dhis.analytics.AnalyticsMetaDataKey.DIMENSIONS) ORGUNIT_DIM_ID(org.hisp.dhis.common.DimensionalObject.ORGUNIT_DIM_ID) DataQueryService(org.hisp.dhis.analytics.DataQueryService) ImmutableMap.copyOf(com.google.common.collect.ImmutableMap.copyOf) HashMap(java.util.HashMap) ORG_UNIT_HIERARCHY(org.hisp.dhis.analytics.AnalyticsMetaDataKey.ORG_UNIT_HIERARCHY) Collectors.toMap(java.util.stream.Collectors.toMap) Calendar(org.hisp.dhis.calendar.Calendar) Map(java.util.Map) DATA_VALUE_SET(org.hisp.dhis.analytics.OutputFormat.DATA_VALUE_SET) DimensionalObjectUtils.getDimensionalItemIds(org.hisp.dhis.common.DimensionalObjectUtils.getDimensionalItemIds) ORG_UNIT_NAME_HIERARCHY(org.hisp.dhis.analytics.AnalyticsMetaDataKey.ORG_UNIT_NAME_HIERARCHY) OrganisationUnit.getParentNameGraphMap(org.hisp.dhis.organisationunit.OrganisationUnit.getParentNameGraphMap) AnalyticsUtils.getDimensionMetadataItemMap(org.hisp.dhis.analytics.util.AnalyticsUtils.getDimensionMetadataItemMap) PERIOD_DIM_ID(org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Grid(org.hisp.dhis.common.Grid) AnalyticsUtils.getCocNameMap(org.hisp.dhis.analytics.util.AnalyticsUtils.getCocNameMap) Sets(com.google.common.collect.Sets) AnalyticsUtils.handleGridForDataValueSet(org.hisp.dhis.analytics.util.AnalyticsUtils.handleGridForDataValueSet) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) OrganisationUnit.getParentGraphMap(org.hisp.dhis.organisationunit.OrganisationUnit.getParentGraphMap) List(java.util.List) Component(org.springframework.stereotype.Component) ORG_UNIT_ANCESTORS(org.hisp.dhis.analytics.AnalyticsMetaDataKey.ORG_UNIT_ANCESTORS) IdentifiableObjectUtils.getUids(org.hisp.dhis.common.IdentifiableObjectUtils.getUids) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) PeriodType(org.hisp.dhis.period.PeriodType) IdentifiableObjectUtils.getLocalPeriodIdentifiers(org.hisp.dhis.common.IdentifiableObjectUtils.getLocalPeriodIdentifiers) ITEMS(org.hisp.dhis.analytics.AnalyticsMetaDataKey.ITEMS) DimensionalObjectUtils.asTypedList(org.hisp.dhis.common.DimensionalObjectUtils.asTypedList) CATEGORYOPTIONCOMBO_DIM_ID(org.hisp.dhis.common.DimensionalObject.CATEGORYOPTIONCOMBO_DIM_ID) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) HashMap(java.util.HashMap) Calendar(org.hisp.dhis.calendar.Calendar) DimensionalObject(org.hisp.dhis.common.DimensionalObject) DimensionalObject(org.hisp.dhis.common.DimensionalObject) List(java.util.List) DimensionalObjectUtils.asTypedList(org.hisp.dhis.common.DimensionalObjectUtils.asTypedList)

Example 3 with PERIOD_DIM_ID

use of org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID in project dhis2-core by dhis2.

the class DataQueryServiceTest method testPeriodGetDimension.

@Test
void testPeriodGetDimension() {
    DimensionalObject dimension = dataQueryService.getDimension(PERIOD_DIM_ID, List.of("TODAY:EVENT_DATE", "YESTERDAY:ENROLLMENT_DATE", "20210101:INCIDENT_DATE", "20210101_20210201:LAST_UPDATED", "2021-02-01_2021-03-01:SCHEDULED_DATE"), null, null, null, true, true, null);
    assertThat(dimension.getItems(), hasSize(5));
    assertThat(dimension.getItems().stream().map(dimensionalItemObject -> (Period) dimensionalItemObject).map(Period::getDateField).collect(Collectors.toList()), containsInAnyOrder("EVENT_DATE", "ENROLLMENT_DATE", "INCIDENT_DATE", "LAST_UPDATED", "SCHEDULED_DATE"));
    assertEquals(getPeriod(dimension, "INCIDENT_DATE").getStartDate(), getMediumDate("2021-01-01"));
    assertEquals(getPeriod(dimension, "INCIDENT_DATE").getEndDate(), getMediumDate("2021-01-01"));
    assertEquals(getPeriod(dimension, "LAST_UPDATED").getStartDate(), getMediumDate("2021-01-01"));
    assertEquals(getPeriod(dimension, "LAST_UPDATED").getEndDate(), getMediumDate("2021-02-01"));
    assertEquals(getPeriod(dimension, "SCHEDULED_DATE").getStartDate(), getMediumDate("2021-02-01"));
    assertEquals(getPeriod(dimension, "SCHEDULED_DATE").getEndDate(), getMediumDate("2021-03-01"));
}
Also used : ReportingRate(org.hisp.dhis.common.ReportingRate) Arrays(java.util.Arrays) CategoryService(org.hisp.dhis.category.CategoryService) OrganisationUnitGroupSetDimension(org.hisp.dhis.organisationunit.OrganisationUnitGroupSetDimension) Autowired(org.springframework.beans.factory.annotation.Autowired) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) Disabled(org.junit.jupiter.api.Disabled) Visualization(org.hisp.dhis.visualization.Visualization) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) ReportingRateMetric(org.hisp.dhis.common.ReportingRateMetric) IndicatorGroup(org.hisp.dhis.indicator.IndicatorGroup) Period(org.hisp.dhis.period.Period) UserService(org.hisp.dhis.user.UserService) OrganisationUnitGroupService(org.hisp.dhis.organisationunit.OrganisationUnitGroupService) AccessStringHelper(org.hisp.dhis.security.acl.AccessStringHelper) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) PERIOD_DIM_ID(org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID) Set(java.util.Set) DimensionType(org.hisp.dhis.common.DimensionType) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) RelativePeriodEnum(org.hisp.dhis.period.RelativePeriodEnum) Test(org.junit.jupiter.api.Test) List(java.util.List) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) ProgramTrackedEntityAttributeDimensionItem(org.hisp.dhis.program.ProgramTrackedEntityAttributeDimensionItem) UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) DataQueryService(org.hisp.dhis.analytics.DataQueryService) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) DataElementService(org.hisp.dhis.dataelement.DataElementService) DataSet(org.hisp.dhis.dataset.DataSet) DateUtils.getMediumDate(org.hisp.dhis.util.DateUtils.getMediumDate) ProgramDataElementDimensionItem(org.hisp.dhis.program.ProgramDataElementDimensionItem) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) Program(org.hisp.dhis.program.Program) HashSet(java.util.HashSet) DataElement(org.hisp.dhis.dataelement.DataElement) Lists(com.google.common.collect.Lists) DIMENSION_NAME_SEP(org.hisp.dhis.common.DimensionalObject.DIMENSION_NAME_SEP) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) Matchers.hasSize(org.hamcrest.Matchers.hasSize) DataQueryRequest(org.hisp.dhis.common.DataQueryRequest) Indicator(org.hisp.dhis.indicator.Indicator) User(org.hisp.dhis.user.User) ErrorCode(org.hisp.dhis.feedback.ErrorCode) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DataElementGroupSet(org.hisp.dhis.dataelement.DataElementGroupSet) IndicatorType(org.hisp.dhis.indicator.IndicatorType) LinkedHashSet(java.util.LinkedHashSet) DimensionalObjectUtils(org.hisp.dhis.common.DimensionalObjectUtils) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) DataElementDomain(org.hisp.dhis.dataelement.DataElementDomain) OPTION_SEP(org.hisp.dhis.common.DimensionalObject.OPTION_SEP) OrganisationUnitGroupSet(org.hisp.dhis.organisationunit.OrganisationUnitGroupSet) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DhisSpringTest(org.hisp.dhis.DhisSpringTest) PeriodType(org.hisp.dhis.period.PeriodType) DataSetService(org.hisp.dhis.dataset.DataSetService) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) ProgramService(org.hisp.dhis.program.ProgramService) IdScheme(org.hisp.dhis.common.IdScheme) Period(org.hisp.dhis.period.Period) DimensionalObject(org.hisp.dhis.common.DimensionalObject) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

List (java.util.List)3 PERIOD_DIM_ID (org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID)3 Sets (com.google.common.collect.Sets)2 Collectors (java.util.stream.Collectors)2 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)2 DataQueryService (org.hisp.dhis.analytics.DataQueryService)2 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)2 DimensionalObject (org.hisp.dhis.common.DimensionalObject)2 IdentifiableObjectUtils.getUids (org.hisp.dhis.common.IdentifiableObjectUtils.getUids)2 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)2 Period (org.hisp.dhis.period.Period)2 PeriodType (org.hisp.dhis.period.PeriodType)2 Component (org.springframework.stereotype.Component)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableMap.copyOf (com.google.common.collect.ImmutableMap.copyOf)1 Lists (com.google.common.collect.Lists)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections.singleton (java.util.Collections.singleton)1 HashMap (java.util.HashMap)1