Search in sources :

Example 46 with Enrollment

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

the class AssignValueImplementer method addOrOverwriteAttribute.

private void addOrOverwriteAttribute(EnrollmentActionRule actionRule, TrackerBundle bundle) {
    Enrollment enrollment = bundle.getEnrollment(actionRule.getEnrollment()).get();
    Optional<TrackedEntity> trackedEntity = bundle.getTrackedEntity(enrollment.getTrackedEntity());
    List<Attribute> attributes;
    if (trackedEntity.isPresent()) {
        attributes = trackedEntity.get().getAttributes();
        Optional<Attribute> optionalAttribute = attributes.stream().filter(at -> at.getAttribute().equals(actionRule.getField())).findAny();
        if (optionalAttribute.isPresent()) {
            optionalAttribute.get().setValue(actionRule.getData());
            return;
        }
    }
    attributes = enrollment.getAttributes();
    Optional<Attribute> optionalAttribute = attributes.stream().filter(at -> at.getAttribute().equals(actionRule.getField())).findAny();
    if (optionalAttribute.isPresent()) {
        optionalAttribute.get().setValue(actionRule.getData());
    } else {
        attributes.add(createAttribute(actionRule.getField(), actionRule.getData()));
    }
}
Also used : DataValue(org.hisp.dhis.tracker.domain.DataValue) RuleActionAssign(org.hisp.dhis.rules.models.RuleActionAssign) ValueType(org.hisp.dhis.common.ValueType) Attribute(org.hisp.dhis.tracker.domain.Attribute) RequiredArgsConstructor(lombok.RequiredArgsConstructor) DataElement(org.hisp.dhis.dataelement.DataElement) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Lists(com.google.common.collect.Lists) Map(java.util.Map) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) ProgramRuleIssue(org.hisp.dhis.tracker.programrule.ProgramRuleIssue) Event(org.hisp.dhis.tracker.domain.Event) EventActionRule(org.hisp.dhis.tracker.programrule.EventActionRule) EnrollmentActionRule(org.hisp.dhis.tracker.programrule.EnrollmentActionRule) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Set(java.util.Set) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Sets(com.google.common.collect.Sets) TrackerErrorCode(org.hisp.dhis.tracker.report.TrackerErrorCode) List(java.util.List) Component(org.springframework.stereotype.Component) NumberUtils(org.apache.commons.lang3.math.NumberUtils) Optional(java.util.Optional) IssueType(org.hisp.dhis.tracker.programrule.IssueType) SettingKey(org.hisp.dhis.setting.SettingKey) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) RuleActionImplementer(org.hisp.dhis.tracker.programrule.RuleActionImplementer) Attribute(org.hisp.dhis.tracker.domain.Attribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Enrollment(org.hisp.dhis.tracker.domain.Enrollment)

Example 47 with Enrollment

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

the class DefaultTrackerProgramRuleService method getAttributes.

// Get all the attributes linked to enrollment from the payload and the DB,
// using the one from payload
// if they are present in both places
private List<TrackedEntityAttributeValue> getAttributes(Enrollment enrollment, TrackerBundle bundle) {
    List<TrackedEntityAttributeValue> attributeValues = attributeValueTrackerConverterService.from(bundle.getPreheat(), enrollment.getAttributes());
    TrackedEntityInstance trackedEntity = bundle.getPreheat().getTrackedEntity(bundle.getIdentifier(), enrollment.getTrackedEntity());
    List<TrackedEntityAttributeValue> payloadAttributeValues = bundle.getTrackedEntity(enrollment.getTrackedEntity()).map(tei -> attributeValueTrackerConverterService.from(bundle.getPreheat(), tei.getAttributes())).orElse(Lists.newArrayList());
    attributeValues.addAll(payloadAttributeValues);
    if (trackedEntity != null) {
        List<String> payloadAttributeValuesIds = payloadAttributeValues.stream().map(av -> av.getAttribute().getUid()).collect(Collectors.toList());
        attributeValues.addAll(trackedEntity.getTrackedEntityAttributeValues().stream().filter(av -> !payloadAttributeValuesIds.contains(av.getAttribute().getUid())).collect(Collectors.toList()));
    }
    return attributeValues;
}
Also used : RuleEffects(org.hisp.dhis.rules.models.RuleEffects) Attribute(org.hisp.dhis.tracker.domain.Attribute) RequiredArgsConstructor(lombok.RequiredArgsConstructor) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) StringUtils(org.apache.commons.lang3.StringUtils) Program(org.hisp.dhis.program.Program) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) RuleEngineConverterService(org.hisp.dhis.tracker.converter.RuleEngineConverterService) Lists(com.google.common.collect.Lists) Service(org.springframework.stereotype.Service) Map(java.util.Map) ProgramInstance(org.hisp.dhis.program.ProgramInstance) Qualifier(org.springframework.beans.factory.annotation.Qualifier) TrackerIdScheme(org.hisp.dhis.tracker.TrackerIdScheme) TrackerProgramRuleService(org.hisp.dhis.tracker.TrackerProgramRuleService) Event(org.hisp.dhis.tracker.domain.Event) NonNull(lombok.NonNull) TrackerConverterService(org.hisp.dhis.tracker.converter.TrackerConverterService) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Set(java.util.Set) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) Stream(java.util.stream.Stream) ProgramRuleEngine(org.hisp.dhis.programrule.engine.ProgramRuleEngine) Collections(java.util.Collections) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) Transactional(org.springframework.transaction.annotation.Transactional) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Example 48 with Enrollment

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

