Search in sources :

Example 46 with ImportOptions

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

the class JacksonEventService method addEvents.

private ImportSummaries addEvents(List<Event> events, TaskId taskId, ImportOptions importOptions) {
    ImportSummaries importSummaries = new ImportSummaries();
    notifier.clear(taskId).notify(taskId, "Importing events");
    Clock clock = new Clock(log).startClock();
    List<Event> create = new ArrayList<>();
    List<Event> update = new ArrayList<>();
    List<String> delete = new ArrayList<>();
    if (importOptions.getImportStrategy().isCreate()) {
        create.addAll(events);
    } else if (importOptions.getImportStrategy().isCreateAndUpdate()) {
        for (Event event : events) {
            if (StringUtils.isEmpty(event.getEvent())) {
                create.add(event);
            } else {
                ProgramStageInstance programStageInstance = manager.getObject(ProgramStageInstance.class, importOptions.getIdSchemes().getProgramStageInstanceIdScheme(), event.getEvent());
                if (programStageInstance == null) {
                    create.add(event);
                } else {
                    update.add(event);
                }
            }
        }
    } else if (importOptions.getImportStrategy().isUpdate()) {
        update.addAll(events);
    } else if (importOptions.getImportStrategy().isDelete()) {
        delete.addAll(events.stream().map(Event::getEvent).collect(Collectors.toList()));
    }
    importSummaries.addImportSummaries(addEvents(create, importOptions));
    importSummaries.addImportSummaries(updateEvents(update, false));
    importSummaries.addImportSummaries(deleteEvents(delete));
    if (taskId != null) {
        notifier.notify(taskId, NotificationLevel.INFO, "Import done. Completed in " + clock.time() + ".", true).addTaskSummary(taskId, importSummaries);
    } else {
        clock.logTime("Import done");
    }
    return importSummaries;
}
Also used : ArrayList(java.util.ArrayList) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) Clock(org.hisp.dhis.system.util.Clock) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 47 with ImportOptions

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

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

the class AbstractEventService method saveEvent.

private ImportSummary saveEvent(Program program, ProgramInstance programInstance, ProgramStage programStage, ProgramStageInstance programStageInstance, OrganisationUnit organisationUnit, Event event, User user, ImportOptions importOptions) {
    Assert.notNull(program, "Program cannot be null");
    Assert.notNull(programInstance, "Program instance cannot be null");
    Assert.notNull(programStage, "Program stage cannot be null");
    ImportSummary importSummary = new ImportSummary(event.getEvent());
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    boolean existingEvent = programStageInstance != null;
    boolean dryRun = importOptions.isDryRun();
    // = new Date();
    Date executionDate = null;
    if (event.getEventDate() != null) {
        executionDate = DateUtils.parseDate(event.getEventDate());
    }
    Date dueDate = new Date();
    if (event.getDueDate() != null) {
        dueDate = DateUtils.parseDate(event.getDueDate());
    }
    String storedBy = getStoredBy(event, importSummary, user);
    String completedBy = getCompletedBy(event, importSummary, user);
    DataElementCategoryOptionCombo aoc = null;
    if ((event.getAttributeCategoryOptions() != null && program.getCategoryCombo() != null) || event.getAttributeOptionCombo() != null) {
        IdScheme idScheme = importOptions.getIdSchemes().getCategoryOptionIdScheme();
        try {
            aoc = inputUtils.getAttributeOptionCombo(program.getCategoryCombo(), event.getAttributeCategoryOptions(), event.getAttributeOptionCombo(), idScheme);
        } catch (IllegalQueryException ex) {
            importSummary.getConflicts().add(new ImportConflict(ex.getMessage(), event.getAttributeCategoryOptions()));
        }
    } else {
        aoc = categoryService.getDefaultDataElementCategoryOptionCombo();
    }
    if (!dryRun) {
        if (programStageInstance == null) {
            programStageInstance = createProgramStageInstance(event, programStage, programInstance, organisationUnit, dueDate, executionDate, event.getStatus().getValue(), event.getCoordinate(), completedBy, event.getEvent(), aoc, importOptions);
        } else {
            updateProgramStageInstance(event, programStage, programInstance, organisationUnit, dueDate, executionDate, event.getStatus().getValue(), event.getCoordinate(), completedBy, programStageInstance, aoc, importOptions);
        }
        updateTrackedEntityInstance(programStageInstance);
        saveTrackedEntityComment(programStageInstance, event, storedBy);
        importSummary.setReference(programStageInstance.getUid());
    }
    Map<String, TrackedEntityDataValue> dataElementValueMap = Maps.newHashMap();
    if (existingEvent) {
        dataElementValueMap = getDataElementDataValueMap(dataValueService.getTrackedEntityDataValues(programStageInstance));
    }
    for (DataValue dataValue : event.getDataValues()) {
        DataElement dataElement;
        if (dataElementValueMap.containsKey(dataValue.getDataElement())) {
            dataElement = dataElementValueMap.get(dataValue.getDataElement()).getDataElement();
        } else {
            dataElement = getDataElement(importOptions.getIdSchemes().getDataElementIdScheme(), dataValue.getDataElement());
        }
        if (dataElement != null) {
            if (validateDataValue(dataElement, dataValue.getValue(), importSummary)) {
                String dataValueStoredBy = dataValue.getStoredBy() != null ? dataValue.getStoredBy() : storedBy;
                if (!dryRun) {
                    TrackedEntityDataValue existingDataValue = dataElementValueMap.get(dataValue.getDataElement());
                    saveDataValue(programStageInstance, dataValueStoredBy, dataElement, dataValue.getValue(), dataValue.getProvidedElsewhere(), existingDataValue, importSummary);
                }
            }
        } else {
            importSummary.getConflicts().add(new ImportConflict("dataElement", dataValue.getDataElement() + " is not a valid data element"));
            importSummary.getImportCount().incrementIgnored();
        }
    }
    importSummary.setStatus(importSummary.getConflicts().isEmpty() ? ImportStatus.SUCCESS : ImportStatus.WARNING);
    return importSummary;
}
Also used : TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) IdScheme(org.hisp.dhis.common.IdScheme) Date(java.util.Date) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 49 with ImportOptions

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

the class AbstractEventService method updateEventForEventDate.

@Override
public void updateEventForEventDate(Event event) {
    ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance(event.getEvent());
    if (programStageInstance == null) {
        return;
    }
    Date executionDate = new Date();
    if (event.getEventDate() != null) {
        executionDate = DateUtils.parseDate(event.getEventDate());
    }
    if (event.getStatus() == EventStatus.COMPLETED) {
        programStageInstance.setStatus(EventStatus.COMPLETED);
    } else {
        programStageInstance.setStatus(EventStatus.VISITED);
    }
    ImportOptions importOptions = new ImportOptions();
    OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), event.getOrgUnit());
    if (organisationUnit == null) {
        organisationUnit = programStageInstance.getOrganisationUnit();
    }
    programStageInstance.setOrganisationUnit(organisationUnit);
    programStageInstance.setExecutionDate(executionDate);
    programStageInstanceService.updateProgramStageInstance(programStageInstance);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Date(java.util.Date) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions)

Example 50 with ImportOptions

use of org.hisp.dhis.dxf2.common.ImportOptions 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)

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