use of org.hisp.dhis.tracker.domain.Attribute 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.Attribute in project dhis2-core by dhis2.
the class AssignValueImplementer method createAttribute.
private Attribute createAttribute(String attributeUid, String newValue) {
Attribute attribute = new Attribute();
attribute.setAttribute(attributeUid);
attribute.setValue(newValue);
return attribute;
}
use of org.hisp.dhis.tracker.domain.Attribute in project dhis2-core by dhis2.
the class TrackedEntityAttributeValidationHook method validateAttributes.
protected void validateAttributes(ValidationErrorReporter reporter, TrackedEntity trackedEntity, TrackedEntityInstance tei, OrganisationUnit orgUnit, TrackedEntityType trackedEntityType) {
checkNotNull(trackedEntity, TrackerImporterAssertErrors.TRACKED_ENTITY_CANT_BE_NULL);
checkNotNull(trackedEntityType, TrackerImporterAssertErrors.TRACKED_ENTITY_TYPE_CANT_BE_NULL);
Map<String, TrackedEntityAttributeValue> valueMap = new HashMap<>();
if (tei != null) {
valueMap = tei.getTrackedEntityAttributeValues().stream().collect(Collectors.toMap(v -> v.getAttribute().getUid(), v -> v));
}
for (Attribute attribute : trackedEntity.getAttributes()) {
TrackedEntityAttribute tea = reporter.getValidationContext().getTrackedEntityAttribute(attribute.getAttribute());
if (tea == null) {
reporter.addError(trackedEntity, E1006, attribute.getAttribute());
continue;
}
if (attribute.getValue() == null) {
Optional<TrackedEntityTypeAttribute> optionalTea = Optional.of(trackedEntityType).map(tet -> tet.getTrackedEntityTypeAttributes().stream()).flatMap(tetAtts -> tetAtts.filter(teaAtt -> teaAtt.getTrackedEntityAttribute().getUid().equals(attribute.getAttribute()) && teaAtt.isMandatory() != null && teaAtt.isMandatory()).findFirst());
if (optionalTea.isPresent())
reporter.addError(trackedEntity, E1076, TrackedEntityAttribute.class.getSimpleName(), attribute.getAttribute());
continue;
}
validateAttributeValue(reporter, trackedEntity, tea, attribute.getValue());
validateAttrValueType(reporter, trackedEntity, attribute, tea);
validateOptionSet(reporter, trackedEntity, tea, attribute.getValue());
validateAttributeUniqueness(reporter, trackedEntity, attribute.getValue(), tea, tei, orgUnit);
validateFileNotAlreadyAssigned(reporter, trackedEntity, attribute, valueMap);
}
}
use of org.hisp.dhis.tracker.domain.Attribute in project dhis2-core by dhis2.
the class AssignValueImplementerTest method testAssignAttributeValueForEnrollmentsWhenAttributeIsAlreadyPresentAndHasTheSameValue.
@Test
void testAssignAttributeValueForEnrollmentsWhenAttributeIsAlreadyPresentAndHasTheSameValue() {
List<Enrollment> enrollments = Lists.newArrayList(getEnrollmentWithAttributeSetSameValue());
bundle.setEnrollments(enrollments);
bundle.setRuleEffects(getRuleEnrollmentEffects(enrollments));
Map<String, List<ProgramRuleIssue>> enrollmentIssues = implementerToTest.validateEnrollments(bundle);
Enrollment enrollment = bundle.getEnrollments().stream().filter(e -> e.getEnrollment().equals(FIRST_ENROLLMENT_ID)).findAny().get();
Optional<Attribute> attribute = enrollment.getAttributes().stream().filter(at -> at.getAttribute().equals(ATTRIBUTE_ID)).findAny();
assertTrue(attribute.isPresent());
assertEquals(TEI_ATTRIBUTE_NEW_VALUE, attribute.get().getValue());
assertEquals(1, enrollmentIssues.size());
assertEquals(1, enrollmentIssues.get(FIRST_ENROLLMENT_ID).size());
assertEquals(WARNING, enrollmentIssues.get(FIRST_ENROLLMENT_ID).get(0).getIssueType());
}
use of org.hisp.dhis.tracker.domain.Attribute in project dhis2-core by dhis2.
the class AssignValueImplementerTest method testAssignAttributeValueForEnrollmentsWhenAttributeIsAlreadyPresentInTeiAndCanBeOverwritten.
@Test
void testAssignAttributeValueForEnrollmentsWhenAttributeIsAlreadyPresentInTeiAndCanBeOverwritten() {
when(systemSettingManager.getBooleanSetting(SettingKey.RULE_ENGINE_ASSIGN_OVERWRITE)).thenReturn(Boolean.TRUE);
List<Enrollment> enrollments = Lists.newArrayList(getEnrollmentWithAttributeNOTSet());
List<TrackedEntity> trackedEntities = Lists.newArrayList(getTrackedEntitiesWithAttributeSet());
bundle.setEnrollments(enrollments);
bundle.setTrackedEntities(trackedEntities);
bundle.setRuleEffects(getRuleEnrollmentEffects(enrollments));
Map<String, List<ProgramRuleIssue>> enrollmentIssues = implementerToTest.validateEnrollments(bundle);
Enrollment enrollment = bundle.getEnrollments().stream().filter(e -> e.getEnrollment().equals(SECOND_ENROLLMENT_ID)).findAny().get();
TrackedEntity trackedEntity = bundle.getTrackedEntities().stream().filter(e -> e.getTrackedEntity().equals(TRACKED_ENTITY_ID)).findAny().get();
Optional<Attribute> enrollmentAttribute = enrollment.getAttributes().stream().filter(at -> at.getAttribute().equals(ATTRIBUTE_ID)).findAny();
Optional<Attribute> teiAttribute = trackedEntity.getAttributes().stream().filter(at -> at.getAttribute().equals(ATTRIBUTE_ID)).findAny();
assertFalse(enrollmentAttribute.isPresent());
assertTrue(teiAttribute.isPresent());
assertEquals(TEI_ATTRIBUTE_NEW_VALUE, teiAttribute.get().getValue());
assertEquals(1, enrollmentIssues.size());
assertEquals(1, enrollmentIssues.get(SECOND_ENROLLMENT_ID).size());
assertEquals(WARNING, enrollmentIssues.get(SECOND_ENROLLMENT_ID).get(0).getIssueType());
}
Aggregations