Search in sources :

Example 6 with Enrollments

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;
}
Also used : ArrayList(java.util.ArrayList) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries)

Example 7 with Enrollments

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);
    }
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) Authorities(org.hisp.dhis.security.Authorities) Date(java.util.Date) Restrictions(org.hisp.dhis.query.Restrictions) TrackedEntityAttributeValueService(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService) TrackedEntityInstanceAuditService(org.hisp.dhis.trackedentity.TrackedEntityInstanceAuditService) ReservedValueService(org.hisp.dhis.reservedvalue.ReservedValueService) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) StringUtils(org.apache.commons.lang3.StringUtils) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) RelationshipService(org.hisp.dhis.relationship.RelationshipService) FileResourceService(org.hisp.dhis.fileresource.FileResourceService) Map(java.util.Map) ProgramInstance(org.hisp.dhis.program.ProgramInstance) JobConfiguration(org.hisp.dhis.scheduling.JobConfiguration) TrackedEntityAttributeStore(org.hisp.dhis.trackedentity.TrackedEntityAttributeStore) EnrollmentService(org.hisp.dhis.dxf2.events.enrollment.EnrollmentService) Query(org.hisp.dhis.query.Query) UserService(org.hisp.dhis.user.UserService) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Collection(java.util.Collection) Set(java.util.Set) SchemaService(org.hisp.dhis.schema.SchemaService) Collectors(java.util.stream.Collectors) QueryService(org.hisp.dhis.query.QueryService) TrackedEntityProgramOwner(org.hisp.dhis.trackedentity.TrackedEntityProgramOwner) ImportReportMode(org.hisp.dhis.dxf2.metadata.feedback.ImportReportMode) Objects(java.util.Objects) TrackerOwnershipManager(org.hisp.dhis.trackedentity.TrackerOwnershipManager) TEA_VALUE_MAX_LENGTH(org.hisp.dhis.trackedentity.TrackedEntityAttributeService.TEA_VALUE_MAX_LENGTH) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) TrackerAccessManager(org.hisp.dhis.trackedentity.TrackerAccessManager) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) Optional(java.util.Optional) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) Geometry(org.locationtech.jts.geom.Geometry) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityInstanceAggregate(org.hisp.dhis.dxf2.events.aggregates.TrackedEntityInstanceAggregate) TrackedEntityTypeService(org.hisp.dhis.trackedentity.TrackedEntityTypeService) Constants(org.hisp.dhis.dxf2.Constants) AuditType(org.hisp.dhis.common.AuditType) GeoUtils(org.hisp.dhis.system.util.GeoUtils) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) Program(org.hisp.dhis.program.Program) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Notifier(org.hisp.dhis.system.notification.Notifier) RelationshipParams(org.hisp.dhis.dxf2.events.RelationshipParams) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) DbmsManager(org.hisp.dhis.dbms.DbmsManager) ImportConflicts(org.hisp.dhis.dxf2.importsummary.ImportConflicts) User(org.hisp.dhis.user.User) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) TrackedEntityInstanceAudit(org.hisp.dhis.audit.payloads.TrackedEntityInstanceAudit) IdSchemes(org.hisp.dhis.common.IdSchemes) FileResource(org.hisp.dhis.fileresource.FileResource) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) DebugUtils(org.hisp.dhis.commons.util.DebugUtils) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) FeatureType(org.hisp.dhis.organisationunit.FeatureType) NotificationLevel(org.hisp.dhis.system.notification.NotificationLevel) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) CurrentUserService(org.hisp.dhis.user.CurrentUserService) CachingMap(org.hisp.dhis.commons.collection.CachingMap) ERROR(org.hisp.dhis.system.notification.NotificationLevel.ERROR) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Collections(java.util.Collections) DateUtils(org.hisp.dhis.util.DateUtils) Transactional(org.springframework.transaction.annotation.Transactional) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) CachingMap(org.hisp.dhis.commons.collection.CachingMap)

