Search in sources :

Example 11 with DimensionItemKeywords

use of org.hisp.dhis.common.DimensionItemKeywords 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();
    User user = currentUserService.getCurrentUser();
    if (DATA_X_DIM_ID.equals(dimension)) {
        List<DimensionalItemObject> dataDimensionItems = new ArrayList<>();
        DimensionItemKeywords dimensionalKeywords = new DimensionItemKeywords();
        for (String uid : items) {
            if (// DATA ELEMENT GROUP
            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());
                    dimensionalKeywords.addKeyword(group);
                }
            } else if (// INDICATOR GROUP
            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());
                    dimensionalKeywords.addKeyword(group);
                }
            } else {
                DimensionalItemObject dimItemObject = dimensionService.getDataDimensionalItemObject(inputIdScheme, uid);
                if (dimItemObject != null) {
                    dataDimensionItems.add(dimItemObject);
                }
            }
        }
        if (dataDimensionItems.isEmpty()) {
            throwIllegalQueryEx(ErrorCode.E7124, DimensionalObject.DATA_X_DIM_ID);
        }
        return new BaseDimensionalObject(dimension, DimensionType.DATA_X, null, DISPLAY_NAME_DATA_X, dataDimensionItems, dimensionalKeywords);
    } else if (CATEGORYOPTIONCOMBO_DIM_ID.equals(dimension)) {
        return new BaseDimensionalObject(dimension, DimensionType.CATEGORY_OPTION_COMBO, null, DISPLAY_NAME_CATEGORYOPTIONCOMBO, getCategoryOptionComboList(items, inputIdScheme));
    } else if (ATTRIBUTEOPTIONCOMBO_DIM_ID.equals(dimension)) {
        return new BaseDimensionalObject(dimension, DimensionType.ATTRIBUTE_OPTION_COMBO, null, DISPLAY_NAME_ATTRIBUTEOPTIONCOMBO, getCategoryOptionComboList(items, inputIdScheme));
    } else if (PERIOD_DIM_ID.equals(dimension)) {
        Calendar calendar = PeriodType.getCalendar();
        I18n i18n = i18nManager.getI18n();
        List<Period> periods = new ArrayList<>();
        DimensionItemKeywords dimensionalKeywords = new DimensionItemKeywords();
        AnalyticsFinancialYearStartKey financialYearStart = systemSettingManager.getSystemSetting(SettingKey.ANALYTICS_FINANCIAL_YEAR_START, AnalyticsFinancialYearStartKey.class);
        boolean containsRelativePeriods = false;
        for (String isoPeriod : items) {
            // Contains isoPeriod and timeField
            IsoPeriodHolder isoPeriodHolder = IsoPeriodHolder.of(isoPeriod);
            if (RelativePeriodEnum.contains(isoPeriodHolder.getIsoPeriod())) {
                containsRelativePeriods = true;
                RelativePeriodEnum relativePeriod = RelativePeriodEnum.valueOf(isoPeriodHolder.getIsoPeriod());
                dimensionalKeywords.addKeyword(isoPeriodHolder.getIsoPeriod(), i18n.getString(isoPeriodHolder.getIsoPeriod()));
                List<Period> relativePeriods = RelativePeriods.getRelativePeriodsFromEnum(relativePeriod, relativePeriodDate, format, true, financialYearStart);
                // If custom time filter is specified, set it in periods
                if (isoPeriodHolder.hasDateField()) {
                    relativePeriods.forEach(period -> period.setDateField(isoPeriodHolder.getDateField()));
                }
                periods.addAll(relativePeriods);
            } else {
                Period period = PeriodType.getPeriodFromIsoString(isoPeriodHolder.getIsoPeriod());
                if (period != null) {
                    if (isoPeriodHolder.hasDateField()) {
                        period.setDescription(isoPeriodHolder.getIsoPeriod());
                        period.setDateField(isoPeriodHolder.getDateField());
                    }
                    dimensionalKeywords.addKeyword(isoPeriodHolder.getIsoPeriod(), format != null ? i18n.getString(format.formatPeriod(period)) : isoPeriodHolder.getIsoPeriod());
                    periods.add(period);
                } else {
                    tryParseDateRange(isoPeriodHolder).ifPresent(periods::add);
                }
            }
        }
        // Remove duplicates
        periods = periods.stream().distinct().collect(Collectors.toList());
        if (containsRelativePeriods) {
            periods.sort(new AscendingPeriodComparator());
        }
        for (Period period : periods) {
            String name = format != null ? format.formatPeriod(period) : null;
            if (!period.getPeriodType().getName().contains(WeeklyPeriodType.NAME)) {
                period.setShortName(name);
            }
            period.setName(name);
            if (!calendar.isIso8601()) {
                period.setUid(getLocalPeriodIdentifier(period, calendar));
            }
        }
        return new BaseDimensionalObject(dimension, DimensionType.PERIOD, null, DISPLAY_NAME_PERIOD, asList(periods), dimensionalKeywords);
    } 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)) {
                String level = DimensionalObjectUtils.getValueFromKeywordParam(ou);
                Integer orgUnitLevel = organisationUnitService.getOrganisationUnitLevelByLevelOrUid(level);
                if (orgUnitLevel != null) {
                    levels.add(orgUnitLevel);
                }
            } 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);
        DimensionItemKeywords dimensionalKeywords = new DimensionItemKeywords();
        if (!levels.isEmpty()) {
            orgUnits.addAll(sort(organisationUnitService.getOrganisationUnitsAtLevels(levels, ousList)));
            dimensionalKeywords.addKeywords(levels.stream().map(level -> organisationUnitService.getOrganisationUnitLevelByLevel(level)).filter(Objects::nonNull).collect(Collectors.toList()));
        }
        if (!groups.isEmpty()) {
            orgUnits.addAll(sort(organisationUnitService.getOrganisationUnits(groups, ousList)));
            dimensionalKeywords.addKeywords(groups.stream().map(group -> new BaseNameableObject(group.getUid(), group.getCode(), group.getName())).collect(Collectors.toList()));
        }
        if (levels.isEmpty() && groups.isEmpty()) {
            orgUnits.addAll(ous);
        }
        if (!dimensionalKeywords.isEmpty()) {
            dimensionalKeywords.addKeywords(ousList);
        }
        if (orgUnits.isEmpty()) {
            throwIllegalQueryEx(ErrorCode.E7124, DimensionalObject.ORGUNIT_DIM_ID);
        }
        // Remove duplicates
        orgUnits = orgUnits.stream().distinct().collect(Collectors.toList());
        return new BaseDimensionalObject(dimension, DimensionType.ORGANISATION_UNIT, null, DISPLAY_NAME_ORGUNIT, orgUnits, dimensionalKeywords);
    } else if (ORGUNIT_GROUP_DIM_ID.equals(dimension)) {
        List<DimensionalItemObject> ougs = new ArrayList<>();
        for (String uid : items) {
            OrganisationUnitGroup organisationUnitGroup = idObjectManager.getObject(OrganisationUnitGroup.class, inputIdScheme, uid);
            if (organisationUnitGroup != null) {
                ougs.add(organisationUnitGroup);
            }
        }
        return new BaseDimensionalObject(dimension, DimensionType.ORGANISATION_UNIT_GROUP, null, DISPLAY_NAME_ORGUNIT_GROUP, ougs);
    } else if (LONGITUDE_DIM_ID.contains(dimension)) {
        return new BaseDimensionalObject(dimension, DimensionType.STATIC, null, DISPLAY_NAME_LONGITUDE, new ArrayList<>());
    } else if (LATITUDE_DIM_ID.contains(dimension)) {
        return new BaseDimensionalObject(dimension, DimensionType.STATIC, null, DISPLAY_NAME_LATITUDE, new ArrayList<>());
    } else {
        DimensionalObject dimObject = idObjectManager.get(DataQueryParams.DYNAMIC_DIM_CLASSES, inputIdScheme, dimension);
        if (dimObject != null && dimObject.isDataDimension()) {
            Class<?> dimClass = HibernateProxyUtils.getRealClass(dimObject);
            Class<? extends DimensionalItemObject> itemClass = DimensionalObject.DIMENSION_CLASS_ITEM_CLASS_MAP.get(dimClass);
            List<DimensionalItemObject> dimItems = !allItems ? asList(idObjectManager.getOrdered(itemClass, inputIdScheme, items)) : getCanReadItems(user, dimObject);
            return new BaseDimensionalObject(dimObject.getDimension(), dimObject.getDimensionType(), null, dimObject.getName(), dimItems, allItems);
        }
    }
    if (allowNull) {
        return null;
    }
    throw new IllegalQueryException(new ErrorMessage(ErrorCode.E7125, dimension));
}
Also used : KEY_USER_ORGUNIT_CHILDREN(org.hisp.dhis.organisationunit.OrganisationUnit.KEY_USER_ORGUNIT_CHILDREN) KEY_IN_GROUP(org.hisp.dhis.analytics.DataQueryParams.KEY_IN_GROUP) DISPLAY_NAME_LONGITUDE(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_LONGITUDE) DataQueryParams.getMeasureCriteriaFromParam(org.hisp.dhis.analytics.DataQueryParams.getMeasureCriteriaFromParam) Date(java.util.Date) DimensionService(org.hisp.dhis.common.DimensionService) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) AnalyticsFinancialYearStartKey(org.hisp.dhis.analytics.AnalyticsFinancialYearStartKey) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage) AnalyticsSecurityManager(org.hisp.dhis.analytics.AnalyticsSecurityManager) Calendar(org.hisp.dhis.calendar.Calendar) I18nFormat(org.hisp.dhis.i18n.I18nFormat) IdentifiableProperty(org.hisp.dhis.common.IdentifiableProperty) ISO_FORMAT(org.hisp.dhis.period.DailyPeriodType.ISO_FORMAT) KEY_USER_ORGUNIT(org.hisp.dhis.organisationunit.OrganisationUnit.KEY_USER_ORGUNIT) DimensionItemKeywords(org.hisp.dhis.common.DimensionItemKeywords) IndicatorGroup(org.hisp.dhis.indicator.IndicatorGroup) AscendingPeriodComparator(org.hisp.dhis.period.comparator.AscendingPeriodComparator) Period(org.hisp.dhis.period.Period) KEY_USER_ORGUNIT_GRANDCHILDREN(org.hisp.dhis.organisationunit.OrganisationUnit.KEY_USER_ORGUNIT_GRANDCHILDREN) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) PERIOD_DIM_ID(org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID) Collection(java.util.Collection) LONGITUDE_DIM_ID(org.hisp.dhis.common.DimensionalObject.LONGITUDE_DIM_ID) FORMAT_DATE(org.hisp.dhis.i18n.I18nFormat.FORMAT_DATE) DISPLAY_NAME_PERIOD(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_PERIOD) Set(java.util.Set) AnalyticsUtils.throwIllegalQueryEx(org.hisp.dhis.analytics.util.AnalyticsUtils.throwIllegalQueryEx) DimensionType(org.hisp.dhis.common.DimensionType) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) Objects(java.util.Objects) RelativePeriodEnum(org.hisp.dhis.period.RelativePeriodEnum) List(java.util.List) Stream(java.util.stream.Stream) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) LocalDate(java.time.LocalDate) AclService(org.hisp.dhis.security.acl.AclService) DISPLAY_NAME_LATITUDE(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_LATITUDE) ATTRIBUTEOPTIONCOMBO_DIM_ID(org.hisp.dhis.common.DimensionalObject.ATTRIBUTEOPTIONCOMBO_DIM_ID) Optional(java.util.Optional) RelativePeriods(org.hisp.dhis.period.RelativePeriods) DimensionalObjectUtils.asTypedList(org.hisp.dhis.common.DimensionalObjectUtils.asTypedList) DISPLAY_NAME_DATA_X(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_DATA_X) ListUtils.sort(org.hisp.dhis.commons.collection.ListUtils.sort) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) ORGUNIT_DIM_ID(org.hisp.dhis.common.DimensionalObject.ORGUNIT_DIM_ID) ORGUNIT_GROUP_DIM_ID(org.hisp.dhis.common.DimensionalObject.ORGUNIT_GROUP_DIM_ID) DataQueryService(org.hisp.dhis.analytics.DataQueryService) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) DimensionalObjectUtils.asList(org.hisp.dhis.common.DimensionalObjectUtils.asList) DISPLAY_NAME_ORGUNIT_GROUP(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_ORGUNIT_GROUP) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) I18n(org.hisp.dhis.i18n.I18n) ArrayList(java.util.ArrayList) LATITUDE_DIM_ID(org.hisp.dhis.common.DimensionalObject.LATITUDE_DIM_ID) BaseNameableObject(org.hisp.dhis.common.BaseNameableObject) KEY_LEVEL(org.hisp.dhis.organisationunit.OrganisationUnit.KEY_LEVEL) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) Service(org.springframework.stereotype.Service) AnalyticsAggregationType(org.hisp.dhis.analytics.AnalyticsAggregationType) DATA_X_DIM_ID(org.hisp.dhis.common.DimensionalObject.DATA_X_DIM_ID) PERIOD_FREE_RANGE_SEPARATOR(org.hisp.dhis.common.DimensionalObject.PERIOD_FREE_RANGE_SEPARATOR) DimensionalObjectUtils.getDimensionalItemIds(org.hisp.dhis.common.DimensionalObjectUtils.getDimensionalItemIds) DataQueryRequest(org.hisp.dhis.common.DataQueryRequest) User(org.hisp.dhis.user.User) ErrorCode(org.hisp.dhis.feedback.ErrorCode) I18nManager(org.hisp.dhis.i18n.I18nManager) WeeklyPeriodType(org.hisp.dhis.period.WeeklyPeriodType) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) DimensionalObjectUtils(org.hisp.dhis.common.DimensionalObjectUtils) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) DISPLAY_NAME_CATEGORYOPTIONCOMBO(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_CATEGORYOPTIONCOMBO) IdentifiableObjectUtils.getLocalPeriodIdentifier(org.hisp.dhis.common.IdentifiableObjectUtils.getLocalPeriodIdentifier) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) DISPLAY_NAME_ORGUNIT(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_ORGUNIT) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) OutputFormat(org.hisp.dhis.analytics.OutputFormat) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ObjectUtils(org.hisp.dhis.util.ObjectUtils) DailyPeriodType(org.hisp.dhis.period.DailyPeriodType) CurrentUserService(org.hisp.dhis.user.CurrentUserService) DateTimeFormatter(java.time.format.DateTimeFormatter) PeriodType(org.hisp.dhis.period.PeriodType) CodeGenerator(org.hisp.dhis.common.CodeGenerator) KEY_ORGUNIT_GROUP(org.hisp.dhis.organisationunit.OrganisationUnit.KEY_ORGUNIT_GROUP) SettingKey(org.hisp.dhis.setting.SettingKey) DISPLAY_NAME_ATTRIBUTEOPTIONCOMBO(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_ATTRIBUTEOPTIONCOMBO) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) IdScheme(org.hisp.dhis.common.IdScheme) KEY_DE_GROUP(org.hisp.dhis.analytics.DataQueryParams.KEY_DE_GROUP) CATEGORYOPTIONCOMBO_DIM_ID(org.hisp.dhis.common.DimensionalObject.CATEGORYOPTIONCOMBO_DIM_ID) Assert(org.springframework.util.Assert) User(org.hisp.dhis.user.User) IndicatorGroup(org.hisp.dhis.indicator.IndicatorGroup) AnalyticsFinancialYearStartKey(org.hisp.dhis.analytics.AnalyticsFinancialYearStartKey) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionItemKeywords(org.hisp.dhis.common.DimensionItemKeywords) ArrayList(java.util.ArrayList) DimensionalObject(org.hisp.dhis.common.DimensionalObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) RelativePeriodEnum(org.hisp.dhis.period.RelativePeriodEnum) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) List(java.util.List) DimensionalObjectUtils.asTypedList(org.hisp.dhis.common.DimensionalObjectUtils.asTypedList) DimensionalObjectUtils.asList(org.hisp.dhis.common.DimensionalObjectUtils.asList) ArrayList(java.util.ArrayList) I18n(org.hisp.dhis.i18n.I18n) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Calendar(org.hisp.dhis.calendar.Calendar) Period(org.hisp.dhis.period.Period) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) AscendingPeriodComparator(org.hisp.dhis.period.comparator.AscendingPeriodComparator) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) Objects(java.util.Objects) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage) BaseNameableObject(org.hisp.dhis.common.BaseNameableObject)

