Search in sources :

Example 21 with OptionSet

use of org.hisp.dhis.option.OptionSet in project dhis2-core by dhis2.

the class DataValueSetImportValidatorTest method testValidateDataValueOptionsExist.

@Test
void testValidateDataValueOptionsExist() {
    DataValue dataValue = createRandomDataValue();
    DataValueContext valueContext = createDataValueContext(dataValue).build();
    valueContext.getDataElement().setOptionSet(new OptionSet());
    DataSetContext dataSetContext = createMinimalDataSetContext().build();
    ImportContext context = createMinimalImportContext(valueContext).build();
    assertTrue(validator.skipDataValue(dataValue, context, dataSetContext, valueContext));
    assertConflict(ErrorCode.E7621, "Data value is not a valid option of the data element option set: `<object1>`", context, dataValue.getDataElement());
}
Also used : DataValue(org.hisp.dhis.dxf2.datavalue.DataValue) DataSetContext(org.hisp.dhis.dxf2.datavalueset.ImportContext.DataSetContext) OptionSet(org.hisp.dhis.option.OptionSet) DataValueContext(org.hisp.dhis.dxf2.datavalueset.ImportContext.DataValueContext) Test(org.junit.jupiter.api.Test)

Example 22 with OptionSet

use of org.hisp.dhis.option.OptionSet in project dhis2-core by dhis2.

the class TrackedEntityAttributeServiceTest method successWhenTeaOptionValueIsValid.

@Test
void successWhenTeaOptionValueIsValid() {
    tea.setUid("uid");
    OptionSet optionSet = new OptionSet();
    Option option = new Option();
    option.setCode("CODE");
    Option option1 = new Option();
    option1.setCode("CODE1");
    optionSet.setOptions(Arrays.asList(null, option, option1));
    tea.setOptionSet(optionSet);
    assertNull(trackedEntityAttributeService.validateValueType(tea, "CODE"));
}
Also used : Option(org.hisp.dhis.option.Option) OptionSet(org.hisp.dhis.option.OptionSet) Test(org.junit.jupiter.api.Test)

Example 23 with OptionSet

use of org.hisp.dhis.option.OptionSet in project dhis2-core by dhis2.

the class DefaultCsvImportService method setOptionSetsFromCsv.

/**
 * Option set format:
 * <p>
 * <ul>
 * <li>option set name</li>
 * <li>option set uid</li>
 * <li>option set code</li>
 * <li>option name</li>
 * <li>option uid</li>
 * <li>option code</li>
 * </ul>
 */
