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;
}
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()));
}
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());
}
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());
}
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));
}
Aggregations