Search in sources :

Example 41 with DataValue

use of org.hisp.dhis.tracker.domain.DataValue 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 42 with DataValue

use of org.hisp.dhis.tracker.domain.DataValue in project dhis2-core by dhis2.

the class EventDataValuesValidationHookTest method failValidationWhenFileResourceIsAlreadyAssigned.

@Test
void failValidationWhenFileResourceIsAlreadyAssigned() {
    setUpIdentifiers();
    DataElement validDataElement = dataElement(ValueType.FILE_RESOURCE);
    when(context.getDataElement(dataElementUid)).thenReturn(validDataElement);
    ProgramStage programStage = programStage(validDataElement);
    when(context.getProgramStage(programStageUid)).thenReturn(programStage);
    ValidationErrorReporter reporter = new ValidationErrorReporter(context);
    FileResource fileResource = new FileResource();
    fileResource.setAssigned(true);
    DataValue validDataValue = dataValue("QX4LpiTZmUH");
    when(context.getFileResource(validDataValue.getValue())).thenReturn(fileResource);
    Event event = Event.builder().programStage(programStage.getUid()).status(EventStatus.SKIPPED).dataValues(Set.of(validDataValue)).build();
    hook.validateEvent(reporter, event);
    assertThat(reporter.getReportList(), hasSize(1));
    assertEquals(TrackerErrorCode.E1009, reporter.getReportList().get(0).getErrorCode());
}
Also used : ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) DataValue(org.hisp.dhis.tracker.domain.DataValue) FileResource(org.hisp.dhis.fileresource.FileResource) Event(org.hisp.dhis.tracker.domain.Event) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) ProgramStage(org.hisp.dhis.program.ProgramStage) Test(org.junit.jupiter.api.Test)

Example 43 with DataValue

use of org.hisp.dhis.tracker.domain.DataValue in project dhis2-core by dhis2.

the class EventDataValuesValidationHookTest method runAndAssertValidationForDataValue.

private void runAndAssertValidationForDataValue(ValueType valueType, String value) {
    setUpIdentifiers();
    DataElement invalidDataElement = dataElement(valueType);
    when(context.getDataElement(dataElementUid)).thenReturn(invalidDataElement);
    ProgramStage programStage = programStage(dataElement());
    when(context.getProgramStage(programStageUid)).thenReturn(programStage);
    ValidationErrorReporter reporter = new ValidationErrorReporter(context);
    DataValue validDataValue = dataValue();
    validDataValue.setDataElement(dataElementUid);
    validDataValue.setValue(value);
    Event event = Event.builder().programStage(programStage.getUid()).status(EventStatus.SKIPPED).dataValues(Set.of(validDataValue)).build();
    hook.validateEvent(reporter, event);
    assertThat(reporter.getReportList(), hasSize(1));
    assertEquals(TrackerErrorCode.E1302, reporter.getReportList().get(0).getErrorCode());
}
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) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) ProgramStage(org.hisp.dhis.program.ProgramStage)

Example 44 with DataValue

use of org.hisp.dhis.tracker.domain.DataValue in project dhis2-core by dhis2.

the class TrackerCsvEventService method readEvents.

