use of org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams in project dhis2-core by dhis2.
the class TrackedEntityInstanceAttributesAggregateTest method testTrackedEntityInstanceIncludeAllAttributesInProtectedProgramNoAccess.
@Test
void testTrackedEntityInstanceIncludeAllAttributesInProtectedProgramNoAccess() {
populatePrerequisites(true);
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");
}
use of org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams in project dhis2-core by dhis2.
the class TrackedEntityInstanceAttributesAggregateTest method testTrackedEntityInstanceIncludeSpecificOpenProgram.
@Test
void testTrackedEntityInstanceIncludeSpecificOpenProgram() {
populatePrerequisites(false);
TrackedEntityInstanceQueryParams queryParams = new TrackedEntityInstanceQueryParams();
queryParams.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
queryParams.setProgram(programA);
TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
final List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
assertAttributes(trackedEntityInstances.get(0).getAttributes(), "A", "B", "C");
}
use of org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams in project dhis2-core by dhis2.
the class TrackedEntityInstanceController method getTrackedEntityInstanceParams.
private 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;
}
use of org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams in project dhis2-core by dhis2.
the class TrackedEntityInstanceAggregate method find.
/**
* Fetches a List of {@see TrackedEntityInstance} based on the list of
* primary keys and search parameters
*
* @param ids a List of {@see TrackedEntityInstance} Primary Keys
* @param params an instance of {@see TrackedEntityInstanceParams}
*
* @return a List of {@see TrackedEntityInstance} objects
*/
public List<TrackedEntityInstance> find(List<Long> ids, TrackedEntityInstanceParams params, TrackedEntityInstanceQueryParams queryParams) {
final User user = currentUserService.getCurrentUser();
if (!userGroupUIDCache.get(user.getUid()).isPresent() && !CollectionUtils.isEmpty(user.getGroups())) {
userGroupUIDCache.put(user.getUid(), user.getGroups().stream().map(group -> group.getUid()).collect(Collectors.toList()));
}
/*
* Create a context with information which will be used to fetch the
* entities
*/
AggregateContext ctx = securityCache.get(user.getUid(), userUID -> getSecurityContext(userUID, userGroupUIDCache.get(userUID).orElse(Lists.newArrayList()))).toBuilder().userId(user.getId()).superUser(user.isSuper()).params(params).queryParams(queryParams).build();
/*
* Async fetch Relationships for the given TrackedEntityInstance id
* (only if isIncludeRelationships = true)
*/
final CompletableFuture<Multimap<String, Relationship>> relationshipsAsync = conditionalAsyncFetch(ctx.getParams().isIncludeRelationships(), () -> trackedEntityInstanceStore.getRelationships(ids), getPool());
/*
* Async fetch Enrollments for the given TrackedEntityInstance id (only
* if isIncludeEnrollments = true)
*/
final CompletableFuture<Multimap<String, Enrollment>> enrollmentsAsync = conditionalAsyncFetch(ctx.getParams().isIncludeEnrollments(), () -> enrollmentAggregate.findByTrackedEntityInstanceIds(ids, ctx), getPool());
/*
* Async fetch all ProgramOwner for the given TrackedEntityInstance id
*/
final CompletableFuture<Multimap<String, ProgramOwner>> programOwnersAsync = conditionalAsyncFetch(ctx.getParams().isIncludeProgramOwners(), () -> trackedEntityInstanceStore.getProgramOwners(ids), getPool());
/*
* Async Fetch TrackedEntityInstances by id
*/
final CompletableFuture<Map<String, TrackedEntityInstance>> teisAsync = supplyAsync(() -> trackedEntityInstanceStore.getTrackedEntityInstances(ids, ctx), getPool());
/*
* Async fetch TrackedEntityInstance Attributes by TrackedEntityInstance
* id
*/
final CompletableFuture<Multimap<String, Attribute>> attributesAsync = supplyAsync(() -> trackedEntityInstanceStore.getAttributes(ids), getPool());
/*
* Async fetch Owned Tei mapped to the provided program attributes by
* TrackedEntityInstance id
*/
final CompletableFuture<Multimap<String, String>> ownedTeiAsync = supplyAsync(() -> trackedEntityInstanceStore.getOwnedTeis(ids, ctx), getPool());
/*
* Execute all queries and merge the results
*/
return allOf(teisAsync, attributesAsync, relationshipsAsync, enrollmentsAsync, ownedTeiAsync).thenApplyAsync(fn -> {
Map<String, TrackedEntityInstance> teis = teisAsync.join();
Multimap<String, Attribute> attributes = attributesAsync.join();
Multimap<String, Relationship> relationships = relationshipsAsync.join();
Multimap<String, Enrollment> enrollments = enrollmentsAsync.join();
Multimap<String, ProgramOwner> programOwners = programOwnersAsync.join();
Multimap<String, String> ownedTeis = ownedTeiAsync.join();
Stream<String> teiUidStream = teis.keySet().parallelStream();
if (queryParams.hasProgram()) {
teiUidStream = teiUidStream.filter(ownedTeis::containsKey);
}
return teiUidStream.map(uid -> {
TrackedEntityInstance tei = teis.get(uid);
tei.setAttributes(filterAttributes(attributes.get(uid), ownedTeis.get(uid), teiAttributesCache.get("ALL_ATTRIBUTES", s -> trackedEntityAttributeService.getTrackedEntityAttributesByTrackedEntityTypes()), programTeiAttributesCache.get("ATTRIBUTES_BY_PROGRAM", s -> trackedEntityAttributeService.getTrackedEntityAttributesByProgram()), ctx));
tei.setRelationships(new ArrayList<>(relationships.get(uid)));
tei.setEnrollments(filterEnrollments(enrollments.get(uid), ownedTeis.get(uid), ctx));
tei.setProgramOwners(new ArrayList<>(programOwners.get(uid)));
return tei;
}).collect(Collectors.toList());
}, getPool()).join();
}
use of org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams in project dhis2-core by dhis2.
the class TrackedEntityInstanceAttributesAggregateAclTest method verifyTeiCantBeAccessedNoPublicAccessOnTrackedEntityType.
@Test
void verifyTeiCantBeAccessedNoPublicAccessOnTrackedEntityType() {
doInTransaction(() -> {
this.persistTrackedEntityInstance();
this.persistTrackedEntityInstance();
this.persistTrackedEntityInstance();
this.persistTrackedEntityInstance();
});
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);
assertThat(trackedEntityInstances, hasSize(0));
}
Aggregations