Search in sources :

Example 1 with UniqueAttributeValue

use of org.hisp.dhis.tracker.preheat.UniqueAttributeValue in project dhis2-core by dhis2.

the class AttributeValidationHook method validateAttributeUniqueness.

protected void validateAttributeUniqueness(ValidationErrorReporter errorReporter, TrackerDto dto, String value, TrackedEntityAttribute trackedEntityAttribute, TrackedEntityInstance trackedEntityInstance, OrganisationUnit organisationUnit) {
    checkNotNull(trackedEntityAttribute, TRACKED_ENTITY_ATTRIBUTE_CANT_BE_NULL);
    if (Boolean.FALSE.equals(trackedEntityAttribute.isUnique()))
        return;
    List<UniqueAttributeValue> uniqueAttributeValues = errorReporter.getValidationContext().getBundle().getPreheat().getUniqueAttributeValues();
    for (UniqueAttributeValue uniqueAttributeValue : uniqueAttributeValues) {
        boolean isTeaUniqueInOrgUnitScope = !trackedEntityAttribute.getOrgunitScope() || Objects.equals(organisationUnit.getUid(), uniqueAttributeValue.getOrgUnitId());
        boolean isTheSameTea = Objects.equals(uniqueAttributeValue.getAttributeUid(), trackedEntityAttribute.getUid());
        boolean hasTheSameValue = Objects.equals(uniqueAttributeValue.getValue(), value);
        boolean isNotSameTei = trackedEntityInstance == null || !Objects.equals(trackedEntityInstance.getUid(), uniqueAttributeValue.getTeiUid());
        if (isTeaUniqueInOrgUnitScope && isTheSameTea && hasTheSameValue && isNotSameTei) {
            TrackerBundle bundle = errorReporter.getValidationContext().getBundle();
            TrackerErrorReport err = TrackerErrorReport.builder().uid(dto.getUid()).trackerType(dto.getTrackerType()).errorCode(TrackerErrorCode.E1064).addArg(value).addArg(trackedEntityAttribute.getUid()).build(bundle);
            errorReporter.addError(err);
            return;
        }
    }
}
Also used : UniqueAttributeValue(org.hisp.dhis.tracker.preheat.UniqueAttributeValue) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) TrackerErrorReport(org.hisp.dhis.tracker.report.TrackerErrorReport)

Aggregations

TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)1 UniqueAttributeValue (org.hisp.dhis.tracker.preheat.UniqueAttributeValue)1 TrackerErrorReport (org.hisp.dhis.tracker.report.TrackerErrorReport)1