use of org.hisp.dhis.dxf2.events.enrollment.Enrollment 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.Enrollment 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.Enrollment in project dhis2-core by dhis2.
the class AbstractEnrollmentService method updateAttributeValues.
private void updateAttributeValues(Enrollment enrollment, ImportOptions importOptions) {
org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance = teiService.getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
Map<String, Attribute> attributeValueMap = Maps.newHashMap();
for (Attribute attribute : enrollment.getAttributes()) {
attributeValueMap.put(attribute.getAttribute(), attribute);
}
trackedEntityInstance.getTrackedEntityAttributeValues().stream().filter(value -> attributeValueMap.containsKey(value.getAttribute().getUid())).forEach(value -> {
Attribute enrollmentAttribute = attributeValueMap.get(value.getAttribute().getUid());
String newValue = enrollmentAttribute.getValue();
value.setValue(newValue);
value.setStoredBy(getStoredBy(enrollmentAttribute, importOptions.getUser()));
trackedEntityAttributeValueService.updateTrackedEntityAttributeValue(value, importOptions.getUser());
attributeValueMap.remove(value.getAttribute().getUid());
});
for (String key : attributeValueMap.keySet()) {
TrackedEntityAttribute attribute = getTrackedEntityAttribute(importOptions.getIdSchemes(), key);
if (attribute != null) {
TrackedEntityAttributeValue value = new TrackedEntityAttributeValue();
Attribute enrollmentAttribute = attributeValueMap.get(key);
value.setValue(enrollmentAttribute.getValue());
value.setAttribute(attribute);
value.setStoredBy(getStoredBy(enrollmentAttribute, importOptions.getUser()));
trackedEntityAttributeValueService.addTrackedEntityAttributeValue(value);
trackedEntityInstance.addAttributeValue(value);
}
}
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.
the class AbstractEnrollmentService method saveTrackedEntityComment.
private void saveTrackedEntityComment(ProgramInstance programInstance, Enrollment enrollment, User user) {
for (Note note : enrollment.getNotes()) {
String noteUid = CodeGenerator.isValidUid(note.getNote()) ? note.getNote() : CodeGenerator.generateUid();
if (!commentService.trackedEntityCommentExists(noteUid) && !StringUtils.isEmpty(note.getValue())) {
TrackedEntityComment comment = new TrackedEntityComment();
comment.setUid(noteUid);
comment.setCommentText(note.getValue());
comment.setCreator(StringUtils.isEmpty(note.getStoredBy()) ? user.getUsername() : note.getStoredBy());
Date created = DateUtils.parseDate(note.getStoredDate());
if (created == null) {
created = new Date();
}
comment.setCreated(created);
comment.setLastUpdatedBy(user);
comment.setLastUpdated(new Date());
commentService.addTrackedEntityComment(comment);
programInstance.getComments().add(comment);
programInstanceService.updateProgramInstance(programInstance, user);
teiService.updateTrackedEntityInstance(programInstance.getEntityInstance(), user);
}
}
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollment 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();
}
Aggregations