Search in sources :

Example 26 with DimensionalObject

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

the class JdbcRawAnalyticsManager method getStatement.

private String getStatement(DataQueryParams params, String partition) {
    List<String> dimensionColumns = params.getDimensions().stream().map(d -> statementBuilder.columnQuote(d.getDimensionName())).collect(Collectors.toList());
    setOrgUnitSelect(params, dimensionColumns);
    SqlHelper sqlHelper = new SqlHelper();
    String sql = "select " + StringUtils.join(dimensionColumns, ", ") + ", " + DIM_NAME_OU + ", value " + "from " + partition + " ax " + "inner join organisationunit ou on ax.ou = ou.uid " + "inner join _periodstructure ps on ax.pe = ps.iso ";
    for (DimensionalObject dim : params.getDimensions()) {
        if (!dim.getItems().isEmpty() && !dim.isFixed()) {
            String col = statementBuilder.columnQuote(dim.getDimensionName());
            if (DimensionalObject.ORGUNIT_DIM_ID.equals(dim.getDimension())) {
                sql += sqlHelper.whereAnd() + " (";
                for (DimensionalItemObject item : dim.getItems()) {
                    OrganisationUnit unit = (OrganisationUnit) item;
                    sql += DIM_NAME_OU + " like '" + unit.getPath() + "%' or ";
                }
                sql = TextUtils.removeLastOr(sql) + ") ";
            } else {
                sql += sqlHelper.whereAnd() + " " + col + " in (" + getQuotedCommaDelimitedString(getUids(dim.getItems())) + ") ";
            }
        }
    }
    if (params.hasStartEndDate()) {
        sql += sqlHelper.whereAnd() + " " + "ps.startdate >= '" + DateUtils.getMediumDateString(params.getStartDate()) + "' and " + "ps.enddate <= '" + DateUtils.getMediumDateString(params.getEndDate()) + "' ";
    }
    return sql;
}
Also used : ORGUNIT_DIM_ID(org.hisp.dhis.common.DimensionalObject.ORGUNIT_DIM_ID) StringUtils(org.apache.commons.lang.StringUtils) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) Resource(javax.annotation.Resource) Autowired(org.springframework.beans.factory.annotation.Autowired) StatementBuilder(org.hisp.dhis.jdbc.StatementBuilder) DateUtils(org.hisp.dhis.system.util.DateUtils) RawAnalyticsManager(org.hisp.dhis.analytics.RawAnalyticsManager) Grid(org.hisp.dhis.common.Grid) Collectors(java.util.stream.Collectors) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) List(java.util.List) IdentifiableObjectUtils.getUids(org.hisp.dhis.common.IdentifiableObjectUtils.getUids) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) Log(org.apache.commons.logging.Log) SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString) LogFactory(org.apache.commons.logging.LogFactory) Collections(java.util.Collections) SqlHelper(org.hisp.dhis.commons.util.SqlHelper) TextUtils(org.hisp.dhis.commons.util.TextUtils) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) SqlHelper(org.hisp.dhis.commons.util.SqlHelper) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString) DimensionalObject(org.hisp.dhis.common.DimensionalObject)

Example 27 with DimensionalObject

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

the class GeoFeatureController method getGeoFeatures.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
/**
     * Returns list of geo features. Returns null if not modified based on the
     * request.
     *
     * @param ou                 the organisation unit parameter.
     * @param displayProperty    the display property.
     * @param relativePeriodDate the date to use as basis for relative periods.
     * @param userOrgUnit        the user organisation unit parameter.
     * @param request            the HTTP request.
     * @param response           the HTTP response.
     * @param includeGroupSets   whether to include organisation unit group sets.
     * @return a list of geo features or null.
     */
