Search in sources :

Example 1 with DataValue

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

the class AssignValueImplementerTest method testAssignDataElementValueForEventsWhenDataElementIsAlreadyPresent.

@Test
void testAssignDataElementValueForEventsWhenDataElementIsAlreadyPresent() {
    List<Event> events = Lists.newArrayList(getEventWithDataValueSet());
    bundle.setEvents(events);
    bundle.setRuleEffects(getRuleEventEffects(events));
    Map<String, List<ProgramRuleIssue>> eventIssues = implementerToTest.validateEvents(bundle);
    Event event = bundle.getEvents().stream().filter(e -> e.getEvent().equals(FIRST_EVENT_ID)).findAny().get();
    Optional<DataValue> newDataValue = event.getDataValues().stream().filter(dv -> dv.getDataElement().equals(dataElementA.getUid())).findAny();
    assertTrue(newDataValue.isPresent());
    assertEquals(DATA_ELEMENT_OLD_VALUE, newDataValue.get().getValue());
    assertEquals(1, eventIssues.size());
    assertEquals(1, eventIssues.get(FIRST_EVENT_ID).size());
    assertEquals(ERROR, eventIssues.get(FIRST_EVENT_ID).get(0).getIssueType());
}
Also used : Strictness(org.mockito.quality.Strictness) WARNING(org.hisp.dhis.tracker.programrule.IssueType.WARNING) DataValue(org.hisp.dhis.tracker.domain.DataValue) BeforeEach(org.junit.jupiter.api.BeforeEach) TRACKED_ENTITY_ATTRIBUTE(org.hisp.dhis.rules.models.AttributeType.TRACKED_ENTITY_ATTRIBUTE) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) RuleEffects(org.hisp.dhis.rules.models.RuleEffects) ERROR(org.hisp.dhis.tracker.programrule.IssueType.ERROR) RuleActionAssign(org.hisp.dhis.rules.models.RuleActionAssign) ValueType(org.hisp.dhis.common.ValueType) Attribute(org.hisp.dhis.tracker.domain.Attribute) Mock(org.mockito.Mock) RuleEffect(org.hisp.dhis.rules.models.RuleEffect) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) ENROLLMENT(org.hisp.dhis.rules.models.TrackerObjectType.ENROLLMENT) DataElement(org.hisp.dhis.dataelement.DataElement) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Lists(com.google.common.collect.Lists) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) DATA_ELEMENT(org.hisp.dhis.rules.models.AttributeType.DATA_ELEMENT) Map(java.util.Map) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) AssignValueImplementer(org.hisp.dhis.tracker.programrule.implementers.AssignValueImplementer) InjectMocks(org.mockito.InjectMocks) Event(org.hisp.dhis.tracker.domain.Event) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) EVENT(org.hisp.dhis.rules.models.TrackerObjectType.EVENT) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Set(java.util.Set) EventStatus(org.hisp.dhis.event.EventStatus) Mockito.when(org.mockito.Mockito.when) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) ProgramStage(org.hisp.dhis.program.ProgramStage) EnrollmentStatus(org.hisp.dhis.tracker.domain.EnrollmentStatus) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) List(java.util.List) RuleAction(org.hisp.dhis.rules.models.RuleAction) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) SettingKey(org.hisp.dhis.setting.SettingKey) ValidationStrategy(org.hisp.dhis.program.ValidationStrategy) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest) DataValue(org.hisp.dhis.tracker.domain.DataValue) Event(org.hisp.dhis.tracker.domain.Event) List(java.util.List) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 2 with DataValue

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

the class AssignValueImplementerTest method testAssignDataElementValueForEventsWhenDataElementIsEmpty.

