Search in sources :

Example 1 with ProgramOwner

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

the class TrackedEntityInstanceSupportService method getTrackedEntityInstance.

@SneakyThrows
public TrackedEntityInstance getTrackedEntityInstance(String id, String pr, List<String> fields) {
    User user = currentUserService.getCurrentUser();
    TrackedEntityInstanceParams trackedEntityInstanceParams = getTrackedEntityInstanceParams(fields);
    TrackedEntityInstance trackedEntityInstance = trackedEntityInstanceService.getTrackedEntityInstance(id, trackedEntityInstanceParams);
    if (trackedEntityInstance == null) {
        throw new NotFoundException("TrackedEntityInstance", id);
    }
    if (pr != null) {
        Program program = programService.getProgram(pr);
        if (program == null) {
            throw new NotFoundException("Program", pr);
        }
        List<String> errors = trackerAccessManager.canRead(user, instanceService.getTrackedEntityInstance(trackedEntityInstance.getTrackedEntityInstance()), program, false);
        if (!errors.isEmpty()) {
            if (program.getAccessLevel() == AccessLevel.CLOSED) {
                throw new WebMessageException(unauthorized(TrackerOwnershipManager.PROGRAM_ACCESS_CLOSED));
            }
            throw new WebMessageException(unauthorized(TrackerOwnershipManager.OWNERSHIP_ACCESS_DENIED));
        }
        if (trackedEntityInstanceParams.isIncludeProgramOwners()) {
            List<ProgramOwner> filteredProgramOwners = trackedEntityInstance.getProgramOwners().stream().filter(tei -> tei.getProgram().equals(pr)).collect(Collectors.toList());
            trackedEntityInstance.setProgramOwners(filteredProgramOwners);
        }
    } else {
        // return only tracked entity type attributes
        TrackedEntityType trackedEntityType = trackedEntityTypeService.getTrackedEntityType(trackedEntityInstance.getTrackedEntityType());
        if (trackedEntityType != null) {
            List<String> tetAttributes = trackedEntityType.getTrackedEntityAttributes().stream().map(TrackedEntityAttribute::getUid).collect(Collectors.toList());
            trackedEntityInstance.setAttributes(trackedEntityInstance.getAttributes().stream().filter(att -> tetAttributes.contains(att.getAttribute())).collect(Collectors.toList()));
        }
    }
    return trackedEntityInstance;
}
Also used : ProgramOwner(org.hisp.dhis.dxf2.events.trackedentity.ProgramOwner) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) TrackedEntityTypeService(org.hisp.dhis.trackedentity.TrackedEntityTypeService) NotFoundException(org.hisp.dhis.webapi.controller.exception.NotFoundException) SneakyThrows(lombok.SneakyThrows) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) RequiredArgsConstructor(lombok.RequiredArgsConstructor) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) Collectors(java.util.stream.Collectors) Program(org.hisp.dhis.program.Program) TrackerOwnershipManager(org.hisp.dhis.trackedentity.TrackerOwnershipManager) List(java.util.List) TrackerAccessManager(org.hisp.dhis.trackedentity.TrackerAccessManager) CurrentUserService(org.hisp.dhis.user.CurrentUserService) Service(org.springframework.stereotype.Service) ProgramOwner(org.hisp.dhis.dxf2.events.trackedentity.ProgramOwner) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) User(org.hisp.dhis.user.User) ProgramService(org.hisp.dhis.program.ProgramService) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) WebMessageUtils.unauthorized(org.hisp.dhis.dxf2.webmessage.WebMessageUtils.unauthorized) AccessLevel(org.hisp.dhis.common.AccessLevel) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) Joiner(com.google.common.base.Joiner) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) User(org.hisp.dhis.user.User) Program(org.hisp.dhis.program.Program) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) NotFoundException(org.hisp.dhis.webapi.controller.exception.NotFoundException) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) SneakyThrows(lombok.SneakyThrows)

Example 2 with ProgramOwner

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

the class TrackedEntityInstanceAggregate method find.

/**
 * Fetches a List of {@see TrackedEntityInstance} based on the list of
 * primary keys and search parameters
 *
 * @param ids a List of {@see TrackedEntityInstance} Primary Keys
 * @param params an instance of {@see TrackedEntityInstanceParams}
 *
 * @return a List of {@see TrackedEntityInstance} objects
 */