the class OwnershipTest method testUpdateEnrollment.

@Test
void testUpdateEnrollment() throws IOException {
    TrackerImportParams enrollmentParams = fromJson("tracker/ownership_enrollment.json", nonSuperUser.getUid());
    List<ProgramInstance> pis = manager.getAll(ProgramInstance.class);
    assertEquals(2, pis.size());
    ProgramInstance pi = pis.stream().filter(e -> e.getUid().equals("TvctPPhpD8u")).findAny().get();
    compareEnrollmentBasicProperties(pi, enrollmentParams.getEnrollments().get(0));
    Enrollment updatedEnrollment = enrollmentParams.getEnrollments().get(0);
    updatedEnrollment.setStatus(EnrollmentStatus.COMPLETED);
    updatedEnrollment.setCompletedBy("admin");
    updatedEnrollment.setCompletedAt(Instant.now());
    updatedEnrollment.setCreatedAtClient(Instant.now());
    updatedEnrollment.setUpdatedAtClient(Instant.now());
    updatedEnrollment.setEnrolledAt(Instant.now());
    updatedEnrollment.setOccurredAt(Instant.now());
    enrollmentParams.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE);
    TrackerImportReport updatedReport = trackerImportService.importTracker(enrollmentParams);
    assertNoImportErrors(updatedReport);
    assertEquals(1, updatedReport.getStats().getUpdated());
    pis = manager.getAll(ProgramInstance.class);
    assertEquals(2, pis.size());
    pi = pis.stream().filter(e -> e.getUid().equals("TvctPPhpD8u")).findAny().get();
    compareEnrollmentBasicProperties(pi, updatedEnrollment);
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Autowired(org.springframework.beans.factory.annotation.Autowired) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) TrackerImportService(org.hisp.dhis.tracker.TrackerImportService) Program(org.hisp.dhis.program.Program) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) TrackerStatus(org.hisp.dhis.tracker.report.TrackerStatus) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) ProgramInstance(org.hisp.dhis.program.ProgramInstance) User(org.hisp.dhis.user.User) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Set(java.util.Set) TrackerImportReport(org.hisp.dhis.tracker.report.TrackerImportReport) IOException(java.io.IOException) Instant(java.time.Instant) TrackedEntityProgramOwner(org.hisp.dhis.trackedentity.TrackedEntityProgramOwner) EnrollmentStatus(org.hisp.dhis.tracker.domain.EnrollmentStatus) TrackerErrorCode(org.hisp.dhis.tracker.report.TrackerErrorCode) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Test(org.junit.jupiter.api.Test) TrackerOwnershipManager(org.hisp.dhis.trackedentity.TrackerOwnershipManager) TrackerImportParams(org.hisp.dhis.tracker.TrackerImportParams) List(java.util.List) TrackerTest(org.hisp.dhis.tracker.TrackerTest) TrackerImportStrategy(org.hisp.dhis.tracker.TrackerImportStrategy) CodeGenerator(org.hisp.dhis.common.CodeGenerator) DateUtils(org.hisp.dhis.util.DateUtils) TrackerImportParams(org.hisp.dhis.tracker.TrackerImportParams) TrackerImportReport(org.hisp.dhis.tracker.report.TrackerImportReport) ProgramInstance(org.hisp.dhis.program.ProgramInstance) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Test(org.junit.jupiter.api.Test) TrackerTest(org.hisp.dhis.tracker.TrackerTest)

Example 49 with Enrollment

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

the class TrackerPreheatTest method testReferenceInvalidation.

