Search in sources :

Example 21 with TrackedEntityType

use of org.hisp.dhis.trackedentity.TrackedEntityType in project dhis2-core by dhis2.

the class EnrollmentCriteriaMapper method getFromUrl.

/**
 * Returns a ProgramInstanceQueryParams based on the given input.
 *
 * @param ou the set of organisation unit identifiers.
 * @param ouMode the OrganisationUnitSelectionMode.
 * @param lastUpdated the last updated for PI.
 * @param lastUpdatedDuration the last updated duration filter.
 * @param program the Program identifier.
 * @param programStatus the ProgramStatus in the given program.
 * @param programStartDate the start date for enrollment in the given
 *        Program.
 * @param programEndDate the end date for enrollment in the given Program.
 * @param trackedEntityType the TrackedEntityType uid.
 * @param trackedEntityInstance the TrackedEntityInstance uid.
 * @param followUp indicates follow up status in the given Program.
 * @param page the page number.
 * @param pageSize the page size.
 * @param totalPages indicates whether to include the total number of pages.
 * @param skipPaging whether to skip paging.
 * @param includeDeleted whether to include soft deleted ones
 * @return a ProgramInstanceQueryParams.
 */
@Transactional(readOnly = true)
public ProgramInstanceQueryParams getFromUrl(Set<String> ou, OrganisationUnitSelectionMode ouMode, Date lastUpdated, String lastUpdatedDuration, String program, ProgramStatus programStatus, Date programStartDate, Date programEndDate, String trackedEntityType, String trackedEntityInstance, Boolean followUp, Integer page, Integer pageSize, boolean totalPages, boolean skipPaging, boolean includeDeleted, List<OrderCriteria> orderCriteria) {
    ProgramInstanceQueryParams params = new ProgramInstanceQueryParams();
    Set<OrganisationUnit> possibleSearchOrgUnits = new HashSet<>();
    User user = currentUserService.getCurrentUser();
    if (user != null) {
        possibleSearchOrgUnits = user.getTeiSearchOrganisationUnitsWithFallback();
    }
    if (ou != null) {
        for (String orgUnit : ou) {
            OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(orgUnit);
            if (organisationUnit == null) {
                throw new IllegalQueryException("Organisation unit does not exist: " + orgUnit);
            }
            if (!organisationUnitService.isInUserHierarchy(organisationUnit.getUid(), possibleSearchOrgUnits)) {
                throw new IllegalQueryException("Organisation unit is not part of the search scope: " + orgUnit);
            }
            params.getOrganisationUnits().add(organisationUnit);
        }
    }
    Program pr = program != null ? programService.getProgram(program) : null;
    if (program != null && pr == null) {
        throw new IllegalQueryException("Program does not exist: " + program);
    }
    TrackedEntityType te = trackedEntityType != null ? trackedEntityTypeService.getTrackedEntityType(trackedEntityType) : null;
    if (trackedEntityType != null && te == null) {
        throw new IllegalQueryException("Tracked entity does not exist: " + program);
    }
    TrackedEntityInstance tei = trackedEntityInstance != null ? trackedEntityInstanceService.getTrackedEntityInstance(trackedEntityInstance) : null;
    if (trackedEntityInstance != null && tei == null) {
        throw new IllegalQueryException("Tracked entity instance does not exist: " + program);
    }
    params.setProgram(pr);
    params.setProgramStatus(programStatus);
    params.setFollowUp(followUp);
    params.setLastUpdated(lastUpdated);
    params.setLastUpdatedDuration(lastUpdatedDuration);
    params.setProgramStartDate(programStartDate);
    params.setProgramEndDate(programEndDate);
    params.setTrackedEntityType(te);
    params.setTrackedEntityInstanceUid(Optional.ofNullable(tei).map(BaseIdentifiableObject::getUid).orElse(null));
    params.setOrganisationUnitMode(ouMode);
    params.setPage(page);
    params.setPageSize(pageSize);
    params.setTotalPages(totalPages);
    params.setSkipPaging(skipPaging);
    params.setIncludeDeleted(includeDeleted);
    params.setUser(user);
    params.setOrder(toOrderParams(orderCriteria));
    return params;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) User(org.hisp.dhis.user.User) Program(org.hisp.dhis.program.Program) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 22 with TrackedEntityType

