Search in sources :

Example 11 with TrackerImportValidationContext

use of org.hisp.dhis.tracker.validation.TrackerImportValidationContext 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 12 with TrackerImportValidationContext

use of org.hisp.dhis.tracker.validation.TrackerImportValidationContext in project dhis2-core by dhis2.

the class PreCheckDataRelationsValidationHook method validateRelationshipReference.

private void validateRelationshipReference(ValidationErrorReporter reporter, Relationship relationship, RelationshipItem item) {
    Optional<String> uid = getUidFromRelationshipItem(item);
    TrackerType trackerType = relationshipItemValueType(item);
    TrackerImportValidationContext ctx = reporter.getValidationContext();
    if (TRACKED_ENTITY.equals(trackerType)) {
        if (uid.isPresent() && !ValidationUtils.trackedEntityInstanceExist(ctx, uid.get())) {
            reporter.addError(relationship, E4012, trackerType.getName(), uid.get());
        }
    } else if (ENROLLMENT.equals(trackerType)) {
        if (uid.isPresent() && !ValidationUtils.enrollmentExist(ctx, uid.get())) {
            reporter.addError(relationship, E4012, trackerType.getName(), uid.get());
        }
    } else if (EVENT.equals(trackerType) && uid.isPresent() && !ValidationUtils.eventExist(ctx, uid.get())) {
        reporter.addError(relationship, E4012, trackerType.getName(), uid.get());
    }
}
Also used : TrackerType(org.hisp.dhis.tracker.TrackerType) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext)

Example 13 with TrackerImportValidationContext

use of org.hisp.dhis.tracker.validation.TrackerImportValidationContext in project dhis2-core by dhis2.

the class PreCheckMandatoryFieldsValidationHook method validateEvent.

@Override
public void validateEvent(ValidationErrorReporter reporter, Event event) {
    reporter.addErrorIf(() -> StringUtils.isEmpty(event.getOrgUnit()), event, E1123, ORG_UNIT);
    reporter.addErrorIf(() -> StringUtils.isEmpty(event.getProgramStage()), event, E1123, "programStage");
    // TODO remove if once metadata import is fixed
    TrackerImportValidationContext context = reporter.getValidationContext();
    ProgramStage programStage = context.getProgramStage(event.getProgramStage());
    if (programStage != null) {
        // Program stages should always have a program! Due to how metadata
        // import is currently implemented
        // it's possible that users run into the edge case that a program
        // stage does not have an associated
        // program. Tell the user it's an issue with the metadata and not
        // the event itself. This should be
        // fixed in the metadata import. For more see
        // https://jira.dhis2.org/browse/DHIS2-12123
        reporter.addErrorIfNull(programStage.getProgram(), event, E1008, event.getProgramStage());
        // the DB entry of the program stage and not the payload itself
        return;
    }
    reporter.addErrorIf(() -> StringUtils.isEmpty(event.getProgram()), event, E1123, "program");
}
Also used : TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) ProgramStage(org.hisp.dhis.program.ProgramStage)

Example 14 with TrackerImportValidationContext

use of org.hisp.dhis.tracker.validation.TrackerImportValidationContext 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 15 with TrackerImportValidationContext

use of org.hisp.dhis.tracker.validation.TrackerImportValidationContext in project dhis2-core by dhis2.

the class EnrollmentInExistingValidationHook method validateEnrollment.

@Override
public void validateEnrollment(ValidationErrorReporter reporter, Enrollment enrollment) {
    checkNotNull(enrollment, ENROLLMENT_CANT_BE_NULL);
    if (EnrollmentStatus.CANCELLED == enrollment.getStatus()) {
        return;
    }
    TrackerImportValidationContext validationContext = reporter.getValidationContext();
    Program program = validationContext.getProgram(enrollment.getProgram());
    checkNotNull(program, PROGRAM_CANT_BE_NULL);
    if ((EnrollmentStatus.COMPLETED == enrollment.getStatus() && Boolean.FALSE.equals(program.getOnlyEnrollOnce()))) {
        return;
    }
    validateTeiNotEnrolledAlready(reporter, enrollment, program);
}
Also used : TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext)

Aggregations

TrackerImportValidationContext (org.hisp.dhis.tracker.validation.TrackerImportValidationContext)46 Program (org.hisp.dhis.program.Program)14 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)14 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)13 ProgramStage (org.hisp.dhis.program.ProgramStage)11 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)10 TrackerImportStrategy (org.hisp.dhis.tracker.TrackerImportStrategy)8 Test (org.junit.jupiter.api.Test)8 List (java.util.List)7 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)7 Event (org.hisp.dhis.tracker.domain.Event)7 Note (org.hisp.dhis.tracker.domain.Note)7 Collectors (java.util.stream.Collectors)6 ProgramInstance (org.hisp.dhis.program.ProgramInstance)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 TrackerErrorCode (org.hisp.dhis.tracker.report.TrackerErrorCode)5 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)4 TrackedEntityComment (org.hisp.dhis.trackedentitycomment.TrackedEntityComment)4 TrackerType (org.hisp.dhis.tracker.TrackerType)4 User (org.hisp.dhis.user.User)4