use of org.hisp.dhis.dataelement.DataElementOperand in project dhis2-core by dhis2.
the class PredictionDataConsolidatorTest method initTest.
// -------------------------------------------------------------------------
// Fixture
// -------------------------------------------------------------------------
@BeforeEach
void initTest() {
dataElementA = createDataElement('A');
dataElementB = createDataElement('B');
dataElementX = createDataElement('X');
dataElementA.setId(1);
dataElementB.setId(2);
dataElementX.setId(3);
cocA = createCategoryOptionCombo('A');
cocB = createCategoryOptionCombo('B');
cocA.setId(4);
cocB.setId(5);
cocA.setUid("CatOptCombA");
cocB.setUid("CatOptCombB");
aocC = createCategoryOptionCombo('C');
aocD = createCategoryOptionCombo('D');
aocC.setId(6);
aocD.setId(7);
aocC.setUid("AttOptionCC");
aocD.setUid("AttOptionCD");
dataElementOperandA = new DataElementOperand(dataElementA, cocA);
dataElementOperandB = new DataElementOperand(dataElementB, cocB);
dataElementOperandX = new DataElementOperand(dataElementX, cocA);
periodA = createPeriod("202201");
periodB = createPeriod("202202");
periodC = createPeriod("202203");
periodA.setUid("Perio202201");
periodB.setUid("Perio202202");
periodC.setUid("Perio202203");
periodA.setId(10);
periodB.setId(11);
periodC.setId(12);
// OrgUnit hierarchy:
//
// Level 1 - Level 2
// -- A
// -- B ------ E
// -- C ------ F
// -- D ------ G
orgUnitA = createOrganisationUnit("A");
orgUnitB = createOrganisationUnit("B");
orgUnitC = createOrganisationUnit("C");
orgUnitD = createOrganisationUnit("D");
orgUnitE = createOrganisationUnit("E", orgUnitB);
orgUnitF = createOrganisationUnit("F", orgUnitC);
orgUnitG = createOrganisationUnit("G", orgUnitD);
orgUnitA.setId(20);
orgUnitB.setId(21);
orgUnitC.setId(22);
orgUnitD.setId(23);
orgUnitE.setId(24);
orgUnitF.setId(25);
orgUnitG.setId(26);
orgUnitA.setUid("orgUnitAAAA");
orgUnitB.setUid("orgUnitBBBB");
orgUnitC.setUid("orgUnitCCCC");
orgUnitD.setUid("orgUnitDDDD");
orgUnitE.setUid("orgUnitEEEE");
orgUnitF.setUid("orgUnitFFFF");
orgUnitG.setUid("orgUnitGGGG");
orgUnitA.setPath("/orgUnitAAAA");
orgUnitB.setPath("/orgUnitBBBB");
orgUnitC.setPath("/orgUnitCCCC");
orgUnitD.setPath("/orgUnitDDDD");
orgUnitE.setPath("/orgUnitBBBB/orgUnitEEEE");
orgUnitF.setPath("/orgUnitCCCC/orgUnitFFFF");
orgUnitG.setPath("/orgUnitDDDD/orgUnitGGGG");
currentUserOrgUnits = Sets.newHashSet(orgUnitA, orgUnitB, orgUnitC, orgUnitD);
levelOneOrgUnits = Lists.newArrayList(orgUnitA, orgUnitB, orgUnitC, orgUnitD);
// DataValue values:
foundValueA = new FoundDimensionItemValue(orgUnitB, periodA, aocC, dataElementA, 25.0);
foundValueB = new FoundDimensionItemValue(orgUnitC, periodA, aocC, dataElementB, 18.0);
foundValueC = new FoundDimensionItemValue(orgUnitB, periodB, aocC, dataElementOperandA, 10.0);
foundValueD = new FoundDimensionItemValue(orgUnitB, periodB, aocD, dataElementOperandX, 30.0);
foundValueE = new FoundDimensionItemValue(orgUnitB, periodC, aocC, dataElementOperandX, 50.0);
// Analytics values with attribute option combo:
foundValueF = new FoundDimensionItemValue(orgUnitB, periodA, aocC, programIndicatorA, 10.0);
foundValueG = new FoundDimensionItemValue(orgUnitB, periodB, aocC, programIndicatorB, 10.0);
foundValueH = new FoundDimensionItemValue(orgUnitB, periodA, aocD, trackedEntityAttributeA, 10.0);
foundValueI = new FoundDimensionItemValue(orgUnitD, periodA, aocC, programIndicatorA, 10.0);
// Analytics values without attribute option combo:
CategoryOptionCombo noAoc = null;
foundValueJ = new FoundDimensionItemValue(orgUnitD, periodA, noAoc, programAttributeA, 100.0);
foundValueK = new FoundDimensionItemValue(orgUnitD, periodA, noAoc, programAttributeB, 200.0);
foundValueL = new FoundDimensionItemValue(orgUnitD, periodB, noAoc, programAttributeA, 300.0);
// old predictor data values:
dataValueX = new DataValue(dataElementX, periodA, orgUnitB, cocA, aocD, "30.0", "Z", null, null, null, false);
dataValueY = new DataValue(dataElementX, periodC, orgUnitB, cocA, aocC, "40.0", "Z", null, null, null, true);
programA = createProgram('A');
programB = createProgram('B');
programA.setUid("ProgramAAAA");
programB.setUid("ProgramBBBB");
programIndicatorA = createProgramIndicator('A', programA, "expressionA", "filterA");
programIndicatorB = createProgramIndicator('B', programB, "expressionB", "filterB");
programIndicatorA.setUid("ProgramIndA");
programIndicatorB.setUid("ProgramIndB");
trackedEntityAttributeA = createTrackedEntityAttribute('A');
trackedEntityAttributeA.setUid("trackEntAtA");
programAttributeA = new ProgramTrackedEntityAttributeDimensionItem(programA, trackedEntityAttributeA);
programAttributeB = new ProgramTrackedEntityAttributeDimensionItem(programB, trackedEntityAttributeA);
programAttributeA.setUid(programA.getUid() + "." + trackedEntityAttributeA.getUid());
programAttributeA.setUid(programB.getUid() + "." + trackedEntityAttributeA.getUid());
items = Sets.newHashSet(// DataValues (always with AOC):
dataElementA, dataElementOperandA, dataElementOperandB, dataElementOperandX, // Analytics with AOC:
programIndicatorA, programIndicatorB, trackedEntityAttributeA, // Analytics without AOC:
programAttributeA, programAttributeB);
dataValueQueryPeriods = Sets.newHashSet(periodA, periodB, periodC);
analyticsQueryPeriods = Sets.newHashSet(periodA, periodB);
existingOutputPeriods = Sets.newHashSet(periodC);
outputDataElementOperand = dataElementOperandX;
when(dataValueFetcher.setIncludeDeleted(true)).thenReturn(dataValueFetcher);
when(dataValueFetcher.setIncludeDescendants(INCLUDE_DESCENDANTS)).thenReturn(dataValueFetcher);
consolidator = new PredictionDataConsolidator(items, INCLUDE_DESCENDANTS, dataValueFetcher, analyticsFetcher);
consolidator.setAnalyticsBatchFetchSize(3);
}
use of org.hisp.dhis.dataelement.DataElementOperand in project dhis2-core by dhis2.
the class PredictionDataValueFetcherTest method initTest.
// -------------------------------------------------------------------------
// Fixture
// -------------------------------------------------------------------------
@BeforeEach
void initTest() {
dataElementA = createDataElement('A');
dataElementB = createDataElement('B');
dataElementX = createDataElement('X');
dataElementA.setId(1);
dataElementB.setId(2);
dataElementX.setId(3);
dataElements = Sets.newHashSet(dataElementA, dataElementB);
cocA = createCategoryOptionCombo('A');
cocB = createCategoryOptionCombo('B');
cocA.setId(4);
cocB.setId(5);
cocA.setUid("CatOptCombA");
cocB.setUid("CatOptCombB");
aocC = createCategoryOptionCombo('C');
aocD = createCategoryOptionCombo('D');
aocC.setId(6);
aocD.setId(7);
aocC.setUid("AttOptionCC");
aocD.setUid("AttOptionCD");
dataElementOperandA = new DataElementOperand(dataElementA, cocA);
dataElementOperandB = new DataElementOperand(dataElementB, cocB);
dataElementOperandX = new DataElementOperand(dataElementX, cocA);
dataElementOperands = Sets.newHashSet(dataElementOperandA, dataElementOperandB, dataElementOperandX);
periodA = createPeriod("202201");
periodB = createPeriod("202202");
periodC = createPeriod("202203");
periodA.setUid("Perio202201");
periodB.setUid("Perio202202");
periodC.setUid("Perio202203");
periodA.setId(10);
periodB.setId(11);
periodC.setId(12);
queryPeriods = Sets.newHashSet(periodA, periodB, periodC);
outputPeriods = Sets.newHashSet(periodC);
// OrgUnit hierarchy:
//
// Level 1 - Level 2
// -- A
// -- B ------ E
// -- C ------ F
// -- D ------ G
orgUnitA = createOrganisationUnit("A");
orgUnitB = createOrganisationUnit("B");
orgUnitC = createOrganisationUnit("C");
orgUnitD = createOrganisationUnit("D");
orgUnitE = createOrganisationUnit("E", orgUnitB);
orgUnitF = createOrganisationUnit("F", orgUnitC);
orgUnitG = createOrganisationUnit("G", orgUnitD);
orgUnitA.setId(20);
orgUnitB.setId(21);
orgUnitC.setId(22);
orgUnitD.setId(23);
orgUnitE.setId(24);
orgUnitF.setId(25);
orgUnitG.setId(26);
orgUnitA.setUid("orgUnitAAAA");
orgUnitB.setUid("orgUnitBBBB");
orgUnitC.setUid("orgUnitCCCC");
orgUnitD.setUid("orgUnitDDDD");
orgUnitE.setUid("orgUnitEEEE");
orgUnitF.setUid("orgUnitFFFF");
orgUnitG.setUid("orgUnitGGGG");
orgUnitA.setPath("/orgUnitAAAA");
orgUnitB.setPath("/orgUnitBBBB");
orgUnitC.setPath("/orgUnitCCCC");
orgUnitD.setPath("/orgUnitDDDD");
orgUnitE.setPath("/orgUnitBBBB/orgUnitEEEE");
orgUnitF.setPath("/orgUnitCCCC/orgUnitFFFF");
orgUnitG.setPath("/orgUnitDDDD/orgUnitGGGG");
currentUserOrgUnits = Sets.newHashSet(orgUnitA, orgUnitB, orgUnitC, orgUnitD);
levelOneOrgUnits = Lists.newArrayList(orgUnitA, orgUnitB, orgUnitC, orgUnitD);
dataValueA = new DataValue(dataElementA, periodA, orgUnitB, cocA, aocC, "10.0", "Y", null, null, null, false);
dataValueB = new DataValue(dataElementA, periodA, orgUnitB, cocB, aocC, "15.0", "Y", null, null, null, false);
dataValueX = new DataValue(dataElementX, periodA, orgUnitB, cocA, aocD, "30.0", "Z", null, null, null, false);
dataValueY = new DataValue(dataElementX, periodC, orgUnitB, cocA, aocC, "40.0", "Z", null, null, null, true);
dataValueZ = new DataValue(dataElementX, periodC, orgUnitE, cocA, aocC, "50.0", "Z", null, null, null, false);
dataValueC = new DataValue(dataElementB, periodB, orgUnitC, cocA, aocC, "18.0", "Y", null, null, null, false);
dataValueD = new DataValue(dataElementB, periodB, orgUnitC, cocB, aocC, "20.0", "Y", null, null, null, true);
deflatedDataValueA = new DeflatedDataValue(dataValueA);
deflatedDataValueB = new DeflatedDataValue(dataValueB);
deflatedDataValueX = new DeflatedDataValue(dataValueX);
deflatedDataValueY = new DeflatedDataValue(dataValueY);
deflatedDataValueZ = new DeflatedDataValue(dataValueZ);
deflatedDataValueC = new DeflatedDataValue(dataValueC);
deflatedDataValueD = new DeflatedDataValue(dataValueD);
foundValueA = new FoundDimensionItemValue(orgUnitB, periodA, aocC, dataElementA, 25.0);
foundValueB = new FoundDimensionItemValue(orgUnitC, periodB, aocC, dataElementB, 18.0);
foundValueC = new FoundDimensionItemValue(orgUnitB, periodA, aocC, dataElementOperandA, 10.0);
foundValueD = new FoundDimensionItemValue(orgUnitB, periodA, aocD, dataElementOperandX, 30.0);
foundValueE = new FoundDimensionItemValue(orgUnitB, periodC, aocC, dataElementOperandX, 50.0);
fetcher = new PredictionDataValueFetcher(dataValueService, categoryService);
}
use of org.hisp.dhis.dataelement.DataElementOperand 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.dataelement.DataElementOperand 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.dataelement.DataElementOperand in project dhis2-core by dhis2.
the class LoadFormAction method getSectionForm.
// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
private void getSectionForm(Collection<DataElement> dataElements, DataSet dataSet) {
sections = new ArrayList<>(dataSet.getSections());
Collections.sort(sections, new SectionOrderComparator());
for (Section section : sections) {
Set<Integer> categoryComboIds = new HashSet<>();
for (DataElementCategoryCombo categoryCombo : section.getCategoryCombos()) {
categoryComboIds.add(categoryCombo.getId());
sectionCategoryComboDataElements.put(section.getId() + "-" + categoryCombo.getId(), section.getDataElementsByCategoryCombo(categoryCombo));
}
if (!categoryComboIds.isEmpty()) {
sectionCombos.put(section.getId(), categoryComboIds);
}
for (DataElementOperand operand : section.getGreyedFields()) {
if (operand != null && operand.getDataElement() != null && operand.getCategoryOptionCombo() != null) {
greyedFields.put(operand.getDataElement().getUid() + ":" + operand.getCategoryOptionCombo().getUid(), true);
}
}
}
}
Aggregations