Search in sources :

Example 1 with Enrollments

use of org.hisp.dhis.dxf2.events.enrollment.Enrollments in project dhis2-core by dhis2.

the class AbstractEventService method addEvent.

protected ImportSummary addEvent(Event event, User user, ImportOptions importOptions) {
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    Program program = getProgram(importOptions.getIdSchemes().getProgramIdScheme(), event.getProgram());
    ProgramStage programStage = getProgramStage(importOptions.getIdSchemes().getProgramStageIdScheme(), event.getProgramStage());
    ProgramInstance programInstance;
    ProgramStageInstance programStageInstance = null;
    if (program == null) {
        return new ImportSummary(ImportStatus.ERROR, "Event.program does not point to a valid program: " + event.getProgram()).incrementIgnored();
    }
    if (programStage == null && program.isRegistration()) {
        return new ImportSummary(ImportStatus.ERROR, "Event.programStage does not point to a valid programStage, and program is multi stage: " + event.getProgramStage()).incrementIgnored();
    } else if (programStage == null) {
        programStage = program.getProgramStageByStage(1);
    }
    Assert.notNull(programStage, "Program stage cannot be null");
    if (!canAccess(program, user)) {
        return new ImportSummary(ImportStatus.ERROR, "Current user does not have permission to access this program").incrementIgnored();
    }
    if (program.isRegistration()) {
        if (event.getTrackedEntityInstance() == null) {
            return new ImportSummary(ImportStatus.ERROR, "No Event.trackedEntityInstance was provided for registration based program").incrementIgnored();
        }
        org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = entityInstanceService.getTrackedEntityInstance(event.getTrackedEntityInstance());
        if (entityInstance == null) {
            return new ImportSummary(ImportStatus.ERROR, "Event.trackedEntityInstance does not point to a valid tracked entity instance: " + event.getTrackedEntityInstance()).incrementIgnored();
        }
        List<ProgramInstance> programInstances = new ArrayList<>(programInstanceService.getProgramInstances(entityInstance, program, ProgramStatus.ACTIVE));
        if (programInstances.isEmpty()) {
            return new ImportSummary(ImportStatus.ERROR, "Tracked entity instance: " + entityInstance.getUid() + " is not enrolled in program: " + program.getUid()).incrementIgnored();
        } else if (programInstances.size() > 1) {
            return new ImportSummary(ImportStatus.ERROR, "Tracked entity instance: " + entityInstance.getUid() + " have multiple active enrollments in program: " + program.getUid()).incrementIgnored();
        }
        programInstance = programInstances.get(0);
        if (!programStage.getRepeatable()) {
            programStageInstance = programStageInstanceService.getProgramStageInstance(programInstance, programStage);
            if (programStageInstance != null && !programStageInstance.getUid().equals(event.getEvent())) {
                return new ImportSummary(ImportStatus.ERROR, "Program stage is not repeatable and an event already exists").incrementIgnored();
            }
        } else {
            if (event.getEvent() != null) {
                programStageInstance = manager.getObject(ProgramStageInstance.class, importOptions.getIdSchemes().getProgramStageInstanceIdScheme(), event.getEvent());
                if (programStageInstance == null) {
                    if (!CodeGenerator.isValidUid(event.getEvent())) {
                        return new ImportSummary(ImportStatus.ERROR, "Event.event did not point to a valid event: " + event.getEvent()).incrementIgnored();
                    }
                }
            }
        }
    } else {
        List<ProgramInstance> programInstances = new ArrayList<>(programInstanceService.getProgramInstances(program, ProgramStatus.ACTIVE));
        if (programInstances.isEmpty()) {
            // Create PI if it doesn't exist (should only be one)
            ProgramInstance pi = new ProgramInstance();
            pi.setEnrollmentDate(new Date());
            pi.setIncidentDate(new Date());
            pi.setProgram(program);
            pi.setStatus(ProgramStatus.ACTIVE);
            programInstanceService.addProgramInstance(pi);
            programInstances.add(pi);
        } else if (programInstances.size() > 1) {
            return new ImportSummary(ImportStatus.ERROR, "Multiple active program instances exists for program: " + program.getUid()).incrementIgnored();
        }
        programInstance = programInstances.get(0);
        if (event.getEvent() != null) {
            programStageInstance = manager.getObject(ProgramStageInstance.class, importOptions.getIdSchemes().getProgramStageInstanceIdScheme(), event.getEvent());
            if (programStageInstance == null) {
                if (importOptions.getIdSchemes().getProgramStageInstanceIdScheme().equals(IdScheme.UID) && !CodeGenerator.isValidUid(event.getEvent())) {
                    return new ImportSummary(ImportStatus.ERROR, "Event.event did not point to a valid event: " + event.getEvent()).incrementIgnored();
                }
            }
        }
    }
    OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), event.getOrgUnit());
    program = programInstance.getProgram();
    if (programStageInstance != null) {
        programStage = programStageInstance.getProgramStage();
    }
    if (organisationUnit == null) {
        return new ImportSummary(ImportStatus.ERROR, "Event.orgUnit does not point to a valid organisation unit: " + event.getOrgUnit()).incrementIgnored();
    }
    if (!programInstance.getProgram().hasOrganisationUnit(organisationUnit)) {
        return new ImportSummary(ImportStatus.ERROR, "Program is not assigned to this organisation unit: " + event.getOrgUnit()).incrementIgnored();
    }
    validateExpiryDays(event, program, null);
    return saveEvent(program, programInstance, programStage, programStageInstance, organisationUnit, event, user, importOptions);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) ArrayList(java.util.ArrayList) Date(java.util.Date) ProgramStage(org.hisp.dhis.program.ProgramStage) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 2 with Enrollments

