Search in sources :

Example 36 with DataElementCategoryOptionCombo

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

the class MarkForFollowupAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() {
    DataElement dataElement = dataElementService.getDataElement(dataElementId);
    Period period = periodService.getPeriod(periodId);
    OrganisationUnit source = organisationUnitService.getOrganisationUnit(sourceId);
    DataElementCategoryOptionCombo categoryOptionCombo = categoryService.getDataElementCategoryOptionCombo(categoryOptionComboId);
    DataValue dataValue = dataValueService.getDataValue(dataElement, period, source, categoryOptionCombo);
    if (dataValue != null) {
        boolean isMarked = dataValue.isFollowup();
        dataValue.setFollowup(!isMarked);
        dataValueService.updateDataValue(dataValue);
        message = !isMarked ? "marked" : "unmarked";
        log.info(!isMarked ? "Data value marked for follow-up" : "Data value unmarked for follow-up");
    }
    return SUCCESS;
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataValue(org.hisp.dhis.datavalue.DataValue) Period(org.hisp.dhis.period.Period) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 37 with DataElementCategoryOptionCombo

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

the class ExpressionUpgrader method upgradeExpression.

private String upgradeExpression(String expression) {
    if (expression == null || expression.trim().isEmpty()) {
        return null;
    }
    boolean changes = false;
    StringBuffer sb = new StringBuffer();
    try {
        // -----------------------------------------------------------------
        // Constants
        // -----------------------------------------------------------------
        Matcher matcher = OLD_CONSTANT_PATTERN.matcher(expression);
        while (matcher.find()) {
            Constant constant = constantService.getConstant(Integer.parseInt(matcher.group(1)));
            String replacement = "C{" + constant.getUid() + "}";
            matcher.appendReplacement(sb, replacement);
            changes = true;
        }
        matcher.appendTail(sb);
        expression = sb.toString();
        // -----------------------------------------------------------------
        // Operands
        // -----------------------------------------------------------------
        matcher = OLD_OPERAND_PATTERN.matcher(expression);
        sb = new StringBuffer();
        while (matcher.find()) {
            DataElement de = dataElementService.getDataElement(Integer.parseInt(matcher.group(1)));
            String replacement = "#{" + de.getUid();
            if (matcher.groupCount() == 2 && matcher.group(2) != null && !matcher.group(2).trim().isEmpty()) {
                DataElementCategoryOptionCombo coc = categoryService.getDataElementCategoryOptionCombo(Integer.parseInt(matcher.group(2)));
                replacement += "." + coc.getUid();
            }
            replacement += "}";
            matcher.appendReplacement(sb, replacement);
            changes = true;
        }
        matcher.appendTail(sb);
        expression = sb.toString();
    } catch (Exception ex) {
        log.error("Failed to upgrade expression: " + expression);
        // Log and continue
        log.error(ex);
    }
    if (changes) {
        log.info("Upgraded expression: " + expression);
    }
    return changes ? expression : null;
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Matcher(java.util.regex.Matcher) Constant(org.hisp.dhis.constant.Constant) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 38 with DataElementCategoryOptionCombo

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

the class ExpressionUpgrader method upgradeDataEntryForms.

private void upgradeDataEntryForms() {
    Collection<DataEntryForm> forms = dataEntryFormService.getAllDataEntryForms();
    for (DataEntryForm form : forms) {
        if (DataEntryForm.CURRENT_FORMAT > form.getFormat() && form.getHtmlCode() != null && !form.getHtmlCode().trim().isEmpty()) {
            try {
                // ---------------------------------------------------------
                // Identifiers
                // ---------------------------------------------------------
                Matcher matcher = IDENTIFIER_PATTERN.matcher(form.getHtmlCode());
                StringBuffer sb = new StringBuffer();
                while (matcher.find()) {
                    DataElement de = dataElementService.getDataElement(Integer.parseInt(matcher.group(1)));
                    DataElementCategoryOptionCombo coc = categoryService.getDataElementCategoryOptionCombo(Integer.parseInt(matcher.group(2)));
                    String replacement = "id=\"" + de.getUid() + "-" + coc.getUid() + "-val\"";
                    matcher.appendReplacement(sb, replacement);
                }
                matcher.appendTail(sb);
                form.setHtmlCode(sb.toString());
                // ---------------------------------------------------------
                // Data element totals
                // ---------------------------------------------------------
                matcher = DATAELEMENT_TOTAL_PATTERN.matcher(form.getHtmlCode());
                sb = new StringBuffer();
                while (matcher.find()) {
                    DataElement de = dataElementService.getDataElement(Integer.parseInt(matcher.group(1)));
                    String replacement = "dataelementid=\"" + de.getUid() + "\"";
                    matcher.appendReplacement(sb, replacement);
                }
                matcher.appendTail(sb);
                form.setHtmlCode(sb.toString());
                // ---------------------------------------------------------
                // Indicators
                // ---------------------------------------------------------
                matcher = INDICATOR_PATTERN.matcher(form.getHtmlCode());
                sb = new StringBuffer();
                while (matcher.find()) {
                    Indicator in = indicatorService.getIndicator(Integer.parseInt(matcher.group(1)));
                    String replacement = "indicatorid=\"" + in.getUid() + "\"";
                    matcher.appendReplacement(sb, replacement);
                }
                matcher.appendTail(sb);
                form.setHtmlCode(sb.toString());
                // ---------------------------------------------------------
                // Update format and save
                // ---------------------------------------------------------
                form.setFormat(DataEntryForm.CURRENT_FORMAT);
                dataEntryFormService.updateDataEntryForm(form);
                log.info("Upgraded custom data entry form: " + form.getName());
            } catch (Exception ex) {
                log.error("Upgrading custom data entry form failed: " + form.getName());
                // Log and continue
                log.error(ex);
            }
        }
    }
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Matcher(java.util.regex.Matcher) DataEntryForm(org.hisp.dhis.dataentryform.DataEntryForm) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) Indicator(org.hisp.dhis.indicator.Indicator)

Example 39 with DataElementCategoryOptionCombo

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

the class DefaultDataQueryService method getDimension.

// TODO optimize so that org unit levels + boundary are used in query instead of fetching all org units one by one
@Override
public DimensionalObject getDimension(String dimension, List<String> items, Date relativePeriodDate, List<OrganisationUnit> userOrgUnits, I18nFormat format, boolean allowNull, boolean allowAllPeriodItems, IdScheme inputIdScheme) {
    final boolean allItems = items.isEmpty();
    if (DATA_X_DIM_ID.equals(dimension)) {
        List<DimensionalItemObject> dataDimensionItems = new ArrayList<>();
        for (String uid : items) {
            if (uid.startsWith(KEY_DE_GROUP)) {
                String groupUid = DimensionalObjectUtils.getUidFromGroupParam(uid);
                DataElementGroup group = idObjectManager.getObject(DataElementGroup.class, inputIdScheme, groupUid);
                if (group != null) {
                    dataDimensionItems.addAll(group.getMembers());
                }
            } else if (uid.startsWith(KEY_IN_GROUP)) {
                String groupUid = DimensionalObjectUtils.getUidFromGroupParam(uid);
                IndicatorGroup group = idObjectManager.getObject(IndicatorGroup.class, inputIdScheme, groupUid);
                if (group != null) {
                    dataDimensionItems.addAll(group.getMembers());
                }
            } else {
                DimensionalItemObject dimItemObject = dimensionService.getDataDimensionalItemObject(inputIdScheme, uid);
                if (dimItemObject != null) {
                    dataDimensionItems.add(dimItemObject);
                }
            }
        }
        if (dataDimensionItems.isEmpty()) {
            throw new IllegalQueryException("Dimension dx is present in query without any valid dimension options");
        }
        DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.DATA_X, null, DISPLAY_NAME_DATA_X, dataDimensionItems);
        return object;
    } else if (CATEGORYOPTIONCOMBO_DIM_ID.equals(dimension)) {
        List<DimensionalItemObject> cocs = new ArrayList<>();
        for (String uid : items) {
            DataElementCategoryOptionCombo coc = idObjectManager.getObject(DataElementCategoryOptionCombo.class, inputIdScheme, uid);
            if (coc != null) {
                cocs.add(coc);
            }
        }
        DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.CATEGORY_OPTION_COMBO, null, DISPLAY_NAME_CATEGORYOPTIONCOMBO, cocs);
        return object;
    } else if (ATTRIBUTEOPTIONCOMBO_DIM_ID.equals(dimension)) {
        List<DimensionalItemObject> aocs = new ArrayList<>();
        for (String uid : items) {
            DataElementCategoryOptionCombo aoc = idObjectManager.getObject(DataElementCategoryOptionCombo.class, inputIdScheme, uid);
            if (aoc != null) {
                aocs.add(aoc);
            }
        }
        DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.ATTRIBUTE_OPTION_COMBO, null, DISPLAY_NAME_ATTRIBUTEOPTIONCOMBO, aocs);
        return object;
    } else if (PERIOD_DIM_ID.equals(dimension)) {
        Calendar calendar = PeriodType.getCalendar();
        List<Period> periods = new ArrayList<>();
        for (String isoPeriod : items) {
            if (RelativePeriodEnum.contains(isoPeriod)) {
                RelativePeriodEnum relativePeriod = RelativePeriodEnum.valueOf(isoPeriod);
                List<Period> relativePeriods = RelativePeriods.getRelativePeriodsFromEnum(relativePeriod, relativePeriodDate, format, true);
                periods.addAll(relativePeriods);
            } else {
                Period period = PeriodType.getPeriodFromIsoString(isoPeriod);
                if (period != null) {
                    periods.add(period);
                }
            }
        }
        // Remove duplicates
        periods = periods.stream().distinct().collect(Collectors.toList());
        if (periods.isEmpty() && !allowAllPeriodItems) {
            throw new IllegalQueryException("Dimension pe is present in query without any valid dimension options");
        }
        for (Period period : periods) {
            String name = format != null ? format.formatPeriod(period) : null;
            period.setName(name);
            period.setShortName(name);
            if (!calendar.isIso8601()) {
                period.setUid(getLocalPeriodIdentifier(period, calendar));
            }
        }
        DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.PERIOD, null, DISPLAY_NAME_PERIOD, asList(periods));
        return object;
    } else if (ORGUNIT_DIM_ID.equals(dimension)) {
        List<DimensionalItemObject> ous = new ArrayList<>();
        List<Integer> levels = new ArrayList<>();
        List<OrganisationUnitGroup> groups = new ArrayList<>();
        for (String ou : items) {
            if (KEY_USER_ORGUNIT.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
                ous.addAll(userOrgUnits);
            } else if (KEY_USER_ORGUNIT_CHILDREN.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
                ous.addAll(OrganisationUnit.getSortedChildren(userOrgUnits));
            } else if (KEY_USER_ORGUNIT_GRANDCHILDREN.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
                ous.addAll(OrganisationUnit.getSortedGrandChildren(userOrgUnits));
            } else if (ou != null && ou.startsWith(KEY_LEVEL)) {
                int level = DimensionalObjectUtils.getLevelFromLevelParam(ou);
                if (level > 0) {
                    levels.add(level);
                }
            } else if (ou != null && ou.startsWith(KEY_ORGUNIT_GROUP)) {
                String uid = DimensionalObjectUtils.getUidFromGroupParam(ou);
                OrganisationUnitGroup group = idObjectManager.getObject(OrganisationUnitGroup.class, inputIdScheme, uid);
                if (group != null) {
                    groups.add(group);
                }
            } else if (!inputIdScheme.is(IdentifiableProperty.UID) || CodeGenerator.isValidUid(ou)) {
                OrganisationUnit unit = idObjectManager.getObject(OrganisationUnit.class, inputIdScheme, ou);
                if (unit != null) {
                    ous.add(unit);
                }
            }
        }
        // Remove duplicates
        ous = ous.stream().distinct().collect(Collectors.toList());
        List<DimensionalItemObject> orgUnits = new ArrayList<>();
        List<OrganisationUnit> ousList = asTypedList(ous);
        if (!levels.isEmpty()) {
            orgUnits.addAll(sort(organisationUnitService.getOrganisationUnitsAtLevels(levels, ousList)));
        }
        if (!groups.isEmpty()) {
            orgUnits.addAll(sort(organisationUnitService.getOrganisationUnits(groups, ousList)));
        }
        if (levels.isEmpty() && groups.isEmpty()) {
            orgUnits.addAll(ous);
        }
        if (orgUnits.isEmpty()) {
            throw new IllegalQueryException("Dimension ou is present in query without any valid dimension options");
        }
        // Remove duplicates
        orgUnits = orgUnits.stream().distinct().collect(Collectors.toList());
        DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.ORGANISATION_UNIT, null, DISPLAY_NAME_ORGUNIT, orgUnits);
        return object;
    } else if (LONGITUDE_DIM_ID.contains(dimension)) {
        DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.STATIC, null, DISPLAY_NAME_LONGITUDE, new ArrayList<>());
        return object;
    } else if (LATITUDE_DIM_ID.contains(dimension)) {
        DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.STATIC, null, DISPLAY_NAME_LATITUDE, new ArrayList<>());
        return object;
    } else {
        DimensionalObject dimObject = idObjectManager.get(DataQueryParams.DYNAMIC_DIM_CLASSES, inputIdScheme, dimension);
        if (dimObject != null && dimObject.isDataDimension()) {
            Class<?> dimClass = ReflectionUtils.getRealClass(dimObject.getClass());
            Class<? extends DimensionalItemObject> itemClass = DimensionalObject.DIMENSION_CLASS_ITEM_CLASS_MAP.get(dimClass);
            List<DimensionalItemObject> dimItems = !allItems ? asList(idObjectManager.getByUidOrdered(itemClass, items)) : dimObject.getItems();
            DimensionalObject object = new BaseDimensionalObject(dimension, dimObject.getDimensionType(), null, dimObject.getName(), dimItems, allItems);
            return object;
        }
    }
    if (allowNull) {
        return null;
    }
    throw new IllegalQueryException("Dimension identifier does not reference any dimension: " + dimension);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) IndicatorGroup(org.hisp.dhis.indicator.IndicatorGroup) Calendar(org.hisp.dhis.calendar.Calendar) Period(org.hisp.dhis.period.Period) DimensionalObject(org.hisp.dhis.common.DimensionalObject) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) RelativePeriodEnum(org.hisp.dhis.period.RelativePeriodEnum) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 40 with DataElementCategoryOptionCombo

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

