Search in sources :

Example 16 with DataElement

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

the class DhisConvenienceTest method createDataElement.

/**
     * @param uniqueCharacter A unique character to identify the object.
     * @param valueType       The value type.
     * @param aggregationType The aggregation type.
     */
public static DataElement createDataElement(char uniqueCharacter, ValueType valueType, AggregationType aggregationType) {
    DataElement dataElement = createDataElement(uniqueCharacter);
    dataElement.setValueType(valueType);
    dataElement.setAggregationType(aggregationType);
    return dataElement;
}
Also used : ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement)

Example 17 with DataElement

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

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

the class GetMetaDataAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() {
    User user = currentUserService.getCurrentUser();
    Date lastUpdated = DateUtils.max(Sets.newHashSet(identifiableObjectManager.getLastUpdated(DataElement.class), identifiableObjectManager.getLastUpdated(OptionSet.class), identifiableObjectManager.getLastUpdated(Indicator.class), identifiableObjectManager.getLastUpdated(DataSet.class), identifiableObjectManager.getLastUpdated(DataElementCategoryCombo.class), identifiableObjectManager.getLastUpdated(DataElementCategory.class), identifiableObjectManager.getLastUpdated(DataElementCategoryOption.class)));
    String tag = lastUpdated != null && user != null ? (DateUtils.getLongDateString(lastUpdated) + SEP + user.getUid()) : null;
    if (ContextUtils.isNotModified(ServletActionContext.getRequest(), ServletActionContext.getResponse(), tag)) {
        return SUCCESS;
    }
    if (user != null && user.getOrganisationUnits().isEmpty()) {
        emptyOrganisationUnits = true;
        return SUCCESS;
    }
    significantZeros = dataElementService.getDataElementsByZeroIsSignificant(true);
    dataElements = dataElementService.getDataElementsWithDataSets();
    for (DataElement dataElement : dataElements) {
        if (dataElement != null && dataElement.getOptionSet() != null) {
            dataElementsWithOptionSet.add(dataElement);
        }
    }
    indicators = indicatorService.getIndicatorsWithDataSets();
    expressionService.substituteExpressions(indicators, null);
    dataSets = dataSetService.getCurrentUserDataSets();
    Set<DataElementCategoryCombo> categoryComboSet = new HashSet<>();
    Set<DataElementCategory> categorySet = new HashSet<>();
    for (DataSet dataSet : dataSets) {
        if (dataSet.getCategoryCombo() != null) {
            categoryComboSet.add(dataSet.getCategoryCombo());
        }
    }
    for (DataElementCategoryCombo categoryCombo : categoryComboSet) {
        if (categoryCombo.getCategories() != null) {
            categorySet.addAll(categoryCombo.getCategories());
        }
    }
    categoryCombos = new ArrayList<>(categoryComboSet);
    categories = new ArrayList<>(categorySet);
    for (DataElementCategory category : categories) {
        List<DataElementCategoryOption> categoryOptions = new ArrayList<>(categoryService.getDataElementCategoryOptions(category));
        Collections.sort(categoryOptions);
        categoryOptionMap.put(category.getUid(), categoryOptions);
    }
    Collections.sort(dataSets);
    Collections.sort(categoryCombos);
    Collections.sort(categories);
    defaultCategoryCombo = categoryService.getDefaultDataElementCategoryCombo();
    return SUCCESS;
}
Also used : User(org.hisp.dhis.user.User) DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) DataSet(org.hisp.dhis.dataset.DataSet) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) ArrayList(java.util.ArrayList) Date(java.util.Date) DataElement(org.hisp.dhis.dataelement.DataElement) DataElementCategoryOption(org.hisp.dhis.dataelement.DataElementCategoryOption) HashSet(java.util.HashSet)

Example 19 with DataElement

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

the class RemoveMinMaxLimitsAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(organisationUnitId);
    DataElement dataElement = dataElementService.getDataElement(dataElementId);
    DataElementCategoryOptionCombo optionCombo = categoryService.getDataElementCategoryOptionCombo(categoryOptionComboId);
    MinMaxDataElement minMaxDataElement = minMaxDataElementService.getMinMaxDataElement(organisationUnit, dataElement, optionCombo);
    if (minMaxDataElement != null) {
        minMaxDataElementService.deleteMinMaxDataElement(minMaxDataElement);
    }
    return SUCCESS;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) 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 20 with DataElement

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

the class AnalyticsUtils method getDimensionMetadataItemMap.

/**
     * Returns a mapping between identifiers and meta data items for the given query.
     *
     * @param params the data query parameters.
     * @return a mapping between identifiers and meta data items.
     */
public static Map<String, MetadataItem> getDimensionMetadataItemMap(DataQueryParams params) {
    List<DimensionalObject> dimensions = params.getDimensionsAndFilters();
    Map<String, MetadataItem> map = new HashMap<>();
    Calendar calendar = PeriodType.getCalendar();
    for (DimensionalObject dimension : dimensions) {
        for (DimensionalItemObject item : dimension.getItems()) {
            if (DimensionType.PERIOD == dimension.getDimensionType() && !calendar.isIso8601()) {
                Period period = (Period) item;
                DateTimeUnit dateTimeUnit = calendar.fromIso(period.getStartDate());
                map.put(period.getPeriodType().getIsoDate(dateTimeUnit), new MetadataItem(period.getDisplayName()));
            } else {
                String legendSet = item.hasLegendSet() ? item.getLegendSet().getUid() : null;
                map.put(item.getDimensionItem(), new MetadataItem(item.getDisplayProperty(params.getDisplayProperty()), legendSet));
            }
            if (DimensionType.ORGANISATION_UNIT == dimension.getDimensionType() && params.isHierarchyMeta()) {
                OrganisationUnit unit = (OrganisationUnit) item;
                for (OrganisationUnit ancestor : unit.getAncestors()) {
                    map.put(ancestor.getUid(), new MetadataItem(ancestor.getDisplayProperty(params.getDisplayProperty())));
                }
            }
            if (DimensionItemType.DATA_ELEMENT == item.getDimensionItemType()) {
                DataElement dataElement = (DataElement) item;
                for (DataElementCategoryOptionCombo coc : dataElement.getCategoryOptionCombos()) {
                    map.put(coc.getUid(), new MetadataItem(coc.getDisplayProperty(params.getDisplayProperty())));
                }
            }
        }
        map.put(dimension.getDimension(), new MetadataItem(dimension.getDisplayProperty(params.getDisplayProperty())));
    }
    return map;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Calendar(org.hisp.dhis.calendar.Calendar) Period(org.hisp.dhis.period.Period) DateUtils.getMediumDateString(org.hisp.dhis.system.util.DateUtils.getMediumDateString) DimensionalObject(org.hisp.dhis.common.DimensionalObject) DataElement(org.hisp.dhis.dataelement.DataElement) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

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