Search in sources :

Example 1 with DataValue

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

the class DataValueController method saveDataValue.

// ---------------------------------------------------------------------
// POST
// ---------------------------------------------------------------------
@PreAuthorize("hasRole('ALL') or hasRole('F_DATAVALUE_ADD')")
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public void saveDataValue(@RequestParam String de, @RequestParam(required = false) String co, @RequestParam(required = false) String cc, @RequestParam(required = false) String cp, @RequestParam String pe, @RequestParam String ou, @RequestParam(required = false) String value, @RequestParam(required = false) String comment, @RequestParam(required = false) boolean followUp, HttpServletResponse response) throws WebMessageException {
    boolean strictPeriods = (Boolean) systemSettingManager.getSystemSetting(SettingKey.DATA_IMPORT_STRICT_PERIODS);
    boolean strictCategoryOptionCombos = (Boolean) systemSettingManager.getSystemSetting(SettingKey.DATA_IMPORT_STRICT_CATEGORY_OPTION_COMBOS);
    boolean strictOrgUnits = (Boolean) systemSettingManager.getSystemSetting(SettingKey.DATA_IMPORT_STRICT_ORGANISATION_UNITS);
    boolean requireCategoryOptionCombo = (Boolean) systemSettingManager.getSystemSetting(SettingKey.DATA_IMPORT_REQUIRE_CATEGORY_OPTION_COMBO);
    // ---------------------------------------------------------------------
    // Input validation
    // ---------------------------------------------------------------------
    DataElement dataElement = getAndValidateDataElement(de);
    DataElementCategoryOptionCombo categoryOptionCombo = getAndValidateCategoryOptionCombo(co, requireCategoryOptionCombo);
    DataElementCategoryOptionCombo attributeOptionCombo = getAndValidateAttributeOptionCombo(cc, cp);
    Period period = getAndValidatePeriod(pe);
    OrganisationUnit organisationUnit = getAndValidateOrganisationUnit(ou);
    validateInvalidFuturePeriod(period, dataElement);
    validateAttributeOptionComboWithOrgUnitAndPeriod(attributeOptionCombo, organisationUnit, period);
    String valueValid = ValidationUtils.dataValueIsValid(value, dataElement);
    if (valueValid != null) {
        throw new WebMessageException(WebMessageUtils.conflict("Invalid value: " + value + ", must match data element type: " + dataElement.getValueType()));
    }
    String commentValid = ValidationUtils.commentIsValid(comment);
    if (commentValid != null) {
        throw new WebMessageException(WebMessageUtils.conflict("Invalid comment: " + comment));
    }
    OptionSet optionSet = dataElement.getOptionSet();
    if (!Strings.isNullOrEmpty(value) && optionSet != null && !optionSet.getOptionCodesAsSet().contains(value)) {
        throw new WebMessageException(WebMessageUtils.conflict("Data value is not a valid option of the data element option set: " + dataElement.getUid()));
    }
    if (strictPeriods && !dataElement.getPeriodTypes().contains(period.getPeriodType())) {
        throw new WebMessageException(WebMessageUtils.conflict("Period type of period: " + period.getIsoDate() + " not valid for data element: " + dataElement.getUid()));
    }
    if (strictCategoryOptionCombos && !dataElement.getCategoryOptionCombos().contains(categoryOptionCombo)) {
        throw new WebMessageException(WebMessageUtils.conflict("Category option combo: " + categoryOptionCombo.getUid() + " must be part of category combo of data element: " + dataElement.getUid()));
    }
    if (strictOrgUnits && !organisationUnit.hasDataElement(dataElement)) {
        throw new WebMessageException(WebMessageUtils.conflict("Data element: " + dataElement.getUid() + " must be assigned through data sets to organisation unit: " + organisationUnit.getUid()));
    }
    // ---------------------------------------------------------------------
    // Locking validation
    // ---------------------------------------------------------------------
    validateDataSetNotLocked(dataElement, period, organisationUnit, attributeOptionCombo);
    // ---------------------------------------------------------------------
    // Period validation
    // ---------------------------------------------------------------------
    validateDataInputPeriodForDataElementAndPeriod(dataElement, period);
    // ---------------------------------------------------------------------
    // Assemble and save data value
    // ---------------------------------------------------------------------
    String storedBy = currentUserService.getCurrentUsername();
    Date now = new Date();
    DataValue dataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
    FileResource fileResource = null;
    if (dataValue == null) {
        if (dataElement.getValueType() == ValueType.FILE_RESOURCE) {
            if (value != null) {
                fileResource = fileResourceService.getFileResource(value);
                if (fileResource == null || fileResource.getDomain() != FileResourceDomain.DATA_VALUE) {
                    throw new WebMessageException(WebMessageUtils.notFound(FileResource.class, value));
                }
                if (fileResource.isAssigned()) {
                    throw new WebMessageException(WebMessageUtils.conflict("File resource already assigned or linked to another data value"));
                }
                fileResource.setAssigned(true);
            } else {
                throw new WebMessageException(WebMessageUtils.conflict("Missing parameter 'value'"));
            }
        }
        dataValue = new DataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo, StringUtils.trimToNull(value), storedBy, now, StringUtils.trimToNull(comment));
        dataValueService.addDataValue(dataValue);
    } else {
        if (value == null && ValueType.TRUE_ONLY.equals(dataElement.getValueType())) {
            if (comment == null) {
                dataValueService.deleteDataValue(dataValue);
                return;
            } else {
                value = "false";
            }
        }
        if (dataElement.isFileType()) {
            fileResourceService.deleteFileResource(dataValue.getValue());
        }
        if (value != null) {
            dataValue.setValue(StringUtils.trimToNull(value));
        }
        if (comment != null) {
            dataValue.setComment(StringUtils.trimToNull(comment));
        }
        if (followUp) {
            dataValue.toggleFollowUp();
        }
        dataValue.setLastUpdated(now);
        dataValue.setStoredBy(storedBy);
        dataValueService.updateDataValue(dataValue);
    }
    if (fileResource != null) {
        fileResourceService.updateFileResource(fileResource);
    }
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) DataValue(org.hisp.dhis.datavalue.DataValue) FileResource(org.hisp.dhis.fileresource.FileResource) Period(org.hisp.dhis.period.Period) OptionSet(org.hisp.dhis.option.OptionSet) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) Date(java.util.Date) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with DataValue

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

