ProgramInstanceRepeatableStageCheck implements Checker { @Override public ImportSummary check(ImmutableEvent event, WorkContext ctx ) { IdScheme scheme = ctx.getImportOptions().getIdSchemes().getProgramStageIdScheme(); ProgramStage programStage = ctx.getProgramStage( scheme, event.getProgramStage() ); ProgramInstance programInstance = ctx.getProgramInstanceMap().get( event.getUid() ); Program program = ctx.getProgramsMap().get( event.getProgram() ); TrackedEntityInstance tei = null; if ( program.isRegistration() ) { tei = ctx.getTrackedEntityInstanceMap().get( event.getUid() ).getLeft(); } if ( programInstance != null && tei != null && program.isRegistration() && !programStage.getRepeatable() && hasProgramStageInstance( ctx.getServiceDelegator().getJdbcTemplate(), programStage.getId(), tei.getId() ) ) { return new ImportSummary( ImportStatus.ERROR, "Program stage is not repeatable and an event already exists" ).setReference( event.getEvent() ) .incrementIgnored(); } return success(); }  @Override ImportSummary check(ImmutableEvent event, WorkContext ctx );  }
@Test public void failOnNonRepeatableStageAndExistingEvents() { 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( programStage.getId() ), eq( tei.getId() ) ) ) .thenReturn( true ); ImportSummary summary = rule.check( new ImmutableEvent( event ), workContext ); assertHasError( summary, event, "Program stage is not repeatable and an event already exists" ); }