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