Search in sources :

Example 16 with TrackedEntityInstanceParams

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

the class TrackedEntityInstanceSupportService method getTrackedEntityInstanceParams.

public TrackedEntityInstanceParams getTrackedEntityInstanceParams(List<String> fields) {
    String joined = Joiner.on("").join(fields);
    if (joined.contains("*")) {
        return TrackedEntityInstanceParams.TRUE;
    }
    TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
    if (joined.contains("relationships")) {
        params.setIncludeRelationships(true);
    }
    if (joined.contains("enrollments")) {
        params.setIncludeEnrollments(true);
    }
    if (joined.contains("events")) {
        params.setIncludeEvents(true);
    }
    if (joined.contains("programOwners")) {
        params.setIncludeProgramOwners(true);
    }
    return params;
}
Also used : TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)

Example 17 with TrackedEntityInstanceParams

use of org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams 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 18 with TrackedEntityInstanceParams

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

the class TrackedEntityInstanceAttributesAggregateAclTest method verifyTeiCanBeAccessedWhenDATA_READPublicAccessOnTrackedEntityType.

@Test
void verifyTeiCanBeAccessedWhenDATA_READPublicAccessOnTrackedEntityType() {
    final String tetUid = CodeGenerator.generateUid();
    doInTransaction(() -> {
        TrackedEntityType trackedEntityTypeZ = createTrackedEntityType('Z');
        trackedEntityTypeZ.setUid(tetUid);
        trackedEntityTypeZ.setName("TrackedEntityTypeZ" + trackedEntityTypeZ.getUid());
        trackedEntityTypeService.addTrackedEntityType(trackedEntityTypeZ);
        // When saving the trackedEntityType using addTrackedEntityType, the
        // public access value is ignored
        // therefore we need to update the previously saved TeiType
        final TrackedEntityType trackedEntityType = trackedEntityTypeService.getTrackedEntityType(trackedEntityTypeZ.getUid());
        trackedEntityType.setPublicAccess(DATA_READ);
        trackedEntityTypeService.updateTrackedEntityType(trackedEntityType);
        this.persistTrackedEntityInstance(ImmutableMap.of("trackedEntityType", trackedEntityType));
        this.persistTrackedEntityInstance(ImmutableMap.of("trackedEntityType", trackedEntityType));
        this.persistTrackedEntityInstance();
        this.persistTrackedEntityInstance();
    });
    final TrackedEntityType trackedEntityType = trackedEntityTypeService.getTrackedEntityType(tetUid);
    TrackedEntityInstanceQueryParams queryParams = new TrackedEntityInstanceQueryParams();
    queryParams.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
    queryParams.setTrackedEntityType(trackedEntityType);
    queryParams.setIncludeAllAttributes(true);
    TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
    final List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    assertThat(trackedEntityInstances, hasSize(2));
}
Also used : TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) 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)

Example 19 with TrackedEntityInstanceParams

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

the class TrackedEntityInstanceAttributesAggregateTest method testTrackedEntityInstanceIncludeSpecificProtectedProgram.

@Test
void testTrackedEntityInstanceIncludeSpecificProtectedProgram() {
    populatePrerequisites(false);
    TrackedEntityInstanceQueryParams queryParams = new TrackedEntityInstanceQueryParams();
    queryParams.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
    queryParams.setProgram(programB);
    TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
    final List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    assertAttributes(trackedEntityInstances.get(0).getAttributes(), "A", "B", "E");
}
Also used : 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)

Example 20 with TrackedEntityInstanceParams

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

the class TrackedEntityInstanceAttributesAggregateTest method testTrackedEntityInstanceIncludeAllAttributes.

@Test
void testTrackedEntityInstanceIncludeAllAttributes() {
    populatePrerequisites(false);
    TrackedEntityInstanceQueryParams queryParams = new TrackedEntityInstanceQueryParams();
    queryParams.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
    queryParams.setTrackedEntityType(trackedEntityTypeA);
    queryParams.setIncludeAllAttributes(true);
    TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
    final List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    assertAttributes(trackedEntityInstances.get(0).getAttributes(), "A", "B", "C", "E");
}
Also used : 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

TrackedEntityInstanceParams (org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)30 TrackedEntityInstance (org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance)26 TrackedEntityInstanceQueryParams (org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)26 TrackerTest (org.hisp.dhis.dxf2.TrackerTest)24 Test (org.junit.jupiter.api.Test)24 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)8 Date (java.util.Date)7 List (java.util.List)6 Event (org.hisp.dhis.dxf2.events.event.Event)6 ProgramOwner (org.hisp.dhis.dxf2.events.trackedentity.ProgramOwner)6 Collectors (java.util.stream.Collectors)5 Relationship (org.hisp.dhis.dxf2.events.trackedentity.Relationship)5 User (org.hisp.dhis.user.User)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Set (java.util.Set)4 QueryItem (org.hisp.dhis.common.QueryItem)4 TrackedEntityInstanceService (org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService)4 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)4 Sets (com.google.common.collect.Sets)3