@Override
public List<Event> readEvents(InputStream inputStream, boolean skipFirst) throws IOException, ParseException {
    final CsvSchema csvSchema = CSV_MAPPER.schemaFor(CsvEventDataValue.class).withUseHeader(skipFirst).withColumnReordering(true);
    List<Event> events = Lists.newArrayList();
    ObjectReader reader = CSV_MAPPER.readerFor(CsvEventDataValue.class).with(csvSchema);
    MappingIterator<CsvEventDataValue> iterator = reader.readValues(inputStream);
    Event event = new Event();
    while (iterator.hasNext()) {
        CsvEventDataValue dataValue = iterator.next();
        if (!Objects.equals(event.getEvent(), dataValue.getEvent())) {
            event = new Event();
            event.setEvent(dataValue.getEvent());
            event.setStatus(StringUtils.isEmpty(dataValue.getStatus()) ? EventStatus.ACTIVE : Enum.valueOf(EventStatus.class, dataValue.getStatus()));
            event.setProgram(dataValue.getProgram());
            event.setProgramStage(dataValue.getProgramStage());
            event.setEnrollment(dataValue.getEnrollment());
            event.setOrgUnit(dataValue.getOrgUnit());
            event.setCreatedAt(DateUtils.instantFromDateAsString(dataValue.getCreatedAt()));
            event.setCreatedAtClient(DateUtils.instantFromDateAsString(dataValue.getCreatedAtClient()));
            event.setUpdatedAt(DateUtils.instantFromDateAsString(dataValue.getUpdatedAt()));
            event.setUpdatedAtClient(DateUtils.instantFromDateAsString(dataValue.getUpdatedAtClient()));
            event.setOccurredAt(DateUtils.instantFromDateAsString(dataValue.getOccurredAt()));
            event.setScheduledAt(DateUtils.instantFromDateAsString(dataValue.getScheduledAt()));
            event.setCompletedAt(DateUtils.instantFromDateAsString(dataValue.getCompletedAt()));
            event.setCompletedBy(dataValue.getCompletedBy());
            event.setStoredBy(dataValue.getStoredBy());
            event.setAttributeOptionCombo(dataValue.getAttributeOptionCombo());
            event.setAttributeCategoryOptions(dataValue.getAttributeCategoryOptions());
            event.setAssignedUser(dataValue.getAssignedUser());
            if (dataValue.getGeometry() != null) {
                event.setGeometry(new WKTReader().read(dataValue.getGeometry()));
            } else if (dataValue.getLongitude() != null && dataValue.getLatitude() != null) {
                event.setGeometry(new WKTReader().read("Point(" + dataValue.getLongitude() + " " + dataValue.getLatitude() + ")"));
            }
            events.add(event);
        }
        if (ObjectUtils.anyNotNull(dataValue.getProvidedElsewhere(), dataValue.getDataElement(), dataValue.getValue(), dataValue.getCreatedAtDataValue(), dataValue.getUpdatedAtDataValue(), dataValue.getStoredByDataValue())) {
            DataValue value = new DataValue();
            value.setProvidedElsewhere(dataValue.getProvidedElsewhere() != null && dataValue.getProvidedElsewhere());
            value.setDataElement(dataValue.getDataElement());
            value.setValue(dataValue.getValue());
            value.setCreatedAt(DateUtils.instantFromDateAsString(dataValue.getCreatedAtDataValue()));
            value.setUpdatedAt(DateUtils.instantFromDateAsString(dataValue.getUpdatedAtDataValue()));
            value.setStoredBy(dataValue.getStoredByDataValue());
            event.getDataValues().add(value);
        }
    }
    return events;
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) DataValue(org.hisp.dhis.tracker.domain.DataValue) Event(org.hisp.dhis.tracker.domain.Event) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) WKTReader(org.locationtech.jts.io.WKTReader)

Example 45 with DataValue

use of org.hisp.dhis.tracker.domain.DataValue in project dhis2-core by dhis2.

the class TrackerCsvEventService method writeEvents.

