Search in sources :

Example 16 with Enrollment

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);
    }
}
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 17 with Enrollment

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;
}
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 18 with Enrollment

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()) + " ).");
        }
    }
}
Also used : ProgramInstance(org.hisp.dhis.program.ProgramInstance) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) Date(java.util.Date) DateUtils.parseDate(org.hisp.dhis.util.DateUtils.parseDate)

Example 19 with Enrollment

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;
}
Also used : TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) Event(org.hisp.dhis.dxf2.events.event.Event) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem)

Example 20 with Enrollment

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));
    }
}
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)

Aggregations

ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)46 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)44 Test (org.junit.jupiter.api.Test)35 Event (org.hisp.dhis.dxf2.events.event.Event)31 Date (java.util.Date)28 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)23 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)20 ArrayList (java.util.ArrayList)18 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)18 ProgramInstance (org.hisp.dhis.program.ProgramInstance)18 List (java.util.List)17 TrackedEntityInstance (org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance)17 TrackedEntityInstanceParams (org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)14 Program (org.hisp.dhis.program.Program)14 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)13 Map (java.util.Map)12 Set (java.util.Set)12 Collectors (java.util.stream.Collectors)12 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)12 Lists (com.google.common.collect.Lists)11