@Test
void testAssignDataElementValueForEventsWhenDataElementIsEmpty() {
    List<Event> events = Lists.newArrayList(getEventWithDataValueNOTSet());
    bundle.setEvents(events);
    bundle.setRuleEffects(getRuleEventEffects(events));
    Map<String, List<ProgramRuleIssue>> eventIssues = implementerToTest.validateEvents(bundle);
    Event event = bundle.getEvents().stream().filter(e -> e.getEvent().equals(SECOND_EVENT_ID)).findAny().get();
    Optional<DataValue> newDataValue = event.getDataValues().stream().filter(dv -> dv.getDataElement().equals(dataElementA.getUid())).findAny();
    assertTrue(newDataValue.isPresent());
    assertEquals(DATA_ELEMENT_NEW_VALUE, newDataValue.get().getValue());
    assertEquals(1, eventIssues.size());
    assertEquals(1, eventIssues.get(SECOND_EVENT_ID).size());
    assertEquals(WARNING, eventIssues.get(SECOND_EVENT_ID).get(0).getIssueType());
}
Also used : Strictness(org.mockito.quality.Strictness) WARNING(org.hisp.dhis.tracker.programrule.IssueType.WARNING) DataValue(org.hisp.dhis.tracker.domain.DataValue) BeforeEach(org.junit.jupiter.api.BeforeEach) TRACKED_ENTITY_ATTRIBUTE(org.hisp.dhis.rules.models.AttributeType.TRACKED_ENTITY_ATTRIBUTE) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) RuleEffects(org.hisp.dhis.rules.models.RuleEffects) ERROR(org.hisp.dhis.tracker.programrule.IssueType.ERROR) RuleActionAssign(org.hisp.dhis.rules.models.RuleActionAssign) ValueType(org.hisp.dhis.common.ValueType) Attribute(org.hisp.dhis.tracker.domain.Attribute) Mock(org.mockito.Mock) RuleEffect(org.hisp.dhis.rules.models.RuleEffect) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) ENROLLMENT(org.hisp.dhis.rules.models.TrackerObjectType.ENROLLMENT) DataElement(org.hisp.dhis.dataelement.DataElement) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Lists(com.google.common.collect.Lists) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) DATA_ELEMENT(org.hisp.dhis.rules.models.AttributeType.DATA_ELEMENT) Map(java.util.Map) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) AssignValueImplementer(org.hisp.dhis.tracker.programrule.implementers.AssignValueImplementer) InjectMocks(org.mockito.InjectMocks) Event(org.hisp.dhis.tracker.domain.Event) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) EVENT(org.hisp.dhis.rules.models.TrackerObjectType.EVENT) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Set(java.util.Set) EventStatus(org.hisp.dhis.event.EventStatus) Mockito.when(org.mockito.Mockito.when) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) ProgramStage(org.hisp.dhis.program.ProgramStage) EnrollmentStatus(org.hisp.dhis.tracker.domain.EnrollmentStatus) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) List(java.util.List) RuleAction(org.hisp.dhis.rules.models.RuleAction) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) SettingKey(org.hisp.dhis.setting.SettingKey) ValidationStrategy(org.hisp.dhis.program.ValidationStrategy) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest) DataValue(org.hisp.dhis.tracker.domain.DataValue) Event(org.hisp.dhis.tracker.domain.Event) List(java.util.List) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 3 with DataValue

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

the class EventTrackerConverterService method from.

