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