Search in sources :

Example 21 with DataElement

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

the class ResourceTableServiceTest method setUpTest.

@Override
public void setUpTest() {
    PeriodType pt = new MonthlyPeriodType();
    DataElement deA = createDataElement('A');
    DataElement deB = createDataElement('B');
    idObjectManager.save(deA);
    idObjectManager.save(deB);
    DataElementGroup degA = createDataElementGroup('A');
    DataElementGroup degB = createDataElementGroup('B');
    degA.addDataElement(deA);
    degB.addDataElement(deB);
    idObjectManager.save(degA);
    idObjectManager.save(degB);
    DataElementGroupSet degsA = createDataElementGroupSet('A');
    degsA.addDataElementGroup(degA);
    degsA.addDataElementGroup(degB);
    idObjectManager.save(degsA);
    OrganisationUnit ouA = createOrganisationUnit('A');
    OrganisationUnit ouB = createOrganisationUnit('B');
    OrganisationUnit ouC = createOrganisationUnit('C');
    ouB.setParent(ouA);
    ouC.setParent(ouA);
    ouA.getChildren().add(ouB);
    ouA.getChildren().add(ouC);
    idObjectManager.save(ouA);
    idObjectManager.save(ouB);
    idObjectManager.save(ouC);
    DataSet dsA = createDataSet('A', pt);
    DataSet dsB = createDataSet('B', pt);
    dsA.addDataSetElement(deA);
    dsB.addDataSetElement(deA);
    dsA.addOrganisationUnit(ouA);
    dsB.addOrganisationUnit(ouA);
    dataSetService.addDataSet(dsA);
    dataSetService.addDataSet(dsB);
}
Also used : MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) PeriodType(org.hisp.dhis.period.PeriodType) DataElement(org.hisp.dhis.dataelement.DataElement) DataElementGroupSet(org.hisp.dhis.dataelement.DataElementGroupSet) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) DataSet(org.hisp.dhis.dataset.DataSet) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup)

Example 22 with DataElement

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

the class DefaultPredictorService method predict.

// -------------------------------------------------------------------------
// Predictor run
// -------------------------------------------------------------------------
@Override
public int predict(Predictor predictor, Date startDate, Date endDate) {
    log.info("Predicting for " + predictor.getName() + " from " + startDate.toString() + " to " + endDate.toString());
    Expression generator = predictor.getGenerator();
    Expression skipTest = predictor.getSampleSkipTest();
    DataElement outputDataElement = predictor.getOutput();
    Set<String> aggregates = expressionService.getAggregatesInExpression(generator.getExpression());
    Map<String, Double> constantMap = constantService.getConstantMap();
    List<Period> outputPeriods = getPeriodsBetweenDates(predictor.getPeriodType(), startDate, endDate);
    ListMap<Period, Period> samplePeriodsMap = getSamplePeriodsMap(outputPeriods, predictor);
    Set<Period> allSamplePeriods = samplePeriodsMap.uniqueValues();
    Set<DataElementOperand> dataElementOperands = getDataElementOperands(aggregates, skipTest);
    User currentUser = currentUserService.getCurrentUser();
    DataElementCategoryOptionCombo outputOptionCombo = predictor.getOutputCombo() == null ? categoryService.getDefaultDataElementCategoryOptionCombo() : predictor.getOutputCombo();
    List<OrganisationUnit> orgUnits = organisationUnitService.getOrganisationUnitsAtOrgUnitLevels(predictor.getOrganisationUnitLevels(), currentUser.getOrganisationUnits());
    int predictionCount = 0;
    for (OrganisationUnit orgUnit : orgUnits) {
        MapMapMap<Period, String, DimensionalItemObject, Double> dataMap = dataElementOperands.isEmpty() ? null : dataValueService.getDataElementOperandValues(dataElementOperands, allSamplePeriods, orgUnit);
        applySkipTest(dataMap, skipTest, constantMap);
        for (Period period : outputPeriods) {
            ListMapMap<String, String, Double> aggregateSampleMap = getAggregateSamples(dataMap, aggregates, samplePeriodsMap.get(period), constantMap);
            for (String aoc : aggregateSampleMap.keySet()) {
                ListMap<String, Double> aggregateValueMap = aggregateSampleMap.get(aoc);
                Double value = expressionService.getExpressionValue(generator, new HashMap<>(), constantMap, null, period.getDaysInPeriod(), aggregateValueMap);
                if (value != null && !value.isNaN() && !value.isInfinite()) {
                    writeDataValue(outputDataElement, period, orgUnit, outputOptionCombo, categoryService.getDataElementCategoryOptionCombo(aoc), value.toString(), currentUser.getUsername());
                    predictionCount++;
                }
            }
        }
    }
    log.info("Generated " + predictionCount + " predictions for " + predictor.getName() + " from " + startDate.toString() + " to " + endDate.toString());
    return predictionCount;
}
Also used : DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) Period(org.hisp.dhis.period.Period) DataElement(org.hisp.dhis.dataelement.DataElement) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) Expression(org.hisp.dhis.expression.Expression) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 23 with DataElement

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