use of org.hisp.dhis.trackedentity.TrackedEntityType in project dhis2-core by dhis2.

the class PreCheckMetaValidationHookTest method verifyTrackedEntityValidationFailsWhenOrgUnitIsNotPresentInDb.

@Test
void verifyTrackedEntityValidationFailsWhenOrgUnitIsNotPresentInDb() {
    // given
    TrackedEntity tei = validTei();
    ValidationErrorReporter reporter = new ValidationErrorReporter(ctx);
    // when
    when(ctx.getTrackedEntityType(TRACKED_ENTITY_TYPE_UID)).thenReturn(new TrackedEntityType());
    validatorToTest.validateTrackedEntity(reporter, tei);
    // then
    hasTrackerError(reporter, E1049, TRACKED_ENTITY, tei.getUid());
}
Also used : TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Example 23 with TrackedEntityType

use of org.hisp.dhis.trackedentity.TrackedEntityType in project dhis2-core by dhis2.

the class RelationshipsValidationHookTest method verifyValidationFailsOnInvalidFromTrackedEntityType.

@Test
void verifyValidationFailsOnInvalidFromTrackedEntityType() {
    RelationshipType relType = createRelTypeConstraint(TRACKED_ENTITY_INSTANCE, PROGRAM_INSTANCE);
    String trackedEntityUid = CodeGenerator.generateUid();
    TrackedEntityType constraintTrackedEntityType = new TrackedEntityType();
    constraintTrackedEntityType.setUid(CodeGenerator.generateUid());
    relType.getFromConstraint().setTrackedEntityType(constraintTrackedEntityType);
    Relationship relationship = Relationship.builder().relationship(CodeGenerator.generateUid()).from(RelationshipItem.builder().trackedEntity(trackedEntityUid).build()).to(RelationshipItem.builder().enrollment(CodeGenerator.generateUid()).build()).relationshipType(relType.getUid()).build();
    when(preheat.getAll(RelationshipType.class)).thenReturn(Collections.singletonList(relType));
    List<TrackedEntity> trackedEntities = new ArrayList<>();
    TrackedEntity trackedEntity = new TrackedEntity();
    trackedEntity.setTrackedEntity(trackedEntityUid);
    trackedEntity.setTrackedEntityType(CodeGenerator.generateUid());
    trackedEntities.add(trackedEntity);
    when(bundle.getTrackedEntities()).thenReturn(trackedEntities);
    reporter = new ValidationErrorReporter(ctx);
    validationHook.validateRelationship(reporter, relationship);
    hasTrackerError(reporter, E4014, RELATIONSHIP, relationship.getUid());
    assertThat(reporter.getReportList().get(0).getErrorMessage(), is("Relationship Type `from` constraint requires a Tracked Entity having type `" + constraintTrackedEntityType.getUid() + "` but `" + trackedEntity.getTrackedEntityType() + "` was found."));
}
Also used : TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Relationship(org.hisp.dhis.tracker.domain.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) ArrayList(java.util.ArrayList) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Example 24 with TrackedEntityType

use of org.hisp.dhis.trackedentity.TrackedEntityType in project dhis2-core by dhis2.

the class RelationshipsValidationHookTest method verifyValidationFailsOnInvalidToTrackedEntityType.