private List<GeoFeature> getGeoFeatures(String ou, DisplayProperty displayProperty, Date relativePeriodDate, String userOrgUnit, HttpServletRequest request, HttpServletResponse response, boolean includeGroupSets, DhisApiVersion apiVersion) {
    Set<String> set = new HashSet<>();
    set.add(ou);
    DataQueryParams params = dataQueryService.getFromUrl(set, null, AggregationType.SUM, null, null, null, null, false, false, false, false, false, false, false, false, false, false, displayProperty, null, null, false, null, relativePeriodDate, userOrgUnit, false, apiVersion);
    DimensionalObject dim = params.getDimension(DimensionalObject.ORGUNIT_DIM_ID);
    List<OrganisationUnit> organisationUnits = DimensionalObjectUtils.asTypedList(dim.getItems());
    FilterUtils.filter(organisationUnits, new OrganisationUnitWithValidCoordinatesFilter());
    boolean modified = !ContextUtils.clearIfNotModified(request, response, organisationUnits);
    if (!modified) {
        return null;
    }
    List<OrganisationUnitGroupSet> groupSets = includeGroupSets ? organisationUnitGroupService.getAllOrganisationUnitGroupSets() : null;
    List<GeoFeature> features = new ArrayList<>();
    Set<OrganisationUnit> roots = currentUserService.getCurrentUser().getDataViewOrganisationUnitsWithFallback();
    for (OrganisationUnit unit : organisationUnits) {
        GeoFeature feature = new GeoFeature();
        Integer ty = unit.getFeatureType() != null ? FEATURE_TYPE_MAP.get(unit.getFeatureType()) : null;
        feature.setId(unit.getUid());
        feature.setCode(unit.getCode());
        feature.setHcd(unit.hasChildrenWithCoordinates());
        feature.setHcu(unit.hasCoordinatesUp());
        feature.setLe(unit.getLevel());
        feature.setPg(unit.getParentGraph(roots));
        feature.setPi(unit.getParent() != null ? unit.getParent().getUid() : null);
        feature.setPn(unit.getParent() != null ? unit.getParent().getDisplayName() : null);
        feature.setTy(ObjectUtils.firstNonNull(ty, 0));
        feature.setCo(unit.getCoordinates());
        feature.setNa(unit.getDisplayProperty(params.getDisplayProperty()));
        if (includeGroupSets) {
            for (OrganisationUnitGroupSet groupSet : groupSets) {
                OrganisationUnitGroup group = unit.getGroupInGroupSet(groupSet);
                if (group != null) {
                    feature.getDimensions().put(groupSet.getUid(), group.getUid());
                }
            }
        }
        features.add(feature);
    }
    Collections.sort(features, (o1, o2) -> Integer.valueOf(o1.getTy()).compareTo(Integer.valueOf(o2.getTy())));
    return features;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ArrayList(java.util.ArrayList) DimensionalObject(org.hisp.dhis.common.DimensionalObject) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) OrganisationUnitWithValidCoordinatesFilter(org.hisp.dhis.system.filter.OrganisationUnitWithValidCoordinatesFilter) OrganisationUnitGroupSet(org.hisp.dhis.organisationunit.OrganisationUnitGroupSet) GeoFeature(org.hisp.dhis.webapi.webdomain.GeoFeature) HashSet(java.util.HashSet)

Example 28 with DimensionalObject

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

the class BaseAnalyticalObject method getDimensionalObject.

/**
     * Assembles a DimensionalObject based on the persisted properties of this
     * AnalyticalObject. Collapses indicators, data elements, data element
     * operands and data sets into the dx dimension.
     * <p>
     * Collapses fixed and relative periods into the pe dimension. Collapses
     * fixed and user organisation units into the ou dimension.
     *
     * @param dimension    the dimension identifier.
     * @param date         the date used for generating relative periods.
     * @param user         the current user.
     * @param dynamicNames whether to use dynamic or static names.
     * @param format       the I18nFormat.
     * @return a DimensionalObject.
     */