Example 12 with DimensionItemKeywords

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

the class AbstractAnalyticsService method getGrid.

protected Grid getGrid(EventQueryParams params) {
    // ---------------------------------------------------------------------
    // Decide access, add constraints and validate
    // ---------------------------------------------------------------------
    securityManager.decideAccessEventQuery(params);
    params = securityManager.withUserConstraints(params);
    queryValidator.validate(params);
    // keywords as well as their periods are removed in the next step,
    // params object is modified
    List<DimensionItemKeywords.Keyword> periodKeywords = params.getDimensions().stream().map(DimensionalObject::getDimensionItemKeywords).filter(dimensionItemKeywords -> dimensionItemKeywords != null && !dimensionItemKeywords.isEmpty()).flatMap(dk -> dk.getKeywords().stream()).collect(Collectors.toList());
    params = new EventQueryParams.Builder(params).withStartEndDatesForPeriods().build();
    // ---------------------------------------------------------------------
    // Headers
    // ---------------------------------------------------------------------
    Grid grid = createGridWithHeaders(params);
    for (DimensionalObject dimension : params.getDimensions()) {
        grid.addHeader(new GridHeader(dimension.getDimension(), dimension.getDimensionDisplayName(), ValueType.TEXT, false, true));
    }
    for (QueryItem item : params.getItems()) {
        /**
         * Special case: If the request contains an item of Org Unit value
         * type and the item UID is linked to coordinates (coordinateField),
         * then create an Header of ValueType COORDINATE and type "Point"
         */
        if (item.getValueType() == ValueType.ORGANISATION_UNIT && params.getCoordinateField().equals(item.getItem().getUid())) {
            grid.addHeader(new GridHeader(item.getItem().getUid(), item.getItem().getDisplayProperty(params.getDisplayProperty()), COORDINATE, false, true, item.getOptionSet(), item.getLegendSet()));
        } else if (hasNonDefaultRepeatableProgramStageOffset(item)) {
            String column = item.getItem().getDisplayProperty(params.getDisplayProperty());
            RepeatableStageParams repeatableStageParams = item.getRepeatableStageParams();
            String name = repeatableStageParams.getDimension();
            grid.addHeader(new GridHeader(name, column, repeatableStageParams.simpleStageValueExpected() ? item.getValueType() : ValueType.REFERENCE, false, true, item.getOptionSet(), item.getLegendSet(), item.getProgramStage().getUid(), item.getRepeatableStageParams()));
        } else {
            final String uid = getItemUid(item);
            final String column = item.getItem().getDisplayProperty(params.getDisplayProperty());
            grid.addHeader(new GridHeader(uid, column, item.getValueType(), false, true, item.getOptionSet(), item.getLegendSet()));
        }
    }
    // ---------------------------------------------------------------------
    // Data
    // ---------------------------------------------------------------------
    long count = 0;
    if (!params.isSkipData() || params.analyzeOnly()) {
        count = addEventData(grid, params);
    }
    // ---------------------------------------------------------------------
    // Meta-data
    // ---------------------------------------------------------------------
    addMetadata(params, periodKeywords, grid);
    if (params.hasDataIdScheme()) {
        substituteData(grid);
    }
    if (params.isPaging()) {
        Pager pager = new Pager(params.getPageWithDefault(), count, params.getPageSizeWithDefault());
        grid.getMetaData().put(PAGER.getKey(), pager);
    }
    maybeApplyHeaders(params, grid);
    return grid;
}
Also used : DIMENSIONS(org.hisp.dhis.analytics.AnalyticsMetaDataKey.DIMENSIONS) ORGUNIT_DIM_ID(org.hisp.dhis.common.DimensionalObject.ORGUNIT_DIM_ID) Arrays(java.util.Arrays) QueryItem(org.hisp.dhis.common.QueryItem) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ValueType(org.hisp.dhis.common.ValueType) StringUtils.joinWith(org.apache.commons.lang3.StringUtils.joinWith) HashMap(java.util.HashMap) AnalyticsSecurityManager(org.hisp.dhis.analytics.AnalyticsSecurityManager) ArrayList(java.util.ArrayList) RepeatableStageParams(org.hisp.dhis.common.RepeatableStageParams) ORG_UNIT_HIERARCHY(org.hisp.dhis.analytics.AnalyticsMetaDataKey.ORG_UNIT_HIERARCHY) Lists(com.google.common.collect.Lists) Calendar(org.hisp.dhis.calendar.Calendar) GridHeader(org.hisp.dhis.common.GridHeader) Map(java.util.Map) DimensionalObjectUtils.getDimensionalItemIds(org.hisp.dhis.common.DimensionalObjectUtils.getDimensionalItemIds) User(org.hisp.dhis.user.User) ORG_UNIT_NAME_HIERARCHY(org.hisp.dhis.analytics.AnalyticsMetaDataKey.ORG_UNIT_NAME_HIERARCHY) DimensionItemKeywords(org.hisp.dhis.common.DimensionItemKeywords) PAGER(org.hisp.dhis.analytics.AnalyticsMetaDataKey.PAGER) COORDINATE(org.hisp.dhis.common.ValueType.COORDINATE) OrganisationUnit.getParentNameGraphMap(org.hisp.dhis.organisationunit.OrganisationUnit.getParentNameGraphMap) EventQueryParams(org.hisp.dhis.analytics.event.EventQueryParams) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) PERIOD_DIM_ID(org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID) Collection(java.util.Collection) EventQueryValidator(org.hisp.dhis.analytics.event.EventQueryValidator) Pager(org.hisp.dhis.common.Pager) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Grid(org.hisp.dhis.common.Grid) Collectors(java.util.stream.Collectors) AnalyticsUtils(org.hisp.dhis.analytics.util.AnalyticsUtils) MetadataItem(org.hisp.dhis.common.MetadataItem) Objects(java.util.Objects) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) OrganisationUnit.getParentGraphMap(org.hisp.dhis.organisationunit.OrganisationUnit.getParentGraphMap) List(java.util.List) Option(org.hisp.dhis.option.Option) IdentifiableObjectUtils.getUids(org.hisp.dhis.common.IdentifiableObjectUtils.getUids) 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) IdScheme(org.hisp.dhis.common.IdScheme) EventQueryParams(org.hisp.dhis.analytics.event.EventQueryParams) QueryItem(org.hisp.dhis.common.QueryItem) Grid(org.hisp.dhis.common.Grid) RepeatableStageParams(org.hisp.dhis.common.RepeatableStageParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) GridHeader(org.hisp.dhis.common.GridHeader) Pager(org.hisp.dhis.common.Pager)