the class AnalyticsUtils method getDataValueSetFromGrid.

/**
     * Generates a data value set based on the given grid with aggregated data.
     * Sets the created and last updated fields to the current date.
     * 
     * @param params the data query parameters.
     * @param grid the grid.
     * @return a data value set.
     */
public static DataValueSet getDataValueSetFromGrid(DataQueryParams params, Grid grid) {
    int dxInx = grid.getIndexOfHeader(DATA_X_DIM_ID);
    int peInx = grid.getIndexOfHeader(PERIOD_DIM_ID);
    int ouInx = grid.getIndexOfHeader(ORGUNIT_DIM_ID);
    int coInx = grid.getIndexOfHeader(CATEGORYOPTIONCOMBO_DIM_ID);
    int aoInx = grid.getIndexOfHeader(ATTRIBUTEOPTIONCOMBO_DIM_ID);
    int vlInx = grid.getWidth() - 1;
    Assert.isTrue(dxInx >= 0, "Data dimension index must be greater than or equal to zero");
    Assert.isTrue(peInx >= 0, "Period dimension index must be greater than or equal to zero");
    Assert.isTrue(ouInx >= 0, "Org unit dimension index must be greater than or equal to zero");
    Assert.isTrue(coInx >= 0, "Category option combo dimension index must be greater than or equal to zero");
    Assert.isTrue(aoInx >= 0, "Attribute option combo dimension index must be greater than or equal to zero");
    Assert.isTrue(vlInx >= 0, "Value index must be greater than or equal to zero");
    String created = DateUtils.getMediumDateString();
    DataValueSet dvs = new DataValueSet();
    Set<String> primaryKeys = Sets.newHashSet();
    for (List<Object> row : grid.getRows()) {
        DataValue dv = new DataValue();
        Object coc = row.get(coInx);
        Object aoc = row.get(aoInx);
        dv.setDataElement(String.valueOf(row.get(dxInx)));
        dv.setPeriod(String.valueOf(row.get(peInx)));
        dv.setOrgUnit(String.valueOf(row.get(ouInx)));
        dv.setCategoryOptionCombo(coc != null ? String.valueOf(coc) : null);
        dv.setAttributeOptionCombo(aoc != null ? String.valueOf(aoc) : null);
        dv.setValue(String.valueOf(row.get(vlInx)));
        dv.setComment(KEY_AGG_VALUE);
        dv.setStoredBy(KEY_AGG_VALUE);
        dv.setCreated(created);
        dv.setLastUpdated(created);
        if (!params.isDuplicatesOnly() || !primaryKeys.add(dv.getPrimaryKey())) {
            dvs.getDataValues().add(dv);
        }
    }
    return dvs;
}
Also used : DataValueSet(org.hisp.dhis.dxf2.datavalueset.DataValueSet) DataValue(org.hisp.dhis.dxf2.datavalue.DataValue) DimensionalObject(org.hisp.dhis.common.DimensionalObject) DateUtils.getMediumDateString(org.hisp.dhis.system.util.DateUtils.getMediumDateString)