@Override
public void writeEvents(OutputStream outputStream, List<Event> events, boolean withHeader) throws IOException {
    final CsvSchema csvSchema = CSV_MAPPER.schemaFor(CsvEventDataValue.class).withLineSeparator("\n").withUseHeader(withHeader);
    ObjectWriter writer = CSV_MAPPER.writer(csvSchema.withUseHeader(withHeader));
    List<CsvEventDataValue> dataValues = new ArrayList<>();
    for (Event event : events) {
        CsvEventDataValue templateDataValue = new CsvEventDataValue();
        templateDataValue.setEvent(event.getEvent());
        templateDataValue.setStatus(event.getStatus() != null ? event.getStatus().name() : null);
        templateDataValue.setProgram(event.getProgram());
        templateDataValue.setProgramStage(event.getProgramStage());
        templateDataValue.setEnrollment(event.getEnrollment());
        templateDataValue.setOrgUnit(event.getOrgUnit());
        templateDataValue.setOrgUnitName(event.getOrgUnitName());
        templateDataValue.setOccurredAt(event.getOccurredAt() == null ? null : event.getOccurredAt().toString());
        templateDataValue.setScheduledAt(event.getScheduledAt() == null ? null : event.getScheduledAt().toString());
        templateDataValue.setFollowup(event.isFollowup());
        templateDataValue.setDeleted(event.isDeleted());
        templateDataValue.setCreatedAt(event.getCreatedAt() == null ? null : event.getCreatedAt().toString());
        templateDataValue.setCreatedAtClient(event.getCreatedAtClient() == null ? null : event.getCreatedAtClient().toString());
        templateDataValue.setUpdatedAt(event.getUpdatedAt() == null ? null : event.getUpdatedAt().toString());
        templateDataValue.setUpdatedAtClient(event.getUpdatedAtClient() == null ? null : event.getUpdatedAtClient().toString());
        templateDataValue.setCompletedBy(event.getCompletedBy());
        templateDataValue.setCompletedAt(event.getCompletedAt() == null ? null : event.getCompletedAt().toString());
        templateDataValue.setUpdatedBy(event.getUpdatedBy());
        templateDataValue.setStoredBy(event.getStoredBy());
        templateDataValue.setCompletedAt(event.getCompletedAt() == null ? null : event.getCompletedAt().toString());
        templateDataValue.setCompletedBy(event.getCompletedBy());
        templateDataValue.setAttributeOptionCombo(event.getAttributeOptionCombo());
        templateDataValue.setAttributeCategoryOptions(event.getAttributeCategoryOptions());
        templateDataValue.setAssignedUser(event.getAssignedUser());
        if (event.getGeometry() != null) {
            templateDataValue.setGeometry(event.getGeometry().toText());
            if (event.getGeometry().getGeometryType().equals("Point")) {
                templateDataValue.setLongitude(event.getGeometry().getCoordinate().x);
                templateDataValue.setLatitude(event.getGeometry().getCoordinate().y);
            }
        }
        for (DataValue value : event.getDataValues()) {
            CsvEventDataValue dataValue = new CsvEventDataValue(templateDataValue);
            dataValue.setDataElement(value.getDataElement());
            dataValue.setValue(value.getValue());
            dataValue.setProvidedElsewhere(value.isProvidedElsewhere());
            dataValue.setCreatedAtDataValue(value.getCreatedAt() == null ? null : value.getCreatedAt().toString());
            dataValue.setUpdatedAtDataValue(value.getUpdatedAt() == null ? null : value.getUpdatedAt().toString());
            if (value.getStoredBy() != null) {
                dataValue.setStoredBy(value.getStoredBy());
            }
            dataValues.add(dataValue);
        }
    }
    writer.writeValue(outputStream, dataValues);
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) DataValue(org.hisp.dhis.tracker.domain.DataValue) ArrayList(java.util.ArrayList) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Event(org.hisp.dhis.tracker.domain.Event)

Aggregations

DataValue (org.hisp.dhis.tracker.domain.DataValue)45 Event (org.hisp.dhis.tracker.domain.Event)35 DataElement (org.hisp.dhis.dataelement.DataElement)33 Test (org.junit.jupiter.api.Test)29 ProgramStage (org.hisp.dhis.program.ProgramStage)26 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)25 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)19 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)10 EventDataValue (org.hisp.dhis.eventdatavalue.EventDataValue)9 Lists (com.google.common.collect.Lists)6 Sets (com.google.common.collect.Sets)6 List (java.util.List)6 Map (java.util.Map)6 Optional (java.util.Optional)6 Set (java.util.Set)6 ValueType (org.hisp.dhis.common.ValueType)6 EventStatus (org.hisp.dhis.event.EventStatus)6 RuleActionAssign (org.hisp.dhis.rules.models.RuleActionAssign)6 SettingKey (org.hisp.dhis.setting.SettingKey)6 SystemSettingManager (org.hisp.dhis.setting.SystemSettingManager)6