Search in sources :

Example 1 with DimensionalItemId

use of org.hisp.dhis.common.DimensionalItemId in project dhis2-core by dhis2.

the class DataDimensionExtractor method getAtomicIds.

/**
 * Breaks down a set of dimensional item ids into the atomic object ids
 * stored in the database. Returns a map from each class of atomic objects
 * to the set of ids for that object class.
 *
 * @param itemIds a set of dimension item object ids.
 * @return map from atomic object classes to sets of atomic ids.
 */
SetMap<Class<? extends IdentifiableObject>, String> getAtomicIds(final Set<DimensionalItemId> itemIds) {
    final SetMap<Class<? extends IdentifiableObject>, String> atomicIds = new SetMap<>();
    for (final DimensionalItemId id : itemIds) {
        if (!id.hasValidIds()) {
            continue;
        }
        switch(id.getDimensionItemType()) {
            case DATA_ELEMENT:
                atomicIds.putValue(DataElement.class, id.getId0());
                break;
            case DATA_ELEMENT_OPERAND:
                atomicIds.putValue(DataElement.class, id.getId0());
                if (id.getId1() != null) {
                    atomicIds.putValue(CategoryOptionCombo.class, id.getId1());
                }
                if (id.getId2() != null) {
                    atomicIds.putValue(CategoryOptionCombo.class, id.getId2());
                }
                break;
            case INDICATOR:
                atomicIds.putValue(Indicator.class, id.getId0());
                break;
            case REPORTING_RATE:
                atomicIds.putValue(DataSet.class, id.getId0());
                break;
            case PROGRAM_DATA_ELEMENT:
                atomicIds.putValue(Program.class, id.getId0());
                atomicIds.putValue(DataElement.class, id.getId1());
                break;
            case PROGRAM_ATTRIBUTE:
                atomicIds.putValue(Program.class, id.getId0());
                atomicIds.putValue(TrackedEntityAttribute.class, id.getId1());
                break;
            case PROGRAM_INDICATOR:
                atomicIds.putValue(ProgramIndicator.class, id.getId0());
                break;
            default:
                log.warn("Unrecognized DimensionItemType " + id.getDimensionItemType().name() + " in getAtomicIds");
                break;
        }
    }
    return atomicIds;
}
Also used : DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) SetMap(org.hisp.dhis.common.SetMap) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 2 with DimensionalItemId

use of org.hisp.dhis.common.DimensionalItemId in project dhis2-core by dhis2.

the class CategoryOptionResolver method resolve.

@Override
public String resolve(String expression) {
    Set<DimensionalItemId> dimItemIds = expressionService.getExpressionDimensionalItemIds(expression, INDICATOR_EXPRESSION);
    for (DimensionalItemId id : dimItemIds) {
        if (id.getItem() != null && id.getId1() != null && id.getId1().startsWith(CATEGORY_OPTION_PREFIX)) {
            CategoryOption co = categoryOptionStore.getByUid(id.getId1().replace(CATEGORY_OPTION_PREFIX, EMPTY_STRING));
            if (co != null) {
                List<String> resolved = co.getCategoryOptionCombos().stream().map(coc -> id.getItem().replace(id.getId1(), coc.getUid())).collect(Collectors.toList());
                expression = expression.replace(id.getItem(), LEFT_BRACKET + Joiner.on("+").join(resolved) + RIGHT_BRACKET);
            }
        }
    }
    return expression;
}
Also used : List(java.util.List) ExpressionService(org.hisp.dhis.expression.ExpressionService) CategoryOption(org.hisp.dhis.category.CategoryOption) DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) Service(org.springframework.stereotype.Service) Set(java.util.Set) INDICATOR_EXPRESSION(org.hisp.dhis.expression.ParseType.INDICATOR_EXPRESSION) AllArgsConstructor(lombok.AllArgsConstructor) Collectors(java.util.stream.Collectors) CategoryOptionStore(org.hisp.dhis.category.CategoryOptionStore) Joiner(com.google.common.base.Joiner) DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) CategoryOption(org.hisp.dhis.category.CategoryOption)

Example 3 with DimensionalItemId

use of org.hisp.dhis.common.DimensionalItemId in project dhis2-core by dhis2.

the class DimensionalItem method getDescription.

@Override
public final Object getDescription(ExprContext ctx, CommonExpressionVisitor visitor) {
    DimensionalItemId itemId = getDimensionalItemId(ctx, visitor);
    DimensionalItemObject item = visitor.getDimensionService().getDataDimensionalItemObject(itemId);
    if (item == null) {
        throw new ParserExceptionWithoutContext("Can't find " + itemId.getDimensionItemType().name() + " for '" + itemId + "'");
    }
    visitor.getItemDescriptions().put(ctx.getText(), item.getDisplayName());
    return ValidationUtils.getNullReplacementValue(getItemValueType(item));
}
Also used : ParserExceptionWithoutContext(org.hisp.dhis.antlr.ParserExceptionWithoutContext) DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ValueTypedDimensionalItemObject(org.hisp.dhis.common.ValueTypedDimensionalItemObject)

