Search in sources :

Example 46 with TrackerBundle

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

the class SetMandatoryFieldValidator method checkMandatoryDataElement.

private List<ProgramRuleIssue> checkMandatoryDataElement(Event event, List<EventActionRule> actionRules, TrackerBundle bundle) {
    ProgramStage programStage = bundle.getPreheat().get(ProgramStage.class, event.getProgramStage());
    Map<String, EventActionRule> mandatoryDataElementsByActionRule = actionRules.stream().filter(eventActionRule -> eventActionRule.getAttributeType() == AttributeType.DATA_ELEMENT).collect(Collectors.toMap(EventActionRule::getField, Function.identity()));
    return validateMandatoryDataValue(programStage, event, Lists.newArrayList(mandatoryDataElementsByActionRule.keySet())).stream().map(e -> new ProgramRuleIssue(mandatoryDataElementsByActionRule.get(e).getRuleUid(), TrackerErrorCode.E1301, Lists.newArrayList(e), IssueType.ERROR)).collect(Collectors.toList());
}
Also used : Event(org.hisp.dhis.tracker.domain.Event) Attribute(org.hisp.dhis.tracker.domain.Attribute) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) ProgramStage(org.hisp.dhis.program.ProgramStage) TrackerErrorCode(org.hisp.dhis.tracker.report.TrackerErrorCode) Objects(java.util.Objects) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) List(java.util.List) Component(org.springframework.stereotype.Component) Lists(com.google.common.collect.Lists) Map(java.util.Map) Optional(java.util.Optional) org.hisp.dhis.rules.models(org.hisp.dhis.rules.models) ValidationUtils.validateMandatoryDataValue(org.hisp.dhis.tracker.validation.hooks.ValidationUtils.validateMandatoryDataValue) org.hisp.dhis.tracker.programrule(org.hisp.dhis.tracker.programrule) ProgramStage(org.hisp.dhis.program.ProgramStage)

Example 47 with TrackerBundle

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

the class PreCheckSecurityOwnershipValidationHook method validateEnrollment.

@Override
public void validateEnrollment(ValidationErrorReporter reporter, Enrollment enrollment) {
    TrackerImportValidationContext context = reporter.getValidationContext();
    TrackerImportStrategy strategy = context.getStrategy(enrollment);
    TrackerBundle bundle = context.getBundle();
    User user = bundle.getUser();
    Program program = strategy.isUpdateOrDelete() ? context.getProgramInstance(enrollment.getEnrollment()).getProgram() : context.getProgram(enrollment.getProgram());
    OrganisationUnit ownerOrgUnit = context.getOwnerOrganisationUnit(enrollment.getTrackedEntity(), enrollment.getProgram());
    checkNotNull(user, USER_CANT_BE_NULL);
    checkNotNull(enrollment, ENROLLMENT_CANT_BE_NULL);
    checkNotNull(program, PROGRAM_CANT_BE_NULL);
    checkEnrollmentOrgUnit(reporter, context, strategy, enrollment, program);
    if (strategy.isDelete()) {
        boolean hasNonDeletedEvents = context.programInstanceHasEvents(enrollment.getEnrollment());
        boolean hasNotCascadeDeleteAuthority = !user.isAuthorized(Authorities.F_ENROLLMENT_CASCADE_DELETE.getAuthority());
        if (hasNonDeletedEvents && hasNotCascadeDeleteAuthority) {
            TrackerErrorReport error = TrackerErrorReport.builder().uid(enrollment.getUid()).trackerType(TrackerType.ENROLLMENT).errorCode(E1103).addArg(user).addArg(enrollment.getEnrollment()).build(bundle);
            reporter.addError(error);
        }
    }
    checkWriteEnrollmentAccess(reporter, enrollment, program, context, ownerOrgUnit);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) Program(org.hisp.dhis.program.Program) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) TrackerImportStrategy(org.hisp.dhis.tracker.TrackerImportStrategy) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) TrackerErrorReport(org.hisp.dhis.tracker.report.TrackerErrorReport)

