Search in sources :

Example 11 with DimensionalItemObject

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

the class ReportTable method getColumnName.

/**
     * Generates a column name based on short-names of the argument objects.
     * Null arguments are ignored in the name.
     * <p/>
     * The period column name must be static when on columns so it can be
     * re-used in reports, hence the name property is used which will be formatted
     * only when the period dimension is on rows.
     */
public static String getColumnName(List<DimensionalItemObject> objects) {
    StringBuffer buffer = new StringBuffer();
    for (DimensionalItemObject object : objects) {
        if (object != null && object instanceof Period) {
            buffer.append(object.getName()).append(SEPARATOR);
        } else {
            buffer.append(object != null ? (object.getShortName() + SEPARATOR) : EMPTY);
        }
    }
    String column = columnEncode(buffer.toString());
    return column.length() > 0 ? column.substring(0, column.lastIndexOf(SEPARATOR)) : TOTAL_COLUMN_NAME;
}
Also used : DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) Period(org.hisp.dhis.period.Period)

Example 12 with DimensionalItemObject

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

the class DefaultDimensionService method getDataDimensionalItemObject.

@Override
public DimensionalItemObject getDataDimensionalItemObject(IdScheme idScheme, String dimensionItem) {
    if (DimensionalObjectUtils.isCompositeDimensionalObject(dimensionItem)) {
        String id0 = splitSafe(dimensionItem, COMPOSITE_DIM_OBJECT_ESCAPED_SEP, 0);
        String id1 = splitSafe(dimensionItem, COMPOSITE_DIM_OBJECT_ESCAPED_SEP, 1);
        String id2 = splitSafe(dimensionItem, COMPOSITE_DIM_OBJECT_ESCAPED_SEP, 2);
        DataElementOperand operand = null;
        ReportingRate reportingRate = null;
        ProgramDataElementDimensionItem programDataElement = null;
        ProgramTrackedEntityAttributeDimensionItem programAttribute = null;
        if ((operand = getDataElementOperand(idScheme, id0, id1, id2)) != null) {
            return operand;
        } else if ((reportingRate = getReportingRate(idScheme, id0, id1)) != null) {
            return reportingRate;
        } else if ((programDataElement = getProgramDataElementDimensionItem(idScheme, id0, id1)) != null) {
            return programDataElement;
        } else if ((programAttribute = getProgramAttributeDimensionItem(idScheme, id0, id1)) != null) {
            return programAttribute;
        }
    } else if (!idScheme.is(IdentifiableProperty.UID) || CodeGenerator.isValidUid(dimensionItem)) {
        DimensionalItemObject itemObject = idObjectManager.get(DataDimensionItem.DATA_DIMENSION_CLASSES, idScheme, dimensionItem);
        if (itemObject != null) {
            return itemObject;
        }
    }
    return null;
}
Also used : DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) ProgramDataElementDimensionItem(org.hisp.dhis.program.ProgramDataElementDimensionItem) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ReportingRate(org.hisp.dhis.common.ReportingRate) ProgramTrackedEntityAttributeDimensionItem(org.hisp.dhis.program.ProgramTrackedEntityAttributeDimensionItem)

Example 13 with DimensionalItemObject

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

the class DefaultDimensionService method getCanReadDimensionItems.

//--------------------------------------------------------------------------
// DimensionService implementation
//--------------------------------------------------------------------------
public List<DimensionalItemObject> getCanReadDimensionItems(String uid) {
    DimensionalObject dimension = idObjectManager.get(DimensionalObject.DYNAMIC_DIMENSION_CLASSES, uid);
    List<DimensionalItemObject> items = new ArrayList<>();
    if (dimension != null && dimension.hasItems()) {
        User user = currentUserService.getCurrentUser();
        items.addAll(getCanReadObjects(user, dimension.getItems()));
    }
    return items;
}
Also used : User(org.hisp.dhis.user.User) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) UniqueArrayList(org.hisp.dhis.commons.collection.UniqueArrayList) ArrayList(java.util.ArrayList) DimensionalObject(org.hisp.dhis.common.DimensionalObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject)

Example 14 with DimensionalItemObject

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

the class DefaultExpressionService method getDimensionalItemIdsInExpression.

