Search in sources :

Example 6 with EventDataValue

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

the class DataValueAclCheck method check.

@Override
public ImportSummary check(ImmutableEvent event, WorkContext ctx) {
    final TrackerAccessManager trackerAccessManager = ctx.getServiceDelegator().getTrackerAccessManager();
    final ProgramStageInstance programStageInstance = ctx.getProgramStageInstanceMap().get(event.getUid());
    Map<String, Set<EventDataValue>> eventDataValueMap = ctx.getEventDataValueMap();
    final User user = ctx.getImportOptions().getUser();
    final ImportSummary importSummary = new ImportSummary();
    // Note that here we are passing a ProgramStageInstance, which during a
    // INSERT
    // operation
    // is going to be null, so the ACL method will not be able to check that
    final Set<EventDataValue> dataValues = eventDataValueMap.get(event.getUid());
    for (EventDataValue dataValue : dataValues) {
        DataElement dataElement = ctx.getDataElementMap().get(dataValue.getDataElement());
        List<String> errors = trackerAccessManager.canWrite(user, programStageInstance, dataElement, true);
        if (!errors.isEmpty()) {
            errors.forEach(error -> importSummary.addConflict(dataElement.getUid(), error));
        }
    }
    return importSummary;
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Set(java.util.Set) User(org.hisp.dhis.user.User) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) TrackerAccessManager(org.hisp.dhis.trackedentity.TrackerAccessManager) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 7 with EventDataValue

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

the class ProgramStageInstanceServiceTest method setUpTest.

