Search in sources :

Example 51 with DimensionalItemObject

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

the class DefaultExpressionService method getDimensionalItemObjectsInIndicators.

@Override
public Set<DimensionalItemObject> getDimensionalItemObjectsInIndicators(Collection<Indicator> indicators) {
    Set<DimensionalItemObject> items = Sets.newHashSet();
    for (Indicator indicator : indicators) {
        items.addAll(getDimensionalItemObjectsInExpression(indicator.getNumerator()));
        items.addAll(getDimensionalItemObjectsInExpression(indicator.getDenominator()));
    }
    return items;
}
Also used : DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) Indicator(org.hisp.dhis.indicator.Indicator)

Example 52 with DimensionalItemObject

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

the class HibernateDataValueStore method getDataElementOperandValues.

@Override
public MapMapMap<Period, String, DimensionalItemObject, Double> getDataElementOperandValues(Collection<DataElementOperand> dataElementOperands, Collection<Period> periods, OrganisationUnit orgUnit) {
    MapMapMap<Period, String, DimensionalItemObject, Double> result = new MapMapMap<>();
    Collection<Integer> periodIdList = IdentifiableObjectUtils.getIdentifiers(periods);
    SetMap<DataElement, DataElementOperand> deosByDataElement = getDeosByDataElement(dataElementOperands);
    if (periods.size() == 0 || dataElementOperands.size() == 0) {
        return result;
    }
    String sql = "select dv.dataelementid, coc.uid, dv.attributeoptioncomboid, dv.periodid, " + "sum( cast( dv.value as " + statementBuilder.getDoubleColumnType() + " ) ) as value " + "from datavalue dv " + "join organisationunit o on o.organisationunitid = dv.sourceid " + "join categoryoptioncombo coc on coc.categoryoptioncomboid = dv.categoryoptioncomboid " + "where o.path like '" + orgUnit.getPath() + "%' " + "and dv.periodid in (" + TextUtils.getCommaDelimitedString(periodIdList) + ") " + "and dv.value is not null " + "and dv.deleted is false " + "and ( ";
    String snippit = "";
    for (DataElement dataElement : deosByDataElement.keySet()) {
        sql += snippit + "( dv.dataelementid = " + dataElement.getId() + getDisaggRestriction(deosByDataElement.get(dataElement)) + " ) ";
        snippit = "or ";
    }
    sql += ") group by dv.dataelementid, coc.uid, dv.attributeoptioncomboid, dv.periodid";
    SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);
    Map<Integer, DataElement> dataElementsById = IdentifiableObjectUtils.getIdentifierMap(deosByDataElement.keySet());
    Map<Integer, Period> periodsById = IdentifiableObjectUtils.getIdentifierMap(periods);
    while (rowSet.next()) {
        Integer dataElementId = rowSet.getInt(1);
        String categoryOptionComboUid = rowSet.getString(2);
        Integer periodId = rowSet.getInt(4);
        Double value = rowSet.getDouble(5);
        DataElement dataElement = dataElementsById.get(dataElementId);
        Period period = periodsById.get(periodId);
        Set<DataElementOperand> deos = deosByDataElement.get(dataElement);
        for (DataElementOperand deo : deos) {
            if (deo.getCategoryOptionCombo() == null || deo.getCategoryOptionCombo().getUid() == categoryOptionComboUid) {
                Double existingValue = result.getValue(period, categoryOptionComboUid, deo);
                if (existingValue != null) {
                    value += existingValue;
                }
                result.putEntry(period, categoryOptionComboUid, deo, value);
            }
        }
    }
    return result;
}
Also used : SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) Period(org.hisp.dhis.period.Period) MapMapMap(org.hisp.dhis.common.MapMapMap) DataElement(org.hisp.dhis.dataelement.DataElement) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject)

Example 53 with DimensionalItemObject

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

the class HibernateDataValueStore method getDataValueMapByAttributeCombo.

