use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method linkEnrollmentSummaries.
// -------------------------------------------------------------------------
// HELPERS
// -------------------------------------------------------------------------
private void linkEnrollmentSummaries(ImportSummaries importSummaries, ImportSummaries enrollmentImportSummaries, List<Enrollment> enrollments) {
importSummaries.getImportSummaries().forEach(is -> is.setEnrollments(new ImportSummaries()));
Map<String, List<Enrollment>> enrollmentsGroupedByTe = enrollments.stream().filter(en -> !StringUtils.isEmpty(en.getTrackedEntityInstance())).collect(Collectors.groupingBy(Enrollment::getTrackedEntityInstance));
Map<String, List<ImportSummary>> summariesGroupedByReference = importSummaries.getImportSummaries().stream().filter(en -> !StringUtils.isEmpty(en.getReference())).collect(Collectors.groupingBy(ImportSummary::getReference));
Map<String, List<ImportSummary>> enrollmentSummariesGroupedByReference = enrollmentImportSummaries.getImportSummaries().stream().filter(en -> !StringUtils.isEmpty(en.getReference())).collect(Collectors.groupingBy(ImportSummary::getReference));
for (Map.Entry<String, List<Enrollment>> set : enrollmentsGroupedByTe.entrySet()) {
if (!summariesGroupedByReference.containsKey(set.getKey())) {
continue;
}
ImportSummary importSummary = summariesGroupedByReference.get(set.getKey()).get(0);
ImportSummaries enrollmentSummaries = new ImportSummaries();
for (Enrollment enrollment : set.getValue()) {
if (!enrollmentSummariesGroupedByReference.containsKey(enrollment.getEnrollment())) {
continue;
}
ImportSummary enrollmentSummary = enrollmentSummariesGroupedByReference.get(enrollment.getEnrollment()).get(0);
enrollmentSummaries.addImportSummary(enrollmentSummary);
}
if (enrollmentImportSummaries.getImportSummaries().isEmpty()) {
continue;
}
importSummary.setEnrollments(enrollmentSummaries);
}
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method addTrackedEntityInstances.
@Override
@Transactional
public ImportSummaries addTrackedEntityInstances(List<TrackedEntityInstance> trackedEntityInstances, ImportOptions importOptions) {
importOptions = updateImportOptions(importOptions);
ImportSummaries importSummaries = new ImportSummaries();
List<Enrollment> enrollments = new ArrayList<>();
List<TrackedEntityInstance> validTeis = resolveImportableTeis(trackedEntityInstances, importSummaries);
List<List<TrackedEntityInstance>> partitions = Lists.partition(validTeis, FLUSH_FREQUENCY);
for (List<TrackedEntityInstance> _trackedEntityInstances : partitions) {
reloadUser(importOptions);
prepareCaches(_trackedEntityInstances, importOptions.getUser());
for (TrackedEntityInstance trackedEntityInstance : _trackedEntityInstances) {
ImportSummary importSummary = addTrackedEntityInstance(trackedEntityInstance, importOptions, false, true);
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;
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.
the class EventBaseCheck method validateProgramInstance.
private void validateProgramInstance(ImmutableEvent event, WorkContext ctx, List<String> errors) {
ProgramInstance programInstance = ctx.getProgramInstanceMap().get(event.getUid());
ImportOptions importOptions = ctx.getImportOptions();
if (programInstance == null) {
errors.add("No program instance found for event: " + event.getEvent());
} else if (COMPLETED.equals(programInstance.getStatus())) {
if (importOptions == null || importOptions.getUser() == null || importOptions.getUser().isAuthorized(F_EDIT_EXPIRED.getAuthority())) {
return;
}
Date referenceDate = parseDate(event.getCreated());
if (referenceDate == null) {
referenceDate = new Date();
}
referenceDate = removeTimeStamp(referenceDate);
if (referenceDate.after(removeTimeStamp(programInstance.getEndDate()))) {
errors.add("Not possible to add event to a completed enrollment. Event created date ( " + referenceDate + " ) is after enrollment completed date ( " + removeTimeStamp(programInstance.getEndDate()) + " ).");
}
}
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.
the class AbstractRelationshipService method includeRelationshipItem.
private org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem includeRelationshipItem(RelationshipItem dao, boolean uidOnly) {
TrackedEntityInstanceParams teiParams = TrackedEntityInstanceParams.FALSE;
org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem relationshipItem = new org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem();
if (dao.getTrackedEntityInstance() != null) {
org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance tei = new org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance();
String uid = dao.getTrackedEntityInstance().getUid();
if (uidOnly) {
tei.clear();
tei.setTrackedEntityInstance(uid);
} else {
tei = trackedEntityInstanceService.getTrackedEntityInstance(dao.getTrackedEntityInstance(), teiParams);
}
relationshipItem.setTrackedEntityInstance(tei);
} else if (dao.getProgramInstance() != null) {
Enrollment enrollment = new Enrollment();
String uid = dao.getProgramInstance().getUid();
if (uidOnly) {
enrollment.clear();
enrollment.setEnrollment(uid);
} else {
enrollment = enrollmentService.getEnrollment(dao.getProgramInstance(), teiParams);
}
relationshipItem.setEnrollment(enrollment);
} else if (dao.getProgramStageInstance() != null) {
Event event = new Event();
String uid = dao.getProgramStageInstance().getUid();
if (uidOnly) {
event.clear();
event.setEvent(uid);
} else {
event = eventService.getEvent(dao.getProgramStageInstance());
event.setRelationships(null);
}
relationshipItem.setEvent(event);
}
return relationshipItem;
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.
the class AbstractEnrollmentService method prepareCaches.
private void prepareCaches(List<Enrollment> enrollments, User user) {
Collection<String> orgUnits = enrollments.stream().map(Enrollment::getOrgUnit).collect(Collectors.toSet());
if (!orgUnits.isEmpty()) {
Query query = Query.from(schemaService.getDynamicSchema(OrganisationUnit.class));
query.setUser(user);
query.add(Restrictions.in("id", orgUnits));
queryService.query(query).forEach(ou -> organisationUnitCache.put(ou.getUid(), (OrganisationUnit) ou));
}
Collection<String> programs = enrollments.stream().map(Enrollment::getProgram).collect(Collectors.toSet());
if (!programs.isEmpty()) {
Query query = Query.from(schemaService.getDynamicSchema(Program.class));
query.setUser(user);
query.add(Restrictions.in("id", programs));
queryService.query(query).forEach(pr -> programCache.put(pr.getUid(), (Program) pr));
}
Collection<String> trackedEntityAttributes = new HashSet<>();
enrollments.forEach(e -> e.getAttributes().forEach(at -> trackedEntityAttributes.add(at.getAttribute())));
if (!trackedEntityAttributes.isEmpty()) {
Query query = Query.from(schemaService.getDynamicSchema(TrackedEntityAttribute.class));
query.setUser(user);
query.add(Restrictions.in("id", trackedEntityAttributes));
queryService.query(query).forEach(tea -> trackedEntityAttributeCache.put(tea.getUid(), (TrackedEntityAttribute) tea));
}
Collection<String> trackedEntityInstances = enrollments.stream().map(Enrollment::getTrackedEntityInstance).collect(toList());
if (!trackedEntityInstances.isEmpty()) {
Query query = Query.from(schemaService.getDynamicSchema(org.hisp.dhis.trackedentity.TrackedEntityInstance.class));
query.setUser(user);
query.add(Restrictions.in("id", trackedEntityInstances));
queryService.query(query).forEach(te -> trackedEntityInstanceCache.put(te.getUid(), (org.hisp.dhis.trackedentity.TrackedEntityInstance) te));
}
}
Aggregations