Search in sources :

Example 21 with Option

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

the class JdbcEventAnalyticsManager method getCollapsedDataItemValue.

/**
     * Returns an item value for the given query, query item and value. Assumes that
     * data dimensions are collapsed for the given query. Returns the short name
     * of the given query item followed by the item value. If the given query item
     * has a legend set, the item value is treated as an id and substituted with
     * the matching legend name. If the given query item has an option set, the 
     * item value is treated as a code and substituted with the matching option 
     * name.
     * 
     * @param params the {@link EventQueryParams}..
     * @param item the {@link QueryItem}.
     * @param itemValue the item value.
     */
private String getCollapsedDataItemValue(EventQueryParams params, QueryItem item, String itemValue) {
    String value = item.getItem().getDisplayShortName() + ITEM_NAME_SEP;
    Legend legend = null;
    Option option = null;
    if (item.hasLegendSet() && (legend = item.getLegendSet().getLegendByUid(itemValue)) != null) {
        return value + legend.getDisplayName();
    } else if (item.hasOptionSet() && (option = item.getOptionSet().getOptionByCode(itemValue)) != null) {
        return value + option.getDisplayName();
    } else {
        itemValue = StringUtils.defaultString(itemValue, NA);
        return value + itemValue;
    }
}
Also used : Legend(org.hisp.dhis.legend.Legend) Option(org.hisp.dhis.option.Option) DateUtils.getMediumDateString(org.hisp.dhis.system.util.DateUtils.getMediumDateString)

Example 22 with Option

use of org.hisp.dhis.option.Option 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 Option

use of org.hisp.dhis.option.Option 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 Option

use of org.hisp.dhis.option.Option 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 Option

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

the class VersionedObjectObjectBundleHook method postCreate.

@Override
public void postCreate(IdentifiableObject persistedObject, ObjectBundle bundle) {
    VersionedObject versionedObject = null;
    if (persistedObject instanceof Section) {
        versionedObject = ((Section) persistedObject).getDataSet();
    } else if (persistedObject instanceof Option) {
        versionedObject = ((Option) persistedObject).getOptionSet();
    }
    if (versionedObject != null) {
        versionedObject.increaseVersion();
        sessionFactory.getCurrentSession().save(versionedObject);
    }
}
Also used : VersionedObject(org.hisp.dhis.common.VersionedObject) Option(org.hisp.dhis.option.Option) Section(org.hisp.dhis.dataset.Section)

Aggregations

Option (org.hisp.dhis.option.Option)40 OptionSet (org.hisp.dhis.option.OptionSet)23 Test (org.junit.jupiter.api.Test)16 List (java.util.List)11 DataElement (org.hisp.dhis.dataelement.DataElement)10 HashMap (java.util.HashMap)8 Legend (org.hisp.dhis.legend.Legend)8 ArrayList (java.util.ArrayList)7 Map (java.util.Map)6 CategoryOption (org.hisp.dhis.category.CategoryOption)6 HashSet (java.util.HashSet)5 ObjectBundle (org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle)5 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 VersionedObject (org.hisp.dhis.common.VersionedObject)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 DhisSpringTest (org.hisp.dhis.DhisSpringTest)3 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)3 OptionGroup (org.hisp.dhis.option.OptionGroup)3 OptionGroupSet (org.hisp.dhis.option.OptionGroupSet)3