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;
}
Aggregations