Search in sources :

Example 11 with TrackedEntityType

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

the class TrackedEntityTrackerConverterService method from.

private TrackedEntityInstance from(TrackerPreheat preheat, TrackedEntity te, TrackedEntityInstance tei) {
    OrganisationUnit organisationUnit = preheat.get(OrganisationUnit.class, te.getOrgUnit());
    TrackedEntityType trackedEntityType = preheat.get(TrackedEntityType.class, te.getTrackedEntityType());
    Date now = new Date();
    if (isNewEntity(tei)) {
        tei = new TrackedEntityInstance();
        tei.setUid(te.getTrackedEntity());
        tei.setCreated(now);
        tei.setCreatedByUserInfo(UserInfoSnapshot.from(preheat.getUser()));
    }
    tei.setLastUpdatedByUserInfo(UserInfoSnapshot.from(preheat.getUser()));
    tei.setStoredBy(te.getStoredBy());
    tei.setLastUpdated(now);
    tei.setDeleted(false);
    tei.setPotentialDuplicate(te.isPotentialDuplicate());
    tei.setCreatedAtClient(DateUtils.fromInstant(te.getCreatedAtClient()));
    tei.setLastUpdatedAtClient(DateUtils.fromInstant(te.getUpdatedAtClient()));
    tei.setOrganisationUnit(organisationUnit);
    tei.setTrackedEntityType(trackedEntityType);
    tei.setInactive(te.isInactive());
    tei.setGeometry(te.getGeometry());
    return tei;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date)

Example 12 with TrackedEntityType

use of org.hisp.dhis.trackedentity.TrackedEntityType 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 13 with TrackedEntityType

use of org.hisp.dhis.trackedentity.TrackedEntityType 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 14 with TrackedEntityType

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

the class ProgramSupplierAclIntegrationTest method verifyUserHasWriteAccessToTrackedEntityTypeForSharing.

@Test
void verifyUserHasWriteAccessToTrackedEntityTypeForSharing() {
    // Given
    final User user = createUser("user1");
    final TrackedEntityType tet = createTrackedEntityType('A');
    tet.setPublicAccess(AccessStringHelper.DATA_READ_WRITE);
    manager.save(tet, false);
    final Program program = createProgram('A');
    program.setTrackedEntityType(tet);
    program.setPublicAccess(AccessStringHelper.DEFAULT);
    manager.save(program, false);
    manager.flush();
    // When
    final Map<String, Program> programs = programSupplier.get(getDefaultImportOptions(), singletonList(event));
    // Then
    assertThat(programs.keySet(), hasSize(1));
    assertTrue(aclService.canDataWrite(user, getTrackedEntityType(programs.get(program.getUid()))));
}
Also used : TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) User(org.hisp.dhis.user.User) Program(org.hisp.dhis.program.Program) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 15 with TrackedEntityType

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

the class ProgramSupplierAclIntegrationTest method verifyUserHasNoWriteAccessToTrackedEntityType.

// 
// TRACKED ENTITY TYPE ACL TESTS
// ----------------------------------------------------------------------------
// 
@Test
void verifyUserHasNoWriteAccessToTrackedEntityType() {
    // Given
    final User demo = createUser("demo");
    final TrackedEntityType tet = createTrackedEntityType('A');
    tet.setPublicAccess(AccessStringHelper.DEFAULT);
    manager.save(tet);
    final Program program = createProgram('A');
    program.setTrackedEntityType(tet);
    program.setPublicAccess(AccessStringHelper.DEFAULT);
    manager.save(program, false);
    manager.flush();
    // When
    final Map<String, Program> programs = programSupplier.get(getDefaultImportOptions(), singletonList(event));
    // Then
    assertThat(programs.keySet(), hasSize(1));
    assertFalse(aclService.canDataWrite(demo, getTrackedEntityType(programs.get(program.getUid()))));
}
Also used : TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) User(org.hisp.dhis.user.User) Program(org.hisp.dhis.program.Program) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) 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