private ProgramStageInstance from(TrackerPreheat preheat, Event event, ProgramStageInstance programStageInstance) {
    ProgramStage programStage = preheat.get(ProgramStage.class, event.getProgramStage());
    Program program = preheat.get(Program.class, event.getProgram());
    OrganisationUnit organisationUnit = preheat.get(OrganisationUnit.class, event.getOrgUnit());
    Date now = new Date();
    if (isNewEntity(programStageInstance)) {
        programStageInstance = new ProgramStageInstance();
        programStageInstance.setUid(!StringUtils.isEmpty(event.getEvent()) ? event.getEvent() : event.getUid());
        programStageInstance.setCreated(now);
        programStageInstance.setStoredBy(event.getStoredBy());
        programStageInstance.setCreatedByUserInfo(UserInfoSnapshot.from(preheat.getUser()));
    }
    programStageInstance.setLastUpdatedByUserInfo(UserInfoSnapshot.from(preheat.getUser()));
    programStageInstance.setLastUpdated(now);
    programStageInstance.setDeleted(false);
    programStageInstance.setCreatedAtClient(DateUtils.fromInstant(event.getCreatedAtClient()));
    programStageInstance.setLastUpdatedAtClient(DateUtils.fromInstant(event.getUpdatedAtClient()));
    programStageInstance.setProgramInstance(getProgramInstance(preheat, TrackerIdScheme.UID, event.getEnrollment(), program));
    programStageInstance.setProgramStage(programStage);
    programStageInstance.setOrganisationUnit(organisationUnit);
    programStageInstance.setExecutionDate(DateUtils.fromInstant(event.getOccurredAt()));
    programStageInstance.setDueDate(DateUtils.fromInstant(event.getScheduledAt()));
    String attributeOptionCombo = event.getAttributeOptionCombo();
    if (attributeOptionCombo != null) {
        programStageInstance.setAttributeOptionCombo(preheat.get(CategoryOptionCombo.class, event.getAttributeOptionCombo()));
    } else {
        programStageInstance.setAttributeOptionCombo((CategoryOptionCombo) preheat.getDefaults().get(CategoryOptionCombo.class));
    }
    programStageInstance.setGeometry(event.getGeometry());
    EventStatus previousStatus = programStageInstance.getStatus();
    Date completedDate = DateUtils.fromInstant(event.getCompletedAt());
    programStageInstance.setStatus(event.getStatus());
    if (!Objects.equal(previousStatus, programStageInstance.getStatus()) && programStageInstance.isCompleted()) {
        programStageInstance.setCompletedDate(completedDate == null ? new Date() : completedDate);
        programStageInstance.setCompletedBy(event.getCompletedBy() != null ? event.getCompletedBy() : preheat.getUsername());
    }
    if (programStage.isEnableUserAssignment()) {
        User assignedUser = preheat.get(User.class, event.getAssignedUser());
        programStageInstance.setAssignedUser(assignedUser);
    }
    if (program.isRegistration() && programStageInstance.getDueDate() == null && programStageInstance.getExecutionDate() != null) {
        programStageInstance.setDueDate(programStageInstance.getExecutionDate());
    }
    for (DataValue dataValue : event.getDataValues()) {
        EventDataValue eventDataValue = new EventDataValue();
        eventDataValue.setValue(dataValue.getValue());
        eventDataValue.setCreated(DateUtils.fromInstant(dataValue.getCreatedAt()));
        eventDataValue.setLastUpdated(new Date());
        eventDataValue.setProvidedElsewhere(dataValue.isProvidedElsewhere());
        // ensure dataElement is referred to by UID as multiple
        // dataElementIdSchemes are supported
        DataElement dataElement = preheat.get(DataElement.class, dataValue.getDataElement());
        eventDataValue.setDataElement(dataElement.getUid());
        eventDataValue.setLastUpdatedByUserInfo(UserInfoSnapshot.from(preheat.getUser()));
        User createdBy = preheat.getUsers().get(dataValue.getCreatedBy());
        eventDataValue.setCreatedByUserInfo(Optional.ofNullable(createdBy).map(u -> UserInfoSnapshot.from(createdBy)).orElseGet(() -> UserInfoSnapshot.from(preheat.getUser())));
        programStageInstance.getEventDataValues().add(eventDataValue);
    }
    if (isNotEmpty(event.getNotes())) {
        programStageInstance.getComments().addAll(notesConverterService.from(preheat, event.getNotes()));
    }
    return programStageInstance;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataElement(org.hisp.dhis.dataelement.DataElement) Program(org.hisp.dhis.program.Program) User(org.hisp.dhis.user.User) DataValue(org.hisp.dhis.tracker.domain.DataValue) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) EventStatus(org.hisp.dhis.event.EventStatus) ProgramStage(org.hisp.dhis.program.ProgramStage) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) Date(java.util.Date) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 4 with DataValue

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

the class AssignValueImplementer method createDataValue.

private DataValue createDataValue(String dataElementUid, String newValue) {
    DataValue dataValue = new DataValue();
    dataValue.setDataElement(dataElementUid);
    dataValue.setValue(newValue);
    return dataValue;
}
Also used : DataValue(org.hisp.dhis.tracker.domain.DataValue)

Example 5 with DataValue

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

the class EventDataValuesValidationHookTest method failsOnActiveEventWithDataElementValueNullAndValidationStrategyOnUpdate.

@Test
void failsOnActiveEventWithDataElementValueNullAndValidationStrategyOnUpdate() {
    setUpIdentifiers();
    DataElement validDataElement = dataElement();
    when(context.getDataElement(dataElementUid)).thenReturn(validDataElement);
    ProgramStage programStage = programStage(validDataElement, true);
    programStage.setValidationStrategy(ValidationStrategy.ON_UPDATE_AND_INSERT);
    when(context.getProgramStage(programStageUid)).thenReturn(programStage);
    ValidationErrorReporter reporter = new ValidationErrorReporter(context);
    DataValue validDataValue = dataValue();
    validDataValue.setValue(null);
    Event event = Event.builder().programStage(programStage.getUid()).status(EventStatus.ACTIVE).dataValues(Set.of(validDataValue)).build();
    hook.validateEvent(reporter, event);
    assertThat(reporter.getReportList(), hasSize(1));
    assertEquals(TrackerErrorCode.E1076, 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) Test(org.junit.jupiter.api.Test)

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