Search in sources :

Example 81 with DataElement

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

the class MinMaxOutlierAnalysisService method generateMinMaxValues.

@Override
public void generateMinMaxValues(Collection<OrganisationUnit> parents, Collection<DataElement> dataElements, Double stdDevFactor) {
    log.info("Starting min-max value generation, no of data elements: " + dataElements.size() + ", no of org units: " + parents.size());
    //Set<Integer> orgUnitIds = new HashSet<>( IdentifiableObjectUtils.getIdentifiers( organisationUnits ) );
    Date from = new DateTime(1, 1, 1, 1, 1).toDate();
    minMaxDataElementService.removeMinMaxDataElements(dataElements, parents);
    log.debug("Deleted existing min-max values");
    BatchHandler<MinMaxDataElement> batchHandler = batchHandlerFactory.createBatchHandler(MinMaxDataElementBatchHandler.class).init();
    for (DataElement dataElement : dataElements) {
        ValueType valueType = dataElement.getValueType();
        if (valueType.isNumeric()) {
            Collection<DataElementCategoryOptionCombo> categoryOptionCombos = dataElement.getCategoryOptionCombos();
            for (DataElementCategoryOptionCombo categoryOptionCombo : categoryOptionCombos) {
                Map<Integer, Double> standardDeviations = dataAnalysisStore.getStandardDeviation(dataElement, categoryOptionCombo, parents, from);
                Map<Integer, Double> averages = dataAnalysisStore.getAverage(dataElement, categoryOptionCombo, parents, from);
                for (Integer unit : averages.keySet()) {
                    Double stdDev = standardDeviations.get(unit);
                    Double avg = averages.get(unit);
                    if (stdDev != null && avg != null) {
                        int min = (int) MathUtils.getLowBound(stdDev, stdDevFactor, avg);
                        int max = (int) MathUtils.getHighBound(stdDev, stdDevFactor, avg);
                        if (ValueType.INTEGER_POSITIVE == valueType || ValueType.INTEGER_ZERO_OR_POSITIVE == valueType) {
                            // Cannot be < 0
                            min = Math.max(0, min);
                        }
                        if (ValueType.INTEGER_NEGATIVE == valueType) {
                            // Cannot be > 0
                            max = Math.min(0, max);
                        }
                        OrganisationUnit source = new OrganisationUnit();
                        source.setId(unit);
                        batchHandler.addObject(new MinMaxDataElement(source, dataElement, categoryOptionCombo, min, max, true));
                    }
                }
            }
        }
    }
    log.info("Min-max value generation done");
    batchHandler.flush();
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ValueType(org.hisp.dhis.common.ValueType) Date(java.util.Date) DateTime(org.joda.time.DateTime) MinMaxDataElementBatchHandler(org.hisp.dhis.jdbc.batchhandler.MinMaxDataElementBatchHandler) DataElement(org.hisp.dhis.dataelement.DataElement) MinMaxDataElement(org.hisp.dhis.minmax.MinMaxDataElement) MinMaxDataElement(org.hisp.dhis.minmax.MinMaxDataElement) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 82 with DataElement

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

the class MinMaxOutlierAnalysisService method analyse.

// -------------------------------------------------------------------------
// DataAnalysisService implementation
// -------------------------------------------------------------------------
@Override
public List<DeflatedDataValue> analyse(Collection<OrganisationUnit> parents, Collection<DataElement> dataElements, Collection<Period> periods, Double stdDevFactor, Date from) {
    Set<DataElement> elements = new HashSet<>(dataElements);
    FilterUtils.filter(elements, DE_NUMERIC_FILTER);
    Set<DataElementCategoryOptionCombo> categoryOptionCombos = new HashSet<>();
    for (DataElement dataElement : elements) {
        categoryOptionCombos.addAll(dataElement.getCategoryOptionCombos());
    }
    log.debug("Starting min-max analysis, no of data elements: " + elements.size() + ", no of parent org units: " + parents.size());
    return dataAnalysisStore.getMinMaxViolations(elements, categoryOptionCombos, periods, parents, MAX_OUTLIERS);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) MinMaxDataElement(org.hisp.dhis.minmax.MinMaxDataElement) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) HashSet(java.util.HashSet)