use of org.hisp.dhis.dxf2.events.enrollment.Enrollments in project dhis2-core by dhis2.

the class AbstractEnrollmentService method updateEnrollments.

// -------------------------------------------------------------------------
// UPDATE
// -------------------------------------------------------------------------
@Override
public ImportSummaries updateEnrollments(List<Enrollment> enrollments, ImportOptions importOptions) {
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    ImportSummaries importSummaries = new ImportSummaries();
    int counter = 0;
    for (Enrollment enrollment : enrollments) {
        importSummaries.addImportSummary(updateEnrollment(enrollment, importOptions));
        if (counter % FLUSH_FREQUENCY == 0) {
            clearSession();
        }
        counter++;
    }
    return importSummaries;
}
Also used : ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions)

Example 3 with Enrollments

use of org.hisp.dhis.dxf2.events.enrollment.Enrollments in project dhis2-core by dhis2.

the class AbstractEnrollmentService method addEnrollments.

// -------------------------------------------------------------------------
// CREATE
// -------------------------------------------------------------------------
@Override
public ImportSummaries addEnrollments(List<Enrollment> enrollments, ImportOptions importOptions) {
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    ImportSummaries importSummaries = new ImportSummaries();
    int counter = 0;
    for (Enrollment enrollment : enrollments) {
        importSummaries.addImportSummary(addEnrollment(enrollment, importOptions));
        if (counter % FLUSH_FREQUENCY == 0) {
            clearSession();
        }
        counter++;
    }
    return importSummaries;
}
Also used : ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions)

Example 4 with Enrollments

use of org.hisp.dhis.dxf2.events.enrollment.Enrollments in project dhis2-core by dhis2.

the class AbstractEnrollmentService method validateRequest.

private ImportSummary validateRequest(Program program, org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance, Enrollment enrollment, OrganisationUnit organisationUnit, ImportOptions importOptions) {
    ImportSummary importSummary = new ImportSummary(enrollment.getEnrollment());
    String error = validateProgramForEnrollment(program, enrollment, organisationUnit, importOptions);
    if (!StringUtils.isEmpty(error)) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.setDescription(error);
        importSummary.incrementIgnored();
        return importSummary;
    }
    ProgramInstanceQueryParams params = new ProgramInstanceQueryParams();
    params.setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL);
    params.setSkipPaging(true);
    params.setProgram(program);
    params.setTrackedEntityInstanceUid(entityInstance.getUid());
    // endpoint is used for bulk import and sync purposes as well
    if (enrollment.getStatus() != EnrollmentStatus.CANCELLED) {
        List<Enrollment> enrollments = getEnrollments(programInstanceService.getProgramInstances(params));
        Set<Enrollment> activeEnrollments = enrollments.stream().filter(e -> e.getStatus() == EnrollmentStatus.ACTIVE).collect(Collectors.toSet());
        // needed
        if (!activeEnrollments.isEmpty() && enrollment.getStatus() == EnrollmentStatus.ACTIVE) {
            importSummary.setStatus(ImportStatus.ERROR);
            importSummary.setDescription("TrackedEntityInstance " + entityInstance.getUid() + " already has an active enrollment in program " + program.getUid());
            importSummary.incrementIgnored();
            return importSummary;
        }
        // imported enrollment has a state other than CANCELLED
        if (program.getOnlyEnrollOnce()) {
            Set<Enrollment> activeOrCompletedEnrollments = enrollments.stream().filter(e -> e.getStatus() == EnrollmentStatus.ACTIVE || e.getStatus() == EnrollmentStatus.COMPLETED).collect(Collectors.toSet());
            if (!activeOrCompletedEnrollments.isEmpty()) {
                importSummary.setStatus(ImportStatus.ERROR);
                importSummary.setDescription("TrackedEntityInstance " + entityInstance.getUid() + " already has an active or completed enrollment in program " + program.getUid() + ", and this program only allows enrolling one time");
                importSummary.incrementIgnored();
                return importSummary;
            }
        }
    }
    checkAttributes(entityInstance, enrollment, importOptions, importSummary);
    if (importSummary.hasConflicts()) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.incrementIgnored();
    }
    return importSummary;
}
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) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams)

Example 5 with Enrollments

use of org.hisp.dhis.dxf2.events.enrollment.Enrollments in project dhis2-core by dhis2.

the class AbstractEnrollmentService method updateEnrollments.

// -------------------------------------------------------------------------
// UPDATE
// -------------------------------------------------------------------------
@Override
public ImportSummaries updateEnrollments(List<Enrollment> enrollments, ImportOptions importOptions, boolean clearSession) {
    List<List<Enrollment>> partitions = Lists.partition(enrollments, FLUSH_FREQUENCY);
    importOptions = updateImportOptions(importOptions);
    ImportSummaries importSummaries = new ImportSummaries();
    List<Event> events = new ArrayList<>();
    for (List<Enrollment> _enrollments : partitions) {
        reloadUser(importOptions);
        prepareCaches(_enrollments, importOptions.getUser());
        for (Enrollment enrollment : _enrollments) {
            ImportSummary importSummary = updateEnrollment(enrollment, importOptions, 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;
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ArrayList(java.util.ArrayList) EnrollmentEvaluationEvent(org.hisp.dhis.programrule.engine.EnrollmentEvaluationEvent) Event(org.hisp.dhis.dxf2.events.event.Event) TrackerEnrollmentWebHookEvent(org.hisp.dhis.programrule.engine.TrackerEnrollmentWebHookEvent) ProgramEnrollmentNotificationEvent(org.hisp.dhis.program.notification.event.ProgramEnrollmentNotificationEvent) List(java.util.List) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) 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