@Override
public SetMap<Class<? extends DimensionalItemObject>, String> getDimensionalItemIdsInExpression(String expression) {
    SetMap<Class<? extends DimensionalItemObject>, String> dimensionItemIdentifiers = new SetMap<>();
    if (expression == null || expression.isEmpty()) {
        return dimensionItemIdentifiers;
    }
    Matcher matcher = VARIABLE_PATTERN.matcher(expression);
    while (matcher.find()) {
        dimensionItemIdentifiers.putValue(VARIABLE_TYPES.get(matcher.group(1)), matcher.group(2));
    }
    return dimensionItemIdentifiers;
}
Also used : DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) Matcher(java.util.regex.Matcher) SetMap(org.hisp.dhis.common.SetMap)

Example 15 with DimensionalItemObject

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

the class DefaultExpressionService method generateExpression.

/**
     * Generates an expression based on the given data maps.
     * 
     * @param expression the expression.
     * @param valueMap the value map.
     * @param constantMap the constant map.
     * @param orgUnitCountMap the organisation unit count map.
     * @param days the number of days.
     * @param missingValueStrategy the missing value strategy.
     * @param aggregateMap the aggregate map.
     * @return an expression.
     */
private String generateExpression(String expression, Map<? extends DimensionalItemObject, Double> valueMap, Map<String, Double> constantMap, Map<String, Integer> orgUnitCountMap, Integer days, MissingValueStrategy missingValueStrategy, Map<String, List<Double>> aggregateMap) {
    if (expression == null || expression.isEmpty()) {
        return null;
    }
    expression = ExpressionUtils.normalizeExpression(expression);
    Map<String, Double> dimensionItemValueMap = valueMap.entrySet().stream().filter(e -> e.getValue() != null).collect(Collectors.toMap(e -> e.getKey().getDimensionItem(), e -> e.getValue()));
    missingValueStrategy = ObjectUtils.firstNonNull(missingValueStrategy, NEVER_SKIP);
    // ---------------------------------------------------------------------
    // Aggregates
    // ---------------------------------------------------------------------
    StringBuffer sb = new StringBuffer();
    Pattern prefix = CustomFunctions.AGGREGATE_PATTERN_PREFIX;
    Matcher matcher = prefix.matcher(expression);
    int scan = 0, len = expression.length(), tail = 0;
    while (scan < len && matcher.find(scan)) {
        int start = matcher.end();
        int end = Expression.matchExpression(expression, start);
        if (end < 0) {
            sb.append(expression.substring(scan, start));
            scan = start + 1;
            tail = start;
        } else if (aggregateMap == null || expression.charAt(start) == '<') {
            sb.append(expression.substring(scan, end));
            scan = end + 1;
            tail = end;
        } else {
            String subExpression = expression.substring(start, end);
            List<Double> samples = aggregateMap.get(subExpression);
            if (samples == null) {
                if (SKIP_IF_ANY_VALUE_MISSING.equals(missingValueStrategy)) {
                    return null;
                }
            } else {
                String literal = (samples == null) ? ("[]") : (samples.toString());
                sb.append(expression.substring(scan, start));
                sb.append(literal);
            }
            scan = end;
            tail = end;
        }
    }
    sb.append(expression.substring(tail));
    expression = sb.toString();
    // ---------------------------------------------------------------------
    // DimensionalItemObjects
    // ---------------------------------------------------------------------
    sb = new StringBuffer();
    matcher = VARIABLE_PATTERN.matcher(expression);
    int matchCount = 0;
    int valueCount = 0;
    while (matcher.find()) {
        matchCount++;
        String dimItem = matcher.group(GROUP_ID);
        final Double value = dimensionItemValueMap.get(dimItem);
        boolean missingValue = value == null;
        if (missingValue && SKIP_IF_ANY_VALUE_MISSING.equals(missingValueStrategy)) {
            return null;
        }
        if (!missingValue) {
            valueCount++;
        }
        String replacement = value != null ? String.valueOf(value) : NULL_REPLACEMENT;
        matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));
    }
    if (SKIP_IF_ALL_VALUES_MISSING.equals(missingValueStrategy) && matchCount > 0 && valueCount == 0) {
        return null;
    }
    expression = TextUtils.appendTail(matcher, sb);
    // ---------------------------------------------------------------------
    // Constants
    // ---------------------------------------------------------------------
    sb = new StringBuffer();
    matcher = CONSTANT_PATTERN.matcher(expression);
    while (matcher.find()) {
        final Double constant = constantMap != null ? constantMap.get(matcher.group(GROUP_ID)) : null;
        String replacement = constant != null ? String.valueOf(constant) : NULL_REPLACEMENT;
        matcher.appendReplacement(sb, replacement);
    }
    expression = TextUtils.appendTail(matcher, sb);
    // ---------------------------------------------------------------------
    // Org unit groups
    // ---------------------------------------------------------------------
    sb = new StringBuffer();
    matcher = OU_GROUP_PATTERN.matcher(expression);
    while (matcher.find()) {
        final Integer count = orgUnitCountMap != null ? orgUnitCountMap.get(matcher.group(GROUP_ID)) : null;
        String replacement = count != null ? String.valueOf(count) : NULL_REPLACEMENT;
        matcher.appendReplacement(sb, replacement);
    }
    expression = TextUtils.appendTail(matcher, sb);
    // ---------------------------------------------------------------------
    // Days
    // ---------------------------------------------------------------------
    sb = new StringBuffer();
    matcher = DAYS_PATTERN.matcher(expression);
    while (matcher.find()) {
        String replacement = days != null ? String.valueOf(days) : NULL_REPLACEMENT;
        matcher.appendReplacement(sb, replacement);
    }
    return TextUtils.appendTail(matcher, sb);
}
Also used : ListMap(org.hisp.dhis.common.ListMap) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) DataElementService(org.hisp.dhis.dataelement.DataElementService) DimensionService(org.hisp.dhis.common.DimensionService) CustomFunctions(org.hisp.dhis.system.jep.CustomFunctions) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) DataElement(org.hisp.dhis.dataelement.DataElement) HashSet(java.util.HashSet) GenericStore(org.hisp.dhis.common.GenericStore) Matcher(java.util.regex.Matcher) ExpressionUtils(org.hisp.dhis.system.util.ExpressionUtils) MissingValueStrategy(org.hisp.dhis.expression.MissingValueStrategy) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) ObjectUtils(org.apache.commons.lang3.ObjectUtils) Map(java.util.Map) IndicatorValue(org.hisp.dhis.indicator.IndicatorValue) Indicator(org.hisp.dhis.indicator.Indicator) Constant(org.hisp.dhis.constant.Constant) DataElementCategoryService(org.hisp.dhis.dataelement.DataElementCategoryService) Period(org.hisp.dhis.period.Period) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) OrganisationUnitGroupService(org.hisp.dhis.organisationunit.OrganisationUnitGroupService) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) Collection(java.util.Collection) Set(java.util.Set) ConstantService(org.hisp.dhis.constant.ConstantService) DateUtils(org.hisp.dhis.system.util.DateUtils) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) SetMap(org.hisp.dhis.common.SetMap) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) List(java.util.List) CachingMap(org.hisp.dhis.commons.collection.CachingMap) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) Pattern(java.util.regex.Pattern) MathUtils(org.hisp.dhis.system.util.MathUtils) TextUtils(org.hisp.dhis.commons.util.TextUtils) Transactional(org.springframework.transaction.annotation.Transactional) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) List(java.util.List)

Aggregations

DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)55 ArrayList (java.util.ArrayList)15 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)15 DimensionalObject (org.hisp.dhis.common.DimensionalObject)15 BaseDimensionalObject (org.hisp.dhis.common.BaseDimensionalObject)14 Test (org.junit.Test)12 Period (org.hisp.dhis.period.Period)11 ListMap (org.hisp.dhis.common.ListMap)10 DataElement (org.hisp.dhis.dataelement.DataElement)10 DhisSpringTest (org.hisp.dhis.DhisSpringTest)7 BaseDimensionalItemObject (org.hisp.dhis.common.BaseDimensionalItemObject)7 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)7 HashMap (java.util.HashMap)6 List (java.util.List)6 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)6 PeriodType (org.hisp.dhis.period.PeriodType)6 SqlRowSet (org.springframework.jdbc.support.rowset.SqlRowSet)5 Matcher (java.util.regex.Matcher)4 Grid (org.hisp.dhis.common.Grid)4 HashSet (java.util.HashSet)3