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