Search in sources :

Example 1 with WorkContext

use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.

the class EventImporter method importAll.

public ImportSummaries importAll(final List<Event> events, final ImportOptions importOptions, final JobConfiguration jobConfiguration) {
    assert importOptions != null;
    final ImportSummaries importSummaries = new ImportSummaries();
    if (events.size() == 0) {
        return importSummaries;
    }
    notifier.clear(jobConfiguration).notify(jobConfiguration, "Importing events");
    final Clock clock = new Clock(log).startClock();
    long now = nanoTime();
    final WorkContext context = workContextLoader.load(importOptions, events);
    log.debug("::: event tracker import context load took : " + (nanoTime() - now));
    final List<List<Event>> partitions = partition(events, BATCH_SIZE);
    for (final List<Event> batch : partitions) {
        final ImportStrategyAccumulator accumulator = new ImportStrategyAccumulator().partitionEvents(batch, importOptions.getImportStrategy(), context.getProgramStageInstanceMap());
        importSummaries.addImportSummaries(eventManager.addEvents(accumulator.getCreate(), context));
        importSummaries.addImportSummaries(eventManager.updateEvents(accumulator.getUpdate(), context));
        importSummaries.addImportSummaries(eventManager.deleteEvents(accumulator.getDelete(), context));
    }
    if (jobConfiguration != null) {
        notifier.notify(jobConfiguration, INFO, "Import done. Completed in " + clock.time() + ".", true).addJobSummary(jobConfiguration, importSummaries, ImportSummaries.class);
    } else {
        clock.logTime("Import done");
    }
    if (ERRORS == importOptions.getReportMode() && isNotEmpty(importSummaries.getImportSummaries())) {
        importSummaries.getImportSummaries().removeIf(is -> !is.hasConflicts());
    }
    return importSummaries;
}
Also used : WorkContext(org.hisp.dhis.dxf2.events.importer.context.WorkContext) Event(org.hisp.dhis.dxf2.events.event.Event) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) List(java.util.List) Clock(org.hisp.dhis.system.util.Clock)

Example 2 with WorkContext

use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.

the class EventManager method updateEvents.

public ImportSummaries updateEvents(final List<Event> events, final WorkContext workContext) {
    final ImportSummaries importSummaries = new ImportSummaries();
    // pre-process events
    executorsByPhase.get(EventProcessorPhase.UPDATE_PRE).execute(workContext, events);
    if (ImportStrategyUtils.isUpdate(workContext.getImportOptions().getImportStrategy())) {
        importSummaries.addImportSummaries(run(workContext, events, checkersRunOnUpdate));
    }
    // collect the UIDs of events that did not pass validation
    final List<String> eventValidationFailedUids = importSummaries.getImportSummaries().stream().filter(i -> i.isStatus(ERROR)).map(ImportSummary::getReference).collect(toList());
    if (eventValidationFailedUids.size() == events.size()) {
        return importSummaries;
    }
    if (!workContext.getImportOptions().isDryRun()) {
        try {
            eventPersistenceService.update(workContext, eventValidationFailedUids.isEmpty() ? events : events.stream().filter(e -> !eventValidationFailedUids.contains(e.getEvent())).collect(toList()));
        } catch (Exception e) {
            handleFailure(workContext, importSummaries, events, IMPORT_ERROR_STRING, UPDATE);
        }
        final List<String> eventPersistenceFailedUids = importSummaries.getImportSummaries().stream().filter(i -> i.isStatus(ERROR)).map(ImportSummary::getReference).collect(toList());
        // Post processing only the events that passed validation and were
        // persisted
        // correctly.
        List<Event> savedEvents = events.stream().filter(e -> !eventPersistenceFailedUids.contains(e.getEvent())).collect(toList());
        executorsByPhase.get(EventProcessorPhase.UPDATE_POST).execute(workContext, savedEvents);
        incrementSummaryTotals(events, importSummaries, UPDATE);
    }
    return importSummaries;
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) DELETE(org.hisp.dhis.importexport.ImportStrategy.DELETE) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Program(org.hisp.dhis.program.Program) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) WorkContext(org.hisp.dhis.dxf2.events.importer.context.WorkContext) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ERROR(org.hisp.dhis.dxf2.importsummary.ImportStatus.ERROR) ImmutableList(com.google.common.collect.ImmutableList) CREATE(org.hisp.dhis.importexport.ImportStrategy.CREATE) CollectionUtils(org.apache.commons.collections.CollectionUtils) Map(java.util.Map) Qualifier(org.springframework.beans.factory.annotation.Qualifier) EventPersistenceService(org.hisp.dhis.dxf2.events.event.persistence.EventPersistenceService) WARNING(org.hisp.dhis.dxf2.importsummary.ImportStatus.WARNING) ImportSummary.error(org.hisp.dhis.dxf2.importsummary.ImportSummary.error) NonNull(lombok.NonNull) Set(java.util.Set) Event(org.hisp.dhis.dxf2.events.event.Event) ProgramStage(org.hisp.dhis.program.ProgramStage) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) CollectionUtils.isNotEmpty(org.apache.commons.collections4.CollectionUtils.isNotEmpty) ImmutableEvent(org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent) UPDATE(org.hisp.dhis.importexport.ImportStrategy.UPDATE) SUCCESS(org.hisp.dhis.dxf2.importsummary.ImportStatus.SUCCESS) IdScheme(org.hisp.dhis.common.IdScheme) Event(org.hisp.dhis.dxf2.events.event.Event) ImmutableEvent(org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries)

