Search in sources :

Example 11 with ImportConflicts

use of org.hisp.dhis.dxf2.importsummary.ImportConflicts in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method checkTrackedEntity.

private List<ImportConflict> checkTrackedEntity(TrackedEntityInstance trackedEntityInstance, ImportOptions importOptions) {
    List<ImportConflict> importConflicts = new ArrayList<>();
    if (trackedEntityInstance.getTrackedEntity() == null) {
        importConflicts.add(new ImportConflict("TrackedEntityInstance.trackedEntity", "Missing required property trackedEntity"));
        return importConflicts;
    }
    TrackedEntity trackedEntity = getTrackedEntity(importOptions.getIdSchemes(), trackedEntityInstance.getTrackedEntity());
    if (trackedEntity == null) {
        importConflicts.add(new ImportConflict("TrackedEntityInstance.trackedEntity", "Invalid trackedEntity" + trackedEntityInstance.getTrackedEntity()));
    }
    return importConflicts;
}
Also used : TrackedEntity(org.hisp.dhis.trackedentity.TrackedEntity) ArrayList(java.util.ArrayList) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 12 with ImportConflicts

use of org.hisp.dhis.dxf2.importsummary.ImportConflicts in project dhis2-core by dhis2.

the class AbstractEnrollmentService method updateEnrollment.

@Override
public ImportSummary updateEnrollment(Enrollment enrollment, ImportOptions importOptions) {
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    if (enrollment == null || enrollment.getEnrollment() == null) {
        return new ImportSummary(ImportStatus.ERROR, "No enrollment or enrollment ID was supplied").incrementIgnored();
    }
    ImportSummary importSummary = new ImportSummary(enrollment.getEnrollment());
    ProgramInstance programInstance = programInstanceService.getProgramInstance(enrollment.getEnrollment());
    if (programInstance == null) {
        return new ImportSummary(ImportStatus.ERROR, "Enrollment ID was not valid.").incrementIgnored();
    }
    Set<ImportConflict> importConflicts = new HashSet<>();
    importConflicts.addAll(checkAttributes(enrollment, importOptions));
    importSummary.setConflicts(importConflicts);
    if (!importConflicts.isEmpty()) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.getImportCount().incrementIgnored();
        return importSummary;
    }
    org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
    Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
    programInstance.setProgram(program);
    programInstance.setEntityInstance(entityInstance);
    if (enrollment.getIncidentDate() != null) {
        programInstance.setIncidentDate(enrollment.getIncidentDate());
    }
    if (enrollment.getEnrollmentDate() != null) {
        programInstance.setEnrollmentDate(enrollment.getEnrollmentDate());
    }
    programInstance.setFollowup(enrollment.getFollowup());
    if (program.getDisplayIncidentDate() && programInstance.getIncidentDate() == null) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.setDescription("DisplayIncidentDate is true but IncidentDate is null ");
        importSummary.incrementIgnored();
        return importSummary;
    }
    if (program.getCaptureCoordinates()) {
        if (enrollment.getCoordinate() != null && enrollment.getCoordinate().isValid()) {
            programInstance.setLatitude(enrollment.getCoordinate().getLatitude());
            programInstance.setLongitude(enrollment.getCoordinate().getLongitude());
        } else {
            programInstance.setLatitude(null);
            programInstance.setLongitude(null);
        }
    }
    if (EnrollmentStatus.fromProgramStatus(programInstance.getStatus()) != enrollment.getStatus()) {
        if (EnrollmentStatus.CANCELLED == enrollment.getStatus()) {
            programInstanceService.cancelProgramInstanceStatus(programInstance);
        } else if (EnrollmentStatus.COMPLETED == enrollment.getStatus()) {
            programInstanceService.completeProgramInstanceStatus(programInstance);
        } else if (EnrollmentStatus.ACTIVE == enrollment.getStatus()) {
            programInstanceService.incompleteProgramInstanceStatus(programInstance);
        }
    }
    updateAttributeValues(enrollment, importOptions);
    updateDateFields(enrollment, programInstance);
    programInstanceService.updateProgramInstance(programInstance);
    manager.update(programInstance.getEntityInstance());
    saveTrackedEntityComment(programInstance, enrollment);
    importSummary.setReference(enrollment.getEnrollment());
    importSummary.getImportCount().incrementUpdated();
    importOptions.setStrategy(ImportStrategy.CREATE_AND_UPDATE);
    importSummary.setEvents(handleEvents(enrollment, programInstance, importOptions));
    return importSummary;
}
Also used : Program(org.hisp.dhis.program.Program) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) HashSet(java.util.HashSet) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 13 with ImportConflicts

use of org.hisp.dhis.dxf2.importsummary.ImportConflicts in project dhis2-core by dhis2.

the class AbstractEnrollmentService method addEnrollment.

