Search in sources :

Example 11 with TrackedEntityInstance

use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance 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 TrackedEntityInstance

use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance 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 TrackedEntityInstance

use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.

the class TrackedEntityInstanceSupplier method get.

@Override
public Map<String, Pair<TrackedEntityInstance, Boolean>> get(ImportOptions importOptions, List<Event> events) {
    if (events == null) {
        return new HashMap<>();
    }
    // @formatter:off
    // Collect all the org unit uids to pass as SQL query argument
    Set<String> teiUids = events.stream().filter(e -> e.getTrackedEntityInstance() != null).map(Event::getTrackedEntityInstance).collect(Collectors.toSet());
    if (isEmpty(teiUids)) {
        return new HashMap<>();
    }
    // Create a map: tei uid -> List [event uid]
    Multimap<String, String> teiToEvent = HashMultimap.create();
    for (Event event : events) {
        teiToEvent.put(event.getTrackedEntityInstance(), event.getUid());
    }
    // 
    // Get all TEI associated to the events
    // 
    Map<String, TrackedEntityInstance> teiMap = getTrackedEntityInstances(teiUids, teiToEvent);
    Map<String, Pair<TrackedEntityInstance, Boolean>> result = new HashMap<>();
    // 
    for (String event : teiMap.keySet()) {
        TrackedEntityInstance tei = teiMap.get(event);
        result.put(event, Pair.of(tei, !importOptions.isSkipLastUpdated() ? aclService.canUpdate(importOptions.getUser(), tei) : null));
    }
    return result;
}
Also used : HashMap(java.util.HashMap) Event(org.hisp.dhis.dxf2.events.event.Event) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Pair(org.apache.commons.lang3.tuple.Pair)

Example 14 with TrackedEntityInstance

use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method checkForExistingTeisIncludingDeleted.

private List<String> checkForExistingTeisIncludingDeleted(List<TrackedEntityInstance> teis, ImportSummaries importSummaries) {
    List<String> foundTeis = teiService.getTrackedEntityInstancesUidsIncludingDeleted(teis.stream().map(TrackedEntityInstance::getTrackedEntityInstance).collect(Collectors.toList()));
    for (String foundTeiUid : foundTeis) {
        ImportSummary is = new ImportSummary(ImportStatus.ERROR, "Tracked entity instance " + foundTeiUid + " already exists or was deleted earlier").setReference(foundTeiUid).incrementIgnored();
        importSummaries.addImportSummary(is);
    }
    return foundTeis;
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary)

Example 15 with TrackedEntityInstance

use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance 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)

Aggregations

TrackedEntityInstance (org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance)61 Test (org.junit.jupiter.api.Test)57 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)50 TrackedEntityInstanceParams (org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)37 TrackedEntityInstanceQueryParams (org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)34 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)29 Date (java.util.Date)26 TrackerTest (org.hisp.dhis.dxf2.TrackerTest)26 ArrayList (java.util.ArrayList)25 List (java.util.List)25 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)25 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)24 Event (org.hisp.dhis.dxf2.events.event.Event)23 Program (org.hisp.dhis.program.Program)22 ProgramInstance (org.hisp.dhis.program.ProgramInstance)21 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)20 Collectors (java.util.stream.Collectors)19 User (org.hisp.dhis.user.User)17 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)16 ImportStatus (org.hisp.dhis.dxf2.importsummary.ImportStatus)16