Search in sources :

Example 21 with DataElementOperand

use of org.hisp.dhis.dataelement.DataElementOperand in project dhis2-core by dhis2.

the class GetValidationResultDetailsAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    validationRule = validationRuleService.getValidationRule(validationRuleId);
    Period period = periodService.getPeriod(periodId);
    OrganisationUnit source = organisationUnitService.getOrganisationUnit(sourceId);
    for (DataElementOperand operand : expressionService.getOperandsInExpression(validationRule.getLeftSide().getExpression())) {
        DataValue dataValue = dataValueService.getDataValue(operand.getDataElement(), period, source, operand.getCategoryOptionCombo());
        String value = dataValue != null ? dataValue.getValue() : NULL_REPLACEMENT;
        leftSideMap.put(operand.getName(), value);
    }
    for (DataElementOperand operand : expressionService.getOperandsInExpression(validationRule.getRightSide().getExpression())) {
        DataValue dataValue = dataValueService.getDataValue(operand.getDataElement(), period, source, operand.getCategoryOptionCombo());
        String value = dataValue != null ? dataValue.getValue() : NULL_REPLACEMENT;
        rightSideMap.put(operand.getName(), value);
    }
    return SUCCESS;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) DataValue(org.hisp.dhis.datavalue.DataValue) Period(org.hisp.dhis.period.Period)

Example 22 with DataElementOperand

use of org.hisp.dhis.dataelement.DataElementOperand in project dhis2-core by dhis2.

the class DefaultValidationService method getDimensionalItemObjects.

/**
     * Gets all required DimensionalItemObjects from their UIDs.
     *
     * @param expressionIdMap UIDs of DimensionalItemObjects to get.
     * @return map of the DimensionalItemObjects.
     */
private Map<String, DimensionalItemObject> getDimensionalItemObjects(SetMap<Class<? extends DimensionalItemObject>, String> expressionIdMap) {
    // 1. Get ids for all the individual IdentifiableObjects within the DimensionalItemObjects:
    SetMap<Class<? extends IdentifiableObject>, String> idsToGet = new SetMap<>();
    getIdentifiableObjectIds(idsToGet, expressionIdMap, DataElementOperand.class, DataElement.class, DataElementCategoryOptionCombo.class);
    getIdentifiableObjectIds(idsToGet, expressionIdMap, ProgramDataElementDimensionItem.class, Program.class, DataElement.class);
    getIdentifiableObjectIds(idsToGet, expressionIdMap, ProgramTrackedEntityAttributeDimensionItem.class, Program.class, TrackedEntityAttribute.class);
    getIdentifiableObjectIds(idsToGet, expressionIdMap, ProgramIndicator.class, ProgramIndicator.class);
    // 2. Look up all the IdentifiableObjects (each class all together, for best performance):
    MapMap<Class<? extends IdentifiableObject>, String, IdentifiableObject> idMap = new MapMap<>();
    for (Map.Entry<Class<? extends IdentifiableObject>, Set<String>> e : idsToGet.entrySet()) {
        idMap.putEntries(e.getKey(), idObjectManager.get(e.getKey(), e.getValue()).stream().collect(Collectors.toMap(o -> o.getUid(), o -> o)));
    }
    // 3. Build the map of DimensionalItemObjects:
    Map<String, DimensionalItemObject> dimObjects = new HashMap<>();
    for (Map.Entry<Class<? extends DimensionalItemObject>, Set<String>> e : expressionIdMap.entrySet()) {
        for (String id : e.getValue()) {
            if (e.getKey() == DataElementOperand.class) {
                DataElementOperand deo = new DataElementOperand((DataElement) idMap.getValue(DataElement.class, getIdPart(id, 0)), (DataElementCategoryOptionCombo) idMap.getValue(DataElementCategoryOptionCombo.class, getIdPart(id, 1)));
                if (deo.getDataElement() != null && (deo.getCategoryOptionCombo() != null || getIdPart(id, 1) == null)) {
                    dimObjects.put(id, deo);
                }
            } else if (e.getKey() == ProgramDataElementDimensionItem.class) {
                ProgramDataElementDimensionItem pde = new ProgramDataElementDimensionItem((Program) idMap.getValue(Program.class, getIdPart(id, 0)), (DataElement) idMap.getValue(DataElement.class, getIdPart(id, 1)));
                if (pde.getProgram() != null && pde.getDataElement() != null) {
                    dimObjects.put(id, pde);
                }
            } else if (e.getKey() == ProgramTrackedEntityAttributeDimensionItem.class) {
                ProgramTrackedEntityAttributeDimensionItem pa = new ProgramTrackedEntityAttributeDimensionItem((Program) idMap.getValue(Program.class, getIdPart(id, 0)), (TrackedEntityAttribute) idMap.getValue(TrackedEntityAttribute.class, getIdPart(id, 1)));
                if (pa.getProgram() != null && pa.getAttribute() != null) {
                    dimObjects.put(id, pa);
                }
            } else if (e.getKey() == ProgramIndicator.class) {
                ProgramIndicator pi = (ProgramIndicator) idMap.getValue(ProgramIndicator.class, id);
                if (pi != null) {
                    dimObjects.put(id, pi);
                }
            }
        }
    }
    return dimObjects;
}
Also used : DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) DataSet(org.hisp.dhis.dataset.DataSet) ImmutableSet(com.google.common.collect.ImmutableSet) Program(org.hisp.dhis.program.Program) ProgramTrackedEntityAttributeDimensionItem(org.hisp.dhis.program.ProgramTrackedEntityAttributeDimensionItem) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) MapMap(org.hisp.dhis.common.MapMap) DataElement(org.hisp.dhis.dataelement.DataElement) ProgramDataElementDimensionItem(org.hisp.dhis.program.ProgramDataElementDimensionItem) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ProgramIndicator(org.hisp.dhis.program.ProgramIndicator) SetMap(org.hisp.dhis.common.SetMap) SetMap(org.hisp.dhis.common.SetMap) MapMap(org.hisp.dhis.common.MapMap)