private void setOptionSetsFromCsv(CsvReader reader, Metadata metadata) throws IOException {
    ListMap<String, Option> nameOptionMap = new ListMap<>();
    Map<String, OptionSet> nameOptionSetMap = new HashMap<>();
    List<Option> options = new ArrayList<>();
    while (reader.readRecord()) {
        String[] values = reader.getValues();
        if (values != null && values.length > 0) {
            OptionSet optionSet = new OptionSet();
            setIdentifiableObject(optionSet, values);
            optionSet.setAutoFields();
            optionSet.setValueType(ValueType.TEXT);
            Option option = new Option();
            option.setName(getSafe(values, 3, 230));
            option.setUid(getSafe(values, 4, CodeGenerator.generateUid(), 11));
            option.setCode(getSafe(values, 5, 50));
            option.setAutoFields();
            if (optionSet.getName() == null || option.getCode() == null) {
                continue;
            }
            nameOptionSetMap.put(optionSet.getName(), optionSet);
            nameOptionMap.putValue(optionSet.getName(), option);
            options.add(option);
        }
    }
    for (Entry<String, OptionSet> optionSetEntry : nameOptionSetMap.entrySet()) {
        optionSetEntry.getValue().setOptions(new ArrayList<>(nameOptionMap.get(optionSetEntry.getKey())));
    }
    metadata.setOptions(options);
    metadata.setOptionSets(new ArrayList<>(nameOptionSetMap.values()));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CategoryOption(org.hisp.dhis.category.CategoryOption) Option(org.hisp.dhis.option.Option) OptionSet(org.hisp.dhis.option.OptionSet) ListMap(org.hisp.dhis.common.ListMap)

Example 24 with OptionSet

use of org.hisp.dhis.option.OptionSet in project dhis2-core by dhis2.

the class DefaultCsvImportService method setOptionGroupSetFromCsv.

/**
 * Option group set format:
 * <p>
 * <ul>
 * <li>option group set name</li>
 * <li>option group set uid</li>
 * <li>option group set code</li>
 * <li>option group set description</li>
 * <li>data dimension</li>
 * <li>option set uid</li>
 * <li>option set code</li>
 * </ul>
 */
private List<OptionGroupSet> setOptionGroupSetFromCsv(CsvReader reader) throws IOException {
    List<OptionGroupSet> optionGroupSets = new ArrayList<>();
    Map<String, OptionSet> mapOptionSet = new HashMap<>();
    while (reader.readRecord()) {
        String[] values = reader.getValues();
        if (values != null && values.length > 0) {
            OptionGroupSet optionGroupSet = new OptionGroupSet();
            optionGroupSet.setAutoFields();
            setIdentifiableObject(optionGroupSet, values);
            optionGroupSet.setDescription(getSafe(values, 4));
            optionGroupSet.setDataDimension(// boolean
            Boolean.parseBoolean(getSafe(values, 3, Boolean.FALSE.toString(), 40)));
            OptionSet optionSet = new OptionSet();
            optionSet.setUid(getSafe(values, 5, 11));
            optionSet.setCode(getSafe(values, 6, 50));
            if (optionSet.getUid() == null && optionSet.getCode() == null) {
                continue;
            }
            OptionSet persistedOptionSet = optionSet.getUid() != null ? mapOptionSet.computeIfAbsent(optionSet.getUid(), key -> optionService.getOptionSet(optionSet.getUid())) : mapOptionSet.computeIfAbsent(optionSet.getCode(), key -> optionService.getOptionSetByCode(optionSet.getCode()));
            if (persistedOptionSet == null) {
                continue;
            }
            optionGroupSet.setOptionSet(optionSet);
            optionGroupSets.add(optionGroupSet);
        }
    }
    return optionGroupSets;
}
Also used : OptionGroupSet(org.hisp.dhis.option.OptionGroupSet) CategoryService(org.hisp.dhis.category.CategoryService) Importance(org.hisp.dhis.validation.Importance) CategoryOption(org.hisp.dhis.category.CategoryOption) ValueType(org.hisp.dhis.common.ValueType) StringUtils(org.apache.commons.lang3.StringUtils) OptionGroup(org.hisp.dhis.option.OptionGroup) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) Metadata(org.hisp.dhis.dxf2.metadata.Metadata) Map(java.util.Map) IndicatorGroup(org.hisp.dhis.indicator.IndicatorGroup) OrganisationUnitGroupService(org.hisp.dhis.organisationunit.OrganisationUnitGroupService) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) OptionGroupSet(org.hisp.dhis.option.OptionGroupSet) Category(org.hisp.dhis.category.Category) Operator(org.hisp.dhis.expression.Operator) DataElementGroupService(org.hisp.dhis.dataelement.DataElementGroupService) List(java.util.List) IndicatorGroupService(org.hisp.dhis.indicator.IndicatorGroupService) Entry(java.util.Map.Entry) CsvReader(com.csvreader.CsvReader) Optional(java.util.Optional) CategoryCombo(org.hisp.dhis.category.CategoryCombo) DataDimensionType(org.hisp.dhis.common.DataDimensionType) ListMap(org.hisp.dhis.common.ListMap) DateUtils.getMediumDate(org.hisp.dhis.util.DateUtils.getMediumDate) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DataElement(org.hisp.dhis.dataelement.DataElement) MissingValueStrategy(org.hisp.dhis.expression.MissingValueStrategy) Service(org.springframework.stereotype.Service) Indicator(org.hisp.dhis.indicator.Indicator) CategoryOptionGroup(org.hisp.dhis.category.CategoryOptionGroup) OptionService(org.hisp.dhis.option.OptionService) CsvUtils(org.hisp.dhis.system.util.CsvUtils) DataElementDomain(org.hisp.dhis.dataelement.DataElementDomain) AggregationType(org.hisp.dhis.analytics.AggregationType) IOException(java.io.IOException) ValidationRule(org.hisp.dhis.validation.ValidationRule) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Option(org.hisp.dhis.option.Option) FeatureType(org.hisp.dhis.organisationunit.FeatureType) OptionSet(org.hisp.dhis.option.OptionSet) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) PeriodType(org.hisp.dhis.period.PeriodType) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Expression(org.hisp.dhis.expression.Expression) AllArgsConstructor(lombok.AllArgsConstructor) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) InputStream(java.io.InputStream) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OptionSet(org.hisp.dhis.option.OptionSet)

Example 25 with OptionSet

use of org.hisp.dhis.option.OptionSet in project dhis2-core by dhis2.

the class OptionObjectBundleHook method validate.

@Override
public void validate(Option option, ObjectBundle bundle, Consumer<ErrorReport> addReports) {
    if (option.getOptionSet() != null) {
        OptionSet optionSet = bundle.getPreheat().get(bundle.getPreheatIdentifier(), OptionSet.class, option.getOptionSet());
        checkDuplicateOption(optionSet, option, addReports);
    }
}
Also used : OptionSet(org.hisp.dhis.option.OptionSet)

Aggregations

OptionSet (org.hisp.dhis.option.OptionSet)40 Option (org.hisp.dhis.option.Option)24 Test (org.junit.jupiter.api.Test)20 DataElement (org.hisp.dhis.dataelement.DataElement)12 List (java.util.List)10 ArrayList (java.util.ArrayList)8 DhisSpringTest (org.hisp.dhis.DhisSpringTest)6 CategoryCombo (org.hisp.dhis.category.CategoryCombo)6 OptionGroup (org.hisp.dhis.option.OptionGroup)6 OptionGroupSet (org.hisp.dhis.option.OptionGroupSet)6 HashSet (java.util.HashSet)5 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)5 ObjectBundle (org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle)5 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 User (org.hisp.dhis.user.User)5 ClassPathResource (org.springframework.core.io.ClassPathResource)5 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 Map (java.util.Map)4 CategoryOption (org.hisp.dhis.category.CategoryOption)4