Search in sources :

Example 6 with DELETED

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.DELETED in project dhis2-core by dhis2.

the class EventManager method resolveImportableEvents.

/**
 * Filters out Events which are already present in the database (regardless
 * of the 'deleted' state) as well as duplicates within the payload (if
 * stage is not repeatable)
 *
 * @param events Events to import
 * @param importSummaries ImportSummaries used for import
 * @return Events that is possible to import (pass validation)
 */
private List<Event> resolveImportableEvents(final List<Event> events, final ImportSummaries importSummaries, final WorkContext workContext) {
    List<Event> importableEvents = new ArrayList<>();
    Set<String> importableStageEvents = new HashSet<>();
    final Set<String> existingProgramStageInstances = workContext.getProgramStageInstanceMap().keySet();
    for (Event eventToImport : events) {
        if (existingProgramStageInstances.contains(eventToImport.getUid())) {
            final ImportSummary is = new ImportSummary(ERROR, "Event " + eventToImport.getUid() + " already exists or was deleted earlier").setReference(eventToImport.getUid()).incrementIgnored();
            importSummaries.addImportSummary(is);
            continue;
        }
        Program program = workContext.getProgramsMap().get(eventToImport.getProgram());
        ProgramStage programStage = workContext.getProgramStage(IdScheme.UID, eventToImport.getProgramStage());
        if (program != null && programStage != null && program.isRegistration() && !programStage.getRepeatable()) {
            String eventContextId = programStage.getUid() + "-" + eventToImport.getEnrollment();
            if (importableStageEvents.contains(eventContextId)) {
                final ImportSummary is = new ImportSummary(ERROR, "ProgramStage " + eventToImport.getProgramStage() + " is not repeatable. Current payload contains duplicate event").setReference(eventToImport.getUid()).incrementIgnored();
                importSummaries.addImportSummary(is);
            } else {
                importableEvents.add(eventToImport);
                importableStageEvents.add(eventContextId);
            }
        } else {
            importableEvents.add(eventToImport);
        }
    }
    return importableEvents;
}
Also used : Program(org.hisp.dhis.program.Program) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ArrayList(java.util.ArrayList) Event(org.hisp.dhis.dxf2.events.event.Event) ImmutableEvent(org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent) ProgramStage(org.hisp.dhis.program.ProgramStage) HashSet(java.util.HashSet)

Example 7 with DELETED

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.DELETED 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 8 with DELETED

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.DELETED 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 9 with DELETED

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.DELETED in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method deleteTrackedEntityInstance.

private ImportSummary deleteTrackedEntityInstance(String uid, TrackedEntityInstance dtoEntityInstance, ImportOptions importOptions) {
    ImportSummary importSummary = new ImportSummary();
    importOptions = updateImportOptions(importOptions);
    boolean teiExists = teiService.trackedEntityInstanceExists(uid);
    if (teiExists) {
        org.hisp.dhis.trackedentity.TrackedEntityInstance daoEntityInstance = teiService.getTrackedEntityInstance(uid);
        if (dtoEntityInstance != null) {
            importSummary.setReference(uid);
            importSummary.setEnrollments(handleEnrollments(dtoEntityInstance, daoEntityInstance, importOptions));
        }
        if (importOptions.getUser() != null) {
            isAllowedToDelete(importOptions.getUser(), daoEntityInstance, importSummary);
            if (importSummary.hasConflicts()) {
                importSummary.setStatus(ImportStatus.ERROR);
                importSummary.setReference(daoEntityInstance.getUid());
                importSummary.incrementIgnored();
                return importSummary;
            }
        }
        teiService.deleteTrackedEntityInstance(daoEntityInstance);
        importSummary.setStatus(ImportStatus.SUCCESS);
        importSummary.setDescription("Deletion of tracked entity instance " + uid + " was successful");
        return importSummary.incrementDeleted();
    } else {
        importSummary.setStatus(ImportStatus.SUCCESS);
        importSummary.setDescription("Tracked entity instance " + uid + " cannot be deleted as it is not present in the system");
        return importSummary.incrementIgnored();
    }
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary)

Example 10 with DELETED

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.DELETED in project dhis2-core by dhis2.

the class ProgramStageInstanceBasicCheck method check.

@Override
public ImportSummary check(final ImmutableEvent event, final WorkContext ctx) {
    final ProgramStageInstance programStageInstance = ctx.getProgramStageInstanceMap().get(event.getEvent());
    if (programStageInstance == null) {
        final ImportSummary error = error("Event ID " + event.getEvent() + " doesn't point to valid event", event.getEvent());
        error.addConflict("Invalid Event ID", event.getEvent());
        return error;
    } else if (programStageInstance.isDeleted() || ctx.getImportOptions().getImportStrategy().isCreate()) {
        return error("Event ID " + event.getEvent() + " was already used and/or deleted. This event can not be modified.").setReference(event.getEvent());
    }
    return success();
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Aggregations

ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)26 Test (org.junit.jupiter.api.Test)12 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)7 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)7 DataElement (org.hisp.dhis.dataelement.DataElement)6 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)6 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)6 IOException (java.io.IOException)5 DhisTest (org.hisp.dhis.DhisTest)5 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)5 Program (org.hisp.dhis.program.Program)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 IdScheme (org.hisp.dhis.common.IdScheme)4 ImportCount (org.hisp.dhis.dxf2.importsummary.ImportCount)4 ProgramStage (org.hisp.dhis.program.ProgramStage)4 Lists (com.google.common.collect.Lists)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 List (java.util.List)3