the class ValidationRuleStoreTest method setUpTest.

// -------------------------------------------------------------------------
// Fixture
// -------------------------------------------------------------------------
@Override
public void setUpTest() throws Exception {
    dataElementA = createDataElement('A');
    dataElementB = createDataElement('B');
    dataElementC = createDataElement('C');
    dataElementD = createDataElement('D');
    dataElementService.addDataElement(dataElementA);
    dataElementService.addDataElement(dataElementB);
    dataElementService.addDataElement(dataElementC);
    dataElementService.addDataElement(dataElementD);
    dataElements = new HashSet<>();
    dataElements.add(dataElementA);
    dataElements.add(dataElementB);
    dataElements.add(dataElementC);
    dataElements.add(dataElementD);
    DataElementCategoryOptionCombo categoryOptionCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
    optionCombos = new HashSet<>();
    optionCombos.add(categoryOptionCombo);
    expressionA = new Expression("expressionA", "descriptionA");
    expressionB = new Expression("expressionB", "descriptionB");
    expressionService.addExpression(expressionB);
    expressionService.addExpression(expressionA);
    periodType = PeriodType.getAvailablePeriodTypes().iterator().next();
}
Also used : Expression(org.hisp.dhis.expression.Expression) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Aggregations

DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)97 DataElement (org.hisp.dhis.dataelement.DataElement)43 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)36 Period (org.hisp.dhis.period.Period)31 DataValue (org.hisp.dhis.datavalue.DataValue)19 ArrayList (java.util.ArrayList)18 Date (java.util.Date)18 DataSet (org.hisp.dhis.dataset.DataSet)15 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)15 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)13 DataElementCategoryOption (org.hisp.dhis.dataelement.DataElementCategoryOption)12 CompleteDataSetRegistration (org.hisp.dhis.dataset.CompleteDataSetRegistration)12 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)10 DataElementCategoryCombo (org.hisp.dhis.dataelement.DataElementCategoryCombo)9 HashSet (java.util.HashSet)8 Test (org.junit.Test)7 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)6 Matcher (java.util.regex.Matcher)5 DataElementCategory (org.hisp.dhis.dataelement.DataElementCategory)5 HashMap (java.util.HashMap)4