@Override
public MapMap<String, DimensionalItemObject, Double> getDataValueMapByAttributeCombo(SetMap<String, DataElementOperand> dataElementOperandsToGet, Date date, OrganisationUnit source, Collection<PeriodType> periodTypes, DataElementCategoryOptionCombo attributeCombo, Set<CategoryOptionGroup> cogDimensionConstraints, Set<DataElementCategoryOption> coDimensionConstraints, MapMap<String, DataElementOperand, Date> lastUpdatedMap) {
    MapMap<String, DimensionalItemObject, Double> map = new MapMap<>();
    if (dataElementOperandsToGet.isEmpty() || periodTypes.isEmpty() || (cogDimensionConstraints != null && cogDimensionConstraints.isEmpty()) || (coDimensionConstraints != null && coDimensionConstraints.isEmpty())) {
        return map;
    }
    String joinCo = coDimensionConstraints == null && cogDimensionConstraints == null ? StringUtils.EMPTY : "join categoryoptioncombos_categoryoptions c_c on dv.attributeoptioncomboid = c_c.categoryoptioncomboid ";
    String joinCog = cogDimensionConstraints == null ? StringUtils.EMPTY : "join categoryoptiongroupmembers cogm on c_c.categoryoptionid = cogm.categoryoptionid ";
    String whereCo = coDimensionConstraints == null ? StringUtils.EMPTY : "and c_c.categoryoptionid in (" + TextUtils.getCommaDelimitedString(getIdentifiers(coDimensionConstraints)) + ") ";
    String whereCog = cogDimensionConstraints == null ? StringUtils.EMPTY : "and cogm.categoryoptiongroupid in (" + TextUtils.getCommaDelimitedString(getIdentifiers(cogDimensionConstraints)) + ") ";
    String whereCombo = attributeCombo == null ? StringUtils.EMPTY : "and dv.attributeoptioncomboid = " + attributeCombo.getId() + " ";
    String sql = "select de.uid, coc.uid, aoc.uid, dv.value, dv.lastupdated, p.startdate, p.enddate " + "from datavalue dv " + "inner join dataelement de on dv.dataelementid = de.dataelementid " + "inner join categoryoptioncombo coc on dv.categoryoptioncomboid = coc.categoryoptioncomboid " + "inner join categoryoptioncombo aoc on dv.attributeoptioncomboid = aoc.categoryoptioncomboid " + "inner join period p on p.periodid = dv.periodid " + joinCo + joinCog + "where de.uid in (" + TextUtils.getQuotedCommaDelimitedString(dataElementOperandsToGet.keySet()) + ") " + "and dv.sourceid = " + source.getId() + " " + "and p.startdate <= '" + DateUtils.getMediumDateString(date) + "' " + "and p.enddate >= '" + DateUtils.getMediumDateString(date) + "' " + "and p.periodtypeid in (" + TextUtils.getCommaDelimitedString(getIds(periodTypes)) + ") " + "and dv.deleted is false " + whereCo + whereCog + whereCombo;
    SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);
    MapMap<String, DataElementOperand, Long> checkForDuplicates = new MapMap<>();
    int rowCount = 0;
    while (rowSet.next()) {
        rowCount++;
        String dataElement = rowSet.getString(1);
        String categoryOptionCombo = rowSet.getString(2);
        String attributeOptionCombo = rowSet.getString(3);
        Double value = MathUtils.parseDouble(rowSet.getString(4));
        Date lastUpdated = rowSet.getDate(5);
        Date periodStartDate = rowSet.getDate(6);
        Date periodEndDate = rowSet.getDate(7);
        long periodInterval = periodEndDate.getTime() - periodStartDate.getTime();
        if (value != null) {
            Set<DataElementOperand> deos = dataElementOperandsToGet.get(dataElement);
            for (DataElementOperand deo : deos) {
                if (deo.getCategoryOptionCombo() == null || deo.getCategoryOptionCombo().getUid().equals(categoryOptionCombo)) {
                    Double existingValue = map.getValue(attributeOptionCombo, deo);
                    Long existingPeriodInterval = checkForDuplicates.getValue(attributeOptionCombo, deo);
                    if (existingPeriodInterval != null) {
                        if (existingPeriodInterval < periodInterval) {
                            // Do not overwrite the previous value if for a shorter interval
                            continue;
                        } else if (existingPeriodInterval > periodInterval) {
                            // Overwrite previous value if for a longer interval
                            existingValue = null;
                            if (lastUpdatedMap != null) {
                                lastUpdatedMap.putEntry(attributeOptionCombo, deo, lastUpdated);
                            }
                        }
                    }
                    if (existingValue != null) {
                        value += existingValue;
                    }
                    map.putEntry(attributeOptionCombo, deo, value);
                    if (lastUpdatedMap != null && lastUpdated != null) {
                        Date existingLastUpdated = lastUpdatedMap.getValue(attributeOptionCombo, deo);
                        if (existingLastUpdated == null || lastUpdated.after(existingLastUpdated)) {
                            lastUpdatedMap.putEntry(attributeOptionCombo, deo, lastUpdated);
                        }
                    }
                    checkForDuplicates.putEntry(attributeOptionCombo, deo, periodInterval);
                }
            }
        }
    }
    log.trace("getDataValueMapByAttributeCombo: " + rowCount + " rows into " + map.size() + " map entries from \"" + sql + "\"");
    return map;
}
Also used : SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) Date(java.util.Date) MapMapMap(org.hisp.dhis.common.MapMapMap) MapMap(org.hisp.dhis.common.MapMap) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject)

