Search in sources :

Example 1 with CREATE

use of org.hisp.dhis.importexport.ImportStrategy.CREATE in project dhis2-core by dhis2.

the class EventManager method addEvents.

public ImportSummaries addEvents(final List<Event> events, final WorkContext workContext) {
    final ImportSummaries importSummaries = new ImportSummaries();
    if (CollectionUtils.isEmpty(events)) {
        return importSummaries;
    }
    // filter out events which are already in the database as well as
    // duplicates in the payload (if stage is not repeatable)
    List<Event> validEvents = resolveImportableEvents(events, importSummaries, workContext);
    if (validEvents.isEmpty()) {
        return importSummaries;
    }
    // pre-process events
    executorsByPhase.get(EventProcessorPhase.INSERT_PRE).execute(workContext, validEvents);
    if (ImportStrategyUtils.isInsert(workContext.getImportOptions().getImportStrategy())) {
        importSummaries.addImportSummaries(run(workContext, validEvents, checkersRunOnInsert));
    }
    // collect the UIDs of events that did not pass validation
    final List<String> invalidEvents = importSummaries.getImportSummaries().stream().filter(i -> i.isStatus(ERROR)).map(ImportSummary::getReference).collect(toList());
    if (invalidEvents.size() == events.size()) {
        return importSummaries;
    }
    if (!workContext.getImportOptions().isDryRun()) {
        // fetch persistable events //
        List<Event> eventsToInsert = invalidEvents.isEmpty() ? validEvents : validEvents.stream().filter(e -> !invalidEvents.contains(e.getEvent())).collect(toList());
        if (isNotEmpty(eventsToInsert)) {
            try {
                // save the entire batch in one transaction
                eventPersistenceService.save(workContext, eventsToInsert);
            } catch (Exception e) {
                handleFailure(workContext, importSummaries, events, IMPORT_ERROR_STRING, CREATE);
            }
        }
        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.INSERT_POST).execute(workContext, savedEvents);
        incrementSummaryTotals(events, importSummaries, CREATE);
    }
    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)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 Collections.singletonList (java.util.Collections.singletonList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors.toList (java.util.stream.Collectors.toList)1 NonNull (lombok.NonNull)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Slf4j (lombok.extern.slf4j.Slf4j)1 CollectionUtils (org.apache.commons.collections.CollectionUtils)1 CollectionUtils.isNotEmpty (org.apache.commons.collections4.CollectionUtils.isNotEmpty)1 IdScheme (org.hisp.dhis.common.IdScheme)1 Event (org.hisp.dhis.dxf2.events.event.Event)1 EventPersistenceService (org.hisp.dhis.dxf2.events.event.persistence.EventPersistenceService)1 WorkContext (org.hisp.dhis.dxf2.events.importer.context.WorkContext)1 ImmutableEvent (org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent)1 ERROR (org.hisp.dhis.dxf2.importsummary.ImportStatus.ERROR)1 SUCCESS (org.hisp.dhis.dxf2.importsummary.ImportStatus.SUCCESS)1