Search in sources :

Example 6 with Relationship

use of org.hisp.dhis.dxf2.events.trackedentity.Relationship in project dhis2-core by dhis2.

the class AbstractRelationshipService method getRelationship.

@Override
@Transactional(readOnly = true)
public Relationship getRelationship(org.hisp.dhis.relationship.Relationship dao, RelationshipParams params, User user) {
    List<String> errors = trackerAccessManager.canRead(user, dao);
    if (!errors.isEmpty()) {
        // Dont include relationship
        return null;
    }
    Relationship relationship = new Relationship();
    relationship.setRelationship(dao.getUid());
    relationship.setRelationshipType(dao.getRelationshipType().getUid());
    relationship.setRelationshipName(dao.getRelationshipType().getName());
    relationship.setFrom(includeRelationshipItem(dao.getFrom(), !params.isIncludeFrom()));
    relationship.setTo(includeRelationshipItem(dao.getTo(), !params.isIncludeTo()));
    relationship.setBidirectional(dao.getRelationshipType().isBidirectional());
    relationship.setCreated(DateUtils.getIso8601NoTz(dao.getCreated()));
    relationship.setLastUpdated(DateUtils.getIso8601NoTz(dao.getLastUpdated()));
    return relationship;
}
Also used : Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with Relationship

use of org.hisp.dhis.dxf2.events.trackedentity.Relationship in project dhis2-core by dhis2.

the class AbstractRelationshipService method addRelationship.

@Override
@Transactional
public ImportSummary addRelationship(Relationship relationship, ImportOptions importOptions) {
    importOptions = updateImportOptions(importOptions);
    // Set up cache if not set already
    if (!cacheExists()) {
        prepareCaches(Lists.newArrayList(relationship), importOptions.getUser());
    }
    ImportSummary importSummary = new ImportSummary(relationship.getRelationship());
    checkRelationship(relationship, importSummary);
    if (importSummary.hasConflicts()) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.getImportCount().incrementIgnored();
        return importSummary;
    }
    org.hisp.dhis.relationship.Relationship daoRelationship = createDAORelationship(relationship);
    Optional<org.hisp.dhis.relationship.Relationship> existing = relationshipService.getRelationshipByRelationship(daoRelationship);
    if (existing.isPresent()) {
        String message = "Relationship " + existing.get().getUid() + " already exists";
        return new ImportSummary(ImportStatus.ERROR, message).setReference(existing.get().getUid()).incrementIgnored();
    }
    // Check access for both sides
    List<String> errors = trackerAccessManager.canWrite(importOptions.getUser(), daoRelationship);
    if (!errors.isEmpty()) {
        return new ImportSummary(ImportStatus.ERROR, errors.toString()).incrementIgnored();
    }
    relationshipService.addRelationship(daoRelationship);
    importSummary.setReference(daoRelationship.getUid());
    importSummary.getImportCount().incrementImported();
    return importSummary;
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with Relationship

use of org.hisp.dhis.dxf2.events.trackedentity.Relationship in project dhis2-core by dhis2.

the class AbstractRelationshipService method deleteRelationship.

private ImportSummary deleteRelationship(String uid, ImportOptions importOptions) {
    ImportSummary importSummary = new ImportSummary();
    importOptions = updateImportOptions(importOptions);
    if (uid.isEmpty()) {
        importSummary.setStatus(ImportStatus.WARNING);
        importSummary.setDescription("Missing required property 'relationship'");
        return importSummary.incrementIgnored();
    }
    org.hisp.dhis.relationship.Relationship daoRelationship = relationshipService.getRelationship(uid);
    if (daoRelationship != null) {
        importSummary.setReference(uid);
        List<String> errors = trackerAccessManager.canWrite(importOptions.getUser(), daoRelationship);
        if (!errors.isEmpty()) {
            importSummary.setDescription(errors.toString());
            importSummary.setStatus(ImportStatus.ERROR);
            importSummary.getImportCount().incrementIgnored();
            return importSummary;
        }
        relationshipService.deleteRelationship(daoRelationship);
        importSummary.setStatus(ImportStatus.SUCCESS);
        importSummary.setDescription("Deletion of relationship " + uid + " was successful");
        return importSummary.incrementDeleted();
    } else {
        importSummary.setStatus(ImportStatus.WARNING);
        importSummary.setDescription("Relationship " + uid + " cannot be deleted as it is not present in the system");
        return importSummary.incrementIgnored();
    }
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary)

Example 9 with Relationship

use of org.hisp.dhis.dxf2.events.trackedentity.Relationship in project dhis2-core by dhis2.

the class AbstractRelationshipService method updateRelationships.

@Override
@Transactional
public ImportSummaries updateRelationships(List<Relationship> relationships, ImportOptions importOptions) {
    List<List<Relationship>> partitions = Lists.partition(relationships, FLUSH_FREQUENCY);
    importOptions = updateImportOptions(importOptions);
    ImportSummaries importSummaries = new ImportSummaries();
    for (List<Relationship> _relationships : partitions) {
        reloadUser(importOptions);
        prepareCaches(_relationships, importOptions.getUser());
        for (Relationship relationship : _relationships) {
            importSummaries.addImportSummary(updateRelationship(relationship, importOptions));
        }
        clearSession();
    }
    return importSummaries;
}
Also used : Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) List(java.util.List) ArrayList(java.util.ArrayList) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with Relationship