@Override
public void setUpTest() {
    mockFormat = new MockI18nFormat();
    organisationUnitA = createOrganisationUnit('A');
    organisationUnitService.addOrganisationUnit(organisationUnitA);
    organisationUnitB = createOrganisationUnit('B');
    organisationUnitService.addOrganisationUnit(organisationUnitB);
    entityInstanceA = createTrackedEntityInstance(organisationUnitA);
    entityInstanceService.addTrackedEntityInstance(entityInstanceA);
    entityInstanceB = createTrackedEntityInstance(organisationUnitB);
    entityInstanceService.addTrackedEntityInstance(entityInstanceB);
    TrackedEntityAttribute attribute = createTrackedEntityAttribute('A');
    attribute.setValueType(ValueType.PHONE_NUMBER);
    attributeService.addTrackedEntityAttribute(attribute);
    TrackedEntityAttributeValue attributeValue = createTrackedEntityAttributeValue('A', entityInstanceA, attribute);
    attributeValue.setValue("123456789");
    attributeValueService.addTrackedEntityAttributeValue(attributeValue);
    entityInstanceA.getTrackedEntityAttributeValues().add(attributeValue);
    entityInstanceService.updateTrackedEntityInstance(entityInstanceA);
    /**
     * Program A
     */
    programA = createProgram('A', new HashSet<>(), organisationUnitA);
    programService.addProgram(programA);
    stageA = createProgramStage('A', 0);
    stageA.setProgram(programA);
    stageA.setSortOrder(1);
    programStageService.saveProgramStage(stageA);
    stageB = new ProgramStage("B", programA);
    stageB.setSortOrder(2);
    programStageService.saveProgramStage(stageB);
    Set<ProgramStage> programStages = new HashSet<>();
    programStages.add(stageA);
    programStages.add(stageB);
    programA.setProgramStages(programStages);
    programService.updateProgram(programA);
    dataElementA = createDataElement('A');
    dataElementB = createDataElement('B');
    dataElementC = createDataElement('C');
    dataElementD = createDataElement('D');
    dataElementService.addDataElement(dataElementA);
    dataElementService.addDataElement(dataElementB);
    dataElementService.addDataElement(dataElementC);
    dataElementService.addDataElement(dataElementD);
    stageDataElementA = new ProgramStageDataElement(stageA, dataElementA, false, 1);
    stageDataElementB = new ProgramStageDataElement(stageA, dataElementB, false, 2);
    stageDataElementC = new ProgramStageDataElement(stageB, dataElementA, false, 1);
    stageDataElementD = new ProgramStageDataElement(stageB, dataElementB, false, 2);
    programStageDataElementService.addProgramStageDataElement(stageDataElementA);
    programStageDataElementService.addProgramStageDataElement(stageDataElementB);
    programStageDataElementService.addProgramStageDataElement(stageDataElementC);
    programStageDataElementService.addProgramStageDataElement(stageDataElementD);
    /*
         * Program B
         */
    Program programB = createProgram('B', new HashSet<>(), organisationUnitB);
    programService.addProgram(programB);
    stageC = new ProgramStage("C", programB);
    stageC.setSortOrder(1);
    programStageService.saveProgramStage(stageC);
    stageD = new ProgramStage("D", programB);
    stageB.setSortOrder(2);
    stageC.setRepeatable(true);
    programStageService.saveProgramStage(stageD);
    programStages = new HashSet<>();
    programStages.add(stageC);
    programStages.add(stageD);
    programB.setProgramStages(programStages);
    programService.updateProgram(programB);
    /**
     * Program Instance and Program Stage Instance
     */
    DateTime testDate1 = DateTime.now();
    testDate1.withTimeAtStartOfDay();
    testDate1 = testDate1.minusDays(70);
    incidenDate = testDate1.toDate();
    DateTime testDate2 = DateTime.now();
    testDate2.withTimeAtStartOfDay();
    enrollmentDate = testDate2.toDate();
    programInstanceA = new ProgramInstance(enrollmentDate, incidenDate, entityInstanceA, programA);
    programInstanceA.setUid("UID-PIA");
    programInstanceService.addProgramInstance(programInstanceA);
    programInstanceB = new ProgramInstance(enrollmentDate, incidenDate, entityInstanceB, programB);
    programInstanceService.addProgramInstance(programInstanceB);
    programStageInstanceA = new ProgramStageInstance(programInstanceA, stageA);
    programStageInstanceA.setDueDate(enrollmentDate);
    programStageInstanceA.setUid("UID-A");
    programStageInstanceB = new ProgramStageInstance(programInstanceA, stageB);
    programStageInstanceB.setDueDate(enrollmentDate);
    programStageInstanceB.setUid("UID-B");
    programStageInstanceC = new ProgramStageInstance(programInstanceB, stageC);
    programStageInstanceC.setDueDate(enrollmentDate);
    programStageInstanceC.setUid("UID-C");
    programStageInstanceD1 = new ProgramStageInstance(programInstanceB, stageD);
    programStageInstanceD1.setDueDate(enrollmentDate);
    programStageInstanceD1.setUid("UID-D1");
    programStageInstanceD2 = new ProgramStageInstance(programInstanceB, stageD);
    programStageInstanceD2.setDueDate(enrollmentDate);
    programStageInstanceD2.setUid("UID-D2");
    /*
         * Prepare data for EventDataValues manipulation tests
         */
    programStageInstanceService.addProgramStageInstance(programStageInstanceA);
    // Check that there are no EventDataValues assigned to PSI
    ProgramStageInstance tempPsiA = programStageInstanceService.getProgramStageInstance(programStageInstanceA.getUid());
    assertEquals(0, tempPsiA.getEventDataValues().size());
    // Prepare EventDataValues to manipulate with
    String storedBy = "test-user";
    eventDataValueA = new EventDataValue(dataElementA.getUid(), "1", UserInfoTestHelper.testUserInfo(storedBy));
    eventDataValueB = new EventDataValue(dataElementB.getUid(), "2", UserInfoTestHelper.testUserInfo(storedBy));
    eventDataValueC = new EventDataValue(dataElementC.getUid(), "3", UserInfoTestHelper.testUserInfo(storedBy));
    eventDataValueD = new EventDataValue(dataElementD.getUid(), "4", UserInfoTestHelper.testUserInfo(storedBy));
    dataElementMap.put(dataElementA.getUid(), dataElementA);
    dataElementMap.put(dataElementB.getUid(), dataElementB);
    dataElementMap.put(dataElementC.getUid(), dataElementC);
    dataElementMap.put(dataElementD.getUid(), dataElementD);
    dataElements = dataElementMap.getAll().collect(toList());
}
Also used : TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) DateTime(org.joda.time.DateTime) MockI18nFormat(org.hisp.dhis.mock.MockI18nFormat) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) HashSet(java.util.HashSet)

