Search in sources :

Example 26 with ImportOptions

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

the class AbstractEnrollmentService method updateEnrollments.

// -------------------------------------------------------------------------
// UPDATE
// -------------------------------------------------------------------------
@Override
public ImportSummaries updateEnrollments(List<Enrollment> enrollments, ImportOptions importOptions) {
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    ImportSummaries importSummaries = new ImportSummaries();
    int counter = 0;
    for (Enrollment enrollment : enrollments) {
        importSummaries.addImportSummary(updateEnrollment(enrollment, importOptions));
        if (counter % FLUSH_FREQUENCY == 0) {
            clearSession();
        }
        counter++;
    }
    return importSummaries;
}
Also used : ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions)

Example 27 with ImportOptions

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

the class JacksonEnrollmentService method addEnrollmentList.

private ImportSummaries addEnrollmentList(List<Enrollment> enrollments, ImportOptions importOptions) {
    ImportSummaries importSummaries = new ImportSummaries();
    List<Enrollment> create = new ArrayList<>();
    List<Enrollment> update = new ArrayList<>();
    List<String> delete = new ArrayList<>();
    if (importOptions.getImportStrategy().isCreate()) {
        create.addAll(enrollments);
    } else if (importOptions.getImportStrategy().isCreateAndUpdate()) {
        for (Enrollment enrollment : enrollments) {
            if (StringUtils.isEmpty(enrollment.getEnrollment())) {
                create.add(enrollment);
            } else {
                if (!programInstanceService.programInstanceExists(enrollment.getEnrollment())) {
                    create.add(enrollment);
                } else {
                    update.add(enrollment);
                }
            }
        }
    } else if (importOptions.getImportStrategy().isUpdate()) {
        update.addAll(enrollments);
    } else if (importOptions.getImportStrategy().isDelete()) {
        delete.addAll(enrollments.stream().map(Enrollment::getEnrollment).collect(Collectors.toList()));
    }
    importSummaries.addImportSummaries(addEnrollments(create, importOptions));
    importSummaries.addImportSummaries(updateEnrollments(update, importOptions));
    importSummaries.addImportSummaries(deleteEnrollments(delete));
    return importSummaries;
}
Also used : ArrayList(java.util.ArrayList) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries)

Example 28 with ImportOptions

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

the class AbstractEnrollmentService method handleEvents.

// -------------------------------------------------------------------------
// HELPERS
// -------------------------------------------------------------------------
private ImportSummaries handleEvents(Enrollment enrollment, ProgramInstance programInstance, ImportOptions importOptions) {
    List<Event> create = new ArrayList<>();
    List<Event> update = new ArrayList<>();
    for (Event event : enrollment.getEvents()) {
        event.setEnrollment(enrollment.getEnrollment());
        event.setProgram(programInstance.getProgram().getUid());
        if (!programStageInstanceService.programStageInstanceExists(event.getEvent())) {
            create.add(event);
        } else {
            update.add(event);
        }
    }
    ImportSummaries importSummaries = new ImportSummaries();
    importSummaries.addImportSummaries(eventService.addEvents(create, importOptions));
    importSummaries.addImportSummaries(eventService.updateEvents(update, false));
    return importSummaries;
}
Also used : ArrayList(java.util.ArrayList) Event(org.hisp.dhis.dxf2.events.event.Event) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries)

Example 29 with ImportOptions

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

the class AbstractEnrollmentService method validateAttributeType.

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

Example 30 with ImportOptions

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

the class AbstractEnrollmentService method addEnrollments.

// -------------------------------------------------------------------------
// CREATE
// -------------------------------------------------------------------------
@Override
public ImportSummaries addEnrollments(List<Enrollment> enrollments, ImportOptions importOptions) {
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    ImportSummaries importSummaries = new ImportSummaries();
    int counter = 0;
    for (Enrollment enrollment : enrollments) {
        importSummaries.addImportSummary(addEnrollment(enrollment, importOptions));
        if (counter % FLUSH_FREQUENCY == 0) {
            clearSession();
        }
        counter++;
    }
    return importSummaries;
}
Also used : ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions)

Aggregations

ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)51 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)39 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)22 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)20 InputStream (java.io.InputStream)19 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)19 ClassPathResource (org.springframework.core.io.ClassPathResource)17 ImportConflict (org.hisp.dhis.dxf2.importsummary.ImportConflict)16 Test (org.junit.Test)16 DhisSpringTest (org.hisp.dhis.DhisSpringTest)15 ArrayList (java.util.ArrayList)11 Date (java.util.Date)11 IOException (java.io.IOException)10 Event (org.hisp.dhis.dxf2.events.event.Event)10 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)10 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)9 TaskId (org.hisp.dhis.scheduling.TaskId)9 Program (org.hisp.dhis.program.Program)8 Lists (com.google.common.collect.Lists)7 HashSet (java.util.HashSet)7