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()));
}
}
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;
}
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);
}
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()));
}
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());
}
Aggregations