Search in sources :

Example 11 with Enrollment

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

the class AbstractEnrollmentService method checkAttributes.

private void checkAttributes(org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance, Enrollment enrollment, ImportOptions importOptions, ImportConflicts importConflicts) {
    Map<TrackedEntityAttribute, Boolean> mandatoryMap = Maps.newHashMap();
    Map<String, String> attributeValueMap = Maps.newHashMap();
    Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
    for (ProgramTrackedEntityAttribute programTrackedEntityAttribute : program.getProgramAttributes()) {
        mandatoryMap.put(programTrackedEntityAttribute.getAttribute(), programTrackedEntityAttribute.isMandatory());
    }
    // ignore attributes which do not belong to this program
    trackedEntityInstance.getTrackedEntityAttributeValues().stream().filter(value -> mandatoryMap.containsKey(value.getAttribute())).forEach(value -> attributeValueMap.put(value.getAttribute().getUid(), value.getValue()));
    for (Attribute attribute : enrollment.getAttributes()) {
        attributeValueMap.put(attribute.getAttribute(), attribute.getValue());
        validateAttributeType(attribute, importOptions, importConflicts);
    }
    List<String> errors = trackerAccessManager.canRead(importOptions.getUser(), trackedEntityInstance);
    if (!errors.isEmpty()) {
        throw new IllegalQueryException(errors.toString());
    }
    checkAttributeForMandatoryMaxLengthAndUniqueness(trackedEntityInstance, importOptions, importConflicts, mandatoryMap, attributeValueMap);
    if (!attributeValueMap.isEmpty()) {
        importConflicts.addConflict(ATTRIBUTE_ATTRIBUTE, "Only program attributes is allowed for enrollment " + attributeValueMap);
    }
    if (!program.getSelectEnrollmentDatesInFuture()) {
        if (Objects.nonNull(enrollment.getEnrollmentDate()) && enrollment.getEnrollmentDate().after(new Date())) {
            importConflicts.addConflict("Enrollment.date", "Enrollment Date can't be future date :" + enrollment.getEnrollmentDate());
        }
    }
    if (!program.getSelectIncidentDatesInFuture()) {
        if (Objects.nonNull(enrollment.getIncidentDate()) && enrollment.getIncidentDate().after(new Date())) {
            importConflicts.addConflict("Enrollment.incidentDate", "Incident Date can't be future date :" + enrollment.getIncidentDate());
        }
    }
}
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) Program(org.hisp.dhis.program.Program) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) Date(java.util.Date)

Example 12 with Enrollment

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

the class ProgramInstanceSupplier method get.

public Map<String, ProgramInstance> get(ImportOptions importOptions, Map<String, Pair<TrackedEntityInstance, Boolean>> teiMap, List<Event> events) {
    if (events == null) {
        return new HashMap<>();
    }
    // Collect all the program instance UIDs to pass as SQL query argument
    Set<String> programInstanceUids = events.stream().filter(e -> StringUtils.isNotEmpty(e.getEnrollment())).map(Event::getEnrollment).collect(Collectors.toSet());
    Map<String, ProgramInstance> programInstances = new HashMap<>();
    if (!programInstanceUids.isEmpty()) {
        // Create a bi-directional map enrollment uid -> event uid
        Multimap<String, String> programInstanceToEvent = HashMultimap.create();
        for (Event event : events) {
            programInstanceToEvent.put(event.getEnrollment(), event.getUid());
        }
        // Collect all the Program Stage Instances specified in the Events
        // (enrollment
        // property)
        programInstances = getProgramInstancesByUid(importOptions, events, programInstanceToEvent, programInstanceUids);
    }
    mapExistingEventsToProgramInstances(importOptions, events, programInstances);
    mapEventsToProgramInstanceByTei(importOptions, events, programInstances, teiMap);
    return programInstances;
}
Also used : HashMap(java.util.HashMap) ProgramInstance(org.hisp.dhis.program.ProgramInstance) Event(org.hisp.dhis.dxf2.events.event.Event)

Example 13 with Enrollment

use of org.hisp.dhis.dxf2.events.enrollment.Enrollment 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 14 with Enrollment

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

the class AbstractTrackedEntityInstanceService method updateTrackedEntityInstance.

