Search in sources :

Example 26 with DataElementCategoryOptionCombo

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

the class HibernateCategoryOptionComboStore method getCategoryOptionCombo.

@Override
public DataElementCategoryOptionCombo getCategoryOptionCombo(DataElementCategoryCombo categoryCombo, Set<DataElementCategoryOption> categoryOptions) {
    String hql = "from DataElementCategoryOptionCombo co where co.categoryCombo = :categoryCombo";
    for (DataElementCategoryOption option : categoryOptions) {
        hql += " and :option" + option.getId() + " in elements (co.categoryOptions)";
    }
    Query query = getQuery(hql);
    query.setEntity("categoryCombo", categoryCombo);
    for (DataElementCategoryOption option : categoryOptions) {
        query.setEntity("option" + option.getId(), option);
    }
    return (DataElementCategoryOptionCombo) query.uniqueResult();
}
Also used : Query(org.hibernate.Query) DataElementCategoryOption(org.hisp.dhis.dataelement.DataElementCategoryOption) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 27 with DataElementCategoryOptionCombo

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

the class HibernateCategoryOptionComboStore method updateNames.

@Override
@SuppressWarnings("unchecked")
public void updateNames() {
    List<DataElementCategoryOptionCombo> categoryOptionCombos = getQuery("from DataElementCategoryOptionCombo co where co.name is null").list();
    int counter = 0;
    Session session = getSession();
    for (DataElementCategoryOptionCombo coc : categoryOptionCombos) {
        session.update(coc);
        if ((counter % 400) == 0) {
            dbmsManager.clearSession();
        }
    }
}
Also used : DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) Session(org.hibernate.Session)

Example 28 with DataElementCategoryOptionCombo

use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo 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 29 with DataElementCategoryOptionCombo

use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo 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 30 with DataElementCategoryOptionCombo

use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo 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)

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