Search in sources :

Example 16 with Attribute

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()));
    }
}
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 17 with Attribute

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;
}
Also used : Attribute(org.hisp.dhis.tracker.domain.Attribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute)

Example 18 with 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);
    }
}
Also used : Attribute(org.hisp.dhis.tracker.domain.Attribute) Constant(org.hisp.dhis.tracker.util.Constant) HashMap(java.util.HashMap) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) E1084(org.hisp.dhis.tracker.report.TrackerErrorCode.E1084) E1085(org.hisp.dhis.tracker.report.TrackerErrorCode.E1085) TRACKED_ENTITY_ATTRIBUTE_VALUE_CANT_BE_NULL(org.hisp.dhis.tracker.validation.hooks.TrackerImporterAssertErrors.TRACKED_ENTITY_ATTRIBUTE_VALUE_CANT_BE_NULL) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) Map(java.util.Map) E1090(org.hisp.dhis.tracker.report.TrackerErrorCode.E1090) ATTRIBUTE_CANT_BE_NULL(org.hisp.dhis.tracker.validation.hooks.TrackerImporterAssertErrors.ATTRIBUTE_CANT_BE_NULL) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) DhisConfigurationProvider(org.hisp.dhis.external.conf.DhisConfigurationProvider) FileResource(org.hisp.dhis.fileresource.FileResource) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) E1077(org.hisp.dhis.tracker.report.TrackerErrorCode.E1077) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) E1076(org.hisp.dhis.tracker.report.TrackerErrorCode.E1076) Set(java.util.Set) E1112(org.hisp.dhis.tracker.report.TrackerErrorCode.E1112) TrackedAttributeValidationService(org.hisp.dhis.tracker.validation.service.attribute.TrackedAttributeValidationService) Collectors(java.util.stream.Collectors) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ValidationUtils.dataValueIsValid(org.hisp.dhis.system.util.ValidationUtils.dataValueIsValid) Component(org.springframework.stereotype.Component) E1009(org.hisp.dhis.tracker.report.TrackerErrorCode.E1009) E1006(org.hisp.dhis.tracker.report.TrackerErrorCode.E1006) Optional(java.util.Optional) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) HashMap(java.util.HashMap) Attribute(org.hisp.dhis.tracker.domain.Attribute) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute)