Example 54 with DimensionalItemObject

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

the class DefaultDimensionService method mergeDimensionalObjects.

/**
     * Sets persistent objects for dimensional associations on the given
     * BaseAnalyticalObject based on the given list of transient DimensionalObjects.
     * <p>
     * Relative periods represented by enums are converted into a RelativePeriods
     * object. User organisation units represented by enums are converted and
     * represented by the user organisation unit persisted properties on the
     * BaseAnalyticalObject.
     *
     * @param object     the BaseAnalyticalObject to merge.
     * @param dimensions the list of dimensions.
     */
private void mergeDimensionalObjects(BaseAnalyticalObject object, List<DimensionalObject> dimensions) {
    if (object == null || dimensions == null) {
        return;
    }
    for (DimensionalObject dimension : dimensions) {
        DimensionType type = getDimensionType(dimension.getDimension());
        String dimensionId = dimension.getDimension();
        List<DimensionalItemObject> items = dimension.getItems();
        if (items != null) {
            List<String> uids = getUids(items);
            if (DATA_X.equals(type)) {
                for (String uid : uids) {
                    DimensionalItemObject dimItemObject = getDataDimensionalItemObject(IdScheme.UID, uid);
                    if (dimItemObject != null) {
                        DataDimensionItem item = DataDimensionItem.create(dimItemObject);
                        object.getDataDimensionItems().add(item);
                    }
                }
            } else if (PERIOD.equals(type)) {
                List<RelativePeriodEnum> enums = new ArrayList<>();
                List<Period> periods = new UniqueArrayList<>();
                for (String isoPeriod : uids) {
                    if (RelativePeriodEnum.contains(isoPeriod)) {
                        enums.add(RelativePeriodEnum.valueOf(isoPeriod));
                    } else {
                        Period period = PeriodType.getPeriodFromIsoString(isoPeriod);
                        if (period != null) {
                            periods.add(period);
                        }
                    }
                }
                object.setRelatives(new RelativePeriods().setRelativePeriodsFromEnums(enums));
                object.setPeriods(periodService.reloadPeriods(new ArrayList<>(periods)));
            } else if (ORGANISATION_UNIT.equals(type)) {
                for (String ou : uids) {
                    if (KEY_USER_ORGUNIT.equals(ou)) {
                        object.setUserOrganisationUnit(true);
                    } else if (KEY_USER_ORGUNIT_CHILDREN.equals(ou)) {
                        object.setUserOrganisationUnitChildren(true);
                    } else if (KEY_USER_ORGUNIT_GRANDCHILDREN.equals(ou)) {
                        object.setUserOrganisationUnitGrandChildren(true);
                    } else if (ou != null && ou.startsWith(KEY_LEVEL)) {
                        int level = DimensionalObjectUtils.getLevelFromLevelParam(ou);
                        if (level > 0) {
                            object.getOrganisationUnitLevels().add(level);
                        }
                    } else if (ou != null && ou.startsWith(KEY_ORGUNIT_GROUP)) {
                        String uid = DimensionalObjectUtils.getUidFromGroupParam(ou);
                        OrganisationUnitGroup group = idObjectManager.get(OrganisationUnitGroup.class, uid);
                        if (group != null) {
                            object.getItemOrganisationUnitGroups().add(group);
                        }
                    } else {
                        OrganisationUnit unit = idObjectManager.get(OrganisationUnit.class, ou);
                        if (unit != null) {
                            object.getOrganisationUnits().add(unit);
                        }
                    }
                }
            } else if (DATA_ELEMENT_GROUP_SET.equals(type)) {
                DataElementGroupSetDimension groupSetDimension = new DataElementGroupSetDimension();
                groupSetDimension.setDimension(idObjectManager.get(DataElementGroupSet.class, dimensionId));
                groupSetDimension.getItems().addAll(idObjectManager.getByUidOrdered(DataElementGroup.class, uids));
                object.getDataElementGroupSetDimensions().add(groupSetDimension);
            } else if (ORGANISATION_UNIT_GROUP_SET.equals(type)) {
                OrganisationUnitGroupSetDimension groupSetDimension = new OrganisationUnitGroupSetDimension();
                groupSetDimension.setDimension(idObjectManager.get(OrganisationUnitGroupSet.class, dimensionId));
                groupSetDimension.getItems().addAll(idObjectManager.getByUidOrdered(OrganisationUnitGroup.class, uids));
                object.getOrganisationUnitGroupSetDimensions().add(groupSetDimension);
            } else if (CATEGORY.equals(type)) {
                CategoryDimension categoryDimension = new CategoryDimension();
                categoryDimension.setDimension(idObjectManager.get(DataElementCategory.class, dimensionId));
                categoryDimension.getItems().addAll(idObjectManager.getByUidOrdered(DataElementCategoryOption.class, uids));
                object.getCategoryDimensions().add(categoryDimension);
            } else if (CATEGORY_OPTION_GROUP_SET.equals(type)) {
                CategoryOptionGroupSetDimension groupSetDimension = new CategoryOptionGroupSetDimension();
                groupSetDimension.setDimension(idObjectManager.get(CategoryOptionGroupSet.class, dimensionId));
                groupSetDimension.getItems().addAll(idObjectManager.getByUidOrdered(CategoryOptionGroup.class, uids));
                object.getCategoryOptionGroupSetDimensions().add(groupSetDimension);
            } else if (PROGRAM_ATTRIBUTE.equals(type)) {
                TrackedEntityAttributeDimension attributeDimension = new TrackedEntityAttributeDimension();
                attributeDimension.setAttribute(idObjectManager.get(TrackedEntityAttribute.class, dimensionId));
                attributeDimension.setLegendSet(dimension.hasLegendSet() ? idObjectManager.get(LegendSet.class, dimension.getLegendSet().getUid()) : null);
                attributeDimension.setFilter(dimension.getFilter());
                object.getAttributeDimensions().add(attributeDimension);
            } else if (PROGRAM_DATA_ELEMENT.equals(type)) {
                TrackedEntityDataElementDimension dataElementDimension = new TrackedEntityDataElementDimension();
                dataElementDimension.setDataElement(idObjectManager.get(DataElement.class, dimensionId));
                dataElementDimension.setLegendSet(dimension.hasLegendSet() ? idObjectManager.get(LegendSet.class, dimension.getLegendSet().getUid()) : null);
                dataElementDimension.setFilter(dimension.getFilter());
                object.getDataElementDimensions().add(dataElementDimension);
            } else if (PROGRAM_INDICATOR.equals(type)) {
                TrackedEntityProgramIndicatorDimension programIndicatorDimension = new TrackedEntityProgramIndicatorDimension();
                programIndicatorDimension.setProgramIndicator(idObjectManager.get(ProgramIndicator.class, dimensionId));
                programIndicatorDimension.setLegendSet(dimension.hasLegendSet() ? idObjectManager.get(LegendSet.class, dimension.getLegendSet().getUid()) : null);
                programIndicatorDimension.setFilter(dimension.getFilter());
                object.getProgramIndicatorDimensions().add(programIndicatorDimension);
            }
        }
    }
}
Also used : DimensionType(org.hisp.dhis.common.DimensionType) RelativePeriods(org.hisp.dhis.period.RelativePeriods) DataElementGroupSetDimension(org.hisp.dhis.dataelement.DataElementGroupSetDimension) CategoryDimension(org.hisp.dhis.dataelement.CategoryDimension) CategoryOptionGroupSetDimension(org.hisp.dhis.dataelement.CategoryOptionGroupSetDimension) TrackedEntityDataElementDimension(org.hisp.dhis.trackedentity.TrackedEntityDataElementDimension) DimensionalObject(org.hisp.dhis.common.DimensionalObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DataElement(org.hisp.dhis.dataelement.DataElement) CategoryOptionGroup(org.hisp.dhis.dataelement.CategoryOptionGroup) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) DataDimensionItem(org.hisp.dhis.common.DataDimensionItem) UniqueArrayList(org.hisp.dhis.commons.collection.UniqueArrayList) List(java.util.List) ArrayList(java.util.ArrayList) TrackedEntityAttributeDimension(org.hisp.dhis.trackedentity.TrackedEntityAttributeDimension) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) CategoryOptionGroupSet(org.hisp.dhis.dataelement.CategoryOptionGroupSet) Period(org.hisp.dhis.period.Period) TrackedEntityProgramIndicatorDimension(org.hisp.dhis.trackedentity.TrackedEntityProgramIndicatorDimension) LegendSet(org.hisp.dhis.legend.LegendSet) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) OrganisationUnitGroupSetDimension(org.hisp.dhis.organisationunit.OrganisationUnitGroupSetDimension) OrganisationUnitGroupSet(org.hisp.dhis.organisationunit.OrganisationUnitGroupSet)

