Search in sources :

Example 11 with EventDataValue

use of org.hisp.dhis.eventdatavalue.EventDataValue in project dhis2-core by dhis2.

the class EventTrackerConverterService method getProgramStageInstanceDataValues.

private List<EventDataValue> getProgramStageInstanceDataValues(TrackerPreheat preheat, Event event) {
    List<EventDataValue> eventDataValues = new ArrayList<>();
    ProgramStageInstance programStageInstance = preheat.getEvent(TrackerIdScheme.UID, event.getEvent());
    if (programStageInstance == null) {
        return eventDataValues;
    }
    Set<String> dataElements = event.getDataValues().stream().map(DataValue::getDataElement).collect(Collectors.toSet());
    for (EventDataValue eventDataValue : programStageInstance.getEventDataValues()) {
        if (!dataElements.contains(eventDataValue.getDataElement())) {
            eventDataValues.add(eventDataValue);
        }
    }
    return eventDataValues;
}
Also used : ArrayList(java.util.ArrayList) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 12 with EventDataValue

use of org.hisp.dhis.eventdatavalue.EventDataValue in project dhis2-core by dhis2.

the class FilteringOutUndeclaredDataElementsProcessorTest method testNotLinkedDataElementsAreRemovedFromEvent.

@Test
void testNotLinkedDataElementsAreRemovedFromEvent() {
    Event event = new Event();
    event.setProgramStage(PROGRAMSTAGE);
    HashSet<DataValue> dataValues = Sets.newHashSet(new DataValue(DATA_ELEMENT_1, "whatever"), new DataValue(DATA_ELEMENT_2, "another value"));
    event.setDataValues(dataValues);
    WorkContext ctx = WorkContext.builder().importOptions(ImportOptions.getDefaultImportOptions()).programsMap(getProgramMap()).eventDataValueMap(new EventDataValueAggregator().aggregateDataValues(ImmutableList.of(event), Collections.emptyMap(), ImportOptions.getDefaultImportOptions())).build();
    preProcessor.process(event, ctx);
    Set<String> allowedDataValues = ctx.getProgramStage(ctx.getImportOptions().getIdSchemes().getProgramStageIdScheme(), PROGRAMSTAGE).getDataElements().stream().map(BaseIdentifiableObject::getUid).collect(Collectors.toSet());
    Set<String> filteredEventDataValues = ctx.getEventDataValueMap().values().stream().flatMap(Collection::stream).map(EventDataValue::getDataElement).collect(Collectors.toSet());
    assertTrue(allowedDataValues.containsAll(filteredEventDataValues));
}
Also used : DataValue(org.hisp.dhis.dxf2.events.event.DataValue) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) EventDataValueAggregator(org.hisp.dhis.dxf2.events.importer.context.EventDataValueAggregator) Event(org.hisp.dhis.dxf2.events.event.Event) WorkContext(org.hisp.dhis.dxf2.events.importer.context.WorkContext) Collection(java.util.Collection) Test(org.junit.jupiter.api.Test)

Example 13 with EventDataValue

use of org.hisp.dhis.eventdatavalue.EventDataValue in project dhis2-core by dhis2.

the class EventTrackerConverterServiceTest method fromForRuleEngineGivenNewEvent.

@Test
void fromForRuleEngineGivenNewEvent() {
    setUpMocks();
    DataElement dataElement = new DataElement();
    dataElement.setUid(CodeGenerator.generateUid());
    when(preheat.get(DataElement.class, dataElement.getUid())).thenReturn(dataElement);
    DataValue dataValue = dataValue(dataElement.getUid(), "900");
    Event event = event(dataValue);
    ProgramStageInstance programStageInstance = converter.fromForRuleEngine(preheat, event);
    assertNotNull(programStageInstance);
    assertNotNull(programStageInstance.getProgramStage());
    assertNotNull(programStageInstance.getProgramStage().getProgram());
    assertNotNull(programStageInstance.getOrganisationUnit());
    assertEquals(PROGRAM_UID, programStageInstance.getProgramStage().getProgram().getUid());
    assertEquals(PROGRAM_STAGE_UID, programStageInstance.getProgramStage().getUid());
    assertEquals(ORGANISATION_UNIT_UID, programStageInstance.getOrganisationUnit().getUid());
    assertEquals(ORGANISATION_UNIT_UID, programStageInstance.getOrganisationUnit().getUid());
    assertEquals(1, programStageInstance.getEventDataValues().size());
    EventDataValue actual = programStageInstance.getEventDataValues().stream().findFirst().get();
    assertEquals(dataValue.getDataElement(), actual.getDataElement());
    assertEquals(dataValue.getValue(), actual.getValue());
    assertTrue(actual.getProvidedElsewhere());
    assertEquals(USERNAME, actual.getCreatedByUserInfo().getUsername());
    assertEquals(USERNAME, actual.getLastUpdatedByUserInfo().getUsername());
}
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 14 with EventDataValue

use of org.hisp.dhis.eventdatavalue.EventDataValue 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 15 with EventDataValue

use of org.hisp.dhis.eventdatavalue.EventDataValue 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)

Aggregations

EventDataValue (org.hisp.dhis.eventdatavalue.EventDataValue)35 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)21 DataElement (org.hisp.dhis.dataelement.DataElement)14 Date (java.util.Date)10 HashSet (java.util.HashSet)10 Test (org.junit.jupiter.api.Test)9 DataValue (org.hisp.dhis.tracker.domain.DataValue)8 HashMap (java.util.HashMap)7 Event (org.hisp.dhis.tracker.domain.Event)7 User (org.hisp.dhis.user.User)7 ArrayList (java.util.ArrayList)6 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)6 Set (java.util.Set)5 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 ProgramInstance (org.hisp.dhis.program.ProgramInstance)5 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 WorkContext (org.hisp.dhis.dxf2.events.importer.context.WorkContext)4 EventStatus (org.hisp.dhis.event.EventStatus)4 Program (org.hisp.dhis.program.Program)4