use of org.hisp.dhis.tracker.domain.DataValue in project dhis2-core by dhis2.
the class EventTrackerConverterServiceTest method fromForRuleEngineGivenExistingEventUpdatesValueOfExistingDataValueOnIdSchemeUID.
@Test
void fromForRuleEngineGivenExistingEventUpdatesValueOfExistingDataValueOnIdSchemeUID() {
setUpMocks();
DataElement dataElement = new DataElement();
dataElement.setUid(CodeGenerator.generateUid());
when(preheat.get(DataElement.class, dataElement.getUid())).thenReturn(dataElement);
ProgramStageInstance existingPsi = programStageInstance();
existingPsi.setEventDataValues(Set.of(eventDataValue(dataElement.getUid(), "658")));
// dataElement is of idScheme UID if the NTI dataElementIdScheme is set
// to UID
DataValue updatedValue = dataValue(dataElement.getUid(), "900");
Event event = event(existingPsi.getUid(), updatedValue);
when(preheat.getEvent(TrackerIdScheme.UID, event.getEvent())).thenReturn(existingPsi);
ProgramStageInstance programStageInstance = converter.fromForRuleEngine(preheat, event);
assertEquals(1, programStageInstance.getEventDataValues().size());
EventDataValue expect1 = new EventDataValue();
expect1.setDataElement(updatedValue.getDataElement());
expect1.setValue(updatedValue.getValue());
assertContainsOnly(programStageInstance.getEventDataValues(), expect1);
}
use of org.hisp.dhis.tracker.domain.DataValue in project dhis2-core by dhis2.
the class EventTrackerConverterServiceTest method fromForRuleEngineGivenExistingEventMergesNewDataValuesWithDBOnes.
@Test
void fromForRuleEngineGivenExistingEventMergesNewDataValuesWithDBOnes() {
setUpMocks();
ProgramStageInstance existingPsi = programStageInstance();
EventDataValue existingDataValue = eventDataValue(CodeGenerator.generateUid(), "658");
existingPsi.setEventDataValues(Set.of(existingDataValue));
DataElement dataElement = new DataElement();
dataElement.setUid(CodeGenerator.generateUid());
when(preheat.get(DataElement.class, dataElement.getUid())).thenReturn(dataElement);
// event refers to a different dataElement then currently associated
// with the event in the DB; thus both
// dataValues will be merged
DataValue newDataValue = dataValue(dataElement.getUid(), "900");
Event event = event(existingPsi.getUid(), newDataValue);
when(preheat.getEvent(TrackerIdScheme.UID, existingPsi.getUid())).thenReturn(existingPsi);
ProgramStageInstance programStageInstance = converter.fromForRuleEngine(preheat, event);
assertEquals(2, programStageInstance.getEventDataValues().size());
EventDataValue expect1 = new EventDataValue();
expect1.setDataElement(existingDataValue.getDataElement());
expect1.setValue(existingDataValue.getValue());
EventDataValue expect2 = new EventDataValue();
expect2.setDataElement(newDataValue.getDataElement());
expect2.setValue(newDataValue.getValue());
assertContainsOnly(programStageInstance.getEventDataValues(), expect1, expect2);
}
use of org.hisp.dhis.tracker.domain.DataValue in project dhis2-core by dhis2.
the class EventTrackerConverterService method to.
@Override
public List<Event> to(List<ProgramStageInstance> programStageInstances) {
List<Event> events = new ArrayList<>();
programStageInstances.forEach(psi -> {
Event event = new Event();
event.setEvent(psi.getUid());
event.setFollowup(BooleanUtils.toBoolean(psi.getProgramInstance().getFollowup()));
event.setStatus(psi.getStatus());
event.setOccurredAt(DateUtils.instantFromDate(psi.getExecutionDate()));
event.setScheduledAt(DateUtils.instantFromDate(psi.getDueDate()));
event.setStoredBy(psi.getStoredBy());
event.setCompletedBy(psi.getCompletedBy());
event.setCompletedAt(DateUtils.instantFromDate(psi.getCompletedDate()));
event.setCreatedAt(DateUtils.instantFromDate(psi.getCreated()));
event.setCreatedAtClient(DateUtils.instantFromDate(psi.getCreatedAtClient()));
event.setUpdatedAt(DateUtils.instantFromDate(psi.getLastUpdated()));
event.setUpdatedAtClient(DateUtils.instantFromDate(psi.getLastUpdatedAtClient()));
event.setGeometry(psi.getGeometry());
event.setDeleted(psi.isDeleted());
OrganisationUnit ou = psi.getOrganisationUnit();
if (ou != null) {
event.setOrgUnit(ou.getUid());
event.setOrgUnitName(ou.getName());
}
event.setEnrollment(psi.getProgramInstance().getUid());
event.setProgramStage(psi.getProgramStage().getUid());
event.setAttributeOptionCombo(psi.getAttributeOptionCombo().getUid());
event.setAttributeCategoryOptions(psi.getAttributeOptionCombo().getCategoryOptions().stream().map(CategoryOption::getUid).collect(Collectors.joining(";")));
Set<EventDataValue> dataValues = psi.getEventDataValues();
for (EventDataValue dataValue : dataValues) {
DataValue value = new DataValue();
value.setCreatedAt(DateUtils.instantFromDate(dataValue.getCreated()));
value.setUpdatedAt(DateUtils.instantFromDate(dataValue.getLastUpdated()));
value.setDataElement(dataValue.getDataElement());
value.setValue(dataValue.getValue());
value.setProvidedElsewhere(dataValue.getProvidedElsewhere());
value.setStoredBy(dataValue.getStoredBy());
value.setLastUpdatedBy(Optional.ofNullable(dataValue.getLastUpdatedByUserInfo()).map(UserInfoSnapshot::getUsername).orElse(""));
value.setCreatedBy(Optional.ofNullable(dataValue.getCreatedByUserInfo()).map(UserInfoSnapshot::getUsername).orElse(""));
event.getDataValues().add(value);
}
events.add(event);
});
return events;
}
use of org.hisp.dhis.tracker.domain.DataValue in project dhis2-core by dhis2.
the class EventPersister method handleDataValues.
private void handleDataValues(Session session, TrackerPreheat preheat, Set<DataValue> payloadDataValues, ProgramStageInstance psi) {
Map<String, EventDataValue> dataValueDBMap = psi.getEventDataValues().stream().collect(Collectors.toMap(EventDataValue::getDataElement, Function.identity()));
for (DataValue dv : payloadDataValues) {
DataElement dateElement = preheat.get(DataElement.class, dv.getDataElement());
checkNotNull(dateElement, "Data element should never be NULL here if validation is enforced before commit.");
EventDataValue eventDataValue = dataValueDBMap.getOrDefault(dv.getDataElement(), new EventDataValue());
eventDataValue.setDataElement(dateElement.getUid());
eventDataValue.setValue(dv.getValue());
eventDataValue.setStoredBy(dv.getStoredBy());
handleDataValueCreatedUpdatedDates(dv, eventDataValue);
if (StringUtils.isEmpty(eventDataValue.getValue())) {
if (dateElement.isFileType()) {
unassignFileResource(session, preheat, dataValueDBMap.get(dv.getDataElement()).getValue());
}
psi.getEventDataValues().remove(eventDataValue);
} else {
if (dateElement.isFileType()) {
assignFileResource(session, preheat, eventDataValue.getValue());
}
psi.getEventDataValues().add(eventDataValue);
}
}
}
use of org.hisp.dhis.tracker.domain.DataValue in project dhis2-core by dhis2.
the class EventDataValuesValidationHook method validateEvent.
@Override
public void validateEvent(ValidationErrorReporter reporter, Event event) {
TrackerImportValidationContext context = reporter.getValidationContext();
ProgramStage programStage = context.getProgramStage(event.getProgramStage());
checkNotNull(programStage, TrackerImporterAssertErrors.PROGRAM_STAGE_CANT_BE_NULL);
for (DataValue dataValue : event.getDataValues()) {
// event dates (createdAt, updatedAt) are ignored and set by the
// system
DataElement dataElement = context.getDataElement(dataValue.getDataElement());
if (dataElement == null) {
reporter.addError(event, TrackerErrorCode.E1304, dataValue.getDataElement());
continue;
}
validateDataElement(reporter, dataElement, dataValue, programStage, event);
if (dataValue.getValue() != null) {
validateOptionSet(reporter, event, dataElement, dataValue.getValue());
}
}
validateMandatoryDataValues(event, context, reporter);
validateDataValueDataElementIsConnectedToProgramStage(reporter, event, programStage);
}
Aggregations