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;
}
Aggregations