Search in sources :

Example 16 with DataValue

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);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DataValue(org.hisp.dhis.tracker.domain.DataValue) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) Event(org.hisp.dhis.tracker.domain.Event) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 17 with DataValue

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);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DataValue(org.hisp.dhis.tracker.domain.DataValue) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) Event(org.hisp.dhis.tracker.domain.Event) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 18 with DataValue

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;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataValue(org.hisp.dhis.tracker.domain.DataValue) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) UserInfoSnapshot(org.hisp.dhis.program.UserInfoSnapshot) ArrayList(java.util.ArrayList) Event(org.hisp.dhis.tracker.domain.Event) CategoryOption(org.hisp.dhis.category.CategoryOption) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue)

Example 19 with DataValue

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);
        }
    }
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DataValue(org.hisp.dhis.tracker.domain.DataValue) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue)

Example 20 with DataValue

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);
}
Also used : ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) DataValue(org.hisp.dhis.tracker.domain.DataValue) ValidationUtils.validateMandatoryDataValue(org.hisp.dhis.tracker.validation.hooks.ValidationUtils.validateMandatoryDataValue) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) ProgramStage(org.hisp.dhis.program.ProgramStage)

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