@Test
void testReferenceInvalidation() {
    TrackerPreheat preheat = new TrackerPreheat();
    // Create root TEI
    TrackedEntityInstance tei = new TrackedEntityInstance();
    tei.setUid(CodeGenerator.generateUid());
    List<TrackedEntityInstance> teiList = new ArrayList<>();
    teiList.add(tei);
    List<String> allEntities = new ArrayList<>();
    allEntities.add(CodeGenerator.generateUid());
    preheat.putTrackedEntities(TrackerIdScheme.UID, teiList, allEntities);
    // Create 2 Enrollments, where TEI is parent
    ProgramInstance programInstance = new ProgramInstance();
    programInstance.setUid(CodeGenerator.generateUid());
    List<ProgramInstance> psList = new ArrayList<>();
    psList.add(programInstance);
    List<Enrollment> allPs = new ArrayList<>();
    allPs.add(new Enrollment() {

        {
            String uid = CodeGenerator.generateUid();
            setEnrollment(uid);
            setTrackedEntity(allEntities.get(0));
        }
    });
    allPs.add(new Enrollment() {

        {
            String uid = CodeGenerator.generateUid();
            setEnrollment(uid);
            setTrackedEntity(allEntities.get(0));
        }
    });
    preheat.putEnrollments(TrackerIdScheme.UID, psList, allPs);
    // Create 4 Enrollments, where TEI is parent
    ProgramStageInstance psi = new ProgramStageInstance();
    psi.setUid(CodeGenerator.generateUid());
    List<ProgramStageInstance> psiList = new ArrayList<>();
    psiList.add(psi);
    List<Event> allEvents = new ArrayList<>();
    allEvents.add(new Event() {

        {
            String uid = CodeGenerator.generateUid();
            setEvent(uid);
            setEnrollment(allPs.get(0).getEnrollment());
        }
    });
    allEvents.add(new Event() {

        {
            String uid = CodeGenerator.generateUid();
            setEvent(uid);
            setEnrollment(allPs.get(0).getEnrollment());
        }
    });
    allEvents.add(new Event() {

        {
            String uid = CodeGenerator.generateUid();
            setEvent(uid);
            setEnrollment(allPs.get(1).getEnrollment());
        }
    });
    allEvents.add(new Event() {

        {
            String uid = CodeGenerator.generateUid();
            setEvent(uid);
            setEnrollment(allPs.get(1).getEnrollment());
        }
    });
    preheat.putEvents(TrackerIdScheme.UID, psiList, allEvents);
    preheat.createReferenceTree();
    Optional<ReferenceTrackerEntity> reference = preheat.getReference(allEvents.get(0).getUid());
    assertThat(reference.get().getUid(), is(allEvents.get(0).getUid()));
    assertThat(reference.get().getParentUid(), is(allPs.get(0).getUid()));
    Optional<ReferenceTrackerEntity> reference2 = preheat.getReference(allEvents.get(1).getUid());
    assertThat(reference2.get().getUid(), is(allEvents.get(1).getUid()));
    assertThat(reference2.get().getParentUid(), is(allPs.get(0).getUid()));
    Optional<ReferenceTrackerEntity> reference3 = preheat.getReference(allEvents.get(2).getUid());
    assertThat(reference3.get().getUid(), is(allEvents.get(2).getUid()));
    assertThat(reference3.get().getParentUid(), is(allPs.get(1).getUid()));
    Optional<ReferenceTrackerEntity> reference4 = preheat.getReference(allEvents.get(3).getUid());
    assertThat(reference4.get().getUid(), is(allEvents.get(3).getUid()));
    assertThat(reference4.get().getParentUid(), is(allPs.get(1).getUid()));
}
Also used : ProgramInstance(org.hisp.dhis.program.ProgramInstance) ArrayList(java.util.ArrayList) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Event(org.hisp.dhis.tracker.domain.Event) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Test(org.junit.jupiter.api.Test)

Example 50 with Enrollment

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

the class TrackerBundleTest method testBasicSetup1.

@Test
void testBasicSetup1() {
    TrackerBundle trackerBundle = TrackerBundle.builder().atomicMode(AtomicMode.ALL).validationMode(ValidationMode.SKIP).trackedEntities(Collections.singletonList(new TrackedEntity())).enrollments(Collections.singletonList(new Enrollment())).events(Collections.singletonList(new Event())).build();
    assertEquals(AtomicMode.ALL, trackerBundle.getAtomicMode());
    assertSame(trackerBundle.getValidationMode(), ValidationMode.SKIP);
    assertFalse(trackerBundle.getTrackedEntities().isEmpty());
    assertFalse(trackerBundle.getEnrollments().isEmpty());
    assertFalse(trackerBundle.getEvents().isEmpty());
}
Also used : TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Event(org.hisp.dhis.tracker.domain.Event) Test(org.junit.jupiter.api.Test)

Aggregations

Enrollment (org.hisp.dhis.tracker.domain.Enrollment)88 Test (org.junit.jupiter.api.Test)67 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)53 Event (org.hisp.dhis.tracker.domain.Event)25 TrackedEntity (org.hisp.dhis.tracker.domain.TrackedEntity)25 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)24 Program (org.hisp.dhis.program.Program)21 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)19 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)17 List (java.util.List)16 Optional (java.util.Optional)15 ProgramInstance (org.hisp.dhis.program.ProgramInstance)15 TrackerPreheat (org.hisp.dhis.tracker.preheat.TrackerPreheat)15 Sets (com.google.common.collect.Sets)14 Collectors (java.util.stream.Collectors)14 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)14 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)14 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)14 BeforeEach (org.junit.jupiter.api.BeforeEach)14 Mockito.when (org.mockito.Mockito.when)14