Search in sources :

Example 36 with Option

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

the class DefaultEventAnalyticsService method addEventReportDimensionalItems.

/**
 * Adds dimensional items to the given list of objects. Send in a list of
 * {@link EventAnalyticsDimensionalItem} and add properties from
 * {@link ValueTypedDimensionalItemObject} parameter.
 *
 * @param eventDimensionalItemObject the
 *        {@link ValueTypedDimensionalItemObject} object to get properties
 *        from.
 * @param objects the list of {@link EventAnalyticsDimensionalItem} objects.
 * @param grid the {@link Grid} from the event analytics request.
 * @param dimension the dimension identifier.
 */
@SuppressWarnings("unchecked")
private void addEventReportDimensionalItems(ValueTypedDimensionalItemObject eventDimensionalItemObject, List<EventAnalyticsDimensionalItem> objects, Grid grid, String dimension) {
    Preconditions.checkNotNull(eventDimensionalItemObject, String.format("Data dimension '%s' is invalid", dimension));
    String parentUid = eventDimensionalItemObject.getUid();
    if (eventDimensionalItemObject.getValueType() == BOOLEAN) {
        objects.add(new EventAnalyticsDimensionalItem(OPT_TRUE, parentUid));
        objects.add(new EventAnalyticsDimensionalItem(OPT_FALSE, parentUid));
    }
    if (eventDimensionalItemObject.hasOptionSet()) {
        for (Option option : eventDimensionalItemObject.getOptionSet().getOptions()) {
            objects.add(new EventAnalyticsDimensionalItem(option, parentUid));
        }
    } else if (eventDimensionalItemObject.hasLegendSet()) {
        List<String> legendOptions = (List<String>) ((Map<String, Object>) grid.getMetaData().get(DIMENSIONS.getKey())).get(dimension);
        if (legendOptions.isEmpty()) {
            List<Legend> legends = eventDimensionalItemObject.getLegendSet().getSortedLegends();
            for (Legend legend : legends) {
                for (int i = legend.getStartValue().intValue(); i < legend.getEndValue().intValue(); i++) {
                    objects.add(new EventAnalyticsDimensionalItem(new Option(String.valueOf(i), String.valueOf(i)), parentUid));
                }
            }
        } else {
            for (String legend : legendOptions) {
                MetadataItem metadataItem = (MetadataItem) ((Map<String, Object>) grid.getMetaData().get(ITEMS.getKey())).get(legend);
                objects.add(new EventAnalyticsDimensionalItem(new Option(metadataItem.getName(), legend), parentUid));
            }
        }
    }
}
Also used : EventAnalyticsDimensionalItem(org.hisp.dhis.analytics.EventAnalyticsDimensionalItem) Legend(org.hisp.dhis.legend.Legend) Option(org.hisp.dhis.option.Option) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MetadataItem(org.hisp.dhis.common.MetadataItem)

Example 37 with Option

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

the class EventDataValuesValidationHookTest method successValidationDataElementOptionValueIsValid.

@Test
void successValidationDataElementOptionValueIsValid() {
    setUpIdentifiers();
    DataValue validDataValue = dataValue("code");
    DataValue nullDataValue = dataValue(null);
    OptionSet optionSet = new OptionSet();
    Option option = new Option();
    option.setCode("CODE");
    Option option1 = new Option();
    option1.setCode("CODE1");
    optionSet.setOptions(Arrays.asList(option, option1));
    DataElement dataElement = dataElement();
    dataElement.setOptionSet(optionSet);
    when(context.getDataElement(dataElementUid)).thenReturn(dataElement);
    ProgramStage programStage = programStage(dataElement);
    when(context.getProgramStage(programStageUid)).thenReturn(programStage);
    ValidationErrorReporter reporter = new ValidationErrorReporter(context);
    Event event = Event.builder().programStage(programStage.getUid()).status(EventStatus.SKIPPED).dataValues(Set.of(validDataValue, nullDataValue)).build();
    hook.validateEvent(reporter, event);
    assertFalse(reporter.hasErrors());
}
Also used : ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) DataValue(org.hisp.dhis.tracker.domain.DataValue) Event(org.hisp.dhis.tracker.domain.Event) Option(org.hisp.dhis.option.Option) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) OptionSet(org.hisp.dhis.option.OptionSet) ProgramStage(org.hisp.dhis.program.ProgramStage) Test(org.junit.jupiter.api.Test)

Example 38 with Option

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

the class EventDataValuesValidationHookTest method failValidationDataElementOptionValueIsInValid.

@Test
void failValidationDataElementOptionValueIsInValid() {
    setUpIdentifiers();
    DataValue validDataValue = dataValue("value");
    validDataValue.setDataElement(dataElementUid);
    OptionSet optionSet = new OptionSet();
    Option option = new Option();
    option.setCode("CODE");
    Option option1 = new Option();
    option1.setCode("CODE1");
    optionSet.setOptions(Arrays.asList(option, option1));
    DataElement dataElement = dataElement();
    dataElement.setOptionSet(optionSet);
    when(context.getDataElement(dataElementUid)).thenReturn(dataElement);
    ProgramStage programStage = programStage(dataElement);
    when(context.getProgramStage(programStageUid)).thenReturn(programStage);
    ValidationErrorReporter reporter = new ValidationErrorReporter(context);
    Event event = Event.builder().programStage(programStage.getUid()).status(EventStatus.SKIPPED).dataValues(Set.of(validDataValue)).build();
    hook.validateEvent(reporter, event);
    assertTrue(reporter.hasErrors());
    assertThat(reporter.getReportList(), hasSize(1));
    assertEquals(1, reporter.getReportList().stream().filter(e -> e.getErrorCode() == TrackerErrorCode.E1125).count());
}
Also used : ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) DataValue(org.hisp.dhis.tracker.domain.DataValue) Event(org.hisp.dhis.tracker.domain.Event) Option(org.hisp.dhis.option.Option) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) OptionSet(org.hisp.dhis.option.OptionSet) ProgramStage(org.hisp.dhis.program.ProgramStage) Test(org.junit.jupiter.api.Test)

Example 39 with Option

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

the class TrackedEntityAttributeValidationHookTest method getTrackedEntityAttributeWithOptionSet.

private TrackedEntityAttribute getTrackedEntityAttributeWithOptionSet() {
    TrackedEntityAttribute trackedEntityAttribute = new TrackedEntityAttribute();
    trackedEntityAttribute.setUid("uid");
    trackedEntityAttribute.setValueType(ValueType.TEXT);
    OptionSet optionSet = new OptionSet();
    Option option = new Option();
    option.setCode("CODE");
    Option option1 = new Option();
    option1.setCode("CODE1");
    optionSet.setOptions(Arrays.asList(option, option1));
    trackedEntityAttribute.setOptionSet(optionSet);
    return trackedEntityAttribute;
}
Also used : TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) Option(org.hisp.dhis.option.Option) OptionSet(org.hisp.dhis.option.OptionSet)

Example 40 with Option

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

the class DhisConvenienceTest method createOptionSet.

public static OptionSet createOptionSet(char uniqueCharacter, Option... options) {
    OptionSet optionSet = createOptionSet(uniqueCharacter);
    for (Option option : options) {
        optionSet.getOptions().add(option);
        option.setOptionSet(optionSet);
    }
    return optionSet;
}
Also used : CategoryOption(org.hisp.dhis.category.CategoryOption) Option(org.hisp.dhis.option.Option) OptionSet(org.hisp.dhis.option.OptionSet)

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