use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.
the class ExpirationDaysCheckTest method failWhenProgramStageInstanceHasNoCompletedDateAndProgramHasExpiryDays.
@Test
void failWhenProgramStageInstanceHasNoCompletedDateAndProgramHasExpiryDays() {
// Given
// Prepare program
Program program = createProgram('P');
program.setCompleteEventsExpiryDays(3);
Map<String, Program> programMap = new HashMap<>();
programMap.put(program.getUid(), program);
when(workContext.getProgramsMap()).thenReturn(programMap);
// Prepare program stage instance
Map<String, ProgramStageInstance> psiMap = new HashMap<>();
ProgramStageInstance psi = new ProgramStageInstance();
psi.setStatus(EventStatus.COMPLETED);
psi.setUid(event.getUid());
psiMap.put(event.getUid(), psi);
when(workContext.getProgramStageInstanceMap()).thenReturn(psiMap);
// Prepare event
event.setProgram(program.getUid());
event.setStatus(EventStatus.COMPLETED);
// When
ImportSummary importSummary = rule.check(new ImmutableEvent(event), workContext);
// Then
assertHasError(importSummary, event, "Event needs to have completed date");
}
use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.
the class ExpirationDaysCheckTest method failWhenProgramStageInstanceHasExecutionDateBeforeAllowedProgramExpiryDaysBasedOnPeriod.
@Test
void failWhenProgramStageInstanceHasExecutionDateBeforeAllowedProgramExpiryDaysBasedOnPeriod() {
// Given
final String monthlyPeriodType = new SimpleDateFormat("yyyyMM").format(new Date());
// Prepare program
Program program = createProgram('P');
program.setExpiryPeriodType(PeriodType.getPeriodTypeFromIsoString(monthlyPeriodType));
program.setExpiryDays(3);
Map<String, Program> programMap = new HashMap<>();
programMap.put(program.getUid(), program);
when(workContext.getProgramsMap()).thenReturn(programMap);
// Prepare program stage instance
Map<String, ProgramStageInstance> psiMap = new HashMap<>();
ProgramStageInstance psi = new ProgramStageInstance();
// month length + 5
psi.setExecutionDate(getTodayMinusDays(35));
// days
psi.setUid(event.getUid());
psiMap.put(event.getUid(), psi);
when(workContext.getProgramStageInstanceMap()).thenReturn(psiMap);
// Prepare event
event.setProgram(program.getUid());
// When
ImportSummary importSummary = rule.check(new ImmutableEvent(event), workContext);
// Then
assertHasError(importSummary, event, "The program's expiry date has passed. It is not possible to make changes to this event");
}
use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.
the class AbstractEventService method addEvents.
@Transactional
@Override
public ImportSummaries addEvents(final List<Event> events, ImportOptions importOptions, final JobConfiguration jobConfiguration) {
notifier.clear(jobConfiguration).notify(jobConfiguration, "Importing events");
importOptions = updateImportOptions(importOptions);
try {
final WorkContext workContext = workContextLoader.load(importOptions, events);
final ImportSummaries importSummaries = eventManager.addEvents(events, workContext);
if (jobConfiguration != null) {
notifier.notify(jobConfiguration, NotificationLevel.INFO, "Import done", true).addJobSummary(jobConfiguration, importSummaries, ImportSummaries.class);
}
return importSummaries;
} catch (RuntimeException ex) {
log.error(DebugUtils.getStackTrace(ex));
notifier.notify(jobConfiguration, ERROR, "Process failed: " + ex.getMessage(), true);
return new ImportSummaries().addImportSummary(new ImportSummary(ImportStatus.ERROR, "The import process failed: " + ex.getMessage()));
}
}
use of org.hisp.dhis.dxf2.events.importer.context.WorkContext 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;
}
use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.
the class EventManager method deleteEvents.
public ImportSummaries deleteEvents(final List<Event> events, final WorkContext workContext) {
final ImportSummaries importSummaries = new ImportSummaries();
// pre-process events
executorsByPhase.get(EventProcessorPhase.DELETE_PRE).execute(workContext, events);
if (ImportStrategyUtils.isDelete(workContext.getImportOptions().getImportStrategy())) {
importSummaries.addImportSummaries(run(workContext, events, checkersRunOnDelete));
}
// 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.delete(workContext, eventValidationFailedUids.isEmpty() ? events : events.stream().filter(e -> !eventValidationFailedUids.contains(e.getEvent())).collect(toList()));
} catch (Exception e) {
handleFailure(workContext, importSummaries, events, IMPORT_ERROR_STRING, DELETE);
}
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.
executorsByPhase.get(EventProcessorPhase.DELETE_POST).execute(workContext, events.stream().filter(e -> !eventPersistenceFailedUids.contains(e.getEvent())).collect(toList()));
incrementSummaryTotals(events, importSummaries, DELETE);
}
return importSummaries;
}
Aggregations