protected DimensionalObject getDimensionalObject(String dimension, Date date, User user, boolean dynamicNames, List<OrganisationUnit> organisationUnitsAtLevel, List<OrganisationUnit> organisationUnitsInGroups, I18nFormat format) {
    List<DimensionalItemObject> items = new ArrayList<>();
    DimensionType type = null;
    if (DATA_X_DIM_ID.equals(dimension)) {
        items.addAll(getDataDimensionNameableObjects());
        type = DimensionType.DATA_X;
    } else if (PERIOD_DIM_ID.equals(dimension)) {
        setPeriodNames(periods, dynamicNames, format);
        items.addAll(periods);
        if (hasRelativePeriods()) {
            items.addAll(relatives.getRelativePeriods(date, format, dynamicNames));
        }
        type = DimensionType.PERIOD;
    } else if (ORGUNIT_DIM_ID.equals(dimension)) {
        items.addAll(organisationUnits);
        items.addAll(transientOrganisationUnits);
        if (userOrganisationUnit && user != null && user.hasOrganisationUnit()) {
            items.addAll(user.getOrganisationUnits());
        }
        if (userOrganisationUnitChildren && user != null && user.hasOrganisationUnit()) {
            user.getOrganisationUnits().forEach(ou -> items.addAll(ou.getSortedChildren()));
        }
        if (userOrganisationUnitGrandChildren && user != null && user.hasOrganisationUnit()) {
            user.getOrganisationUnits().forEach(ou -> items.addAll(ou.getSortedGrandChildren()));
        }
        if (organisationUnitLevels != null && !organisationUnitLevels.isEmpty() && organisationUnitsAtLevel != null) {
            // Must be set externally
            items.addAll(organisationUnitsAtLevel);
        }
        if (itemOrganisationUnitGroups != null && !itemOrganisationUnitGroups.isEmpty() && organisationUnitsInGroups != null) {
            // Must be set externally
            items.addAll(organisationUnitsInGroups);
        }
        type = DimensionType.ORGANISATION_UNIT;
    } else if (CATEGORYOPTIONCOMBO_DIM_ID.equals(dimension)) {
        items.addAll(transientCategoryOptionCombos);
        type = DimensionType.CATEGORY_OPTION_COMBO;
    } else if (STATIC_DIMS.contains(dimension)) {
        type = DimensionType.STATIC;
    } else {
        // Embedded dimensions
        Optional<DimensionalObject> object = Optional.empty();
        if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.DATA_ELEMENT_GROUP_SET, dataElementGroupSetDimensions)).isPresent()) {
            items.addAll(object.get().getItems());
            type = DimensionType.DATA_ELEMENT_GROUP_SET;
        }
        if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.ORGANISATION_UNIT_GROUP_SET, organisationUnitGroupSetDimensions)).isPresent()) {
            items.addAll(object.get().getItems());
            type = DimensionType.ORGANISATION_UNIT_GROUP_SET;
        }
        if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.CATEGORY, categoryDimensions)).isPresent()) {
            items.addAll(object.get().getItems());
            type = DimensionType.CATEGORY;
        }
        if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.CATEGORY_OPTION_GROUP_SET, categoryOptionGroupSetDimensions)).isPresent()) {
            items.addAll(object.get().getItems());
            type = DimensionType.CATEGORY_OPTION_GROUP_SET;
        }
        // Tracked entity attribute
        Map<String, TrackedEntityAttributeDimension> attributes = Maps.uniqueIndex(attributeDimensions, TrackedEntityAttributeDimension::getUid);
        if (attributes.containsKey(dimension)) {
            TrackedEntityAttributeDimension tead = attributes.get(dimension);
            return new BaseDimensionalObject(dimension, DimensionType.PROGRAM_ATTRIBUTE, null, tead.getDisplayName(), tead.getLegendSet(), tead.getFilter());
        }
        // Tracked entity data element
        Map<String, TrackedEntityDataElementDimension> dataElements = Maps.uniqueIndex(dataElementDimensions, TrackedEntityDataElementDimension::getUid);
        if (dataElements.containsKey(dimension)) {
            TrackedEntityDataElementDimension tedd = dataElements.get(dimension);
            return new BaseDimensionalObject(dimension, DimensionType.PROGRAM_DATA_ELEMENT, null, tedd.getDisplayName(), tedd.getLegendSet(), tedd.getFilter());
        }
        // Tracked entity program indicator
        Map<String, TrackedEntityProgramIndicatorDimension> programIndicators = Maps.uniqueIndex(programIndicatorDimensions, TrackedEntityProgramIndicatorDimension::getUid);
        if (programIndicators.containsKey(dimension)) {
            TrackedEntityProgramIndicatorDimension teid = programIndicators.get(dimension);
            return new BaseDimensionalObject(dimension, DimensionType.PROGRAM_INDICATOR, null, teid.getDisplayName(), teid.getLegendSet(), teid.getFilter());
        }
    }
    IdentifiableObjectUtils.removeDuplicates(items);
    return new BaseDimensionalObject(dimension, type, items);
}
Also used : TrackedEntityAttributeDimension(org.hisp.dhis.trackedentity.TrackedEntityAttributeDimension) ArrayList(java.util.ArrayList) TrackedEntityProgramIndicatorDimension(org.hisp.dhis.trackedentity.TrackedEntityProgramIndicatorDimension) TrackedEntityDataElementDimension(org.hisp.dhis.trackedentity.TrackedEntityDataElementDimension) DimensionalObject(org.hisp.dhis.common.DimensionalObject)

Example 29 with DimensionalObject

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

the class UserCredentials method getDimensionConstraints.

/**
     * Returns the dimensions to use as constrains (filters) in data analytics
     * aggregation.
     */
public Set<DimensionalObject> getDimensionConstraints() {
    Set<DimensionalObject> constraints = new HashSet<>();
    for (CategoryOptionGroupSet cogs : cogsDimensionConstraints) {
        cogs.setDimensionType(DimensionType.CATEGORY_OPTION_GROUP_SET);
        constraints.add(cogs);
    }
    for (DataElementCategory cat : catDimensionConstraints) {
        cat.setDimensionType(DimensionType.CATEGORY);
        constraints.add(cat);
    }
    return constraints;
}
Also used : CategoryOptionGroupSet(org.hisp.dhis.dataelement.CategoryOptionGroupSet) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) HashSet(java.util.HashSet) DimensionalObject(org.hisp.dhis.common.DimensionalObject)

Example 30 with DimensionalObject

use of org.hisp.dhis.common.DimensionalObject 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)

Aggregations

DimensionalObject (org.hisp.dhis.common.DimensionalObject)44 ArrayList (java.util.ArrayList)14 BaseDimensionalObject (org.hisp.dhis.common.BaseDimensionalObject)14 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)13 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)11 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)10 Test (org.junit.Test)8 List (java.util.List)6 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)5 Grid (org.hisp.dhis.common.Grid)5 DataElement (org.hisp.dhis.dataelement.DataElement)5 Period (org.hisp.dhis.period.Period)5 DateUtils.getMediumDateString (org.hisp.dhis.system.util.DateUtils.getMediumDateString)5 Calendar (org.hisp.dhis.calendar.Calendar)4 User (org.hisp.dhis.user.User)4 CategoryOptionGroupSet (org.hisp.dhis.dataelement.CategoryOptionGroupSet)3 DataSet (org.hisp.dhis.dataset.DataSet)3 Indicator (org.hisp.dhis.indicator.Indicator)3 OrganisationUnitGroup (org.hisp.dhis.organisationunit.OrganisationUnitGroup)3 ListGrid (org.hisp.dhis.system.grid.ListGrid)3