Search in sources :

Example 1 with TRACKED_ENTITY_INSTANCE

use of org.hisp.dhis.relationship.RelationshipEntity.TRACKED_ENTITY_INSTANCE in project dhis2-core by dhis2.

the class PreCheckDataRelationsValidationHookTest method verifyValidationFailsWhenLinkedTrackedEntityIsNotFound.

@Test
void verifyValidationFailsWhenLinkedTrackedEntityIsNotFound() {
    RelationshipType relType = createRelTypeConstraint(TRACKED_ENTITY_INSTANCE, TRACKED_ENTITY_INSTANCE);
    Relationship relationship = Relationship.builder().relationship(CodeGenerator.generateUid()).from(RelationshipItem.builder().trackedEntity("validTrackedEntity").build()).to(RelationshipItem.builder().trackedEntity("anotherValidTrackedEntity").build()).relationshipType(relType.getUid()).build();
    hook.validateRelationship(reporter, relationship);
    assertTrue(reporter.hasErrors());
    assertTrue(reporter.hasErrorReport(r -> r.getErrorCode() == TrackerErrorCode.E4012));
    assertThat(reporter.getReportList().stream().map(TrackerErrorReport::getErrorMessage).collect(Collectors.toList()), hasItem("Could not find `trackedEntity`: `validTrackedEntity`, linked to Relationship."));
    assertThat(reporter.getReportList().stream().map(TrackerErrorReport::getErrorMessage).collect(Collectors.toList()), hasItem("Could not find `trackedEntity`: `anotherValidTrackedEntity`, linked to Relationship."));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CategoryService(org.hisp.dhis.category.CategoryService) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) CategoryOption(org.hisp.dhis.category.CategoryOption) TrackerIdentifier(org.hisp.dhis.tracker.TrackerIdentifier) RelationshipConstraint(org.hisp.dhis.relationship.RelationshipConstraint) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ReferenceTrackerEntity(org.hisp.dhis.tracker.preheat.ReferenceTrackerEntity) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Category(org.hisp.dhis.category.Category) RelationshipItem(org.hisp.dhis.tracker.domain.RelationshipItem) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ProgramType(org.hisp.dhis.program.ProgramType) Optional(java.util.Optional) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) CategoryCombo(org.hisp.dhis.category.CategoryCombo) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DataDimensionType(org.hisp.dhis.common.DataDimensionType) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Mock(org.mockito.Mock) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Relationship(org.hisp.dhis.tracker.domain.Relationship) Program(org.hisp.dhis.program.Program) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) TrackerIdentifierParams(org.hisp.dhis.tracker.TrackerIdentifierParams) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) RelationshipType(org.hisp.dhis.relationship.RelationshipType) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TRACKED_ENTITY_INSTANCE(org.hisp.dhis.relationship.RelationshipEntity.TRACKED_ENTITY_INSTANCE) RelationshipEntity(org.hisp.dhis.relationship.RelationshipEntity) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Event(org.hisp.dhis.tracker.domain.Event) TrackerErrorReport(org.hisp.dhis.tracker.report.TrackerErrorReport) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) ProgramStage(org.hisp.dhis.program.ProgramStage) Mockito.verify(org.mockito.Mockito.verify) TrackerErrorCode(org.hisp.dhis.tracker.report.TrackerErrorCode) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Matchers.hasItem(org.hamcrest.Matchers.hasItem) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Collections(java.util.Collections) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) Relationship(org.hisp.dhis.tracker.domain.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) TrackerErrorReport(org.hisp.dhis.tracker.report.TrackerErrorReport) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 2 with TRACKED_ENTITY_INSTANCE

use of org.hisp.dhis.relationship.RelationshipEntity.TRACKED_ENTITY_INSTANCE in project dhis2-core by dhis2.

the class AbstractRelationshipService method prepareCaches.