public List<TrackedEntityInstance> find(List<Long> ids, TrackedEntityInstanceParams params, TrackedEntityInstanceQueryParams queryParams) {
    final User user = currentUserService.getCurrentUser();
    if (!userGroupUIDCache.get(user.getUid()).isPresent() && !CollectionUtils.isEmpty(user.getGroups())) {
        userGroupUIDCache.put(user.getUid(), user.getGroups().stream().map(group -> group.getUid()).collect(Collectors.toList()));
    }
    /*
         * Create a context with information which will be used to fetch the
         * entities
         */
    AggregateContext ctx = securityCache.get(user.getUid(), userUID -> getSecurityContext(userUID, userGroupUIDCache.get(userUID).orElse(Lists.newArrayList()))).toBuilder().userId(user.getId()).superUser(user.isSuper()).params(params).queryParams(queryParams).build();
    /*
         * Async fetch Relationships for the given TrackedEntityInstance id
         * (only if isIncludeRelationships = true)
         */
    final CompletableFuture<Multimap<String, Relationship>> relationshipsAsync = conditionalAsyncFetch(ctx.getParams().isIncludeRelationships(), () -> trackedEntityInstanceStore.getRelationships(ids), getPool());
    /*
         * Async fetch Enrollments for the given TrackedEntityInstance id (only
         * if isIncludeEnrollments = true)
         */
    final CompletableFuture<Multimap<String, Enrollment>> enrollmentsAsync = conditionalAsyncFetch(ctx.getParams().isIncludeEnrollments(), () -> enrollmentAggregate.findByTrackedEntityInstanceIds(ids, ctx), getPool());
    /*
         * Async fetch all ProgramOwner for the given TrackedEntityInstance id
         */
    final CompletableFuture<Multimap<String, ProgramOwner>> programOwnersAsync = conditionalAsyncFetch(ctx.getParams().isIncludeProgramOwners(), () -> trackedEntityInstanceStore.getProgramOwners(ids), getPool());
    /*
         * Async Fetch TrackedEntityInstances by id
         */
    final CompletableFuture<Map<String, TrackedEntityInstance>> teisAsync = supplyAsync(() -> trackedEntityInstanceStore.getTrackedEntityInstances(ids, ctx), getPool());
    /*
         * Async fetch TrackedEntityInstance Attributes by TrackedEntityInstance
         * id
         */
    final CompletableFuture<Multimap<String, Attribute>> attributesAsync = supplyAsync(() -> trackedEntityInstanceStore.getAttributes(ids), getPool());
    /*
         * Async fetch Owned Tei mapped to the provided program attributes by
         * TrackedEntityInstance id
         */
    final CompletableFuture<Multimap<String, String>> ownedTeiAsync = supplyAsync(() -> trackedEntityInstanceStore.getOwnedTeis(ids, ctx), getPool());
    /*
         * Execute all queries and merge the results
         */
    return allOf(teisAsync, attributesAsync, relationshipsAsync, enrollmentsAsync, ownedTeiAsync).thenApplyAsync(fn -> {
        Map<String, TrackedEntityInstance> teis = teisAsync.join();
        Multimap<String, Attribute> attributes = attributesAsync.join();
        Multimap<String, Relationship> relationships = relationshipsAsync.join();
        Multimap<String, Enrollment> enrollments = enrollmentsAsync.join();
        Multimap<String, ProgramOwner> programOwners = programOwnersAsync.join();
        Multimap<String, String> ownedTeis = ownedTeiAsync.join();
        Stream<String> teiUidStream = teis.keySet().parallelStream();
        if (queryParams.hasProgram()) {
            teiUidStream = teiUidStream.filter(ownedTeis::containsKey);
        }
        return teiUidStream.map(uid -> {
            TrackedEntityInstance tei = teis.get(uid);
            tei.setAttributes(filterAttributes(attributes.get(uid), ownedTeis.get(uid), teiAttributesCache.get("ALL_ATTRIBUTES", s -> trackedEntityAttributeService.getTrackedEntityAttributesByTrackedEntityTypes()), programTeiAttributesCache.get("ATTRIBUTES_BY_PROGRAM", s -> trackedEntityAttributeService.getTrackedEntityAttributesByProgram()), ctx));
            tei.setRelationships(new ArrayList<>(relationships.get(uid)));
            tei.setEnrollments(filterEnrollments(enrollments.get(uid), ownedTeis.get(uid), ctx));
            tei.setProgramOwners(new ArrayList<>(programOwners.get(uid)));
            return tei;
        }).collect(Collectors.toList());
    }, getPool()).join();
}
Also used : RequiredArgsConstructor(lombok.RequiredArgsConstructor) CompletableFuture(java.util.concurrent.CompletableFuture) Multimap(com.google.common.collect.Multimap) Program(org.hisp.dhis.program.Program) ArrayList(java.util.ArrayList) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) Lists(com.google.common.collect.Lists) CompletableFuture.allOf(java.util.concurrent.CompletableFuture.allOf) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) Map(java.util.Map) ProgramOwner(org.hisp.dhis.dxf2.events.trackedentity.ProgramOwner) User(org.hisp.dhis.user.User) ThreadPoolManager.getPool(org.hisp.dhis.dxf2.events.aggregates.ThreadPoolManager.getPool) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceStore(org.hisp.dhis.dxf2.events.trackedentity.store.TrackedEntityInstanceStore) NonNull(lombok.NonNull) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Collection(java.util.Collection) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) Set(java.util.Set) Collectors(java.util.stream.Collectors) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) AclStore(org.hisp.dhis.dxf2.events.trackedentity.store.AclStore) CacheProvider(org.hisp.dhis.cache.CacheProvider) List(java.util.List) CollectionUtils(org.hisp.dhis.commons.collection.CollectionUtils) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) CurrentUserService(org.hisp.dhis.user.CurrentUserService) Cache(org.hisp.dhis.cache.Cache) PostConstruct(javax.annotation.PostConstruct) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) User(org.hisp.dhis.user.User) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) Multimap(com.google.common.collect.Multimap) Stream(java.util.stream.Stream) Map(java.util.Map)