use of org.hisp.dhis.dxf2.events.trackedentity.Relationship in project dhis2-core by dhis2.

the class TrackedEntityInstanceAggregateTest method testTrackedEntityInstanceRelationshipsTei2Event.

@Test
void testTrackedEntityInstanceRelationshipsTei2Event() {
    final String[] relationshipItemsUid = new String[2];
    doInTransaction(() -> {
        org.hisp.dhis.trackedentity.TrackedEntityInstance t1 = this.persistTrackedEntityInstance();
        org.hisp.dhis.trackedentity.TrackedEntityInstance t2 = this.persistTrackedEntityInstanceWithEnrollmentAndEvents();
        sessionFactory.getCurrentSession().flush();
        sessionFactory.getCurrentSession().clear();
        t2 = manager.getByUid(org.hisp.dhis.trackedentity.TrackedEntityInstance.class, Collections.singletonList(t2.getUid())).get(0);
        ProgramInstance pi = t2.getProgramInstances().iterator().next();
        final ProgramStageInstance psi = pi.getProgramStageInstances().iterator().next();
        this.persistRelationship(t1, psi);
        relationshipItemsUid[0] = t1.getUid();
        relationshipItemsUid[1] = psi.getUid();
    });
    TrackedEntityInstanceQueryParams queryParams = new TrackedEntityInstanceQueryParams();
    queryParams.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
    queryParams.setTrackedEntityType(trackedEntityTypeA);
    queryParams.setIncludeAllAttributes(true);
    TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
    params.setIncludeRelationships(true);
    params.setIncludeEnrollments(true);
    params.setIncludeEvents(true);
    final List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    // Fetch the TEI which is the vertex of the relationship TEI <-->
    // ENROLLMENT
    Optional<TrackedEntityInstance> trackedEntityInstance = trackedEntityInstances.stream().filter(t -> t.getTrackedEntityInstance().equals(relationshipItemsUid[0])).findFirst();
    if (trackedEntityInstance.isPresent()) {
        assertThat(trackedEntityInstance.get().getRelationships(), hasSize(1));
        final Relationship relationship = trackedEntityInstance.get().getRelationships().get(0);
        assertThat(relationship.getFrom().getTrackedEntityInstance().getTrackedEntityInstance(), is(relationshipItemsUid[0]));
        assertThat(relationship.getTo().getEvent().getEvent(), is(relationshipItemsUid[1]));
    } else {
        fail();
    }
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) QueryItem(org.hisp.dhis.common.QueryItem) Date(java.util.Date) ValueType(org.hisp.dhis.common.ValueType) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) DateTimeFormatMatcher.hasDateTimeFormat(org.hisp.dhis.matchers.DateTimeFormatMatcher.hasDateTimeFormat) Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) EnrollmentStatus(org.hisp.dhis.dxf2.events.enrollment.EnrollmentStatus) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) ProgramInstance(org.hisp.dhis.program.ProgramInstance) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ProgramOwner(org.hisp.dhis.dxf2.events.trackedentity.ProgramOwner) User(org.hisp.dhis.user.User) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) TrackedEntityProgramOwnerService(org.hisp.dhis.trackedentity.TrackedEntityProgramOwnerService) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) SessionFactory(org.hibernate.SessionFactory) QueryOperator(org.hisp.dhis.common.QueryOperator) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) Set(java.util.Set) AggregationType(org.hisp.dhis.analytics.AggregationType) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) EventStatus(org.hisp.dhis.event.EventStatus) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Event(org.hisp.dhis.dxf2.events.event.Event) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) List(java.util.List) FeatureType(org.hisp.dhis.organisationunit.FeatureType) ChronoUnit(java.time.temporal.ChronoUnit) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) TrackerTest(org.hisp.dhis.dxf2.TrackerTest) Collections(java.util.Collections) DateUtils(org.hisp.dhis.util.DateUtils) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Test(org.junit.jupiter.api.Test) TrackerTest(org.hisp.dhis.dxf2.TrackerTest)

Aggregations

Relationship (org.hisp.dhis.dxf2.events.trackedentity.Relationship)30 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)14 ArrayList (java.util.ArrayList)11 Transactional (org.springframework.transaction.annotation.Transactional)10 TrackedEntityInstance (org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance)9 Test (org.junit.jupiter.api.Test)9 List (java.util.List)8 RelationshipType (org.hisp.dhis.relationship.RelationshipType)7 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 TrackedEntityInstanceParams (org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)6 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)6 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)6 Optional (java.util.Optional)5 Set (java.util.Set)5 Event (org.hisp.dhis.dxf2.events.event.Event)5 Lists (com.google.common.collect.Lists)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4 CurrentUserService (org.hisp.dhis.user.CurrentUserService)4