Search in sources :

Example 51 with DataElementCategoryOptionCombo

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

the class FacilityReportingServiceImpl method saveValue.

private void saveValue(OrganisationUnit unit, Period period, org.hisp.dhis.dataelement.DataElement dataElement, DataValue dv) {
    String value = dv.getValue().trim();
    DataElementCategoryOptionCombo catOptCombo = categoryService.getDataElementCategoryOptionCombo(dv.getCategoryOptComboID());
    org.hisp.dhis.datavalue.DataValue dataValue = dataValueService.getDataValue(dataElement, period, unit, catOptCombo);
    if (dataValue == null) {
        dataValue = new org.hisp.dhis.datavalue.DataValue(dataElement, period, unit, catOptCombo, categoryService.getDefaultDataElementCategoryOptionCombo(), value, "", new Date(), "");
        dataValueService.addDataValue(dataValue);
    } else {
        dataValue.setValue(value);
        dataValue.setLastUpdated(new Date());
        dataValueService.updateDataValue(dataValue);
    }
}
Also used : DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) Date(java.util.Date)

Example 52 with DataElementCategoryOptionCombo

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

the class FacilityReportingServiceImpl method saveDataSetValues.

@Override
public void saveDataSetValues(OrganisationUnit unit, DataSetValue dataSetValue) throws NotAllowedException {
    org.hisp.dhis.dataset.DataSet dataSet = dataSetService.getDataSet(dataSetValue.getId());
    if (!dataSetAssociatedWithOrgUnit(unit, dataSet)) {
        log.info("Failed to save data value set for: " + unit.getName() + ", " + dataSet.getName() + " - Org unit and data set not associated.");
        throw NotAllowedException.INVALID_DATASET_ASSOCIATION;
    }
    Period period = getPeriod(dataSetValue.getPeriodName(), dataSet.getPeriodType());
    if (period == null) {
        log.info("Failed to save data value set for: " + unit.getName() + ", " + dataSet.getName() + " - Period not found.");
        throw NotAllowedException.INVALID_PERIOD;
    }
    log.info("Recieved data value set for: " + unit.getName() + ", " + dataSet.getName() + ", " + period.getIsoDate());
    Map<Integer, org.hisp.dhis.dataelement.DataElement> dataElementMap = getDataElementIdMapping(dataSet);
    for (DataValue dataValue : dataSetValue.getDataValues()) {
        org.hisp.dhis.dataelement.DataElement dataElement = dataElementMap.get(dataValue.getId());
        if (dataElement == null) {
            log.info("Data value submitted for data element " + dataValue.getId() + ", that is not in data set '" + dataSet.getName() + "'");
            continue;
        }
        if (StringUtils.isEmpty(dataValue.getValue())) {
            log.debug("Empty data value for data element " + dataValue.getId() + " not saved");
            continue;
        }
        saveValue(unit, period, dataElement, dataValue);
    }
    DataElementCategoryOptionCombo optionCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
    CompleteDataSetRegistration registration = registrationService.getCompleteDataSetRegistration(dataSet, period, unit, optionCombo);
    if (registration != null) {
        registrationService.deleteCompleteDataSetRegistration(registration);
    }
    registration = new CompleteDataSetRegistration();
    registration.setDataSet(dataSet);
    registration.setPeriod(period);
    registration.setSource(unit);
    registration.setDate(new Date());
    registration.setStoredBy(currentUserService.getCurrentUser().getUsername());
    registrationService.saveCompleteDataSetRegistration(registration);
    log.info("Saved and registered data value set as complete: " + unit.getName() + ", " + dataSet.getName() + ", " + period.getIsoDate());
}
Also used : DataValue(org.hisp.dhis.api.mobile.model.DataValue) Period(org.hisp.dhis.period.Period) Date(java.util.Date) DataElement(org.hisp.dhis.api.mobile.model.DataElement) CompleteDataSetRegistration(org.hisp.dhis.dataset.CompleteDataSetRegistration) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 53 with DataElementCategoryOptionCombo

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

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

Example 55 with DataElementCategoryOptionCombo

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

the class ChartController method getHistoryChart.

@RequestMapping(value = { "/history/data", "/history/data.png" }, method = RequestMethod.GET)
public void getHistoryChart(@RequestParam String de, @RequestParam String co, @RequestParam String cp, @RequestParam String pe, @RequestParam String ou, @RequestParam(defaultValue = "525", required = false) int width, @RequestParam(defaultValue = "300", required = false) int height, HttpServletResponse response) throws IOException, WebMessageException {
    DataElement dataElement = dataElementService.getDataElement(de);
    if (dataElement == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Data element does not exist: " + de));
    }
    DataElementCategoryOptionCombo categoryOptionCombo = categoryService.getDataElementCategoryOptionCombo(co);
    if (categoryOptionCombo == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Category option combo does not exist: " + co));
    }
    DataElementCategoryOptionCombo attributeOptionCombo = categoryService.getDataElementCategoryOptionCombo(cp);
    if (attributeOptionCombo == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Category option combo does not exist: " + cp));
    }
    Period period = PeriodType.getPeriodFromIsoString(pe);
    if (period == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Period does not exist: " + pe));
    }
    OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(ou);
    if (organisationUnit == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Organisation unit does not exist: " + ou));
    }
    contextUtils.configureResponse(response, ContextUtils.CONTENT_TYPE_PNG, CacheStrategy.RESPECT_SYSTEM_SETTING, "chart.png", false);
    JFreeChart chart = chartService.getJFreeChartHistory(dataElement, categoryOptionCombo, attributeOptionCombo, period, organisationUnit, 13, i18nManager.getI18nFormat());
    ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, width, height);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Period(org.hisp.dhis.period.Period) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) JFreeChart(org.jfree.chart.JFreeChart) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)59 DataElement (org.hisp.dhis.dataelement.DataElement)26 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)22 Period (org.hisp.dhis.period.Period)22 Date (java.util.Date)13 DataValue (org.hisp.dhis.datavalue.DataValue)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)13 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)11 ArrayList (java.util.ArrayList)10 DataElementCategoryOption (org.hisp.dhis.dataelement.DataElementCategoryOption)9 CompleteDataSetRegistration (org.hisp.dhis.dataset.CompleteDataSetRegistration)7 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)6 DataSet (org.hisp.dhis.dataset.DataSet)6 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)6 HashSet (java.util.HashSet)5 DataElementCategoryCombo (org.hisp.dhis.dataelement.DataElementCategoryCombo)4 Test (org.junit.Test)4 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)4 Matcher (java.util.regex.Matcher)3 EventSearchParams (org.hisp.dhis.dxf2.events.event.EventSearchParams)3