Example 8 with EventDataValue

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

the class TrackerNotificationWebHookServiceTest method initTest.

@BeforeEach
public void initTest() {
    trackedEntityAttribute = createTrackedEntityAttribute('A');
    dataElement = createDataElement('D');
    organisationUnitA = createOrganisationUnit('A');
    programA = createProgram('A', new HashSet<>(), organisationUnitA);
    programTrackedEntityAttribute = createProgramTrackedEntityAttribute(programA, trackedEntityAttribute);
    programA.getProgramAttributes().add(programTrackedEntityAttribute);
    TrackedEntityInstance tei = createTrackedEntityInstance(organisationUnitA);
    trackedEntityAttributeValue = createTrackedEntityAttributeValue('I', tei, trackedEntityAttribute);
    tei.getTrackedEntityAttributeValues().add(trackedEntityAttributeValue);
    programStageA = createProgramStage('A', programA);
    programInstance = new ProgramInstance();
    programInstance.setAutoFields();
    programInstance.setProgram(programA);
    programInstance.setOrganisationUnit(organisationUnitA);
    programInstance.setEnrollmentDate(new Date());
    programInstance.setIncidentDate(new Date());
    programInstance.setEntityInstance(tei);
    programStageInstance = new ProgramStageInstance();
    programStageInstance.setAutoFields();
    programStageInstance.setProgramStage(programStageA);
    programStageInstance.setOrganisationUnit(organisationUnitA);
    programInstance.setEnrollmentDate(new Date());
    programStageInstance.setExecutionDate(new Date());
    programStageInstance.setDueDate(new Date());
    programStageInstance.setProgramInstance(programInstance);
    dataValue = new EventDataValue();
    dataValue.setValue("dataValue123");
    dataValue.setDataElement(dataElement.getUid());
    dataValue.setAutoFields();
    programStageInstance.getEventDataValues().add(dataValue);
    programNotification = new ProgramNotificationTemplate();
    programNotification.setNotificationRecipient(ProgramNotificationRecipient.WEB_HOOK);
    programNotification.setMessageTemplate(URL);
    programNotification.setDeliveryChannels(Sets.newHashSet(DeliveryChannel.HTTP));
    programA.setNotificationTemplates(Sets.newHashSet(programNotification));
    programStageNotification = new ProgramNotificationTemplate();
    programStageNotification.setNotificationRecipient(ProgramNotificationRecipient.WEB_HOOK);
    programStageNotification.setMessageTemplate(URL);
    programStageNotification.setDeliveryChannels(Sets.newHashSet(DeliveryChannel.HTTP));
    programStageA.setNotificationTemplates(Sets.newHashSet(programStageNotification));
    responseEntity = new ResponseEntity<>(HttpStatus.OK);
}
Also used : ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) Date(java.util.Date) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) HashSet(java.util.HashSet) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 9 with EventDataValue

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

the class ProgramRuleEntityMapperServiceTest method setUpProgramRules.