@Test
void verifyValidationFailsOnInvalidToTrackedEntityType() {
    RelationshipType relType = createRelTypeConstraint(PROGRAM_INSTANCE, TRACKED_ENTITY_INSTANCE);
    String trackedEntityUid = CodeGenerator.generateUid();
    TrackedEntityType constraintTrackedEntityType = new TrackedEntityType();
    constraintTrackedEntityType.setUid(CodeGenerator.generateUid());
    relType.getToConstraint().setTrackedEntityType(constraintTrackedEntityType);
    Relationship relationship = Relationship.builder().relationship(CodeGenerator.generateUid()).from(RelationshipItem.builder().enrollment(CodeGenerator.generateUid()).build()).to(RelationshipItem.builder().trackedEntity(trackedEntityUid).build()).relationshipType(relType.getUid()).build();
    when(preheat.getAll(RelationshipType.class)).thenReturn(Collections.singletonList(relType));
    TrackedEntityType teiTrackedEntityType = new TrackedEntityType();
    teiTrackedEntityType.setUid(CodeGenerator.generateUid());
    TrackedEntityInstance trackedEntityInstance = new TrackedEntityInstance();
    trackedEntityInstance.setUid(trackedEntityUid);
    trackedEntityInstance.setTrackedEntityType(teiTrackedEntityType);
    when(ctx.getTrackedEntityInstance(trackedEntityUid)).thenReturn(trackedEntityInstance);
    reporter = new ValidationErrorReporter(ctx);
    validationHook.validateRelationship(reporter, relationship);
    hasTrackerError(reporter, E4014, RELATIONSHIP, relationship.getUid());
    assertThat(reporter.getReportList().get(0).getErrorMessage(), is("Relationship Type `to` constraint requires a Tracked Entity having type `" + constraintTrackedEntityType.getUid() + "` but `" + teiTrackedEntityType.getUid() + "` was found."));
}
Also used : TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) Relationship(org.hisp.dhis.tracker.domain.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Test(org.junit.jupiter.api.Test)

Example 25 with TrackedEntityType

use of org.hisp.dhis.trackedentity.TrackedEntityType in project dhis2-core by dhis2.

the class TrackedEntityAttributeValidationHookTest method shouldFailOptionSetNotValid.

@Test
void shouldFailOptionSetNotValid() {
    TrackedEntityAttribute trackedEntityAttribute = getTrackedEntityAttributeWithOptionSet();
    when(validationContext.getTrackedEntityAttribute(anyString())).thenReturn(trackedEntityAttribute);
    when(validationContext.getTrackedEntityType(anyString())).thenReturn(new TrackedEntityType());
    TrackedEntity trackedEntity = TrackedEntity.builder().attributes(Collections.singletonList(Attribute.builder().attribute("uid").value("wrongCode").build())).trackedEntityType("trackedEntityType").build();
    ValidationErrorReporter reporter = new ValidationErrorReporter(validationContext);
    trackedEntityAttributeValidationHook.validateTrackedEntity(reporter, trackedEntity);
    assertTrue(reporter.hasErrors());
    assertEquals(1, reporter.getReportList().size());
    assertEquals(1, reporter.getReportList().stream().filter(e -> e.getErrorCode() == TrackerErrorCode.E1125).count());
}
Also used : TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Aggregations

TrackedEntityType (org.hisp.dhis.trackedentity.TrackedEntityType)57 Test (org.junit.jupiter.api.Test)27 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)22 Program (org.hisp.dhis.program.Program)19 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)16 User (org.hisp.dhis.user.User)16 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)15 TrackedEntity (org.hisp.dhis.tracker.domain.TrackedEntity)15 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)12 ProgramInstance (org.hisp.dhis.program.ProgramInstance)11 Date (java.util.Date)10 HashSet (java.util.HashSet)10 ProgramStage (org.hisp.dhis.program.ProgramStage)8 CategoryCombo (org.hisp.dhis.category.CategoryCombo)6 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)6 RelationshipType (org.hisp.dhis.relationship.RelationshipType)6 TrackedEntityTypeAttribute (org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute)6 Collectors (java.util.stream.Collectors)5 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)5 CategoryOption (org.hisp.dhis.category.CategoryOption)5