Search in sources :

Example 31 with TrackerBundle

use of org.hisp.dhis.tracker.bundle.TrackerBundle in project dhis2-core by dhis2.

the class RelationshipsValidationHook method validateRelationship.

public void validateRelationship(ValidationErrorReporter reporter, Relationship relationship) {
    TrackerImportValidationContext context = reporter.getValidationContext();
    TrackerBundle bundle = context.getBundle();
    boolean isValid = validateMandatoryData(reporter, relationship, bundle.getPreheat().getAll(RelationshipType.class));
    // Relationship
    if (isValid) {
        validateRelationshipLinkToOneEntity(reporter, relationship);
        validateRelationshipConstraint(reporter, relationship, bundle);
        validateAutoRelationship(reporter, relationship);
        validateReferences(reporter, relationship, relationship.getFrom());
        validateReferences(reporter, relationship, relationship.getTo());
    }
}
Also used : TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) RelationshipType(org.hisp.dhis.relationship.RelationshipType) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle)

Example 32 with TrackerBundle

use of org.hisp.dhis.tracker.bundle.TrackerBundle in project dhis2-core by dhis2.

the class RepeatedEventsValidationHook method validate.

@Override
public void validate(ValidationErrorReporter reporter, TrackerImportValidationContext context) {
    TrackerBundle bundle = context.getBundle();
    Map<Pair<String, String>, List<Event>> eventsByEnrollmentAndNotRepeatableProgramStage = bundle.getEvents().stream().filter(e -> !reporter.isInvalid(e)).filter(e -> !context.getStrategy(e).isDelete()).filter(e -> {
        ProgramStage programStage = context.getProgramStage(e.getProgramStage());
        return programStage.getProgram().isRegistration() && !programStage.getRepeatable();
    }).collect(Collectors.groupingBy(e -> Pair.of(e.getProgramStage(), e.getEnrollment())));
    for (Map.Entry<Pair<String, String>, List<Event>> mapEntry : eventsByEnrollmentAndNotRepeatableProgramStage.entrySet()) {
        if (mapEntry.getValue().size() > 1) {
            for (Event event : mapEntry.getValue()) {
                reporter.addError(event, TrackerErrorCode.E1039, mapEntry.getKey().getLeft());
            }
        }
    }
    bundle.getEvents().forEach(e -> validateNotMultipleEvents(reporter, context, e));
}
Also used : Event(org.hisp.dhis.tracker.domain.Event) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Collectors(java.util.stream.Collectors) ProgramStage(org.hisp.dhis.program.ProgramStage) TrackerErrorCode(org.hisp.dhis.tracker.report.TrackerErrorCode) List(java.util.List) Component(org.springframework.stereotype.Component) Pair(org.apache.commons.lang3.tuple.Pair) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) Map(java.util.Map) TrackerImportStrategy(org.hisp.dhis.tracker.TrackerImportStrategy) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Event(org.hisp.dhis.tracker.domain.Event) List(java.util.List) ProgramStage(org.hisp.dhis.program.ProgramStage) Map(java.util.Map) Pair(org.apache.commons.lang3.tuple.Pair)

Example 33 with TrackerBundle

use of org.hisp.dhis.tracker.bundle.TrackerBundle 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)

Example 34 with TrackerBundle

use of org.hisp.dhis.tracker.bundle.TrackerBundle in project dhis2-core by dhis2.

the class AttributeValidationHook method validateAttrValueType.

