Search in sources :

Example 11 with Attribute

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

the class AssignValueImplementer method isTheSameValue.

private boolean isTheSameValue(EnrollmentActionRule actionRule, TrackerPreheat preheat) {
    TrackedEntityAttribute attribute = preheat.get(TrackedEntityAttribute.class, actionRule.getField());
    String value = actionRule.getValue();
    Optional<Attribute> optionalAttribute = actionRule.getAttributes().stream().filter(at -> at.getAttribute().equals(actionRule.getField())).findAny();
    if (optionalAttribute.isPresent()) {
        return areEquals(value, optionalAttribute.get().getValue(), attribute.getValueType());
    }
    return false;
}
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) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) Attribute(org.hisp.dhis.tracker.domain.Attribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute)

Example 12 with Attribute

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

the class LastUpdateImportTest method shouldUpdateTeiIfTeiIsUpdated.

@Test
void shouldUpdateTeiIfTeiIsUpdated() throws IOException {
    TrackerImportParams trackerImportParams = fromJson("tracker/single_tei.json", user.getUid());
    trackerImportParams.setImportStrategy(TrackerImportStrategy.UPDATE);
    Attribute attribute = new Attribute();
    attribute.setAttribute("toUpdate000");
    attribute.setValue("value");
    trackedEntity.setAttributes(Collections.singletonList(attribute));
    Date lastUpdateBefore = trackedEntityInstanceService.getTrackedEntityInstance(trackedEntity.getTrackedEntity()).getLastUpdated();
    assertNoImportErrors(trackerImportService.importTracker(trackerImportParams));
    assertTrue(manager.get(TrackedEntityInstance.class, trackedEntity.getTrackedEntity()).getLastUpdated().getTime() > lastUpdateBefore.getTime());
}
Also used : TrackerImportParams(org.hisp.dhis.tracker.TrackerImportParams) Attribute(org.hisp.dhis.tracker.domain.Attribute) Date(java.util.Date) Test(org.junit.jupiter.api.Test) TrackerTest(org.hisp.dhis.tracker.TrackerTest)

Example 13 with Attribute

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

the class AbstractTrackerPersister method handleTrackedEntityAttributeValues.

protected void handleTrackedEntityAttributeValues(Session session, TrackerPreheat preheat, List<Attribute> payloadAttributes, TrackedEntityInstance trackedEntityInstance) {
    if (payloadAttributes.isEmpty()) {
        return;
    }
    Map<String, TrackedEntityAttributeValue> attributeValueByUid = trackedEntityInstance.getTrackedEntityAttributeValues().stream().collect(Collectors.toMap(teav -> teav.getAttribute().getUid(), Function.identity()));
    payloadAttributes.forEach(attribute -> {
        // We cannot get the value from attributeToStore because it uses
        // encryption logic, so we need to use the one from payload
        boolean isDelete = StringUtils.isEmpty(attribute.getValue());
        TrackedEntityAttributeValue trackedEntityAttributeValue = attributeValueByUid.get(attribute.getAttribute());
        boolean isUpdated = false;
        boolean isNew = Objects.isNull(trackedEntityAttributeValue);
        if (isDelete && isNew) {
            return;
        }
        if (isDelete) {
            delete(session, preheat, trackedEntityAttributeValue, trackedEntityInstance);
        } else {
            if (!isNew) {
                isUpdated = !trackedEntityAttributeValue.getPlainValue().equals(attribute.getValue());
            }
            trackedEntityAttributeValue = Optional.ofNullable(trackedEntityAttributeValue).orElseGet(() -> new TrackedEntityAttributeValue().setAttribute(getTrackedEntityAttributeFromPreheat(preheat, attribute.getAttribute())).setEntityInstance(trackedEntityInstance)).setStoredBy(attribute.getStoredBy()).setValue(attribute.getValue());
            saveOrUpdate(session, preheat, isNew, trackedEntityInstance, trackedEntityAttributeValue, isUpdated);
        }
        handleReservedValue(trackedEntityAttributeValue);
    });
}
Also used : ValueType(org.hisp.dhis.common.ValueType) Attribute(org.hisp.dhis.tracker.domain.Attribute) RequiredArgsConstructor(lombok.RequiredArgsConstructor) ReservedValueService(org.hisp.dhis.reservedvalue.ReservedValueService) Session(org.hibernate.Session) AuditType(org.hisp.dhis.common.AuditType) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackerObjectReport(org.hisp.dhis.tracker.report.TrackerObjectReport) Function(java.util.function.Function) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) TrackedEntityAttributeValueAuditService(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueAuditService) AtomicMode(org.hisp.dhis.tracker.AtomicMode) AccessLevel(lombok.AccessLevel) Map(java.util.Map) FlushMode(org.hisp.dhis.tracker.FlushMode) TrackerSideEffectDataBundle(org.hisp.dhis.tracker.job.TrackerSideEffectDataBundle) TrackedEntityAttributeValueAudit(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueAudit) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) FileResource(org.hisp.dhis.fileresource.FileResource) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Set(java.util.Set) TrackerType(org.hisp.dhis.tracker.TrackerType) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Optional(java.util.Optional) TrackerDto(org.hisp.dhis.tracker.domain.TrackerDto) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) TrackerTypeReport(org.hisp.dhis.tracker.report.TrackerTypeReport) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)