Example 4 with DimensionalItemId

use of org.hisp.dhis.common.DimensionalItemId in project dhis2-core by dhis2.

the class DimensionalItem method evaluate.

@Override
public final Object evaluate(ExprContext ctx, CommonExpressionVisitor visitor) {
    DimensionalItemId itemId = getDimensionalItemId(ctx, visitor);
    DimensionalItemObject item = visitor.getParams().getItemMap().get(itemId);
    Object value = (item != null) ? visitor.getParams().getValueMap().get(item) : null;
    return visitor.getState().handleNulls(value, getItemValueType(item));
}
Also used : DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ValueTypedDimensionalItemObject(org.hisp.dhis.common.ValueTypedDimensionalItemObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ValueTypedDimensionalItemObject(org.hisp.dhis.common.ValueTypedDimensionalItemObject)

Example 5 with DimensionalItemId

use of org.hisp.dhis.common.DimensionalItemId in project dhis2-core by dhis2.

the class DimensionServiceTest method testGetDataDimensionalItemObjectMapReturnsItemsWithAllOffsets.

@Test
void testGetDataDimensionalItemObjectMapReturnsItemsWithAllOffsets() {
    Map<DimensionalItemId, DimensionalItemObject> result;
    DimensionalItemId deAId = new DimensionalItemId(DATA_ELEMENT, deA.getUid());
    DimensionalItemId deBId = new DimensionalItemId(DATA_ELEMENT, deB.getUid());
    DimensionalItemId deCId = new DimensionalItemId(DATA_ELEMENT, deC.getUid());
    DimensionalItemId deAOffset1Id = new DimensionalItemId(DATA_ELEMENT, deA.getUid(), queryModsA);
    DimensionalItemId deBOffset1Id = new DimensionalItemId(DATA_ELEMENT, deB.getUid(), queryModsA);
    DimensionalItemId deCOffset1Id = new DimensionalItemId(DATA_ELEMENT, deC.getUid(), queryModsA);
    DimensionalItemId deAOffset2Id = new DimensionalItemId(DATA_ELEMENT, deA.getUid(), queryModsB);
    DimensionalItemId deBOffset2Id = new DimensionalItemId(DATA_ELEMENT, deB.getUid(), queryModsB);
    DimensionalItemId deCOffset2Id = new DimensionalItemId(DATA_ELEMENT, deC.getUid(), queryModsB);
    DataElement deAOffset1 = makeDataElementWithQueryModsFrom(deA, queryModsA);
    DataElement deBOffset1 = makeDataElementWithQueryModsFrom(deB, queryModsA);
    DataElement deCOffset1 = makeDataElementWithQueryModsFrom(deC, queryModsA);
    DataElement deAOffset2 = makeDataElementWithQueryModsFrom(deA, queryModsB);
    DataElement deBOffset2 = makeDataElementWithQueryModsFrom(deB, queryModsB);
    DataElement deCOffset2 = makeDataElementWithQueryModsFrom(deC, queryModsB);
    Set<DimensionalItemId> dimensionalItemIds = Sets.newHashSet(deAId, deBId, deCId, deAOffset1Id, deBOffset1Id, deCOffset1Id, deAOffset2Id, deBOffset2Id, deCOffset2Id);
    ImmutableMap<DimensionalItemId, DimensionalItemObject> dimensionalItemMap = ImmutableMap.<DimensionalItemId, DimensionalItemObject>builder().put(deAId, deA).put(deBId, deB).put(deCId, deC).put(deAOffset1Id, deAOffset1).put(deBOffset1Id, deBOffset1).put(deCOffset1Id, deCOffset1).put(deAOffset2Id, deAOffset2).put(deBOffset2Id, deBOffset2).put(deCOffset2Id, deCOffset2).build();
    // When
    result = dimensionService.getDataDimensionalItemObjectMap(dimensionalItemIds);
    // Then
    assertMapEquals(dimensionalItemMap, result);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseDimensionalItemObject(org.hisp.dhis.common.BaseDimensionalItemObject) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

DimensionalItemId (org.hisp.dhis.common.DimensionalItemId)46 Test (org.junit.jupiter.api.Test)32 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)21 DhisSpringTest (org.hisp.dhis.DhisSpringTest)13 Indicator (org.hisp.dhis.indicator.Indicator)12 ProgramIndicator (org.hisp.dhis.program.ProgramIndicator)12 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 HashMap (java.util.HashMap)11 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)9 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)9 IndicatorValue (org.hisp.dhis.indicator.IndicatorValue)8 IndicatorType (org.hisp.dhis.indicator.IndicatorType)6 DataElement (org.hisp.dhis.dataelement.DataElement)5 List (java.util.List)4 BaseDimensionalItemObject (org.hisp.dhis.common.BaseDimensionalItemObject)4 Period (org.hisp.dhis.period.Period)4 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 AllArgsConstructor (lombok.AllArgsConstructor)3