@Override
public ImportSummary addEnrollment(Enrollment enrollment, ImportOptions importOptions) {
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    ImportSummary importSummary = new ImportSummary(enrollment.getEnrollment());
    org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
    TrackedEntityInstance trackedEntityInstance = trackedEntityInstanceService.getTrackedEntityInstance(entityInstance);
    Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
    ProgramInstanceQueryParams params = new ProgramInstanceQueryParams();
    params.setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL);
    params.setSkipPaging(true);
    params.setProgram(program);
    params.setTrackedEntityInstance(entityInstance);
    params.setProgramStatus(ProgramStatus.ACTIVE);
    List<Enrollment> enrollments = getEnrollments(programInstanceService.getProgramInstances(params));
    if (!enrollments.isEmpty()) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.setDescription("TrackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance() + " already have an active enrollment in program " + program.getUid());
        importSummary.incrementIgnored();
        return importSummary;
    }
    if (program.getOnlyEnrollOnce()) {
        params.setProgramStatus(ProgramStatus.COMPLETED);
        enrollments = getEnrollments(programInstanceService.getProgramInstances(params));
        if (!enrollments.isEmpty()) {
            importSummary.setStatus(ImportStatus.ERROR);
            importSummary.setDescription("TrackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance() + " already have a completed enrollment in program " + program.getUid() + ", and this program is" + " configured to only allow enrolling one time.");
            importSummary.incrementIgnored();
            return importSummary;
        }
    }
    Set<ImportConflict> importConflicts = new HashSet<>();
    importConflicts.addAll(checkAttributes(enrollment, importOptions));
    importSummary.setConflicts(importConflicts);
    if (!importConflicts.isEmpty()) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.incrementIgnored();
        return importSummary;
    }
    OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), enrollment.getOrgUnit());
    ProgramInstance programInstance = programInstanceService.enrollTrackedEntityInstance(entityInstance, program, enrollment.getEnrollmentDate(), enrollment.getIncidentDate(), organisationUnit, enrollment.getEnrollment());
    if (programInstance == null) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.setDescription("Could not enroll TrackedEntityInstance " + enrollment.getTrackedEntityInstance() + " into program " + enrollment.getProgram());
        importSummary.incrementIgnored();
        return importSummary;
    }
    if (program.getDisplayIncidentDate() && programInstance.getIncidentDate() == null) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.setDescription("DisplayIncidentDate is true but IncidentDate is null ");
        importSummary.incrementIgnored();
        return importSummary;
    }
    if (program.getCaptureCoordinates()) {
        if (enrollment.getCoordinate() != null && enrollment.getCoordinate().isValid()) {
            programInstance.setLatitude(enrollment.getCoordinate().getLatitude());
            programInstance.setLongitude(enrollment.getCoordinate().getLongitude());
        } else {
            programInstance.setLatitude(null);
            programInstance.setLongitude(null);
        }
    }
    updateAttributeValues(enrollment, importOptions);
    updateDateFields(enrollment, programInstance);
    programInstance.setFollowup(enrollment.getFollowup());
    programInstanceService.updateProgramInstance(programInstance);
    manager.update(programInstance.getEntityInstance());
    saveTrackedEntityComment(programInstance, enrollment);
    importSummary.setReference(programInstance.getUid());
    importSummary.getImportCount().incrementImported();
    importOptions.setStrategy(ImportStrategy.CREATE_AND_UPDATE);
    importSummary.setEvents(handleEvents(enrollment, programInstance, importOptions));
    return importSummary;
}
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.dxf2.events.trackedentity.TrackedEntityInstance) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams) HashSet(java.util.HashSet) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 14 with ImportConflicts

use of org.hisp.dhis.dxf2.importsummary.ImportConflicts in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method checkScope.

private List<ImportConflict> checkScope(org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance, TrackedEntityAttribute trackedEntityAttribute, String value, OrganisationUnit organisationUnit) {
    List<ImportConflict> importConflicts = new ArrayList<>();
    if (trackedEntityAttribute == null || value == null) {
        return importConflicts;
    }
    String errorMessage = trackedEntityAttributeService.validateScope(trackedEntityAttribute, value, trackedEntityInstance, organisationUnit, null);
    if (errorMessage != null) {
        importConflicts.add(new ImportConflict("Attribute.value", errorMessage));
    }
    return importConflicts;
}
Also used : ArrayList(java.util.ArrayList) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 15 with ImportConflicts

use of org.hisp.dhis.dxf2.importsummary.ImportConflicts in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method validateAttributeType.

//--------------------------------------------------------------------------
// VALIDATION
//--------------------------------------------------------------------------
private List<ImportConflict> validateAttributeType(Attribute attribute, ImportOptions importOptions) {
    List<ImportConflict> importConflicts = Lists.newArrayList();
    if (attribute == null || attribute.getValue() == null) {
        return importConflicts;
    }
    TrackedEntityAttribute trackedEntityAttribute = getTrackedEntityAttribute(importOptions.getIdSchemes(), attribute.getAttribute());
    if (trackedEntityAttribute == null) {
        importConflicts.add(new ImportConflict("Attribute.attribute", "Does not point to a valid attribute."));
        return importConflicts;
    }
    String errorMessage = trackedEntityAttributeService.validateValueType(trackedEntityAttribute, attribute.getValue());
    if (errorMessage != null) {
        importConflicts.add(new ImportConflict("Attribute.value", errorMessage));
    }
    return importConflicts;
}
Also used : TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Aggregations

ImportConflict (org.hisp.dhis.dxf2.importsummary.ImportConflict)13 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)9 ArrayList (java.util.ArrayList)8 HashSet (java.util.HashSet)7 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)7 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)6 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)6 ImportConflicts (org.hisp.dhis.dxf2.importsummary.ImportConflicts)5 ImportStatus (org.hisp.dhis.dxf2.importsummary.ImportStatus)5 Program (org.hisp.dhis.program.Program)5 ProgramInstance (org.hisp.dhis.program.ProgramInstance)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 Date (java.util.Date)4 Map (java.util.Map)4 Set (java.util.Set)4 IdSchemes (org.hisp.dhis.common.IdSchemes)4 Lists (com.google.common.collect.Lists)3 Collections (java.util.Collections)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3