Example 3 with DataValue

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

the class RegistrationSingleEventServiceTest method createEvent.

private Event createEvent(String program, String programStage, String orgUnit, String person) {
    Event event = new Event();
    event.setProgram(program);
    event.setProgramStage(programStage);
    event.setOrgUnit(orgUnit);
    event.setTrackedEntityInstance(person);
    event.setEventDate("2013-01-01");
    event.getDataValues().add(new DataValue(dataElementA.getUid(), "10"));
    return event;
}
Also used : DataValue(org.hisp.dhis.dxf2.events.event.DataValue) Event(org.hisp.dhis.dxf2.events.event.Event)

Example 4 with DataValue

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

the class NoRegistrationSingleEventServiceTest method createEvent.

private Event createEvent(String program, String orgUnit) {
    Event event = new Event();
    event.setProgram(program);
    event.setOrgUnit(orgUnit);
    event.setEventDate("2013-01-01");
    event.getDataValues().add(new DataValue(dataElementA.getUid(), "10"));
    return event;
}
Also used : DataValue(org.hisp.dhis.dxf2.events.event.DataValue) Event(org.hisp.dhis.dxf2.events.event.Event)

Example 5 with DataValue

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

the class DataValueSetServiceTest method testImportDataValuesWithCategoryOptionComboIdScheme.

@Test
public void testImportDataValuesWithCategoryOptionComboIdScheme() throws Exception {
    in = new ClassPathResource("datavalueset/dataValueSetCCode.xml").getInputStream();
    ImportOptions options = new ImportOptions().setCategoryOptionComboIdScheme("CODE");
    ImportSummary summary = dataValueSetService.saveDataValueSet(in, options);
    assertEquals(summary.getConflicts().toString(), 0, summary.getConflicts().size());
    assertEquals(3, summary.getImportCount().getImported());
    assertEquals(0, summary.getImportCount().getUpdated());
    assertEquals(0, summary.getImportCount().getDeleted());
    assertEquals(0, summary.getImportCount().getIgnored());
    assertEquals(ImportStatus.SUCCESS, summary.getStatus());
    Collection<DataValue> dataValues = mockDataValueBatchHandler.getInserts();
    assertNotNull(dataValues);
    assertEquals(3, dataValues.size());
}
Also used : DataValue(org.hisp.dhis.datavalue.DataValue) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ClassPathResource(org.springframework.core.io.ClassPathResource) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

DataValue (org.hisp.dhis.datavalue.DataValue)22 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)20 DhisSpringTest (org.hisp.dhis.DhisSpringTest)17 Test (org.junit.Test)17 ClassPathResource (org.springframework.core.io.ClassPathResource)16 DataElement (org.hisp.dhis.dataelement.DataElement)8 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)7 DataValue (org.hisp.dhis.dxf2.events.event.DataValue)7 Event (org.hisp.dhis.dxf2.events.event.Event)7 Period (org.hisp.dhis.period.Period)7 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)6 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)6 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 DataValueAudit (org.hisp.dhis.datavalue.DataValueAudit)4 IdScheme (org.hisp.dhis.common.IdScheme)3 IdSchemes (org.hisp.dhis.common.IdSchemes)3 CompleteDataSetRegistration (org.hisp.dhis.dataset.CompleteDataSetRegistration)3