Example 8 with Enrollments

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;
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ArrayList(java.util.ArrayList) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) List(java.util.List) ArrayList(java.util.ArrayList) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with Enrollments

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));
    }
}
Also used : Authorities(org.hisp.dhis.security.Authorities) FIRST_PAGE(org.hisp.dhis.common.SlimPager.FIRST_PAGE) EventService(org.hisp.dhis.dxf2.events.event.EventService) Date(java.util.Date) Restrictions(org.hisp.dhis.query.Restrictions) TrackedEntityAttributeValueService(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) StringUtils(org.apache.commons.lang3.StringUtils) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) ProgramStageInstanceService(org.hisp.dhis.program.ProgramStageInstanceService) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) EnrollmentEvaluationEvent(org.hisp.dhis.programrule.engine.EnrollmentEvaluationEvent) Map(java.util.Map) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) NoteHelper(org.hisp.dhis.dxf2.events.NoteHelper) JobConfiguration(org.hisp.dhis.scheduling.JobConfiguration) SetValuedMap(org.apache.commons.collections4.SetValuedMap) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams) Query(org.hisp.dhis.query.Query) UserService(org.hisp.dhis.user.UserService) Transactional(javax.transaction.Transactional) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) Collection(java.util.Collection) Set(java.util.Set) SchemaService(org.hisp.dhis.schema.SchemaService) Collectors(java.util.stream.Collectors) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) QueryService(org.hisp.dhis.query.QueryService) Event(org.hisp.dhis.dxf2.events.event.Event) Objects(java.util.Objects) TrackerOwnershipManager(org.hisp.dhis.trackedentity.TrackerOwnershipManager) TEA_VALUE_MAX_LENGTH(org.hisp.dhis.trackedentity.TrackedEntityAttributeService.TEA_VALUE_MAX_LENGTH) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) CollectionUtils.isNotEmpty(org.apache.commons.collections4.CollectionUtils.isNotEmpty) TrackerEnrollmentWebHookEvent(org.hisp.dhis.programrule.engine.TrackerEnrollmentWebHookEvent) TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) TrackerAccessManager(org.hisp.dhis.trackedentity.TrackerAccessManager) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) TrackedEntityCommentService(org.hisp.dhis.trackedentitycomment.TrackedEntityCommentService) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramEnrollmentNotificationEvent(org.hisp.dhis.program.notification.event.ProgramEnrollmentNotificationEvent) Constants(org.hisp.dhis.dxf2.Constants) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) GeoUtils(org.hisp.dhis.system.util.GeoUtils) ObjectUtils.defaultIfNull(org.apache.commons.lang3.ObjectUtils.defaultIfNull) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) Program(org.hisp.dhis.program.Program) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Notifier(org.hisp.dhis.system.notification.Notifier) RelationshipParams(org.hisp.dhis.dxf2.events.RelationshipParams) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) SlimPager(org.hisp.dhis.common.SlimPager) DbmsManager(org.hisp.dhis.dbms.DbmsManager) ImportConflicts(org.hisp.dhis.dxf2.importsummary.ImportConflicts) User(org.hisp.dhis.user.User) IdentifiableObjectCallable(org.hisp.dhis.system.callable.IdentifiableObjectCallable) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) I18nManager(org.hisp.dhis.i18n.I18nManager) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) IdSchemes(org.hisp.dhis.common.IdSchemes) Pager(org.hisp.dhis.common.Pager) RelationshipService(org.hisp.dhis.dxf2.events.relationship.RelationshipService) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DEFAULT_PAGE_SIZE(org.hisp.dhis.common.Pager.DEFAULT_PAGE_SIZE) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) Maps(com.google.common.collect.Maps) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) DebugUtils(org.hisp.dhis.commons.util.DebugUtils) Note(org.hisp.dhis.dxf2.events.event.Note) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ProgramStatus(org.hisp.dhis.program.ProgramStatus) Collectors.toList(java.util.stream.Collectors.toList) CollectionUtils(org.hisp.dhis.commons.collection.CollectionUtils) FeatureType(org.hisp.dhis.organisationunit.FeatureType) NotificationLevel(org.hisp.dhis.system.notification.NotificationLevel) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) CurrentUserService(org.hisp.dhis.user.CurrentUserService) CachingMap(org.hisp.dhis.commons.collection.CachingMap) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) ERROR(org.hisp.dhis.system.notification.NotificationLevel.ERROR) CodeGenerator(org.hisp.dhis.common.CodeGenerator) ProgramService(org.hisp.dhis.program.ProgramService) Collections(java.util.Collections) DateUtils(org.hisp.dhis.util.DateUtils) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) Query(org.hisp.dhis.query.Query) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) HashSet(java.util.HashSet)

Example 10 with Enrollments

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;
}
Also used : ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries)

Aggregations

ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)18 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)16 List (java.util.List)12 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)12 ArrayList (java.util.ArrayList)11 Lists (com.google.common.collect.Lists)9 Map (java.util.Map)9 Set (java.util.Set)9 Collectors (java.util.stream.Collectors)9 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)9 ImportStatus (org.hisp.dhis.dxf2.importsummary.ImportStatus)8 ProgramInstanceService (org.hisp.dhis.program.ProgramInstanceService)8 JobConfiguration (org.hisp.dhis.scheduling.JobConfiguration)8 CurrentUserService (org.hisp.dhis.user.CurrentUserService)8 Program (org.hisp.dhis.program.Program)7 ProgramInstance (org.hisp.dhis.program.ProgramInstance)7 ProgramInstanceQueryParams (org.hisp.dhis.program.ProgramInstanceQueryParams)7 SlimPager (org.hisp.dhis.common.SlimPager)6 TrackedEntityInstanceParams (org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)5 Date (java.util.Date)4