use of org.hisp.dhis.dxf2.events.importer.ServiceDelegator in project dhis2-core by dhis2.
the class ProgramInstanceRepeatableStageCheckTest method successOnNonRepeatableStageAndExistingEventsOnNewEnrollment.
@Test
void successOnNonRepeatableStageAndExistingEventsOnNewEnrollment() {
// Data preparation
Program program = createProgram('P');
TrackedEntityInstance tei = createTrackedEntityInstance('A', createOrganisationUnit('A'));
event.setProgramStage(CodeGenerator.generateUid());
event.setProgram(program.getUid());
event.setTrackedEntityInstance(tei.getUid());
ProgramStage programStage = createProgramStage('A', program);
programStage.setRepeatable(false);
when(workContext.getProgramStage(programStageIdScheme, event.getProgramStage())).thenReturn(programStage);
Map<String, Program> programMap = new HashMap<>();
programMap.put(program.getUid(), program);
Map<String, ProgramInstance> programInstanceMap = new HashMap<>();
ProgramInstance programInstance = new ProgramInstance();
programInstanceMap.put(event.getUid(), programInstance);
Pair<TrackedEntityInstance, Boolean> teiPair = Pair.of(tei, true);
Map<String, Pair<TrackedEntityInstance, Boolean>> teiMap = new HashMap<>();
teiMap.put(event.getUid(), teiPair);
when(workContext.getTrackedEntityInstanceMap()).thenReturn(teiMap);
when(workContext.getProgramsMap()).thenReturn(programMap);
when(workContext.getProgramInstanceMap()).thenReturn(programInstanceMap);
when(workContext.getServiceDelegator()).thenReturn(serviceDelegator);
when(serviceDelegator.getJdbcTemplate()).thenReturn(jdbcTemplate);
when(jdbcTemplate.queryForObject(anyString(), eq(Boolean.class), eq(programInstance.getId()), eq(programStage.getId()), eq(tei.getId()))).thenReturn(false);
// Method under test
ImportSummary summary = rule.check(new ImmutableEvent(event), workContext);
assertNoError(summary);
}
use of org.hisp.dhis.dxf2.events.importer.ServiceDelegator in project dhis2-core by dhis2.
the class ProgramInstanceRepeatableStageCheckTest method failOnNonRepeatableStageAndExistingEvents.
@Test
void failOnNonRepeatableStageAndExistingEvents() {
// Data preparation
Program program = createProgram('P');
TrackedEntityInstance tei = createTrackedEntityInstance('A', createOrganisationUnit('A'));
event.setProgramStage(CodeGenerator.generateUid());
event.setProgram(program.getUid());
event.setTrackedEntityInstance(tei.getUid());
ProgramStage programStage = createProgramStage('A', program);
programStage.setRepeatable(false);
when(workContext.getProgramStage(programStageIdScheme, event.getProgramStage())).thenReturn(programStage);
Map<String, Program> programMap = new HashMap<>();
programMap.put(program.getUid(), program);
Map<String, ProgramInstance> programInstanceMap = new HashMap<>();
ProgramInstance programInstance = new ProgramInstance();
programInstanceMap.put(event.getUid(), programInstance);
Pair<TrackedEntityInstance, Boolean> teiPair = Pair.of(tei, true);
Map<String, Pair<TrackedEntityInstance, Boolean>> teiMap = new HashMap<>();
teiMap.put(event.getUid(), teiPair);
when(workContext.getTrackedEntityInstanceMap()).thenReturn(teiMap);
when(workContext.getProgramsMap()).thenReturn(programMap);
when(workContext.getProgramInstanceMap()).thenReturn(programInstanceMap);
when(workContext.getServiceDelegator()).thenReturn(serviceDelegator);
when(serviceDelegator.getJdbcTemplate()).thenReturn(jdbcTemplate);
when(jdbcTemplate.queryForObject(anyString(), eq(Boolean.class), eq(programInstance.getId()), eq(programStage.getId()), eq(tei.getId()))).thenReturn(true);
// Method under test
ImportSummary summary = rule.check(new ImmutableEvent(event), workContext);
assertHasError(summary, event, "Program stage is not repeatable and an event already exists");
}
use of org.hisp.dhis.dxf2.events.importer.ServiceDelegator in project dhis2-core by dhis2.
the class WorkContextLoader method load.
@Transactional(readOnly = true)
public WorkContext load(ImportOptions importOptions, List<Event> events) {
sessionFactory.getCurrentSession().flush();
ImportOptions localImportOptions = importOptions;
// API allows a null Import Options
if (localImportOptions == null) {
localImportOptions = ImportOptions.getDefaultImportOptions();
}
initializeUser(localImportOptions);
// Make sure all events have the 'uid' field populated
events = uidGen.assignUidToEvents(events);
final Map<String, ProgramStageInstance> programStageInstanceMap = programStageInstanceSupplier.get(localImportOptions, events);
final Map<String, Pair<TrackedEntityInstance, Boolean>> teiMap = trackedEntityInstanceSupplier.get(localImportOptions, events);
final Map<String, OrganisationUnit> orgUniMap = organisationUnitSupplier.get(localImportOptions, events);
return WorkContext.builder().importOptions(localImportOptions).programsMap(programSupplier.get(localImportOptions, events)).programStageInstanceMap(programStageInstanceMap).organisationUnitMap(orgUniMap).trackedEntityInstanceMap(teiMap).programInstanceMap(programInstanceSupplier.get(localImportOptions, teiMap, events)).categoryOptionComboMap(categoryOptionComboSupplier.get(localImportOptions, events)).dataElementMap(dataElementSupplier.get(localImportOptions, events)).notesMap(noteSupplier.get(localImportOptions, events)).assignedUserMap(assignedUserSupplier.get(localImportOptions, events)).eventDataValueMap(new EventDataValueAggregator().aggregateDataValues(events, programStageInstanceMap, localImportOptions)).programWithOrgUnitsMap(programOrgUnitSupplier.get(localImportOptions, events, orgUniMap)).serviceDelegator(serviceDelegatorSupplier.get()).build();
}
Aggregations