private void prepareCaches(List<Relationship> relationships, User user) {
    Map<RelationshipEntity, List<String>> relationshipEntities = new HashMap<>();
    Map<String, List<Relationship>> relationshipTypeMap = relationships.stream().collect(Collectors.groupingBy(Relationship::getRelationshipType));
    // Find all the RelationshipTypes first, so we know what the uids refer
    // to
    Query query = Query.from(schemaService.getDynamicSchema(RelationshipType.class));
    query.setUser(user);
    query.add(Restrictions.in("id", relationshipTypeMap.keySet()));
    queryService.query(query).forEach(rt -> relationshipTypeCache.put(rt.getUid(), (RelationshipType) rt));
    // Group all uids into their respective RelationshipEntities
    relationshipTypeCache.values().forEach(relationshipType -> {
        List<String> fromUids = relationshipTypeMap.get(relationshipType.getUid()).stream().map((r) -> getUidOfRelationshipItem(r.getFrom())).collect(Collectors.toList());
        List<String> toUids = relationshipTypeMap.get(relationshipType.getUid()).stream().map((r) -> getUidOfRelationshipItem(r.getTo())).collect(Collectors.toList());
        // Merge existing results with newly found ones.
        relationshipEntities.merge(relationshipType.getFromConstraint().getRelationshipEntity(), fromUids, (old, _new) -> ListUtils.union(old, _new));
        relationshipEntities.merge(relationshipType.getToConstraint().getRelationshipEntity(), toUids, (old, _new) -> ListUtils.union(old, _new));
    });
    // Find and put all Relationship members in their respective cache
    if (relationshipEntities.get(TRACKED_ENTITY_INSTANCE) != null) {
        teiDaoService.getTrackedEntityInstancesByUid(relationshipEntities.get(TRACKED_ENTITY_INSTANCE), user).forEach(tei -> trackedEntityInstanceCache.put(tei.getUid(), tei));
    }
    if (relationshipEntities.get(PROGRAM_INSTANCE) != null) {
        Query piQuery = Query.from(schemaService.getDynamicSchema(ProgramInstance.class));
        piQuery.setUser(user);
        piQuery.add(Restrictions.in("id", relationshipEntities.get(PROGRAM_INSTANCE)));
        queryService.query(piQuery).forEach(pi -> programInstanceCache.put(pi.getUid(), (ProgramInstance) pi));
    }
    if (relationshipEntities.get(PROGRAM_STAGE_INSTANCE) != null) {
        Query psiQuery = Query.from(schemaService.getDynamicSchema(ProgramStageInstance.class));
        psiQuery.setUser(user);
        psiQuery.add(Restrictions.in("id", relationshipEntities.get(PROGRAM_STAGE_INSTANCE)));
        queryService.query(psiQuery).forEach(psi -> programStageInstanceCache.put(psi.getUid(), (ProgramStageInstance) psi));
    }
}
Also used : EventService(org.hisp.dhis.dxf2.events.event.EventService) Restrictions(org.hisp.dhis.query.Restrictions) StringUtils(org.apache.commons.lang3.StringUtils) RelationshipUtils(org.hisp.dhis.commons.util.RelationshipUtils) Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) RelationshipConstraint(org.hisp.dhis.relationship.RelationshipConstraint) PROGRAM_STAGE_INSTANCE(org.hisp.dhis.relationship.RelationshipEntity.PROGRAM_STAGE_INSTANCE) Map(java.util.Map) ProgramInstance(org.hisp.dhis.program.ProgramInstance) EnrollmentService(org.hisp.dhis.dxf2.events.enrollment.EnrollmentService) Query(org.hisp.dhis.query.Query) UserService(org.hisp.dhis.user.UserService) ListUtils(org.hisp.dhis.commons.collection.ListUtils) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) SchemaService(org.hisp.dhis.schema.SchemaService) Collectors(java.util.stream.Collectors) QueryService(org.hisp.dhis.query.QueryService) Event(org.hisp.dhis.dxf2.events.event.Event) ImportReportMode(org.hisp.dhis.dxf2.metadata.feedback.ImportReportMode) Objects(java.util.Objects) List(java.util.List) TrackerAccessManager(org.hisp.dhis.trackedentity.TrackerAccessManager) Optional(java.util.Optional) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) PagingAndSortingCriteriaAdapter(org.hisp.dhis.webapi.controller.event.webrequest.PagingAndSortingCriteriaAdapter) HashMap(java.util.HashMap) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Function(java.util.function.Function) PROGRAM_INSTANCE(org.hisp.dhis.relationship.RelationshipEntity.PROGRAM_INSTANCE) ArrayList(java.util.ArrayList) RelationshipParams(org.hisp.dhis.dxf2.events.RelationshipParams) Lists(com.google.common.collect.Lists) DbmsManager(org.hisp.dhis.dbms.DbmsManager) ImportConflicts(org.hisp.dhis.dxf2.importsummary.ImportConflicts) RelationshipType(org.hisp.dhis.relationship.RelationshipType) User(org.hisp.dhis.user.User) TRACKED_ENTITY_INSTANCE(org.hisp.dhis.relationship.RelationshipEntity.TRACKED_ENTITY_INSTANCE) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) RelationshipEntity(org.hisp.dhis.relationship.RelationshipEntity) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) CurrentUserService(org.hisp.dhis.user.CurrentUserService) DateUtils(org.hisp.dhis.util.DateUtils) Transactional(org.springframework.transaction.annotation.Transactional) Query(org.hisp.dhis.query.Query) HashMap(java.util.HashMap) ProgramInstance(org.hisp.dhis.program.ProgramInstance) RelationshipType(org.hisp.dhis.relationship.RelationshipType) List(java.util.List) ArrayList(java.util.ArrayList) RelationshipEntity(org.hisp.dhis.relationship.RelationshipEntity) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Aggregations

Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 ProgramInstance (org.hisp.dhis.program.ProgramInstance)2 RelationshipConstraint (org.hisp.dhis.relationship.RelationshipConstraint)2 RelationshipEntity (org.hisp.dhis.relationship.RelationshipEntity)2 TRACKED_ENTITY_INSTANCE (org.hisp.dhis.relationship.RelationshipEntity.TRACKED_ENTITY_INSTANCE)2 RelationshipType (org.hisp.dhis.relationship.RelationshipType)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Function (java.util.function.Function)1 StringUtils (org.apache.commons.lang3.StringUtils)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.hasItem (org.hamcrest.Matchers.hasItem)1