protected void validateAttrValueType(ValidationErrorReporter errorReporter, TrackerDto dto, Attribute attr, TrackedEntityAttribute teAttr) {
    checkNotNull(attr, ATTRIBUTE_CANT_BE_NULL);
    checkNotNull(teAttr, TRACKED_ENTITY_ATTRIBUTE_CANT_BE_NULL);
    ValueType valueType = teAttr.getValueType();
    TrackerImportValidationContext context = errorReporter.getValidationContext();
    String error;
    if (valueType.equals(ValueType.ORGANISATION_UNIT)) {
        error = context.getOrganisationUnit(attr.getValue()) == null ? " Value " + attr.getValue() + " is not a valid org unit value" : null;
    } else if (valueType.equals(ValueType.USERNAME)) {
        error = context.usernameExists(attr.getValue()) ? null : " Value " + attr.getValue() + " is not a valid username value";
    } else {
        // org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:945)
        try {
            error = teAttrService.validateValueType(teAttr, attr.getValue());
        } catch (Exception e) {
            error = e.getMessage();
        }
    }
    if (error != null) {
        TrackerBundle bundle = context.getBundle();
        TrackerErrorReport err = TrackerErrorReport.builder().uid(dto.getUid()).trackerType(dto.getTrackerType()).errorCode(TrackerErrorCode.E1007).addArg(valueType.toString()).addArg(error).build(bundle);
        errorReporter.addError(err);
    }
}
Also used : ValueType(org.hisp.dhis.common.ValueType) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) TrackerErrorReport(org.hisp.dhis.tracker.report.TrackerErrorReport)

Example 35 with TrackerBundle

use of org.hisp.dhis.tracker.bundle.TrackerBundle in project dhis2-core by dhis2.

the class EventCategoryOptValidationHookTest method setUp.

@BeforeEach
public void setUp() {
    initServices();
    hook = new EventCategoryOptValidationHook(i18nManager);
    catOption = createCategoryOption('A');
    category = createCategory('A', catOption);
    catCombo = createCategoryCombo('A', category);
    attOptionCombo = createCategoryOptionCombo(catCombo, catOption);
    defaultCatCombo = new CategoryCombo();
    defaultCatCombo.setName(DEFAULT_CATEGORY_COMBO_NAME);
    defaultCatOption = new CategoryOption();
    defaultCatOption.setName(DEFAULT_NAME);
    defaultCatOptionCombo = createCategoryOptionCombo(defaultCatCombo, defaultCatOption);
    program = createProgram('A');
    program.setCategoryCombo(catCombo);
    event = new Event();
    event.setEvent(CodeGenerator.generateUid());
    event.setProgram(program.getUid());
    event.setOccurredAt(EVENT_INSTANT);
    User user = createUser('A');
    TrackerBundle bundle = TrackerBundle.builder().user(user).build();
    when(validationContext.getBundle()).thenReturn(bundle);
    when(validationContext.getProgram(program.getUid())).thenReturn(program);
    reporter = new ValidationErrorReporter(validationContext);
}
Also used : User(org.hisp.dhis.user.User) CategoryCombo(org.hisp.dhis.category.CategoryCombo) CategoryOption(org.hisp.dhis.category.CategoryOption) Event(org.hisp.dhis.tracker.domain.Event) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)74 Test (org.junit.jupiter.api.Test)43 Event (org.hisp.dhis.tracker.domain.Event)29 User (org.hisp.dhis.user.User)21 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)19 List (java.util.List)17 Enrollment (org.hisp.dhis.tracker.domain.Enrollment)15 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)14 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)14 Mockito.mock (org.mockito.Mockito.mock)14 TrackerErrorCode (org.hisp.dhis.tracker.report.TrackerErrorCode)13 TrackerImportStrategy (org.hisp.dhis.tracker.TrackerImportStrategy)12 TrackedEntity (org.hisp.dhis.tracker.domain.TrackedEntity)12 TrackerImportValidationContext (org.hisp.dhis.tracker.validation.TrackerImportValidationContext)12 Collections (java.util.Collections)11 ProgramStage (org.hisp.dhis.program.ProgramStage)11 Objects (java.util.Objects)10 Program (org.hisp.dhis.program.Program)10 ValidationMode (org.hisp.dhis.tracker.ValidationMode)10 Relationship (org.hisp.dhis.tracker.domain.Relationship)10