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));
}
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;
}
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;
}
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;
}
use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.
the class QueryPlannerTest method testSetGetCopy.
// -------------------------------------------------------------------------
// Tests
// -------------------------------------------------------------------------
@Test
public void testSetGetCopy() {
List<DimensionalItemObject> desA = getList(deA, deB);
List<DimensionalItemObject> ousA = getList(ouA, ouB);
List<DimensionalItemObject> ousB = getList(ouC, ouD);
List<DimensionalItemObject> pesA = getList(createPeriod("2000Q1"), createPeriod("2000Q2"));
List<DimensionalItemObject> pesB = getList(createPeriod("200001"), createPeriod("200002"));
DataQueryParams paramsA = DataQueryParams.newBuilder().withDataElements(desA).withOrganisationUnits(ousA).withPeriods(pesA).build();
DataQueryParams paramsB = DataQueryParams.newBuilder(paramsA).withOrganisationUnits(ousB).withPeriods(pesB).build();
assertEquals(desA, paramsA.getDataElements());
assertEquals(ousA, paramsA.getOrganisationUnits());
assertEquals(pesA, paramsA.getPeriods());
assertEquals(desA, paramsB.getDataElements());
assertEquals(ousB, paramsB.getOrganisationUnits());
assertEquals(pesB, paramsB.getPeriods());
}
Aggregations