the class DefaultProgramDataEntryService method prepareDataEntryFormForAdd.

@Override
public String prepareDataEntryFormForAdd(String htmlCode, I18n i18n, ProgramStage programStage) {
    // ---------------------------------------------------------------------
    // Inline Javascript to add to HTML before outputting
    // ---------------------------------------------------------------------
    final String jQueryCalendar = "<script>$DATAPICKER-METHOD(\"$PROGRAMSTAGEID-$DATAELEMENTID-val\", false);</script>";
    StringBuffer sb = new StringBuffer();
    // ---------------------------------------------------------------------
    // Pattern to match data elements in the HTML code
    // ---------------------------------------------------------------------
    Matcher dataElementMatcher = INPUT_PATTERN.matcher(htmlCode);
    int tabindex = 0;
    // ---------------------------------------------------------------------
    // Iterate through all matching data element fields
    // ---------------------------------------------------------------------
    Map<String, ProgramStageDataElement> programStageDataElementMap = getProgramStageDataElementMap(programStage);
    while (dataElementMatcher.find()) {
        // -----------------------------------------------------------------
        // Get HTML input field code
        // -----------------------------------------------------------------
        String compulsory = "null";
        boolean allowProvidedElsewhere = false;
        String dateMethod = "datePickerValid";
        String inputHTML = dataElementMatcher.group(1);
        Matcher identifierMatcher = IDENTIFIER_PATTERN_FIELD.matcher(inputHTML);
        if (identifierMatcher.find() && identifierMatcher.groupCount() > 0) {
            // -------------------------------------------------------------
            // Get ID of data element
            // -------------------------------------------------------------
            String programStageUid = identifierMatcher.group(1);
            String dataElementUid = identifierMatcher.group(2);
            DataElement dataElement = null;
            String programStageName = programStage.getDisplayName();
            if (!programStageUid.equals(programStage.getUid())) {
                dataElement = dataElementService.getDataElement(dataElementUid);
                ProgramStage otherProgramStage = programStageService.getProgramStage(programStageUid);
                programStageName = otherProgramStage != null ? otherProgramStage.getDisplayName() : "N/A";
            } else {
                ProgramStageDataElement psde = programStageDataElementMap.get(dataElementUid);
                if (psde == null) {
                    return i18n.getString("some_data_element_not_exist");
                }
                dataElement = psde.getDataElement();
                compulsory = BooleanUtils.toStringTrueFalse(psde.isCompulsory());
                allowProvidedElsewhere = psde.getAllowProvidedElsewhere();
                if (psde.getAllowFutureDate()) {
                    dateMethod = "datePicker";
                }
            }
            if (dataElement == null) {
                continue;
            }
            // -------------------------------------------------------------
            // Find existing value of data element in data set
            // -------------------------------------------------------------
            TrackedEntityDataValue entityInstanceDataValue = null;
            String dataElementValue = EMPTY;
            if (inputHTML.contains("title=\"\"")) {
                inputHTML = inputHTML.replace("title=\"\"", "title=\"[ " + dataElement.getUid() + " - " + dataElement.getName() + " - " + dataElement.getValueType() + " ]\" ");
            } else {
                inputHTML += "title=\"[ " + dataElement.getUid() + " - " + dataElement.getName() + " - " + dataElement.getValueType() + " ]\" ";
            }
            // -------------------------------------------------------------
            // Set field for dataElement
            // -------------------------------------------------------------
            tabindex++;
            ValueType valueType = dataElement.getValueType();
            if (valueType.isText() || valueType.isNumeric() || ValueType.USERNAME == valueType) {
                inputHTML = populateCustomDataEntryForTextBox(dataElement, inputHTML, dataElementValue);
            } else if (ValueType.DATE == valueType || ValueType.DATETIME == valueType) {
                inputHTML = populateCustomDataEntryForDate(inputHTML, dataElementValue);
            } else if (ValueType.TRUE_ONLY == valueType) {
                inputHTML = populateCustomDataEntryForTrueOnly(dataElement, inputHTML, dataElementValue);
            } else if (ValueType.BOOLEAN == valueType) {
                inputHTML = populateCustomDataEntryForBoolean(dataElement, inputHTML, dataElementValue, i18n);
            }
            // -----------------------------------------------------------
            // Check if this dataElement is from another programStage then
            // disable
            // If programStagsInstance is completed then disabled it
            // -----------------------------------------------------------
            String disabled = "";
            if (!programStageUid.equals(programStage.getUid())) {
                disabled = "disabled=\"\"";
            } else {
                if (ValueType.DATE == valueType || ValueType.DATETIME == valueType) {
                    inputHTML += jQueryCalendar;
                }
                if (allowProvidedElsewhere) {
                    // Add ProvidedByOtherFacility checkbox
                    inputHTML = addProvidedElsewherCheckbox(inputHTML, entityInstanceDataValue, programStage);
                }
            }
            inputHTML = inputHTML.replace("$DATAELEMENTID", String.valueOf(dataElementUid));
            inputHTML = inputHTML.replace("$VALUE", dataElementValue);
            inputHTML = inputHTML.replace("$PROGRAMSTAGEID", String.valueOf(programStageUid));
            inputHTML = inputHTML.replace("$PROGRAMSTAGENAME", programStageName);
            inputHTML = inputHTML.replace("$DATAELEMENTNAME", dataElement.getName());
            inputHTML = inputHTML.replace("$DATAELEMENTTYPE", dataElement.getValueType().toString());
            inputHTML = inputHTML.replace("$DISABLED", disabled);
            inputHTML = inputHTML.replace("$COMPULSORY", compulsory);
            inputHTML = inputHTML.replace("$SAVEMODE", "false");
            inputHTML = inputHTML.replace("$TABINDEX", tabindex + "");
            inputHTML = inputHTML.replace("$DATAPICKER-METHOD", dateMethod);
            inputHTML = inputHTML.replaceAll("\\$", "\\\\\\$");
            dataElementMatcher.appendReplacement(sb, inputHTML);
        }
    }
    dataElementMatcher.appendTail(sb);
    return populateI18nStrings(sb.toString(), i18n);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Matcher(java.util.regex.Matcher) ValueType(org.hisp.dhis.common.ValueType) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue)

