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