Example 83 with DataElement

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

the class DefaultDataEntryFormService method getDataElementsInDataEntryForm.

@Override
public Set<DataElement> getDataElementsInDataEntryForm(DataSet dataSet) {
    if (dataSet == null || !dataSet.hasDataEntryForm()) {
        return null;
    }
    Map<String, DataElement> dataElementMap = Maps.uniqueIndex(dataSet.getDataElements(), de -> de.getUid());
    Set<DataElement> dataElements = new HashSet<>();
    Matcher inputMatcher = INPUT_PATTERN.matcher(dataSet.getDataEntryForm().getHtmlCode());
    while (inputMatcher.find()) {
        String inputHtml = inputMatcher.group();
        Matcher identifierMatcher = IDENTIFIER_PATTERN.matcher(inputHtml);
        Matcher dataElementTotalMatcher = DATAELEMENT_TOTAL_PATTERN.matcher(inputHtml);
        DataElement dataElement = null;
        if (identifierMatcher.find() && identifierMatcher.groupCount() > 0) {
            String dataElementId = identifierMatcher.group(1);
            dataElement = dataElementMap.get(dataElementId);
        } else if (dataElementTotalMatcher.find() && dataElementTotalMatcher.groupCount() > 0) {
            String dataElementId = dataElementTotalMatcher.group(1);
            dataElement = dataElementMap.get(dataElementId);
        }
        if (dataElement != null) {
            dataElements.add(dataElement);
        }
    }
    return dataElements;
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Matcher(java.util.regex.Matcher) HashSet(java.util.HashSet)

Example 84 with DataElement

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

the class DataValueSMSListener method storeDataValue.

private boolean storeDataValue(String sender, OrganisationUnit orgunit, Map<String, String> parsedMessage, SMSCode code, SMSCommand command, Date date, DataSet dataSet) {
    String storedBy = SmsUtils.getUser(sender, command, userService.getUsersByPhoneNumber(sender)).getUsername();
    if (StringUtils.isBlank(storedBy)) {
        storedBy = "[unknown] from [" + sender + "]";
    }
    DataElementCategoryOptionCombo optionCombo = dataElementCategoryService.getDataElementCategoryOptionCombo(code.getOptionId());
    Period period = getPeriod(command, date);
    DataValue dv = dataValueService.getDataValue(code.getDataElement(), period, orgunit, optionCombo);
    String value = parsedMessage.get(code.getCode());
    Set<SMSSpecialCharacter> specialCharacters = command.getSpecialCharacters();
    for (SMSSpecialCharacter each : specialCharacters) {
        if (each.getName().equalsIgnoreCase(value)) {
            value = each.getValue();
            break;
        }
    }
    if (!StringUtils.isEmpty(value)) {
        boolean newDataValue = false;
        if (dv == null) {
            dv = new DataValue();
            dv.setCategoryOptionCombo(optionCombo);
            dv.setSource(orgunit);
            dv.setDataElement(code.getDataElement());
            dv.setPeriod(period);
            dv.setComment("");
            newDataValue = true;
        }
        if (ValueType.BOOLEAN == dv.getDataElement().getValueType()) {
            if ("Y".equals(value.toUpperCase()) || "YES".equals(value.toUpperCase())) {
                value = "true";
            } else if ("N".equals(value.toUpperCase()) || "NO".equals(value.toUpperCase())) {
                value = "false";
            }
        } else if (dv.getDataElement().getValueType().isInteger()) {
            try {
                Integer.parseInt(value);
            } catch (NumberFormatException e) {
                return false;
            }
        }
        dv.setValue(value);
        dv.setLastUpdated(new java.util.Date());
        dv.setStoredBy(storedBy);
        if (newDataValue) {
            dataValueService.addDataValue(dv);
        } else {
            dataValueService.updateDataValue(dv);
        }
    }
    if (code.getFormula() != null) {
        try {
            String formula = code.getFormula();
            String targetDataElementId = formula.substring(1, formula.length());
            String operation = String.valueOf(formula.charAt(0));
            DataElement targetDataElement = dataElementService.getDataElement(Integer.parseInt(targetDataElementId));
            if (targetDataElement == null) {
                return false;
            }
            DataValue targetDataValue = dataValueService.getDataValue(targetDataElement, period, orgunit, dataElementCategoryService.getDefaultDataElementCategoryOptionCombo());
            int targetValue = 0;
            boolean newTargetDataValue = false;
            if (targetDataValue == null) {
                targetDataValue = new DataValue();
                targetDataValue.setCategoryOptionCombo(dataElementCategoryService.getDefaultDataElementCategoryOptionCombo());
                targetDataValue.setSource(orgunit);
                targetDataValue.setDataElement(targetDataElement);
                targetDataValue.setPeriod(period);
                targetDataValue.setComment("");
                newTargetDataValue = true;
            } else {
                targetValue = Integer.parseInt(targetDataValue.getValue());
            }
            if (operation.equals("+")) {
                targetValue = targetValue + Integer.parseInt(value);
            } else if (operation.equals("-")) {
                targetValue = targetValue - Integer.parseInt(value);
            }
            targetDataValue.setValue(String.valueOf(targetValue));
            targetDataValue.setLastUpdated(new java.util.Date());
            targetDataValue.setStoredBy(storedBy);
            if (newTargetDataValue) {
                dataValueService.addDataValue(targetDataValue);
            } else {
                dataValueService.updateDataValue(targetDataValue);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    return true;
}
Also used : DataValue(org.hisp.dhis.datavalue.DataValue) Date(java.util.Date) Period(org.hisp.dhis.period.Period) SMSParserException(org.hisp.dhis.sms.parse.SMSParserException) DataElement(org.hisp.dhis.dataelement.DataElement) SMSSpecialCharacter(org.hisp.dhis.sms.command.SMSSpecialCharacter) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 85 with DataElement

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

the class DefaultCsvImportService method dataElementsFromCsv.

private List<DataElement> dataElementsFromCsv(CsvReader reader) throws IOException {
    DataElementCategoryCombo categoryCombo = categoryService.getDefaultDataElementCategoryCombo();
    List<DataElement> list = new ArrayList<>();
    while (reader.readRecord()) {
        String[] values = reader.getValues();
        if (values != null && values.length > 0) {
            DataElement object = new DataElement();
            setIdentifiableObject(object, values);
            object.setShortName(getSafe(values, 3, object.getName(), 50));
            object.setDescription(getSafe(values, 4, null, null));
            object.setFormName(getSafe(values, 5, null, 230));
            String domainType = getSafe(values, 6, DataElementDomain.AGGREGATE.getValue(), 16);
            object.setDomainType(DataElementDomain.fromValue(domainType));
            object.setValueType(ValueType.valueOf(getSafe(values, 7, ValueType.INTEGER.toString(), 50)));
            object.setAggregationType(AggregationType.valueOf(getSafe(values, 8, AggregationType.SUM.toString(), 50)));
            String categoryComboUid = getSafe(values, 9, null, 11);
            object.setUrl(getSafe(values, 10, null, 255));
            object.setZeroIsSignificant(Boolean.valueOf(getSafe(values, 11, "false", null)));
            String optionSetUid = getSafe(values, 12, null, 11);
            String commentOptionSetUid = getSafe(values, 13, null, 11);
            object.setAutoFields();
            DataElementCategoryCombo cc = new DataElementCategoryCombo();
            cc.setUid(categoryComboUid);
            cc.setAutoFields();
            if (categoryComboUid == null) {
                cc.setUid(categoryCombo.getUid());
            }
            object.setDataElementCategoryCombo(cc);
            if (optionSetUid != null) {
                OptionSet optionSet = new OptionSet();
                optionSet.setUid(optionSetUid);
                optionSet.setAutoFields();
                object.setOptionSet(optionSet);
            }
            if (commentOptionSetUid != null) {
                OptionSet optionSet = new OptionSet();
                optionSet.setUid(commentOptionSetUid);
                optionSet.setAutoFields();
                object.setCommentOptionSet(optionSet);
            }
            list.add(object);
        }
    }
    return list;
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) ArrayList(java.util.ArrayList) OptionSet(org.hisp.dhis.option.OptionSet)

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