use of org.hisp.dhis.dxf2.events.enrollment.Enrollments in project dhis2-core by dhis2.
the class EnrollmentController method getEnrollments.
// -------------------------------------------------------------------------
// READ
// -------------------------------------------------------------------------
@RequestMapping(value = "", method = RequestMethod.GET)
@PreAuthorize("hasRole('ALL') or hasRole('F_PROGRAM_ENROLLMENT') or hasRole('F_PROGRAM_UNENROLLMENT') or hasRole('F_PROGRAM_ENROLLMENT_READ')")
@ResponseBody
public RootNode getEnrollments(@RequestParam(required = false) String ou, @RequestParam(required = false) OrganisationUnitSelectionMode ouMode, @RequestParam(required = false) String program, @RequestParam(required = false) ProgramStatus programStatus, @RequestParam(required = false) Boolean followUp, @RequestParam(required = false) Date lastUpdated, @RequestParam(required = false) Date programStartDate, @RequestParam(required = false) Date programEndDate, @RequestParam(required = false) String trackedEntity, @RequestParam(required = false) String trackedEntityInstance, @RequestParam(required = false) String enrollment, @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) boolean totalPages, @RequestParam(required = false) boolean skipPaging) {
List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
if (fields.isEmpty()) {
fields.add("enrollment,created,lastUpdated,trackedEntity,trackedEntityInstance,program,status,orgUnit,orgUnitName,enrollmentDate,incidentDate,followup");
}
Set<String> orgUnits = TextUtils.splitToArray(ou, TextUtils.SEMICOLON);
List<Enrollment> enrollments;
if (enrollment == null) {
ProgramInstanceQueryParams params = programInstanceService.getFromUrl(orgUnits, ouMode, lastUpdated, program, programStatus, programStartDate, programEndDate, trackedEntity, trackedEntityInstance, followUp, page, pageSize, totalPages, skipPaging);
enrollments = new ArrayList<>(enrollmentService.getEnrollments(programInstanceService.getProgramInstances(params)));
} else {
Set<String> enrollmentIds = TextUtils.splitToArray(enrollment, TextUtils.SEMICOLON);
enrollments = enrollmentIds != null ? enrollmentIds.stream().map(enrollmentId -> enrollmentService.getEnrollment(enrollmentId)).collect(Collectors.toList()) : null;
}
RootNode rootNode = NodeUtils.createMetadata();
rootNode.addChild(fieldFilterService.filter(Enrollment.class, enrollments, fields));
return rootNode;
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollments in project dhis2-core by dhis2.
the class AbstractEnrollmentService method addEnrollments.
@Override
public ImportSummaries addEnrollments(List<Enrollment> enrollments, ImportOptions importOptions, JobConfiguration jobId) {
notifier.clear(jobId).notify(jobId, "Importing enrollments");
importOptions = updateImportOptions(importOptions);
try {
ImportSummaries importSummaries = addEnrollments(enrollments, importOptions, true);
if (jobId != null) {
notifier.notify(jobId, NotificationLevel.INFO, "Import done", true).addJobSummary(jobId, importSummaries, ImportSummaries.class);
}
return importSummaries;
} catch (RuntimeException ex) {
log.error(DebugUtils.getStackTrace(ex));
notifier.notify(jobId, ERROR, "Process failed: " + ex.getMessage(), true);
return new ImportSummaries().addImportSummary(new ImportSummary(ImportStatus.ERROR, "The import process failed: " + ex.getMessage()));
}
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollments in project dhis2-core by dhis2.
the class AbstractEnrollmentService method addEnrollments.
@Override
public ImportSummaries addEnrollments(List<Enrollment> enrollments, ImportOptions importOptions, org.hisp.dhis.trackedentity.TrackedEntityInstance daoTrackedEntityInstance, boolean clearSession) {
importOptions = updateImportOptions(importOptions);
ImportSummaries importSummaries = new ImportSummaries();
List<String> conflictingEnrollmentUids = checkForExistingEnrollmentsIncludingDeleted(enrollments, importSummaries);
List<Enrollment> validEnrollments = enrollments.stream().filter(e -> !conflictingEnrollmentUids.contains(e.getEnrollment())).collect(toList());
List<List<Enrollment>> partitions = Lists.partition(validEnrollments, FLUSH_FREQUENCY);
List<Event> events = new ArrayList<>();
for (List<Enrollment> _enrollments : partitions) {
reloadUser(importOptions);
prepareCaches(_enrollments, importOptions.getUser());
for (Enrollment enrollment : _enrollments) {
ImportSummary importSummary = addEnrollment(enrollment, importOptions, daoTrackedEntityInstance, false);
importSummaries.addImportSummary(importSummary);
if (importSummary.isStatus(ImportStatus.SUCCESS)) {
List<Event> enrollmentEvents = enrollment.getEvents();
enrollmentEvents.forEach(e -> e.setEnrollment(enrollment.getEnrollment()));
events.addAll(enrollmentEvents);
}
}
if (clearSession && enrollments.size() >= FLUSH_FREQUENCY) {
clearSession();
}
}
ImportSummaries eventImportSummaries = eventService.processEventImport(events, importOptions, null);
linkEventSummaries(importSummaries, eventImportSummaries, events);
return importSummaries;
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollments 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.enrollment.Enrollments in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method updateTrackedEntityInstances.
// -------------------------------------------------------------------------
// UPDATE
// -------------------------------------------------------------------------
private ImportSummaries updateTrackedEntityInstances(List<TrackedEntityInstance> trackedEntityInstances, ImportOptions importOptions) {
List<List<TrackedEntityInstance>> partitions = Lists.partition(trackedEntityInstances, FLUSH_FREQUENCY);
importOptions = updateImportOptions(importOptions);
ImportSummaries importSummaries = new ImportSummaries();
List<Enrollment> enrollments = new ArrayList<>();
for (List<TrackedEntityInstance> _trackedEntityInstances : partitions) {
reloadUser(importOptions);
prepareCaches(_trackedEntityInstances, importOptions.getUser());
for (TrackedEntityInstance trackedEntityInstance : _trackedEntityInstances) {
ImportSummary importSummary = updateTrackedEntityInstance(trackedEntityInstance, null, importOptions, false, false);
importSummaries.addImportSummary(importSummary);
if (importSummary.isStatus(ImportStatus.SUCCESS)) {
enrollments.addAll(trackedEntityInstance.getEnrollments());
}
}
clearSession();
}
ImportSummaries enrollmentImportSummaries = enrollmentService.addEnrollmentList(enrollments, importOptions);
linkEnrollmentSummaries(importSummaries, enrollmentImportSummaries, enrollments);
return importSummaries;
}
Aggregations