Example 14 with Attribute

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

the class EnrollmentAttributeValidationHook method validateEnrollment.

@Override
public void validateEnrollment(ValidationErrorReporter reporter, Enrollment enrollment) {
    TrackerImportValidationContext context = reporter.getValidationContext();
    Program program = context.getProgram(enrollment.getProgram());
    checkNotNull(program, TrackerImporterAssertErrors.PROGRAM_CANT_BE_NULL);
    TrackedEntityInstance tei = context.getTrackedEntityInstance(enrollment.getTrackedEntity());
    OrganisationUnit orgUnit = context.getOrganisationUnit(getOrgUnitUidFromTei(context, enrollment.getTrackedEntity()));
    Map<String, String> attributeValueMap = Maps.newHashMap();
    for (Attribute attribute : enrollment.getAttributes()) {
        validateRequiredProperties(reporter, enrollment, attribute, program);
        TrackedEntityAttribute teAttribute = context.getTrackedEntityAttribute(attribute.getAttribute());
        if (attribute.getAttribute() != null && attribute.getValue() != null && teAttribute != null) {
            attributeValueMap.put(attribute.getAttribute(), attribute.getValue());
            validateAttrValueType(reporter, enrollment, attribute, teAttribute);
            validateOptionSet(reporter, enrollment, teAttribute, attribute.getValue());
            validateAttributeUniqueness(reporter, enrollment, attribute.getValue(), teAttribute, tei, orgUnit);
        }
    }
    validateMandatoryAttributes(reporter, program, attributeValueMap, enrollment);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) Attribute(org.hisp.dhis.tracker.domain.Attribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Example 15 with Attribute

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

the class EnrollmentAttributeValidationHook method validateRequiredProperties.

protected void validateRequiredProperties(ValidationErrorReporter reporter, Enrollment enrollment, Attribute attribute, Program program) {
    reporter.addErrorIfNull(attribute.getAttribute(), enrollment, E1075, attribute);
    Optional<ProgramTrackedEntityAttribute> optionalTrackedAttr = program.getProgramAttributes().stream().filter(pa -> pa.getAttribute().getUid().equals(attribute.getAttribute()) && pa.isMandatory()).findFirst();
    if (optionalTrackedAttr.isPresent()) {
        reporter.addErrorIfNull(attribute.getValue(), enrollment, E1076, TrackedEntityAttribute.class.getSimpleName(), attribute.getAttribute());
    }
    if (attribute.getAttribute() != null) {
        TrackedEntityAttribute teAttribute = reporter.getValidationContext().getTrackedEntityAttribute(attribute.getAttribute());
        reporter.addErrorIfNull(teAttribute, enrollment, E1006, attribute.getAttribute());
    }
}
Also used : Attribute(org.hisp.dhis.tracker.domain.Attribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Program(org.hisp.dhis.program.Program) E1019(org.hisp.dhis.tracker.report.TrackerErrorCode.E1019) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) E1018(org.hisp.dhis.tracker.report.TrackerErrorCode.E1018) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) Map(java.util.Map) TrackerIdScheme(org.hisp.dhis.tracker.TrackerIdScheme) ReferenceTrackerEntity(org.hisp.dhis.tracker.preheat.ReferenceTrackerEntity) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) E1076(org.hisp.dhis.tracker.report.TrackerErrorCode.E1076) Set(java.util.Set) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) TrackedAttributeValidationService(org.hisp.dhis.tracker.validation.service.attribute.TrackedAttributeValidationService) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) E1075(org.hisp.dhis.tracker.report.TrackerErrorCode.E1075) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Component(org.springframework.stereotype.Component) E1006(org.hisp.dhis.tracker.report.TrackerErrorCode.E1006) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) Optional(java.util.Optional) Collections(java.util.Collections) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute)

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