Example 3 with ProgramOwner

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

the class ProgramOwnerRowCallbackHandler method getProgramOwner.

private ProgramOwner getProgramOwner(ResultSet rs) throws SQLException {
    ProgramOwner programOwner = new ProgramOwner();
    programOwner.setOwnerOrgUnit(rs.getString("ouuid"));
    programOwner.setProgram(rs.getString("prguid"));
    programOwner.setTrackedEntityInstance(rs.getString("key"));
    return programOwner;
}
Also used : ProgramOwner(org.hisp.dhis.dxf2.events.trackedentity.ProgramOwner)

Example 4 with ProgramOwner

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

the class TrackedEntityInstanceAggregateTest method testTrackedEntityInstanceProgramOwners.

@Test
void testTrackedEntityInstanceProgramOwners() {
    doInTransaction(() -> {
        final org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance = persistTrackedEntityInstance();
        programOwnerService.createOrUpdateTrackedEntityProgramOwner(trackedEntityInstance, programA, organisationUnitA);
    });
    TrackedEntityInstanceQueryParams queryParams = new TrackedEntityInstanceQueryParams();
    queryParams.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
    queryParams.setTrackedEntityType(trackedEntityTypeA);
    queryParams.setIncludeAllAttributes(true);
    TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
    params.setIncludeProgramOwners(true);
    final List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    assertThat(trackedEntityInstances.get(0).getProgramOwners(), hasSize(1));
    ProgramOwner programOwner = trackedEntityInstances.get(0).getProgramOwners().get(0);
    assertThat(programOwner.getProgram(), is(programA.getUid()));
    assertThat(programOwner.getOwnerOrgUnit(), is(organisationUnitA.getUid()));
    assertThat(programOwner.getTrackedEntityInstance(), is(trackedEntityInstances.get(0).getTrackedEntityInstance()));
}
Also used : ProgramOwner(org.hisp.dhis.dxf2.events.trackedentity.ProgramOwner) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Test(org.junit.jupiter.api.Test) TrackerTest(org.hisp.dhis.dxf2.TrackerTest)

Aggregations

ProgramOwner (org.hisp.dhis.dxf2.events.trackedentity.ProgramOwner)4 TrackedEntityInstanceParams (org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)3 TrackedEntityInstance (org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance)3 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 RequiredArgsConstructor (lombok.RequiredArgsConstructor)2 Program (org.hisp.dhis.program.Program)2 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)2 TrackedEntityInstanceQueryParams (org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)2 CurrentUserService (org.hisp.dhis.user.CurrentUserService)2 User (org.hisp.dhis.user.User)2 Joiner (com.google.common.base.Joiner)1 Lists (com.google.common.collect.Lists)1 Multimap (com.google.common.collect.Multimap)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Map (java.util.Map)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletableFuture.allOf (java.util.concurrent.CompletableFuture.allOf)1