Example 3 with WorkContext

use of org.hisp.dhis.dxf2.events.importer.context.WorkContext 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 4 with WorkContext

use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.

the class EventManager method updateEvent.

public ImportSummary updateEvent(final Event event, final WorkContext workContext) {
    final List<Event> singleEvent = singletonList(event);
    final ImportSummaries importSummaries = updateEvents(singleEvent, workContext);
    if (isNotEmpty(importSummaries.getImportSummaries())) {
        return importSummaries.getImportSummaries().get(0);
    } else {
        return error("Not updated", event.getEvent());
    }
}
Also used : Event(org.hisp.dhis.dxf2.events.event.Event) ImmutableEvent(org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries)

Example 5 with WorkContext

use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.

the class DefaultEventPersistenceService method update.

/**
 * Updates the list of given events using a single transaction.
 *
 * @param context a {@see WorkContext}
 * @param events a List of {@see Event}
 */
@Override
@Transactional
public void update(final WorkContext context, final List<Event> events) {
    if (isNotEmpty(events)) {
        ProgramStageInstanceMapper mapper = new ProgramStageInstanceMapper(context);
        List<ProgramStageInstance> programStageInstances = jdbcEventStore.updateEvents(events.stream().map(mapper::map).collect(Collectors.toList()));
        jdbcEventCommentStore.saveAllComments(programStageInstances);
        if (!context.getImportOptions().isSkipLastUpdated()) {
            updateTeis(context, events);
        }
    }
}
Also used : ProgramStageInstanceMapper(org.hisp.dhis.dxf2.events.importer.mapper.ProgramStageInstanceMapper) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)47 ImmutableEvent (org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent)35 Test (org.junit.jupiter.api.Test)35 BaseValidationTest (org.hisp.dhis.dxf2.events.importer.validation.BaseValidationTest)26 Program (org.hisp.dhis.program.Program)23 HashMap (java.util.HashMap)20 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)16 DhisConvenienceTest.createProgram (org.hisp.dhis.DhisConvenienceTest.createProgram)14 ProgramInstance (org.hisp.dhis.program.ProgramInstance)14 WorkContext (org.hisp.dhis.dxf2.events.importer.context.WorkContext)13 Event (org.hisp.dhis.dxf2.events.event.Event)12 ProgramStage (org.hisp.dhis.program.ProgramStage)11 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)9 DataValue (org.hisp.dhis.dxf2.events.event.DataValue)9 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)9 Set (java.util.Set)7 DhisConvenienceTest.createTrackedEntityInstance (org.hisp.dhis.DhisConvenienceTest.createTrackedEntityInstance)7 IdScheme (org.hisp.dhis.common.IdScheme)6 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)6 User (org.hisp.dhis.user.User)5