Search in sources :

Example 46 with WorkContext

use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.

the class FilteredDataValueCheck method check.

@Override
public ImportSummary check(ImmutableEvent event, WorkContext ctx) {
    final Set<String> eventDataValuesUids = Optional.ofNullable(event).map(ImmutableEvent::getDataValues).orElse(Collections.emptySet()).stream().map(DataValue::getDataElement).collect(Collectors.toSet());
    final ImportSummary importSummary = new ImportSummary();
    if (!eventDataValuesUids.isEmpty() && Objects.nonNull(event) && StringUtils.isNotBlank(event.getProgramStage())) {
        Set<String> filteredEventDataValuesUids = getFilteredDataValues(event.getDataValues(), getDataElementUidsFromProgramStage(event.getProgramStage(), ctx)).stream().map(DataValue::getDataElement).collect(Collectors.toSet());
        Set<String> ignoredDataValues = eventDataValuesUids.stream().filter(uid -> !filteredEventDataValuesUids.contains(uid)).collect(Collectors.toSet());
        if (!ignoredDataValues.isEmpty()) {
            importSummary.setStatus(ImportStatus.WARNING);
            importSummary.setReference(event.getUid());
            importSummary.setDescription("Data Values " + ignoredDataValues.stream().collect(Collectors.joining(",", "[", "]")) + " ignored because " + "not defined in program stage " + event.getProgramStage());
            importSummary.incrementImported();
        }
    }
    return importSummary;
}
Also used : DataValue(org.hisp.dhis.dxf2.events.event.DataValue) Set(java.util.Set) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) Objects(java.util.Objects) WorkContext(org.hisp.dhis.dxf2.events.importer.context.WorkContext) Component(org.springframework.stereotype.Component) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) FilteringOutUndeclaredDataElementsProcessor.getDataElementUidsFromProgramStage(org.hisp.dhis.dxf2.events.importer.shared.preprocess.FilteringOutUndeclaredDataElementsProcessor.getDataElementUidsFromProgramStage) ImmutableEvent(org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent) Checker(org.hisp.dhis.dxf2.events.importer.Checker) Optional(java.util.Optional) Collections(java.util.Collections) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) FilteringOutUndeclaredDataElementsProcessor.getFilteredDataValues(org.hisp.dhis.dxf2.events.importer.shared.preprocess.FilteringOutUndeclaredDataElementsProcessor.getFilteredDataValues) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ImmutableEvent(org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent)

Example 47 with WorkContext

use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.

the class ProgramInstancePreProcessorTest method verifyEnrollmentIsSetOnEventWhenOneProgramInstanceIsFound.

@Test
void verifyEnrollmentIsSetOnEventWhenOneProgramInstanceIsFound() {
    // 
    // Tracked Entity Instance
    // 
    TrackedEntityInstance tei = createTrackedEntityInstance(createOrganisationUnit('A'));
    when(workContext.getTrackedEntityInstance(event.getUid())).thenReturn(Optional.of(tei));
    ProgramInstance programInstance = new ProgramInstance();
    programInstance.setUid(CodeGenerator.generateUid());
    when(programInstanceStore.get(tei, program, ProgramStatus.ACTIVE)).thenReturn(Lists.newArrayList(programInstance));
    event.setProgram(program.getUid());
    // 
    // Method under test
    // 
    subject.process(event, workContext);
    assertThat(event.getEnrollment(), is(programInstance.getUid()));
    assertThat(programInstanceMap.get(event.getUid()), is(programInstance));
}
Also used : ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) DhisConvenienceTest.createTrackedEntityInstance(org.hisp.dhis.DhisConvenienceTest.createTrackedEntityInstance) BasePreProcessTest(org.hisp.dhis.dxf2.events.importer.BasePreProcessTest) Test(org.junit.jupiter.api.Test)

Example 48 with WorkContext

use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.

the class ProgramInstancePreProcessorTest method verifyEnrollmentIsSetWithProgramWithoutRegistrationAndOneProgramStageInstance.