Example 55 with DimensionalItemObject

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

the class AnalyticalObjectEmbeddedDimensionUpgrader method upgradeGrupSetDimensions.

@SuppressWarnings("unchecked")
private void upgradeGrupSetDimensions(String favorite, String dimension, String item, Class<? extends AnalyticalObject> favoriteClazz, Class<? extends DimensionalObject> dimensionClass, Class<? extends DimensionalItemObject> itemClass, BiConsumer<BaseDimensionalEmbeddedObject, AnalyticalObject> consumer) {
    String groupSetSqlPattern = "select distinct d.{favorite}id, gsm.{dimension}id " + "from {favorite}_{item}s d " + "inner join {dimension}members gsm on d.{item}id=gsm.{item}id";
    String groupSetSql = TextUtils.replace(groupSetSqlPattern, "{favorite}", favorite, "{dimension}", dimension, "{item}", item);
    log.debug(String.format("Group set SQL: %s", groupSetSql));
    String groupSqlPattern = "select d.{item}id " + "from {favorite}_{item}s d " + "inner join {dimension}members gsm on d.{item}id=gsm.{item}id " + "where d.{favorite}id={favoriteId} " + "and gsm.{dimension}id={dimensionId} " + "order by d.sort_order";
    SqlRowSet groupSetRs = jdbcTemplate.queryForRowSet(groupSetSql);
    while (groupSetRs.next()) {
        int favoriteId = groupSetRs.getInt(1);
        int dimensionId = groupSetRs.getInt(2);
        AnalyticalObject analyticalObject = idObjectManager.get(favoriteClazz, favoriteId);
        DimensionalObject groupSet = idObjectManager.get(dimensionClass, dimensionId);
        Assert.notNull(analyticalObject, String.format("Analytical object not found: %s, class: %s", favoriteId, favoriteClazz));
        Assert.notNull(groupSet, String.format("Group set not found: %s, class: %s", dimensionId, dimensionClass));
        String groupSql = TextUtils.replace(groupSqlPattern, "{favorite}", favorite, "{dimension}", dimension, "{item}", item, "{favoriteId}", String.valueOf(favoriteId), "{dimensionId}", String.valueOf(dimensionId));
        log.debug(String.format("Group SQL: %s", groupSql));
        SqlRowSet groupRs = jdbcTemplate.queryForRowSet(groupSql);
        List<Integer> groupIds = new ArrayList<>();
        while (groupRs.next()) {
            groupIds.add(groupRs.getInt(1));
        }
        List<DimensionalItemObject> groups = (List<DimensionalItemObject>) idObjectManager.getById(itemClass, groupIds);
        Assert.notNull(groups, "Groups cannot be null");
        BaseDimensionalEmbeddedObject embeddedDimension = new BaseDimensionalEmbeddedObject(groupSet, groups);
        consumer.accept(embeddedDimension, analyticalObject);
        idObjectManager.update(analyticalObject);
        log.info(String.format("Added %s group set dimension: %s with groups: %d for favorite: %s", favorite, groupSet.getUid(), groups.size(), analyticalObject.getUid()));
    }
    String dropSql = TextUtils.replace("drop table {favorite}_{item}s", "{favorite}", favorite, "{item}", item);
    jdbcTemplate.update(dropSql);
    log.info(String.format("Dropped table, update done for favorite: %s and dimension: %s", favorite, dimension));
}
Also used : SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) DimensionalObject(org.hisp.dhis.common.DimensionalObject)

Aggregations

DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)55 ArrayList (java.util.ArrayList)15 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)15 DimensionalObject (org.hisp.dhis.common.DimensionalObject)15 BaseDimensionalObject (org.hisp.dhis.common.BaseDimensionalObject)14 Test (org.junit.Test)12 Period (org.hisp.dhis.period.Period)11 ListMap (org.hisp.dhis.common.ListMap)10 DataElement (org.hisp.dhis.dataelement.DataElement)10 DhisSpringTest (org.hisp.dhis.DhisSpringTest)7 BaseDimensionalItemObject (org.hisp.dhis.common.BaseDimensionalItemObject)7 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)7 HashMap (java.util.HashMap)6 List (java.util.List)6 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)6 PeriodType (org.hisp.dhis.period.PeriodType)6 SqlRowSet (org.springframework.jdbc.support.rowset.SqlRowSet)5 Matcher (java.util.regex.Matcher)4 Grid (org.hisp.dhis.common.Grid)4 HashSet (java.util.HashSet)3