Search in sources :

Example 26 with EventDataValue

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

the class UserInfoUpdatePreProcessor method updateDataValueUserInfo.

@Override
protected void updateDataValueUserInfo(ProgramStageInstance existingPsi, EventDataValue dataValue, UserInfoSnapshot userInfo) {
    if (existingPsi != null) {
        Optional<EventDataValue> existingPsiEventDataValue = findEventDataValues(existingPsi, dataValue);
        if (existingPsiEventDataValue.isPresent()) {
            EventDataValue eventDataValue = existingPsiEventDataValue.get();
            dataValue.setCreatedByUserInfo(eventDataValue.getCreatedByUserInfo());
        } else {
            dataValue.setCreatedByUserInfo(userInfo);
        }
    }
    dataValue.setLastUpdatedByUserInfo(userInfo);
}
Also used : EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue)

Example 27 with EventDataValue

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

the class ProgramRuleEngineTest method setupEvents.

private void setupEvents() {
    organisationUnitA = createOrganisationUnit('A');
    organisationUnitService.addOrganisationUnit(organisationUnitA);
    organisationUnitB = createOrganisationUnit('B');
    organisationUnitService.addOrganisationUnit(organisationUnitB);
    programA = createProgram('A', new HashSet<>(), organisationUnitA);
    programS = createProgram('S', new HashSet<>(), organisationUnitB);
    programB = createProgram('E', new HashSet<>(), organisationUnitA);
    programService.addProgram(programA);
    programService.addProgram(programS);
    programService.addProgram(programB);
    ProgramTrackedEntityAttribute attribute = createProgramTrackedEntityAttribute(programS, attributeB);
    attribute.setUid("ATTR-UID");
    ProgramTrackedEntityAttribute programAttributeEmail = createProgramTrackedEntityAttribute(programB, attributeEmail);
    attribute.setUid("ATTR-UID2");
    programTrackedEntityAttributeStore.save(attribute);
    programTrackedEntityAttributeStore.save(attribute);
    programA.setProgramAttributes(Arrays.asList(attribute));
    programS.setProgramAttributes(Arrays.asList(attribute));
    programB.setProgramAttributes(Arrays.asList(programAttributeEmail));
    programService.updateProgram(programA);
    programService.updateProgram(programS);
    programService.updateProgram(programB);
    ProgramStage programStageA = createProgramStage('A', programA);
    ProgramStage programStageAge = createProgramStage('S', programA);
    ProgramStage programStageB = createProgramStage('B', programA);
    ProgramStage programStageC = createProgramStage('C', programA);
    programStageService.saveProgramStage(programStageA);
    programStageService.saveProgramStage(programStageAge);
    programStageService.saveProgramStage(programStageB);
    programStageService.saveProgramStage(programStageC);
    ProgramStageDataElement programStageDataElementA = createProgramStageDataElement(programStageA, dataElementA, 1);
    ProgramStageDataElement programStageDataElementB = createProgramStageDataElement(programStageB, dataElementB, 2);
    ProgramStageDataElement programStageDataElementC = createProgramStageDataElement(programStageC, dataElementC, 3);
    ProgramStageDataElement programStageDataElementD = createProgramStageDataElement(programStageC, dataElementD, 4);
    ProgramStageDataElement programStageDataElementDate = createProgramStageDataElement(programStageAge, dataElementDate, 5);
    ProgramStageDataElement programStageDataElementAge = createProgramStageDataElement(programStageAge, dataElementAge, 6);
    programStageDataElementService.addProgramStageDataElement(programStageDataElementA);
    programStageDataElementService.addProgramStageDataElement(programStageDataElementB);
    programStageDataElementService.addProgramStageDataElement(programStageDataElementC);
    programStageDataElementService.addProgramStageDataElement(programStageDataElementD);
    programStageDataElementService.addProgramStageDataElement(programStageDataElementDate);
    programStageDataElementService.addProgramStageDataElement(programStageDataElementAge);
    programStageA.setSortOrder(1);
    programStageB.setSortOrder(2);
    programStageC.setSortOrder(3);
    programStageA.setProgramStageDataElements(Sets.newHashSet(programStageDataElementA, programStageDataElementB, programStageDataElementDate));
    programStageAge.setProgramStageDataElements(Sets.newHashSet(programStageDataElementDate));
    programStageB.setProgramStageDataElements(Sets.newHashSet(programStageDataElementA, programStageDataElementB));
    programStageC.setProgramStageDataElements(Sets.newHashSet(programStageDataElementC, programStageDataElementD));
    programStageService.updateProgramStage(programStageA);
    programStageService.updateProgramStage(programStageB);
    programStageService.updateProgramStage(programStageC);
    programStageService.updateProgramStage(programStageAge);
    programA.setProgramStages(Sets.newHashSet(programStageA, programStageB, programStageC, programStageAge));
    programService.updateProgram(programA);
    TrackedEntityInstance entityInstanceA = createTrackedEntityInstance(organisationUnitA);
    entityInstanceService.addTrackedEntityInstance(entityInstanceA);
    TrackedEntityInstance entityInstanceB = createTrackedEntityInstance(organisationUnitB);
    entityInstanceService.addTrackedEntityInstance(entityInstanceB);
    TrackedEntityInstance entityInstanceS = createTrackedEntityInstance(organisationUnitB);
    entityInstanceService.addTrackedEntityInstance(entityInstanceS);
    TrackedEntityInstance entityInstanceE = createTrackedEntityInstance(organisationUnitA);
    entityInstanceService.addTrackedEntityInstance(entityInstanceE);
    TrackedEntityAttributeValue attributeValue = new TrackedEntityAttributeValue(attributeA, entityInstanceA, "test");
    trackedEntityAttributeValueService.addTrackedEntityAttributeValue(attributeValue);
    TrackedEntityAttributeValue attributeValueB = new TrackedEntityAttributeValue(attributeB, entityInstanceB, "xmen");
    trackedEntityAttributeValueService.addTrackedEntityAttributeValue(attributeValueB);
    TrackedEntityAttributeValue attributeValueS = new TrackedEntityAttributeValue(attributeB, entityInstanceS, "xmen");
    TrackedEntityAttributeValue attributeValueEmail = new TrackedEntityAttributeValue(attributeEmail, entityInstanceE, "zubair@dhis2.org");
    trackedEntityAttributeValueService.addTrackedEntityAttributeValue(attributeValueS);
    entityInstanceA.setTrackedEntityAttributeValues(Sets.newHashSet(attributeValue));
    entityInstanceService.updateTrackedEntityInstance(entityInstanceA);
    entityInstanceB.setTrackedEntityAttributeValues(Sets.newHashSet(attributeValueB));
    entityInstanceService.updateTrackedEntityInstance(entityInstanceB);
    entityInstanceS.setTrackedEntityAttributeValues(Sets.newHashSet(attributeValueS));
    entityInstanceService.updateTrackedEntityInstance(entityInstanceS);
    entityInstanceE.setTrackedEntityAttributeValues(Sets.newHashSet(attributeValueEmail));
    entityInstanceService.updateTrackedEntityInstance(entityInstanceE);
    DateTime testDate1 = DateTime.now();
    testDate1.withTimeAtStartOfDay();
    testDate1 = testDate1.minusDays(70);
    Date incidentDate = testDate1.toDate();
    DateTime testDate2 = DateTime.now();
    testDate2.withTimeAtStartOfDay();
    Date enrollmentDate = testDate2.toDate();
    ProgramInstance programInstanceA = programInstanceService.enrollTrackedEntityInstance(entityInstanceA, programA, enrollmentDate, incidentDate, organisationUnitA);
    programInstanceA.setUid("UID-P1");
    programInstanceService.updateProgramInstance(programInstanceA);
    ProgramInstance programInstanceE = programInstanceService.enrollTrackedEntityInstance(entityInstanceE, programB, enrollmentDate, incidentDate, organisationUnitA);
    programInstanceE.setUid("UID-P2");
    programInstanceService.updateProgramInstance(programInstanceE);
    ProgramInstance programInstanceS = programInstanceService.enrollTrackedEntityInstance(entityInstanceS, programS, enrollmentDate, incidentDate, organisationUnitB);
    programInstanceS.setUid("UID-PS");
    programInstanceService.updateProgramInstance(programInstanceS);
    ProgramStageInstance programStageInstanceA = new ProgramStageInstance(programInstanceA, programStageA);
    programStageInstanceA.setDueDate(enrollmentDate);
    programStageInstanceA.setExecutionDate(new Date());
    programStageInstanceA.setUid("UID-PS1");
    programStageInstanceService.addProgramStageInstance(programStageInstanceA);
    eventDataValueDate = new EventDataValue();
    eventDataValueDate.setDataElement(dataElementDate.getUid());
    eventDataValueDate.setAutoFields();
    eventDataValueDate.setValue(dob);
    eventDataValueAge = new EventDataValue();
    eventDataValueAge.setDataElement(dataElementAge.getUid());
    eventDataValueAge.setAutoFields();
    eventDataValueAge.setValue(dob);
    ProgramStageInstance programStageInstanceDate = new ProgramStageInstance(programInstanceA, programStageAge);
    programStageInstanceDate.setDueDate(enrollmentDate);
    programStageInstanceDate.setExecutionDate(psEventDate);
    programStageInstanceDate.setUid("UID-PS12");
    programStageInstanceDate.setEventDataValues(Sets.newHashSet(eventDataValueDate));
    programStageInstanceService.addProgramStageInstance(programStageInstanceDate);
    ProgramStageInstance programStageInstanceAge = new ProgramStageInstance(programInstanceA, programStageAge);
    programStageInstanceAge.setDueDate(enrollmentDate);
    programStageInstanceAge.setExecutionDate(psEventDate);
    programStageInstanceAge.setUid("UID-PS13");
    programStageInstanceAge.setEventDataValues(Sets.newHashSet(eventDataValueAge));
    programStageInstanceService.addProgramStageInstance(programStageInstanceAge);
    ProgramStageInstance programStageInstanceB = new ProgramStageInstance(programInstanceA, programStageB);
    programStageInstanceB.setDueDate(enrollmentDate);
    programStageInstanceB.setExecutionDate(new Date());
    programStageInstanceB.setUid("UID-PS2");
    programStageInstanceService.addProgramStageInstance(programStageInstanceB);
    ProgramStageInstance programStageInstanceC = new ProgramStageInstance(programInstanceA, programStageC);
    programStageInstanceC.setDueDate(enrollmentDate);
    programStageInstanceC.setExecutionDate(new Date());
    programStageInstanceC.setUid("UID-PS3");
    programStageInstanceService.addProgramStageInstance(programStageInstanceC);
    programInstanceA.getProgramStageInstances().addAll(Sets.newHashSet(programStageInstanceA, programStageInstanceB, programStageInstanceC, programStageInstanceAge));
    programInstanceService.updateProgramInstance(programInstanceA);
}
Also used : ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ProgramStage(org.hisp.dhis.program.ProgramStage) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) DateTime(org.joda.time.DateTime) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 28 with EventDataValue

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

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

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

the class EventDataValueTest method testEventDataValue.

@Test
void testEventDataValue() throws IOException {
    TrackerImportParams trackerImportParams = fromJson("tracker/event_with_data_values.json");
    TrackerImportReport trackerImportReport = trackerImportService.importTracker(trackerImportParams);
    assertEquals(TrackerStatus.OK, trackerImportReport.getStatus());
    List<ProgramStageInstance> events = manager.getAll(ProgramStageInstance.class);
    assertEquals(1, events.size());
    ProgramStageInstance psi = events.get(0);
    Set<EventDataValue> eventDataValues = psi.getEventDataValues();
    assertEquals(4, eventDataValues.size());
}
Also used : TrackerImportParams(org.hisp.dhis.tracker.TrackerImportParams) TrackerImportReport(org.hisp.dhis.tracker.report.TrackerImportReport) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Test(org.junit.jupiter.api.Test) TrackerTest(org.hisp.dhis.tracker.TrackerTest)

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