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());
}
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());
}
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());
}
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;
}
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);
}
Aggregations