private ImportSummary updateTrackedEntityInstance(TrackedEntityInstance dtoEntityInstance, String programId, ImportOptions importOptions, boolean singleUpdate, boolean handleEnrollments) {
    ImportSummary importSummary = new ImportSummary(dtoEntityInstance.getTrackedEntityInstance());
    importOptions = updateImportOptions(importOptions);
    dtoEntityInstance.trimValuesToNull();
    checkAttributes(dtoEntityInstance, importOptions, importSummary, true);
    org.hisp.dhis.trackedentity.TrackedEntityInstance daoEntityInstance = teiService.getTrackedEntityInstance(dtoEntityInstance.getTrackedEntityInstance(), importOptions.getUser());
    List<String> errors = trackerAccessManager.canWrite(importOptions.getUser(), daoEntityInstance);
    OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), dtoEntityInstance.getOrgUnit());
    Program program = getProgram(importOptions.getIdSchemes(), programId);
    if (daoEntityInstance == null || !errors.isEmpty() || organisationUnit == null || importSummary.hasConflicts()) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.getImportCount().incrementIgnored();
        if (daoEntityInstance == null) {
            String message = "You are trying to add or update tracked entity instance " + dtoEntityInstance.getTrackedEntityInstance() + " that has already been deleted";
            importSummary.addConflict("TrackedEntityInstance", message);
        } else if (!errors.isEmpty()) {
            importSummary.setDescription(errors.toString());
        } else if (organisationUnit == null) {
            String message = "Org unit " + dtoEntityInstance.getOrgUnit() + " does not exist";
            importSummary.addConflict("OrganisationUnit", message);
        }
        return importSummary;
    }
    daoEntityInstance.setOrganisationUnit(organisationUnit);
    daoEntityInstance.setInactive(dtoEntityInstance.isInactive());
    daoEntityInstance.setPotentialDuplicate(dtoEntityInstance.isPotentialDuplicate());
    if (dtoEntityInstance.getGeometry() != null) {
        FeatureType featureType = daoEntityInstance.getTrackedEntityType().getFeatureType();
        if (featureType.equals(FeatureType.NONE) || !featureType.equals(FeatureType.getTypeFromName(dtoEntityInstance.getGeometry().getGeometryType()))) {
            importSummary.addConflict(dtoEntityInstance.getTrackedEntityInstance(), "Geometry does not conform to feature type '" + featureType + "'");
            importSummary.getImportCount().incrementIgnored();
            return importSummary;
        } else {
            daoEntityInstance.setGeometry(dtoEntityInstance.getGeometry());
        }
    } else if (!FeatureType.NONE.equals(dtoEntityInstance.getFeatureType()) && dtoEntityInstance.getCoordinates() != null) {
        try {
            daoEntityInstance.setGeometry(GeoUtils.getGeometryFromCoordinatesAndType(dtoEntityInstance.getFeatureType(), dtoEntityInstance.getCoordinates()));
        } catch (IOException e) {
            importSummary.addConflict(dtoEntityInstance.getTrackedEntityInstance(), "Could not parse coordinates");
            importSummary.getImportCount().incrementIgnored();
            return importSummary;
        }
    } else {
        daoEntityInstance.setGeometry(null);
    }
    if (!importOptions.isIgnoreEmptyCollection() || !dtoEntityInstance.getAttributes().isEmpty()) {
        updateAttributeValues(dtoEntityInstance, daoEntityInstance, program, importOptions.getUser());
    }
    updateDateFields(dtoEntityInstance, daoEntityInstance);
    teiService.updateTrackedEntityInstance(daoEntityInstance);
    importSummary.setReference(daoEntityInstance.getUid());
    importSummary.getImportCount().incrementUpdated();
    if (singleUpdate && (!importOptions.isIgnoreEmptyCollection() || !dtoEntityInstance.getRelationships().isEmpty())) {
        importSummary.setRelationships(handleRelationships(dtoEntityInstance, daoEntityInstance, importOptions));
    }
    if (handleEnrollments) {
        importSummary.setEnrollments(handleEnrollments(dtoEntityInstance, daoEntityInstance, importOptions));
    } else {
        for (Enrollment enrollment : dtoEntityInstance.getEnrollments()) {
            enrollment.setTrackedEntityType(dtoEntityInstance.getTrackedEntityType());
            enrollment.setTrackedEntityInstance(daoEntityInstance.getUid());
        }
    }
    return importSummary;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) FeatureType(org.hisp.dhis.organisationunit.FeatureType) Program(org.hisp.dhis.program.Program) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) IOException(java.io.IOException)

Example 15 with Enrollment

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

the class AbstractTrackedEntityInstanceService method handleEnrollments.

private ImportSummaries handleEnrollments(TrackedEntityInstance dtoEntityInstance, org.hisp.dhis.trackedentity.TrackedEntityInstance daoEntityInstance, ImportOptions importOptions) {
    List<Enrollment> create = new ArrayList<>();
    List<Enrollment> update = new ArrayList<>();
    List<Enrollment> delete = new ArrayList<>();
    for (Enrollment enrollment : dtoEntityInstance.getEnrollments()) {
        enrollment.setTrackedEntityType(dtoEntityInstance.getTrackedEntityType());
        enrollment.setTrackedEntityInstance(daoEntityInstance.getUid());
        if (importOptions.getImportStrategy().isSync() && enrollment.isDeleted()) {
            delete.add(enrollment);
        } else if (!programInstanceService.programInstanceExists(enrollment.getEnrollment())) {
            create.add(enrollment);
        } else {
            update.add(enrollment);
        }
    }
    ImportSummaries importSummaries = new ImportSummaries();
    importSummaries.addImportSummaries(enrollmentService.deleteEnrollments(delete, importOptions, false));
    importSummaries.addImportSummaries(enrollmentService.updateEnrollments(update, importOptions, false));
    importSummaries.addImportSummaries(enrollmentService.addEnrollments(create, importOptions, daoEntityInstance, false));
    return importSummaries;
}
Also used : ArrayList(java.util.ArrayList) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries)

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