Search in sources :

Example 46 with DataValue

use of org.hisp.dhis.datavalue.DataValue in project dhis2-core by dhis2.

the class FormUtils method fillWithDataValues.

public static void fillWithDataValues(Form form, Collection<DataValue> dataValues) {
    Map<String, Field> operandFieldMap = buildCacheMap(form);
    for (DataValue dataValue : dataValues) {
        DataElement dataElement = dataValue.getDataElement();
        DataElementCategoryOptionCombo categoryOptionCombo = dataValue.getCategoryOptionCombo();
        Field field = operandFieldMap.get(dataElement.getUid() + SEP + categoryOptionCombo.getUid());
        if (field != null) {
            field.setValue(dataValue.getValue());
            field.setComment(dataValue.getComment());
        }
    }
}
Also used : Field(org.hisp.dhis.webapi.webdomain.form.Field) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) DataValue(org.hisp.dhis.datavalue.DataValue) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 47 with DataValue

use of org.hisp.dhis.datavalue.DataValue in project dhis2-core by dhis2.

the class FormUtilsImpl method getDataValueMap.

@Override
public Map<String, String> getDataValueMap(OrganisationUnit organisationUnit, DataSet dataSet, Period period) {
    Map<String, String> dataValueMap = new HashMap<>();
    List<DataValue> values = dataValueService.getDataValues(new DataExportParams().setDataElements(dataSet.getDataElements()).setPeriods(Sets.newHashSet(period)).setOrganisationUnits(Sets.newHashSet(organisationUnit)));
    for (DataValue dataValue : values) {
        DataElement dataElement = dataValue.getDataElement();
        DataElementCategoryOptionCombo optionCombo = dataValue.getCategoryOptionCombo();
        String key = String.format("DE%dOC%d", dataElement.getId(), optionCombo.getId());
        String value = dataValue.getValue();
        dataValueMap.put(key, value);
    }
    return dataValueMap;
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DeflatedDataValue(org.hisp.dhis.datavalue.DeflatedDataValue) DataValue(org.hisp.dhis.datavalue.DataValue) DataExportParams(org.hisp.dhis.datavalue.DataExportParams) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 48 with DataValue

use of org.hisp.dhis.datavalue.DataValue in project dhis2-core by dhis2.

the class DataValueSMSListener method sendSuccessFeedback.

protected void sendSuccessFeedback(String sender, SMSCommand command, Map<String, String> parsedMessage, Date date, OrganisationUnit orgunit) {
    String reportBack = "Thank you! Values entered: ";
    String notInReport = "Missing values for: ";
    Period period = null;
    Map<String, DataValue> codesWithDataValues = new TreeMap<>();
    List<String> codesWithoutDataValues = new ArrayList<>();
    for (SMSCode code : command.getCodes()) {
        DataElementCategoryOptionCombo optionCombo = dataElementCategoryService.getDataElementCategoryOptionCombo(code.getOptionId());
        period = getPeriod(command, date);
        DataValue dv = dataValueService.getDataValue(code.getDataElement(), period, orgunit, optionCombo);
        if (dv == null && !StringUtils.isEmpty(code.getCode())) {
            codesWithoutDataValues.add(code.getCode());
        } else if (dv != null) {
            codesWithDataValues.put(code.getCode(), dv);
        }
    }
    for (String key : codesWithDataValues.keySet()) {
        DataValue dv = codesWithDataValues.get(key);
        String value = dv.getValue();
        if (ValueType.BOOLEAN == dv.getDataElement().getValueType()) {
            if ("true".equals(value)) {
                value = "Yes";
            } else if ("false".equals(value)) {
                value = "No";
            }
        }
        reportBack += key + "=" + value + " ";
    }
    Collections.sort(codesWithoutDataValues);
    for (String key : codesWithoutDataValues) {
        notInReport += key + ",";
    }
    notInReport = notInReport.substring(0, notInReport.length() - 1);
    if (smsSender.isConfigured()) {
        if (command.getSuccessMessage() != null && !StringUtils.isEmpty(command.getSuccessMessage())) {
            smsSender.sendMessage(null, command.getSuccessMessage(), sender);
        } else {
            smsSender.sendMessage(null, reportBack, sender);
        }
    } else {
        Log.info("No sms configuration found.");
    }
}
Also used : DataValue(org.hisp.dhis.datavalue.DataValue) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) SMSCode(org.hisp.dhis.sms.command.code.SMSCode) TreeMap(java.util.TreeMap) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 49 with DataValue

use of org.hisp.dhis.datavalue.DataValue in project dhis2-core by dhis2.

the class J2MEDataValueSMSListener method storeDataValue.

private void storeDataValue(String sender, OrganisationUnit orgUnit, Map<String, String> parsedMessage, SMSCode code, SMSCommand command, Period period, DataSet dataset) {
    String upperCaseCode = code.getCode().toUpperCase();
    String storedBy = SmsUtils.getUser(sender, command, userService.getUsersByPhoneNumber(sender)).getUsername();
    if (StringUtils.isBlank(storedBy)) {
        storedBy = "[unknown] from [" + sender + "]";
    }
    DataElementCategoryOptionCombo optionCombo = dataElementCategoryService.getDataElementCategoryOptionCombo(code.getOptionId());
    DataValue dv = dataValueService.getDataValue(code.getDataElement(), period, orgUnit, optionCombo);
    String value = parsedMessage.get(upperCaseCode);
    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";
            }
        }
        dv.setValue(value);
        dv.setLastUpdated(new java.util.Date());
        dv.setStoredBy(storedBy);
        if (ValidationUtils.dataValueIsValid(value, dv.getDataElement()) != null) {
            // not a valid value for data element
            return;
        }
        if (newDataValue) {
            dataValueService.addDataValue(dv);
        } else {
            dataValueService.updateDataValue(dv);
        }
    }
}
Also used : DataValue(org.hisp.dhis.datavalue.DataValue) Date(java.util.Date) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Aggregations

DataValue (org.hisp.dhis.datavalue.DataValue)49 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)20 Period (org.hisp.dhis.period.Period)19 Test (org.junit.Test)19 DhisSpringTest (org.hisp.dhis.DhisSpringTest)18 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)18 ClassPathResource (org.springframework.core.io.ClassPathResource)16 DataElement (org.hisp.dhis.dataelement.DataElement)15 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)11 Date (java.util.Date)7 ArrayList (java.util.ArrayList)4 CompleteDataSetRegistration (org.hisp.dhis.dataset.CompleteDataSetRegistration)4 DataValueAudit (org.hisp.dhis.datavalue.DataValueAudit)4 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)3 DataSet (org.hisp.dhis.dataset.DataSet)3 DataExportParams (org.hisp.dhis.datavalue.DataExportParams)3 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)3 SMSCode (org.hisp.dhis.sms.command.code.SMSCode)3