private void setUpProgramRules() {
    program = createProgram('P');
    programStage = createProgramStage('S', program);
    TrackedEntityAttribute attribute = createTrackedEntityAttribute('Z');
    attribute.setName("Tracked_entity_attribute_A");
    DataElement dataElement1 = createDataElement('E');
    dataElement1.setFormName("DateElement_E");
    programRuleVariableA = createProgramRuleVariable('A', program);
    programRuleVariableB = createProgramRuleVariable('B', program);
    programRuleVariableC = createProgramRuleVariable('C', program);
    programRuleVariableA = setProgramRuleVariable(programRuleVariableA, ProgramRuleVariableSourceType.CALCULATED_VALUE, program, null, createDataElement('D'), null);
    programRuleVariableB = setProgramRuleVariable(programRuleVariableB, ProgramRuleVariableSourceType.TEI_ATTRIBUTE, program, null, null, attribute);
    programRuleVariableC = setProgramRuleVariable(programRuleVariableC, ProgramRuleVariableSourceType.DATAELEMENT_CURRENT_EVENT, program, null, dataElement1, null);
    programRuleVariables.add(programRuleVariableA);
    programRuleVariables.add(programRuleVariableB);
    programRuleVariables.add(programRuleVariableC);
    programRuleA = createProgramRule('A', program);
    programRuleB = createProgramRule('B', program);
    programRuleD = createProgramRule('D', program);
    assignAction = createProgramRuleAction('I');
    sendMessageAction = createProgramRuleAction('J');
    displayText = createProgramRuleAction('D');
    assignAction = setProgramRuleAction(assignAction, ProgramRuleActionType.ASSIGN, "test_variable", "2+2");
    displayText = setProgramRuleAction(displayText, ProgramRuleActionType.DISPLAYTEXT, "test_variable", "2+2");
    sendMessageAction = setProgramRuleAction(sendMessageAction, ProgramRuleActionType.SENDMESSAGE, null, null);
    programRuleA = setProgramRule(programRuleA, "", Sets.newHashSet(assignAction, displayText), 1);
    programRuleB = setProgramRule(programRuleB, "", Sets.newHashSet(sendMessageAction), 4);
    programRuleD = setProgramRule(programRuleD, "", Sets.newHashSet(sendMessageAction), null);
    programRules.add(programRuleA);
    programRules.add(programRuleB);
    programRules.add(programRuleC);
    programRules.add(programRuleD);
    dataElement = createDataElement('D');
    dataElement.setValueType(ValueType.TEXT);
    organisationUnit = createOrganisationUnit('O');
    trackedEntityAttribute = createTrackedEntityAttribute('A', ValueType.TEXT);
    trackedEntityInstance = createTrackedEntityInstance('I', organisationUnit, trackedEntityAttribute);
    trackedEntityAttributeValue = createTrackedEntityAttributeValue('E', trackedEntityInstance, trackedEntityAttribute);
    trackedEntityAttributeValue.setValue(SAMPLE_VALUE_A);
    trackedEntityInstance.setTrackedEntityAttributeValues(Sets.newHashSet(trackedEntityAttributeValue));
    eventDataValueA = new EventDataValue();
    eventDataValueA.setDataElement(dataElement.getUid());
    eventDataValueA.setValue(SAMPLE_VALUE_A);
    eventDataValueA.setAutoFields();
    eventDataValueB = new EventDataValue();
    eventDataValueB.setDataElement(dataElement.getUid());
    eventDataValueB.setValue(SAMPLE_VALUE_B);
    eventDataValueB.setAutoFields();
    programInstanceB = new ProgramInstance(new Date(), new Date(), trackedEntityInstance, program);
    programInstance = new ProgramInstance(new Date(), new Date(), trackedEntityInstance, program);
    programInstance.setOrganisationUnit(organisationUnit);
    programInstance.setStatus(ProgramStatus.ACTIVE);
    programInstance.setAutoFields();
    programInstance.setEnrollmentDate(new Date());
    programInstance.setIncidentDate(new Date());
    programInstance.setEntityInstance(trackedEntityInstance);
    programStageInstanceA = new ProgramStageInstance(programInstance, programStage);
    programStageInstanceB = new ProgramStageInstance(programInstance, programStage);
    programStageInstanceC = new ProgramStageInstance(programInstance, programStage);
    programStageInstanceA.setOrganisationUnit(organisationUnit);
    programStageInstanceA.setAutoFields();
    programStageInstanceA.setDueDate(new Date());
    programStageInstanceA.setExecutionDate(new Date());
    programStageInstanceA.setEventDataValues(Sets.newHashSet(eventDataValueA));
    programStageInstanceB.setOrganisationUnit(organisationUnit);
    programStageInstanceB.setAutoFields();
    programStageInstanceB.setDueDate(new Date());
    programStageInstanceB.setExecutionDate(new Date());
    programStageInstanceB.setEventDataValues(Sets.newHashSet(eventDataValueB));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramInstance(org.hisp.dhis.program.ProgramInstance) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) Date(java.util.Date) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 10 with EventDataValue

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

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