@Test
void verifyEnrollmentIsSetWithProgramWithoutRegistrationAndOneProgramStageInstance() throws SQLException {
    // crete a Program "without registration"
    Program programWithoutReg = createProgram('W');
    programWithoutReg.setProgramType(ProgramType.WITHOUT_REGISTRATION);
    // add the program to the work context map
    Map<String, Program> programMap = new HashMap<>();
    programMap.put(programWithoutReg.getUid(), programWithoutReg);
    // make sure tha map is returned when invoking the mock work context
    when(workContext.getProgramsMap()).thenReturn(programMap);
    ProgramInstance programInstance = new ProgramInstance();
    programInstance.setUid(CodeGenerator.generateUid());
    programInstance.setId(100L);
    when(workContext.getServiceDelegator().getJdbcTemplate()).thenReturn(jdbcTemplate);
    // 
    // simulate one record returned from query
    // 
    when(mockResultSet.next()).thenReturn(true).thenReturn(false);
    when(mockResultSet.getLong("programinstanceid")).thenReturn(programInstance.getId());
    when(mockResultSet.getString("uid")).thenReturn(programInstance.getUid());
    // Mock jdbc call
    mockResultSetExtractor(mockResultSet);
    event.setProgram(programWithoutReg.getUid());
    // method under test
    subject.process(event, workContext);
    assertThat(event.getEnrollment(), is(programInstance.getUid()));
    assertThat(programInstanceMap.get(event.getUid()).getUid(), is(programInstance.getUid()));
    assertThat(programInstanceMap.get(event.getUid()).getProgram().getUid(), is(programWithoutReg.getUid()));
    assertThat(sql.getValue(), is("select pi.programinstanceid, pi.programid, pi.uid from programinstance pi where pi.programid = ? and pi.status = ?"));
}
Also used : Program(org.hisp.dhis.program.Program) DhisConvenienceTest.createProgram(org.hisp.dhis.DhisConvenienceTest.createProgram) HashMap(java.util.HashMap) ProgramInstance(org.hisp.dhis.program.ProgramInstance) BasePreProcessTest(org.hisp.dhis.dxf2.events.importer.BasePreProcessTest) Test(org.junit.jupiter.api.Test)

Example 49 with WorkContext

use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.

the class ProgramInstancePreProcessorTest method verifyEnrollmentIsNotSetWithProgramWithoutRegistrationAndMultipleProgramStageInstances.

@Test
void verifyEnrollmentIsNotSetWithProgramWithoutRegistrationAndMultipleProgramStageInstances() throws SQLException {
    // crete a Program "without registration"
    Program programWithoutReg = createProgram('W');
    programWithoutReg.setProgramType(ProgramType.WITHOUT_REGISTRATION);
    // add the program to the work context map
    Map<String, Program> programMap = new HashMap<>();
    programMap.put(programWithoutReg.getUid(), programWithoutReg);
    // make sure tha map is returned when invoking the mock work context
    when(workContext.getProgramsMap()).thenReturn(programMap);
    ProgramInstance programInstance1 = new ProgramInstance();
    programInstance1.setUid(CodeGenerator.generateUid());
    programInstance1.setId(100L);
    ProgramInstance programInstance2 = new ProgramInstance();
    programInstance2.setUid(CodeGenerator.generateUid());
    programInstance2.setId(100L);
    when(workContext.getServiceDelegator().getJdbcTemplate()).thenReturn(jdbcTemplate);
    // 
    // simulate 2 records returned from query
    // 
    when(mockResultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false);
    when(mockResultSet.getLong("programinstanceid")).thenReturn(programInstance1.getId(), programInstance2.getId());
    when(mockResultSet.getString("uid")).thenReturn(programInstance1.getUid(), programInstance2.getUid());
    // Mock jdbc call
    mockResultSetExtractor(mockResultSet);
    event.setProgram(programWithoutReg.getUid());
    // method under test
    subject.process(event, workContext);
    assertThat(event.getEnrollment(), is(nullValue()));
    assertThat(sql.getValue(), is("select pi.programinstanceid, pi.programid, pi.uid from programinstance pi where pi.programid = ? and pi.status = ?"));
}
Also used : Program(org.hisp.dhis.program.Program) DhisConvenienceTest.createProgram(org.hisp.dhis.DhisConvenienceTest.createProgram) HashMap(java.util.HashMap) ProgramInstance(org.hisp.dhis.program.ProgramInstance) BasePreProcessTest(org.hisp.dhis.dxf2.events.importer.BasePreProcessTest) Test(org.junit.jupiter.api.Test)

Example 50 with WorkContext

use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.

the class ProgramOrgUnitCheckTest method failWhenProgramHasNoOrgUnitMatchingEventOrgUnit.

@Test
void failWhenProgramHasNoOrgUnitMatchingEventOrgUnit() {
    // assign a UID to the event's org unit
    event.setOrgUnit(CodeGenerator.generateUid());
    // Prepare data
    Program program = createProgram('P');
    program.setId(1);
    OrganisationUnit ou = new OrganisationUnit();
    ou.setId(1);
    ou.setUid(event.getOrgUnit());
    when(workContext.getOrganisationUnitMap()).thenReturn(ImmutableMap.of(event.getUid(), ou));
    when(workContext.getProgramWithOrgUnitsMap()).thenReturn(new HashMap<>());
    ProgramInstance pi = new ProgramInstance();
    pi.setProgram(program);
    Map<String, ProgramInstance> programInstanceMap = new HashMap<>();
    programInstanceMap.put(event.getUid(), pi);
    when(workContext.getProgramInstanceMap()).thenReturn(programInstanceMap);
    // method under test
    ImportSummary summary = rule.check(new ImmutableEvent(event), workContext);
    assertHasError(summary, event, "Program is not assigned to this Organisation Unit: " + event.getOrgUnit());
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) DhisConvenienceTest.createProgram(org.hisp.dhis.DhisConvenienceTest.createProgram) HashMap(java.util.HashMap) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ImmutableEvent(org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent) Test(org.junit.jupiter.api.Test) BaseValidationTest(org.hisp.dhis.dxf2.events.importer.validation.BaseValidationTest)

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