Search in sources :

Example 31 with WorkContext

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");
}
Also used : Program(org.hisp.dhis.program.Program) DhisConvenienceTest.createProgram(org.hisp.dhis.DhisConvenienceTest.createProgram) HashMap(java.util.HashMap) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) ImmutableEvent(org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent) Test(org.junit.jupiter.api.Test) BaseValidationTest(org.hisp.dhis.dxf2.events.importer.validation.BaseValidationTest)

Example 32 with WorkContext

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");
}
Also used : Program(org.hisp.dhis.program.Program) DhisConvenienceTest.createProgram(org.hisp.dhis.DhisConvenienceTest.createProgram) HashMap(java.util.HashMap) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) ImmutableEvent(org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent) Test(org.junit.jupiter.api.Test) BaseValidationTest(org.hisp.dhis.dxf2.events.importer.validation.BaseValidationTest)

Example 33 with WorkContext

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()));
    }
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) WorkContext(org.hisp.dhis.dxf2.events.importer.context.WorkContext) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) Transactional(org.springframework.transaction.annotation.Transactional)

Example 34 with WorkContext

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;
}
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 35 with WorkContext

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;
}
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) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries)

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