Search in sources :

Example 31 with ImportSummary

use of org.hisp.dhis.dxf2.common.ImportSummary in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method getTrackedEntityInstance.

public org.hisp.dhis.trackedentity.TrackedEntityInstance getTrackedEntityInstance(TrackedEntityInstance trackedEntityInstance, ImportOptions importOptions, ImportSummary importSummary) {
    if (StringUtils.isEmpty(trackedEntityInstance.getOrgUnit())) {
        importSummary.getConflicts().add(new ImportConflict(trackedEntityInstance.getTrackedEntityInstance(), "No org unit ID in tracked entity instance object."));
        return null;
    }
    org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = new org.hisp.dhis.trackedentity.TrackedEntityInstance();
    OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), trackedEntityInstance.getOrgUnit());
    if (organisationUnit == null) {
        importSummary.getConflicts().add(new ImportConflict(trackedEntityInstance.getTrackedEntityInstance(), "Invalid org unit ID: " + trackedEntityInstance.getOrgUnit()));
        return null;
    }
    entityInstance.setOrganisationUnit(organisationUnit);
    TrackedEntity trackedEntity = getTrackedEntity(importOptions.getIdSchemes(), trackedEntityInstance.getTrackedEntity());
    if (trackedEntity == null) {
        importSummary.getConflicts().add(new ImportConflict(trackedEntityInstance.getTrackedEntityInstance(), "Invalid tracked entity ID: " + trackedEntityInstance.getTrackedEntity()));
        return null;
    }
    entityInstance.setTrackedEntity(trackedEntity);
    entityInstance.setUid(CodeGenerator.isValidUid(trackedEntityInstance.getTrackedEntityInstance()) ? trackedEntityInstance.getTrackedEntityInstance() : CodeGenerator.generateUid());
    return entityInstance;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TrackedEntity(org.hisp.dhis.trackedentity.TrackedEntity) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 32 with ImportSummary

use of org.hisp.dhis.dxf2.common.ImportSummary in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method updateTrackedEntityInstance.

@Override
public ImportSummary updateTrackedEntityInstance(TrackedEntityInstance trackedEntityInstance, ImportOptions importOptions) {
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    ImportSummary importSummary = new ImportSummary(trackedEntityInstance.getTrackedEntityInstance());
    trackedEntityInstance.trimValuesToNull();
    Set<ImportConflict> importConflicts = new HashSet<>();
    importConflicts.addAll(checkRelationships(trackedEntityInstance));
    importConflicts.addAll(checkAttributes(trackedEntityInstance, importOptions));
    org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = manager.get(org.hisp.dhis.trackedentity.TrackedEntityInstance.class, trackedEntityInstance.getTrackedEntityInstance());
    if (entityInstance == null) {
        importConflicts.add(new ImportConflict("TrackedEntityInstance", "trackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance() + " does not point to valid trackedEntityInstance"));
    }
    OrganisationUnit organisationUnit = manager.get(OrganisationUnit.class, trackedEntityInstance.getOrgUnit());
    if (organisationUnit == null) {
        importConflicts.add(new ImportConflict("OrganisationUnit", "orgUnit " + trackedEntityInstance.getOrgUnit() + " does not point to valid organisation unit"));
    }
    importSummary.setConflicts(importConflicts);
    if (!importConflicts.isEmpty()) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.getImportCount().incrementIgnored();
        return importSummary;
    }
    entityInstance.setInactive(trackedEntityInstance.isInactive());
    removeRelationships(entityInstance);
    removeAttributeValues(entityInstance);
    teiService.updateTrackedEntityInstance(entityInstance);
    updateRelationships(trackedEntityInstance, entityInstance);
    updateAttributeValues(trackedEntityInstance, entityInstance);
    updateDateFields(trackedEntityInstance, entityInstance);
    teiService.updateTrackedEntityInstance(entityInstance);
    importSummary.setStatus(ImportStatus.SUCCESS);
    importSummary.setReference(entityInstance.getUid());
    importSummary.getImportCount().incrementUpdated();
    importOptions.setStrategy(ImportStrategy.CREATE_AND_UPDATE);
    importSummary.setEnrollments(handleEnrollments(trackedEntityInstance, entityInstance, importOptions));
    return importSummary;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) HashSet(java.util.HashSet) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 33 with ImportSummary

use of org.hisp.dhis.dxf2.common.ImportSummary 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 34 with ImportSummary

use of org.hisp.dhis.dxf2.common.ImportSummary in project dhis2-core by dhis2.

the class AbstractEventService method validateDataValue.

private boolean validateDataValue(DataElement dataElement, String value, ImportSummary importSummary) {
    String status = ValidationUtils.dataValueIsValid(value, dataElement);
    if (status != null) {
        importSummary.getConflicts().add(new ImportConflict(dataElement.getUid(), status));
        importSummary.getImportCount().incrementIgnored();
        return false;
    }
    return true;
}
Also used : ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 35 with ImportSummary

use of org.hisp.dhis.dxf2.common.ImportSummary in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method addTrackedEntityInstance.

@Override
public ImportSummary addTrackedEntityInstance(TrackedEntityInstance trackedEntityInstance, ImportOptions importOptions) {
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    ImportSummary importSummary = new ImportSummary(trackedEntityInstance.getTrackedEntityInstance());
    trackedEntityInstance.trimValuesToNull();
    Set<ImportConflict> importConflicts = new HashSet<>();
    importConflicts.addAll(checkTrackedEntity(trackedEntityInstance, importOptions));
    importConflicts.addAll(checkAttributes(trackedEntityInstance, importOptions));
    importSummary.setConflicts(importConflicts);
    if (!importConflicts.isEmpty()) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.getImportCount().incrementIgnored();
        return importSummary;
    }
    org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = getTrackedEntityInstance(trackedEntityInstance, importOptions, importSummary);
    if (entityInstance == null) {
        return importSummary;
    }
    teiService.addTrackedEntityInstance(entityInstance);
    updateRelationships(trackedEntityInstance, entityInstance);
    updateAttributeValues(trackedEntityInstance, entityInstance);
    updateDateFields(trackedEntityInstance, entityInstance);
    teiService.updateTrackedEntityInstance(entityInstance);
    importSummary.setReference(entityInstance.getUid());
    importSummary.getImportCount().incrementImported();
    importOptions.setStrategy(ImportStrategy.CREATE_AND_UPDATE);
    importSummary.setEnrollments(handleEnrollments(trackedEntityInstance, entityInstance, importOptions));
    return importSummary;
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) HashSet(java.util.HashSet) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Aggregations

ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)102 Test (org.junit.Test)58 DhisSpringTest (org.hisp.dhis.DhisSpringTest)49 ClassPathResource (org.springframework.core.io.ClassPathResource)39 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)29 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)25 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)22 DataValue (org.hisp.dhis.datavalue.DataValue)18 Event (org.hisp.dhis.dxf2.events.event.Event)17 InputStream (java.io.InputStream)14 Date (java.util.Date)11 ImportConflict (org.hisp.dhis.dxf2.importsummary.ImportConflict)11 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)11 IOException (java.io.IOException)10 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)10 DhisTest (org.hisp.dhis.DhisTest)9 DhisApiVersion (org.hisp.dhis.common.DhisApiVersion)7 DataElement (org.hisp.dhis.dataelement.DataElement)7 ImportStatus (org.hisp.dhis.dxf2.importsummary.ImportStatus)7 ApiVersion (org.hisp.dhis.webapi.mvc.annotation.ApiVersion)7