Example 13 with DimensionItemKeywords

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

the class AnalyticsServiceMetadataTest method metadataContainsRelativePeriodItem.

@SuppressWarnings("unchecked")
@Test
void metadataContainsRelativePeriodItem() {
    List<DimensionalItemObject> periods = new ArrayList<>();
    periods.add(new MonthlyPeriodType().createPeriod(new DateTime(2014, 4, 1, 0, 0).toDate()));
    BaseDimensionalObject periodDimension = new BaseDimensionalObject("pe", DimensionType.PERIOD, periods);
    DimensionItemKeywords dimensionalKeywords = new DimensionItemKeywords();
    dimensionalKeywords.addKeyword(THIS_QUARTER.name(), "This quarter");
    periodDimension.setDimensionalKeywords(dimensionalKeywords);
    DataQueryParams params = DataQueryParams.newBuilder().withDimensions(Lists.newArrayList(periodDimension, new BaseDimensionalObject("dx", DimensionType.DATA_X, DISPLAY_NAME_DATA_X, "display name", Lists.newArrayList(createDataElement('A', new CategoryCombo()), createDataElement('B', new CategoryCombo()))))).withSkipData(true).build();
    initMock(params);
    Grid grid = target.getAggregatedDataValueGrid(params);
    Map<String, Object> items = (Map<String, Object>) grid.getMetaData().get("items");
    assertTrue(items.containsKey(THIS_QUARTER.name()));
}
Also used : BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionItemKeywords(org.hisp.dhis.common.DimensionItemKeywords) Grid(org.hisp.dhis.common.Grid) ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) DateTime(org.joda.time.DateTime) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) CategoryCombo(org.hisp.dhis.category.CategoryCombo) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseNameableObject(org.hisp.dhis.common.BaseNameableObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 14 with DimensionItemKeywords

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

the class DataQueryServiceDimensionItemKeywordTest method convertAnalyticsRequestWithOuLevelToDataQueryParam.

@Test
void convertAnalyticsRequestWithOuLevelToDataQueryParam() {
    mockDimensionService();
    when(organisationUnitService.getOrganisationUnitLevelByLevel(2)).thenReturn(buildOrgUnitLevel(2, "level2UID", "District", null));
    when(organisationUnitService.getOrganisationUnitLevelByLevelOrUid("2")).thenReturn(2);
    when(organisationUnitService.getOrganisationUnitsAtLevels(Mockito.anyList(), Mockito.anyList())).thenReturn(Lists.newArrayList(new OrganisationUnit(), new OrganisationUnit()));
    rb.addOuFilter("LEVEL-2;ImspTQPwCqd");
    rb.addDimension(concatenateUuid(DATA_ELEMENT_1, DATA_ELEMENT_2, DATA_ELEMENT_3));
    rb.addPeDimension(PERIOD_DIMENSION);
    DataQueryRequest request = DataQueryRequest.newBuilder().filter(rb.getFilterParams()).dimension(rb.getDimensionParams()).build();
    DataQueryParams params = target.getFromRequest(request);
    DimensionalObject filter = params.getFilters().get(0);
    DimensionItemKeywords keywords = filter.getDimensionItemKeywords();
    assertEquals(1, keywords.getKeywords().size());
    assertNotNull(keywords.getKeyword("level2UID"));
    assertEquals("District", keywords.getKeyword("level2UID").getMetadataItem().getName());
    assertNull(keywords.getKeyword("level2UID").getMetadataItem().getCode());
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionItemKeywords(org.hisp.dhis.common.DimensionItemKeywords) DataQueryRequest(org.hisp.dhis.common.DataQueryRequest) DimensionalObject(org.hisp.dhis.common.DimensionalObject) Test(org.junit.jupiter.api.Test)

Example 15 with DimensionItemKeywords

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

the class DataQueryServiceDimensionItemKeywordTest method convertAnalyticsRequestWithOrgUnitLevelAsFilter.

@Test
void convertAnalyticsRequestWithOrgUnitLevelAsFilter() {
    OrganisationUnit level2OuA = new OrganisationUnit("Bo");
    OrganisationUnit level2OuB = new OrganisationUnit("Bombali");
    mockDimensionService();
    when(organisationUnitService.getOrganisationUnitLevelByLevelOrUid("wjP19dkFeIk")).thenReturn(2);
    when(idObjectManager.getObject(OrganisationUnit.class, UID, "ImspTQPwCqd")).thenReturn(rootOu);
    when(organisationUnitService.getOrganisationUnitsAtLevels(Mockito.anyList(), Mockito.anyList())).thenReturn(Lists.newArrayList(level2OuA, level2OuB));
    when(organisationUnitService.getOrganisationUnitLevelByLevel(2)).thenReturn(buildOrgUnitLevel(2, "level2UID", "District", null));
    rb.addOuFilter("LEVEL-wjP19dkFeIk;ImspTQPwCqd");
    rb.addDimension(concatenateUuid(DATA_ELEMENT_1, DATA_ELEMENT_2, DATA_ELEMENT_3));
    rb.addPeDimension(PERIOD_DIMENSION);
    DataQueryRequest request = DataQueryRequest.newBuilder().filter(rb.getFilterParams()).dimension(rb.getDimensionParams()).build();
    DataQueryParams params = target.getFromRequest(request);
    DimensionalObject filter = params.getFilters().get(0);
    DimensionItemKeywords keywords = filter.getDimensionItemKeywords();
    assertEquals(2, keywords.getKeywords().size());
    assertNotNull(keywords.getKeyword("level2UID"));
    assertEquals("District", keywords.getKeyword("level2UID").getMetadataItem().getName());
    assertNull(keywords.getKeyword("level2UID").getMetadataItem().getCode());
    assertNotNull(keywords.getKeyword(rootOu.getUid()));
    assertEquals("Sierra Leone", keywords.getKeyword(rootOu.getUid()).getMetadataItem().getName());
    assertEquals(rootOu.getCode(), keywords.getKeyword(rootOu.getUid()).getMetadataItem().getCode());
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionItemKeywords(org.hisp.dhis.common.DimensionItemKeywords) DataQueryRequest(org.hisp.dhis.common.DataQueryRequest) DimensionalObject(org.hisp.dhis.common.DimensionalObject) Test(org.junit.jupiter.api.Test)

Aggregations

DimensionItemKeywords (org.hisp.dhis.common.DimensionItemKeywords)15 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)13 Test (org.junit.jupiter.api.Test)12 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)11 DimensionalObject (org.hisp.dhis.common.DimensionalObject)9 DataQueryRequest (org.hisp.dhis.common.DataQueryRequest)8 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 BaseDimensionalObject (org.hisp.dhis.common.BaseDimensionalObject)6 BaseNameableObject (org.hisp.dhis.common.BaseNameableObject)6 Grid (org.hisp.dhis.common.Grid)6 ArrayList (java.util.ArrayList)5 CategoryCombo (org.hisp.dhis.category.CategoryCombo)5 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)3 DataElementGroup (org.hisp.dhis.dataelement.DataElementGroup)3 IndicatorGroup (org.hisp.dhis.indicator.IndicatorGroup)3 MonthlyPeriodType (org.hisp.dhis.period.MonthlyPeriodType)3 Period (org.hisp.dhis.period.Period)3 DateTime (org.joda.time.DateTime)3