Search in sources :

Example 41 with TrackedEntityInstance

use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance 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 42 with TrackedEntityInstance

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

the class TrackedEntityInstanceServiceTest method testDeleteTrackedEntityInstances.

@Test
void testDeleteTrackedEntityInstances() {
    List<TrackedEntityInstance> teis = Lists.newArrayList(trackedEntityInstanceService.getTrackedEntityInstance(maleA.getUid()), trackedEntityInstanceService.getTrackedEntityInstance(maleB.getUid()));
    ImportOptions importOptions = new ImportOptions();
    importOptions.setImportStrategy(ImportStrategy.DELETE);
    trackedEntityInstanceService.deleteTrackedEntityInstances(teis, importOptions);
    assertNull(trackedEntityInstanceService.getTrackedEntityInstance(maleA.getUid()));
    assertNull(trackedEntityInstanceService.getTrackedEntityInstance(maleB.getUid()));
}
Also used : TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 43 with TrackedEntityInstance

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

the class TrackedEntityInstanceServiceTest method testAddAlreadyDeletedTeiInBulk.

@Test
void testAddAlreadyDeletedTeiInBulk() {
    ImportOptions importOptions = new ImportOptions();
    trackedEntityInstanceService.addTrackedEntityInstance(teiMaleA, importOptions);
    trackedEntityInstanceService.deleteTrackedEntityInstance(teiMaleA.getTrackedEntityInstance());
    manager.flush();
    importOptions.setImportStrategy(ImportStrategy.CREATE);
    teiMaleA.setDeleted(true);
    teiMaleB.setTrackedEntityInstance("teiUid00002");
    teiFemaleA.setTrackedEntityInstance("teiUid00003");
    List<TrackedEntityInstance> teis = new ArrayList<>();
    teis.add(teiMaleA);
    teis.add(teiMaleB);
    teis.add(teiFemaleA);
    ImportSummaries importSummaries = trackedEntityInstanceService.addTrackedEntityInstances(teis, importOptions);
    assertEquals(ImportStatus.ERROR, importSummaries.getStatus());
    assertEquals(1, importSummaries.getIgnored());
    assertEquals(2, importSummaries.getImported());
    assertTrue(importSummaries.getImportSummaries().stream().anyMatch(is -> is.getDescription().contains("already exists or was deleted earlier")));
    manager.flush();
    List<String> uids = new ArrayList<>();
    uids.add(teiMaleA.getTrackedEntityInstance());
    uids.add(teiMaleB.getTrackedEntityInstance());
    uids.add(teiFemaleA.getTrackedEntityInstance());
    List<String> fetchedUids = teiDaoService.getTrackedEntityInstancesUidsIncludingDeleted(uids);
    assertTrue(Sets.difference(new HashSet<>(uids), new HashSet<>(fetchedUids)).isEmpty());
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) Arrays(java.util.Arrays) Date(java.util.Date) TrackedEntityAttributeValueService(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) TextPattern(org.hisp.dhis.textpattern.TextPattern) Autowired(org.springframework.beans.factory.annotation.Autowired) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) Disabled(org.junit.jupiter.api.Disabled) StringUtils(org.apache.commons.lang3.StringUtils) Collections.singletonList(java.util.Collections.singletonList) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) EnrollmentStatus(org.hisp.dhis.dxf2.events.enrollment.EnrollmentStatus) Locale(java.util.Locale) SessionFactory(org.hibernate.SessionFactory) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) EventStatus(org.hisp.dhis.event.EventStatus) Collectors(java.util.stream.Collectors) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) Event(org.hisp.dhis.dxf2.events.event.Event) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) TextPatternMethod(org.hisp.dhis.textpattern.TextPatternMethod) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ProgramType(org.hisp.dhis.program.ProgramType) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) TrackedEntityTypeService(org.hisp.dhis.trackedentity.TrackedEntityTypeService) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) LocalDateTime(java.time.LocalDateTime) Program(org.hisp.dhis.program.Program) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) Objects(org.hisp.dhis.common.Objects) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) DateTime(org.joda.time.DateTime) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ProgramStage(org.hisp.dhis.program.ProgramStage) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DateTimeFormatter(java.time.format.DateTimeFormatter) CodeGenerator(org.hisp.dhis.common.CodeGenerator) TextPatternSegment(org.hisp.dhis.textpattern.TextPatternSegment) ArrayList(java.util.ArrayList) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 44 with TrackedEntityInstance

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

the class TrackedEntityInstanceServiceTest method testUpdateTeiAfterChangingTextPatternForGeneratedAttribute.

@Test
void testUpdateTeiAfterChangingTextPatternForGeneratedAttribute() {
    TrackedEntityInstance trackedEntityInstance = trackedEntityInstanceService.getTrackedEntityInstance(maleA.getUid());
    assertNotNull(trackedEntityInstance.getEnrollments());
    assertEquals(1, trackedEntityInstance.getEnrollments().size());
    Enrollment enrollment1 = trackedEntityInstance.getEnrollments().get(0);
    enrollment1.setStatus(EnrollmentStatus.COMPLETED);
    enrollment1.setCompletedBy("test");
    enrollment1.setCompletedDate(new Date());
    Enrollment enrollment2 = new Enrollment();
    enrollment2.setTrackedEntityInstance(maleA.getUid());
    TextPattern textPattern = new TextPattern(Lists.newArrayList(new TextPatternSegment(TextPatternMethod.RANDOM, "RANDOM(#######)")));
    textPattern.setOwnerUid("owneruid");
    textPattern.setOwnerObject(Objects.CONSTANT);
    uniqueIdAttribute.setTextPattern(textPattern);
    trackedEntityAttributeService.updateTrackedEntityAttribute(uniqueIdAttribute);
    enrollment2.setEnrollmentDate(new Date());
    enrollment2.setOrgUnit(organisationUnitA.getUid());
    enrollment2.setProgram(programA.getUid());
    enrollment2.setStatus(EnrollmentStatus.ACTIVE);
    trackedEntityInstance.getEnrollments().add(enrollment2);
    ImportSummary importSummary = trackedEntityInstanceService.updateTrackedEntityInstance(trackedEntityInstance, null, null, true);
    assertEquals(ImportStatus.SUCCESS, importSummary.getStatus());
    assertEquals(ImportStatus.SUCCESS, importSummary.getEnrollments().getStatus());
}
Also used : TextPattern(org.hisp.dhis.textpattern.TextPattern) TextPatternSegment(org.hisp.dhis.textpattern.TextPatternSegment) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) Date(java.util.Date) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 45 with TrackedEntityInstance

use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance 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)

Aggregations

TrackedEntityInstance (org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance)61 Test (org.junit.jupiter.api.Test)57 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)50 TrackedEntityInstanceParams (org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)37 TrackedEntityInstanceQueryParams (org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)34 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)29 Date (java.util.Date)26 TrackerTest (org.hisp.dhis.dxf2.TrackerTest)26 ArrayList (java.util.ArrayList)25 List (java.util.List)25 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)25 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)24 Event (org.hisp.dhis.dxf2.events.event.Event)23 Program (org.hisp.dhis.program.Program)22 ProgramInstance (org.hisp.dhis.program.ProgramInstance)21 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)20 Collectors (java.util.stream.Collectors)19 User (org.hisp.dhis.user.User)17 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)16 ImportStatus (org.hisp.dhis.dxf2.importsummary.ImportStatus)16