Example 23 with DataElementOperand

use of org.hisp.dhis.dataelement.DataElementOperand in project dhis2-core by dhis2.

the class DefaultValidationService method validateRequiredComments.

@Override
public List<DataElementOperand> validateRequiredComments(DataSet dataSet, Period period, OrganisationUnit organisationUnit, DataElementCategoryOptionCombo attributeOptionCombo) {
    List<DataElementOperand> violations = new ArrayList<>();
    if (dataSet.isNoValueRequiresComment()) {
        for (DataElement de : dataSet.getDataElements()) {
            for (DataElementCategoryOptionCombo co : de.getCategoryOptionCombos()) {
                DataValue dv = dataValueService.getDataValue(de, period, organisationUnit, co, attributeOptionCombo);
                boolean missingValue = dv == null || StringUtils.trimToNull(dv.getValue()) == null;
                boolean missingComment = dv == null || StringUtils.trimToNull(dv.getComment()) == null;
                if (missingValue && missingComment) {
                    violations.add(new DataElementOperand(de, co));
                }
            }
        }
    }
    return violations;
}
Also used : DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) DataElement(org.hisp.dhis.dataelement.DataElement) DataValue(org.hisp.dhis.datavalue.DataValue) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 24 with DataElementOperand

use of org.hisp.dhis.dataelement.DataElementOperand in project dhis2-core by dhis2.

the class DataSetCompletenessServiceTest method setUpTest.