Example 24 with DataElement

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

the class DefaultProgramIndicatorService method getSubstitutedExpression.

/**
     * Generates an expression where all items are substituted with a sample value
     * in order to maintain a valid expression syntax.
     *
     * @param expression the expression.
     */
private String getSubstitutedExpression(String expression) {
    StringBuffer expr = new StringBuffer();
    Matcher matcher = ProgramIndicator.EXPRESSION_PATTERN.matcher(expression);
    while (matcher.find()) {
        String key = matcher.group(1);
        String uid = matcher.group(2);
        if (ProgramIndicator.KEY_DATAELEMENT.equals(key)) {
            String de = matcher.group(3);
            ProgramStage programStage = programStageService.getProgramStage(uid);
            DataElement dataElement = dataElementService.getDataElement(de);
            if (programStage != null && dataElement != null) {
                String sample = ValidationUtils.getSubstitutionValue(dataElement.getValueType());
                matcher.appendReplacement(expr, sample);
            } else {
                return ProgramIndicator.INVALID_IDENTIFIERS_IN_EXPRESSION;
            }
        } else if (ProgramIndicator.KEY_ATTRIBUTE.equals(key)) {
            TrackedEntityAttribute attribute = attributeService.getTrackedEntityAttribute(uid);
            if (attribute != null) {
                String sample = ValidationUtils.getSubstitutionValue(attribute.getValueType());
                matcher.appendReplacement(expr, sample);
            } else {
                return ProgramIndicator.INVALID_IDENTIFIERS_IN_EXPRESSION;
            }
        } else if (ProgramIndicator.KEY_CONSTANT.equals(key)) {
            Constant constant = constantService.getConstant(uid);
            if (constant != null) {
                matcher.appendReplacement(expr, String.valueOf(constant.getValue()));
            } else {
                return ProgramIndicator.INVALID_IDENTIFIERS_IN_EXPRESSION;
            }
        } else if (ProgramIndicator.KEY_PROGRAM_VARIABLE.equals(key)) {
            String sampleValue = VARIABLE_SAMPLE_VALUE_MAP.get(uid);
            if (sampleValue != null) {
                matcher.appendReplacement(expr, sampleValue);
            } else {
                return ProgramIndicator.UNKNOWN_VARIABLE;
            }
        }
    }
    matcher.appendTail(expr);
    return expr.toString();
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Matcher(java.util.regex.Matcher) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) Constant(org.hisp.dhis.constant.Constant)

Example 25 with DataElement

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

the class DefaultProgramService method getGeneratedProgramDataElements.

// -------------------------------------------------------------------------
// ProgramDataElement
// -------------------------------------------------------------------------
@Override
public List<ProgramDataElementDimensionItem> getGeneratedProgramDataElements(String programUid) {
    Program program = getProgram(programUid);
    List<ProgramDataElementDimensionItem> programDataElements = Lists.newArrayList();
    if (program == null) {
        return programDataElements;
    }
    for (DataElement element : program.getDataElements()) {
        programDataElements.add(new ProgramDataElementDimensionItem(program, element));
    }
    Collections.sort(programDataElements);
    return programDataElements;
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement)

Aggregations

DataElement (org.hisp.dhis.dataelement.DataElement)254 Test (org.junit.Test)137 DhisSpringTest (org.hisp.dhis.DhisSpringTest)99 User (org.hisp.dhis.user.User)51 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)47 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)44 DataSet (org.hisp.dhis.dataset.DataSet)39 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)36 ArrayList (java.util.ArrayList)31 List (java.util.List)31 ClassPathResource (org.springframework.core.io.ClassPathResource)26 HashSet (java.util.HashSet)23 Period (org.hisp.dhis.period.Period)20 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)19 DataElementGroup (org.hisp.dhis.dataelement.DataElementGroup)18 DataElementCategoryCombo (org.hisp.dhis.dataelement.DataElementCategoryCombo)16 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)15 DataValue (org.hisp.dhis.datavalue.DataValue)15 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)14 Matcher (java.util.regex.Matcher)13