Search in sources :

Example 36 with DimensionalItemId

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

the class DataDimensionExtractor method getItemObjectMap.

/**
 * Gets a map from dimension item ids to their dimension item objects.
 *
 * @param itemIds a set of ids of the dimension item objects to get.
 * @param atomicObjects a map from each class of atomic objects to a map
 *        that associates each id of that class with an atomic object.
 * @return a map from the item ids to the dimension item objects.
 */
Map<DimensionalItemId, DimensionalItemObject> getItemObjectMap(final Set<DimensionalItemId> itemIds, final MapMap<Class<? extends IdentifiableObject>, String, IdentifiableObject> atomicObjects) {
    final Map<DimensionalItemId, DimensionalItemObject> itemObjectMap = new HashMap<>();
    for (final DimensionalItemId id : itemIds) {
        if (id.hasValidIds()) {
            final BaseDimensionalItemObject dimensionalItemObject = getBaseDimensionalItemObject(atomicObjects, id);
            if (dimensionalItemObject != null) {
                dimensionalItemObject.setQueryMods(id.getQueryMods());
                itemObjectMap.put(id, dimensionalItemObject);
            }
        }
    }
    return itemObjectMap;
}
Also used : DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseDimensionalItemObject(org.hisp.dhis.common.BaseDimensionalItemObject) HashMap(java.util.HashMap) BaseDimensionalItemObject(org.hisp.dhis.common.BaseDimensionalItemObject)

Example 37 with DimensionalItemId

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

the class CategoryOptionGroupTaglessResolver method resolve.

/**
 * Resolves a Data Element Operand expression containing one or two Category
 * Option Group UID to an equivalent expression where the associated
 * Category Option Combos for the given Category Option Group are "exploded"
 * into the expression.
 *
 * Resolves one of the expressions below:
 *
 * 1) #{DEUID.COGUID.AOCUID} 2) #{DEUID.COCUID.COGUID} 3)
 * #{DEUID.COG1UID.COG2UID}
 *
 * to:
 *
 * 1) #{DEUID.COCUID1.AOCUID} + #{DEUID.COCUID2.AOCUID} +
 * #{DEUID.COCUID3.AOCUID} where COCUID1,2,3... are resolved by fetching all
 * COCUID by COGUID
 *
 * 2) #{DEUID.COCUID} + #{DEUID.COCUID1} + #{DEUID.COCUID2} +
 * #{DEUID.COCUID3} + #{DEUID.COCUID4} where COCUID1,2,3... are resolved by
 * fetching all COCUID by COGUID
 *
 * 3) #{DEUID.COCUID1} + #{DEUID.COCUID2} + #{DEUID.COCUID3} +
 * #{DEUID.COCUID4} where COCUID1 and COCUID2 are resolved from COG1UID and
 * COCUID3 and COCUID4 are resolved from COGUID2
 *
 * DEUID = Data Element UID COCUID = Category Option Combo UID COGUID =
 * Category Option Group UID
 *
 * @param expression a Data Element Expression
 * @return an expression containing additional CategoryOptionCombos based on
 *         the given Category Option Group
 */
@Override
@Transactional(readOnly = true)
public String resolve(String expression) {
    // Get a DimensionalItemId from the expression. The expression is parsed
    // and
    // each element placed in the DimensionalItemId
    Set<DimensionalItemId> dimItemIds = expressionService.getExpressionDimensionalItemIds(expression, INDICATOR_EXPRESSION);
    List<String> resolvedOperands = new ArrayList<>();
    if (isDataElementOperand(dimItemIds)) {
        Optional<DimensionalItemId> dimItemId = dimItemIds.stream().findFirst();
        if (dimItemId.isPresent()) {
            DimensionalItemId dimensionalItemId = dimItemId.get();
            // First element is always the Data Element Id
            String dataElementUid = dimensionalItemId.getId0();
            resolvedOperands.addAll(evaluate(dataElementUid, dimensionalItemId.getId1(), dimensionalItemId.getId2()));
            resolvedOperands.addAll(evaluate(dataElementUid, dimensionalItemId.getId2(), null));
        }
    }
    if (resolvedOperands.isEmpty()) {
        // nothing to resolve, add the expression as it is
        resolvedOperands.add(expression);
    }
    return Joiner.on("+").join(resolvedOperands);
}
Also used : DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) Transactional(org.springframework.transaction.annotation.Transactional)

Example 38 with DimensionalItemId

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

the class DataElementGroupResolver 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.getId0() != null && id.getId0().startsWith(DATA_ELEMENT_GROUP_PREFIX)) {
            DataElementGroup deGroup = dataElementGroupStore.getByUid(id.getId0().replace(DATA_ELEMENT_GROUP_PREFIX, EMPTY_STRING));
            if (deGroup != null) {
                List<String> resolved = deGroup.getMembers().stream().map(de -> id.getItem().replace(id.getId0(), de.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) DataElementGroupStore(org.hisp.dhis.dataelement.DataElementGroupStore) 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) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) Collectors(java.util.stream.Collectors) Joiner(com.google.common.base.Joiner) DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup)

Example 39 with DimensionalItemId

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

the class CategoryOptionGroupResolver 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_GROUP_PREFIX)) {
            List<String> cogUidList = Arrays.stream(id.getId1().replace(CATEGORY_OPTION_GROUP_PREFIX, EMPTY_STRING).split(LOGICAL_AND)).collect(Collectors.toList());
            expression = getExpression(expression, id, cogUidList, id.getId0());
        }
    }
    return expression;
}
Also used : DimensionalItemId(org.hisp.dhis.common.DimensionalItemId)

Example 40 with DimensionalItemId

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

the class CategoryOptionResolverTest method verifyExpressionIsNotResolvedWhenExpressionIsNotValid.

@Test
void verifyExpressionIsNotResolvedWhenExpressionIsNotValid() {
    // arrange
    dimensionalItemId = new DimensionalItemId(DimensionItemType.DATA_ELEMENT_OPERAND, uid1, uid2, uid3, createIndicatorExpression());
    String expression = "lsdjflakjdflkajdslfhaglakujdhfg";
    when(expressionService.getExpressionDimensionalItemIds(expression, INDICATOR_EXPRESSION)).thenReturn(Sets.newHashSet(dimensionalItemId));
    // act
    String resolvedExpression = resolver.resolve(expression);
    // assert
    assertEquals(expression, resolvedExpression);
}
Also used : DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

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