// -------------------------------------------------------------------------
// Fixture
// -------------------------------------------------------------------------
@Override
public void setUpTest() {
    setExternalTestDir(locationManager);
    categoryOptionCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
    periodType = new MonthlyPeriodType();
    periodA = createPeriod(periodType, getDate(2000, 1, 1), getDate(2000, 1, 31));
    periodB = createPeriod(periodType, getDate(2000, 2, 1), getDate(2000, 2, 28));
    periodC = createPeriod(new QuarterlyPeriodType(), getDate(2000, 1, 1), getDate(2000, 3, 31));
    periodIdA = periodService.addPeriod(periodA);
    periodService.addPeriod(periodB);
    periodIdC = periodService.addPeriod(periodC);
    unitA = createOrganisationUnit('A');
    unitB = createOrganisationUnit('B');
    unitC = createOrganisationUnit('C');
    unitD = createOrganisationUnit('D');
    unitE = createOrganisationUnit('E');
    unitF = createOrganisationUnit('F');
    unitG = createOrganisationUnit('G');
    unitH = createOrganisationUnit('H');
    unitB.setParent(unitA);
    unitC.setParent(unitA);
    unitE.setParent(unitB);
    unitF.setParent(unitB);
    unitG.setParent(unitC);
    unitH.setParent(unitC);
    unitA.getChildren().add(unitB);
    unitA.getChildren().add(unitC);
    unitB.getChildren().add(unitE);
    unitB.getChildren().add(unitF);
    unitC.getChildren().add(unitG);
    unitC.getChildren().add(unitH);
    unitIdA = organisationUnitService.addOrganisationUnit(unitA);
    unitIdB = organisationUnitService.addOrganisationUnit(unitB);
    unitIdC = organisationUnitService.addOrganisationUnit(unitC);
    organisationUnitService.addOrganisationUnit(unitD);
    organisationUnitService.addOrganisationUnit(unitE);
    organisationUnitService.addOrganisationUnit(unitF);
    organisationUnitService.addOrganisationUnit(unitG);
    organisationUnitService.addOrganisationUnit(unitH);
    unitIdsA = new HashSet<>();
    unitIdsA.add(unitIdA);
    unitIdsA.add(unitIdB);
    unitIdsA.add(unitIdC);
    groupA = createOrganisationUnitGroup('A');
    groupB = createOrganisationUnitGroup('B');
    groupC = createOrganisationUnitGroup('C');
    groupA.addOrganisationUnit(unitA);
    groupB.addOrganisationUnit(unitA);
    groupB.addOrganisationUnit(unitB);
    groupC.addOrganisationUnit(unitE);
    groupC.addOrganisationUnit(unitF);
    organisationUnitGroupService.addOrganisationUnitGroup(groupA);
    organisationUnitGroupService.addOrganisationUnitGroup(groupB);
    organisationUnitGroupService.addOrganisationUnitGroup(groupC);
    dataSetA = createDataSet('A', periodType);
    dataSetB = createDataSet('B', periodType);
    dataSetC = createDataSet('C', periodType);
    dataElementA = createDataElement('A');
    dataElementB = createDataElement('B');
    dataElementService.addDataElement(dataElementA);
    dataElementService.addDataElement(dataElementB);
    dataSetA.getCompulsoryDataElementOperands().add(new DataElementOperand(dataElementA, categoryOptionCombo));
    dataSetA.getCompulsoryDataElementOperands().add(new DataElementOperand(dataElementB, categoryOptionCombo));
    dataSetB.getCompulsoryDataElementOperands().add(new DataElementOperand(dataElementA, categoryOptionCombo));
    onTimeA = getDate(2000, 2, 10);
    tooLateA = getDate(2000, 2, 25);
    onTimeB = getDate(2000, 3, 10);
    tooLateB = getDate(2000, 3, 25);
}
Also used : DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) QuarterlyPeriodType(org.hisp.dhis.period.QuarterlyPeriodType)

Example 25 with DataElementOperand

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

DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)28 DataElement (org.hisp.dhis.dataelement.DataElement)12 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)9 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)6 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)6 Period (org.hisp.dhis.period.Period)6 Test (org.junit.Test)6 DataSet (org.hisp.dhis.dataset.DataSet)5 List (java.util.List)4 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 DhisSpringTest (org.hisp.dhis.DhisSpringTest)3 DataElementCategoryCombo (org.hisp.dhis.dataelement.DataElementCategoryCombo)3 Section (org.hisp.dhis.dataset.Section)3 ProgramDataElementDimensionItem (org.hisp.dhis.program.ProgramDataElementDimensionItem)3 User (org.hisp.dhis.user.User)3 Date (java.util.Date)2 HashMap (java.util.HashMap)2