Example 48 with TrackerBundle

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

the class PreCheckSecurityOwnershipValidationHook method checkOrgUnitInSearchScope.

private void checkOrgUnitInSearchScope(ValidationErrorReporter reporter, TrackerDto dto, OrganisationUnit orgUnit) {
    TrackerBundle bundle = reporter.getValidationContext().getBundle();
    User user = bundle.getUser();
    checkNotNull(user, USER_CANT_BE_NULL);
    checkNotNull(orgUnit, ORGANISATION_UNIT_CANT_BE_NULL);
    if (!organisationUnitService.isInUserSearchHierarchyCached(user, orgUnit)) {
        TrackerErrorReport error = TrackerErrorReport.builder().uid(dto.getUid()).trackerType(dto.getTrackerType()).errorCode(TrackerErrorCode.E1003).addArg(orgUnit).addArg(user).build(reporter.getValidationContext().getBundle());
        reporter.addError(error);
    }
}
Also used : User(org.hisp.dhis.user.User) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) TrackerErrorReport(org.hisp.dhis.tracker.report.TrackerErrorReport)

Example 49 with TrackerBundle

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

the class PreCheckSecurityOwnershipValidationHook method checkWriteEnrollmentAccess.

private void checkWriteEnrollmentAccess(ValidationErrorReporter reporter, Enrollment enrollment, Program program, TrackerImportValidationContext context, OrganisationUnit ownerOrgUnit) {
    TrackerBundle bundle = reporter.getValidationContext().getBundle();
    User user = bundle.getUser();
    checkNotNull(user, USER_CANT_BE_NULL);
    checkNotNull(program, PROGRAM_CANT_BE_NULL);
    checkProgramWriteAccess(reporter, enrollment, user, program);
    if (program.isRegistration()) {
        String trackedEntity = context.getStrategy(enrollment).isDelete() ? context.getProgramInstance(enrollment.getEnrollment()).getEntityInstance().getUid() : enrollment.getTrackedEntity();
        checkNotNull(program.getTrackedEntityType(), TRACKED_ENTITY_TYPE_CANT_BE_NULL);
        checkTeiTypeAndTeiProgramAccess(reporter, enrollment, user, trackedEntity, ownerOrgUnit, program);
    }
}
Also used : User(org.hisp.dhis.user.User) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle)

Example 50 with TrackerBundle

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

the class DuplicateRelationshipsPreProcessorTest method test_on_identical_rels_1_is_removed.

/*
     * Verifies that:
     *
     * - given 2 identical relationships
     *
     * - one is removed
     */
@Test
void test_on_identical_rels_1_is_removed() {
    String relType = REL_TYPE_NONBIDIRECTIONAL_UID;
    String fromTeiUid = CodeGenerator.generateUid();
    String toTeiUid = CodeGenerator.generateUid();
    Relationship relationship1 = Relationship.builder().relationship(CodeGenerator.generateUid()).relationshipType(relType).from(RelationshipItem.builder().trackedEntity(fromTeiUid).build()).to(RelationshipItem.builder().trackedEntity(toTeiUid).build()).build();
    Relationship relationship2 = Relationship.builder().relationship(CodeGenerator.generateUid()).relationshipType(relType).from(RelationshipItem.builder().trackedEntity(fromTeiUid).build()).to(RelationshipItem.builder().trackedEntity(toTeiUid).build()).build();
    TrackerBundle bundle = TrackerBundle.builder().preheat(this.preheat).relationships(Lists.newArrayList(relationship1, relationship2)).build();
    preProcessor.process(bundle);
    assertThat(bundle.getRelationships(), hasSize(1));
}
Also used : Relationship(org.hisp.dhis.tracker.domain.Relationship) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Test(org.junit.jupiter.api.Test)

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