use of org.hisp.dhis.dxf2.events.enrollment.Enrollments in project dhis2-core by dhis2.
the class JacksonEnrollmentService method addEnrollmentList.
@Override
public ImportSummaries addEnrollmentList(List<Enrollment> enrollments, ImportOptions importOptions) {
ImportSummaries importSummaries = new ImportSummaries();
importOptions = updateImportOptions(importOptions);
List<Enrollment> create = new ArrayList<>();
List<Enrollment> update = new ArrayList<>();
List<Enrollment> delete = new ArrayList<>();
if (importOptions.getImportStrategy().isCreate()) {
create.addAll(enrollments);
} else if (importOptions.getImportStrategy().isCreateAndUpdate()) {
sortCreatesAndUpdates(enrollments, create, update);
} else if (importOptions.getImportStrategy().isUpdate()) {
update.addAll(enrollments);
} else if (importOptions.getImportStrategy().isDelete()) {
delete.addAll(enrollments);
} else if (importOptions.getImportStrategy().isSync()) {
for (Enrollment enrollment : enrollments) {
if (enrollment.isDeleted()) {
delete.add(enrollment);
} else {
sortCreatesAndUpdates(enrollment, create, update);
}
}
}
importSummaries.addImportSummaries(addEnrollments(create, importOptions, null, true));
importSummaries.addImportSummaries(updateEnrollments(update, importOptions, true));
importSummaries.addImportSummaries(deleteEnrollments(delete, importOptions, true));
if (ImportReportMode.ERRORS == importOptions.getReportMode()) {
importSummaries.getImportSummaries().removeIf(is -> !is.hasConflicts());
}
return importSummaries;
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollments 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.Enrollments 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.Enrollments 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));
}
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollments in project dhis2-core by dhis2.
the class AbstractEnrollmentService method deleteEnrollments.
@Override
public ImportSummaries deleteEnrollments(List<Enrollment> enrollments, ImportOptions importOptions, boolean clearSession) {
ImportSummaries importSummaries = new ImportSummaries();
importOptions = updateImportOptions(importOptions);
int counter = 0;
for (Enrollment enrollment : enrollments) {
importSummaries.addImportSummary(deleteEnrollment(enrollment.getEnrollment(), enrollment, importOptions));
if (clearSession && counter % FLUSH_FREQUENCY == 0) {
clearSession();
}
counter++;
}
return importSummaries;
}
Aggregations