Example 19 with Attribute

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());
}
Also used : Strictness(org.mockito.quality.Strictness) WARNING(org.hisp.dhis.tracker.programrule.IssueType.WARNING) DataValue(org.hisp.dhis.tracker.domain.DataValue) BeforeEach(org.junit.jupiter.api.BeforeEach) TRACKED_ENTITY_ATTRIBUTE(org.hisp.dhis.rules.models.AttributeType.TRACKED_ENTITY_ATTRIBUTE) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) RuleEffects(org.hisp.dhis.rules.models.RuleEffects) ERROR(org.hisp.dhis.tracker.programrule.IssueType.ERROR) RuleActionAssign(org.hisp.dhis.rules.models.RuleActionAssign) ValueType(org.hisp.dhis.common.ValueType) Attribute(org.hisp.dhis.tracker.domain.Attribute) Mock(org.mockito.Mock) RuleEffect(org.hisp.dhis.rules.models.RuleEffect) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) ENROLLMENT(org.hisp.dhis.rules.models.TrackerObjectType.ENROLLMENT) DataElement(org.hisp.dhis.dataelement.DataElement) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Lists(com.google.common.collect.Lists) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) DATA_ELEMENT(org.hisp.dhis.rules.models.AttributeType.DATA_ELEMENT) Map(java.util.Map) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) AssignValueImplementer(org.hisp.dhis.tracker.programrule.implementers.AssignValueImplementer) InjectMocks(org.mockito.InjectMocks) Event(org.hisp.dhis.tracker.domain.Event) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) EVENT(org.hisp.dhis.rules.models.TrackerObjectType.EVENT) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Set(java.util.Set) EventStatus(org.hisp.dhis.event.EventStatus) Mockito.when(org.mockito.Mockito.when) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) ProgramStage(org.hisp.dhis.program.ProgramStage) EnrollmentStatus(org.hisp.dhis.tracker.domain.EnrollmentStatus) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) List(java.util.List) RuleAction(org.hisp.dhis.rules.models.RuleAction) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) SettingKey(org.hisp.dhis.setting.SettingKey) ValidationStrategy(org.hisp.dhis.program.ValidationStrategy) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest) Attribute(org.hisp.dhis.tracker.domain.Attribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) List(java.util.List) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 20 with Attribute

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());
}
Also used : Strictness(org.mockito.quality.Strictness) WARNING(org.hisp.dhis.tracker.programrule.IssueType.WARNING) DataValue(org.hisp.dhis.tracker.domain.DataValue) BeforeEach(org.junit.jupiter.api.BeforeEach) TRACKED_ENTITY_ATTRIBUTE(org.hisp.dhis.rules.models.AttributeType.TRACKED_ENTITY_ATTRIBUTE) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) RuleEffects(org.hisp.dhis.rules.models.RuleEffects) ERROR(org.hisp.dhis.tracker.programrule.IssueType.ERROR) RuleActionAssign(org.hisp.dhis.rules.models.RuleActionAssign) ValueType(org.hisp.dhis.common.ValueType) Attribute(org.hisp.dhis.tracker.domain.Attribute) Mock(org.mockito.Mock) RuleEffect(org.hisp.dhis.rules.models.RuleEffect) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) ENROLLMENT(org.hisp.dhis.rules.models.TrackerObjectType.ENROLLMENT) DataElement(org.hisp.dhis.dataelement.DataElement) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Lists(com.google.common.collect.Lists) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) DATA_ELEMENT(org.hisp.dhis.rules.models.AttributeType.DATA_ELEMENT) Map(java.util.Map) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) AssignValueImplementer(org.hisp.dhis.tracker.programrule.implementers.AssignValueImplementer) InjectMocks(org.mockito.InjectMocks) Event(org.hisp.dhis.tracker.domain.Event) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) EVENT(org.hisp.dhis.rules.models.TrackerObjectType.EVENT) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Set(java.util.Set) EventStatus(org.hisp.dhis.event.EventStatus) Mockito.when(org.mockito.Mockito.when) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) ProgramStage(org.hisp.dhis.program.ProgramStage) EnrollmentStatus(org.hisp.dhis.tracker.domain.EnrollmentStatus) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) List(java.util.List) RuleAction(org.hisp.dhis.rules.models.RuleAction) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) SettingKey(org.hisp.dhis.setting.SettingKey) ValidationStrategy(org.hisp.dhis.program.ValidationStrategy) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest) 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) List(java.util.List) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Aggregations

Attribute (org.hisp.dhis.tracker.domain.Attribute)24 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)22 Map (java.util.Map)11 Optional (java.util.Optional)11 Set (java.util.Set)11 TrackedEntity (org.hisp.dhis.tracker.domain.TrackedEntity)11 Test (org.junit.jupiter.api.Test)11 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)10 Enrollment (org.hisp.dhis.tracker.domain.Enrollment)10 List (java.util.List)9 ValueType (org.hisp.dhis.common.ValueType)9 TrackerPreheat (org.hisp.dhis.tracker.preheat.TrackerPreheat)9 Lists (com.google.common.collect.Lists)8 Sets (com.google.common.collect.Sets)8 DataElement (org.hisp.dhis.dataelement.DataElement)8 RuleActionAssign (org.hisp.dhis.rules.models.RuleActionAssign)8 SettingKey (org.hisp.dhis.setting.SettingKey)8 SystemSettingManager (org.hisp.dhis.setting.SystemSettingManager)8 DataValue (org.hisp.dhis.tracker.domain.DataValue)8 Event (org.hisp.dhis.tracker.domain.Event)8