Search in sources :

Example 1 with MapMapMap

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

Aggregations

DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)1 MapMapMap (org.hisp.dhis.common.MapMapMap)1 DataElement (org.hisp.dhis.dataelement.DataElement)1 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)1 Period (org.hisp.dhis.period.Period)1 SqlRowSet (org.springframework.jdbc.support.rowset.SqlRowSet)1