@Test public void addBasicScriptVariablesWithProgramStageCheck() { final Map<String, Object> variables = new HashMap<>(); fhirResourceMapping.setImpProgramStageRefLookupScript( programStageRefLookupScript ); Mockito.doReturn( Reference.createIdReference( "jshweyuio19a" ) ).when( scriptExecutor ) .execute( Mockito.same( programStageRefLookupScript ), Mockito.eq( FhirVersion.R4 ), Mockito.anyMap(), Mockito.anyMap(), Mockito.anyMap(), Mockito.eq( Reference.class ) ); Assert.assertTrue( transformer.addBasicScriptVariables( variables, context, new RuleInfo<>( programStageRule, Collections.emptyList() ), fhirResourceMapping ) ); Mockito.verify( scriptExecutor ).execute( Mockito.same( programStageRefLookupScript ), Mockito.eq( FhirVersion.R4 ), Mockito.anyMap(), Mockito.anyMap(), Mockito.anyMap(), Mockito.eq( Reference.class ) ); }
protected boolean addBasicScriptVariables( @Nonnull Map<String, Object> variables, @Nonnull FhirToDhisTransformerContext context, @Nonnull RuleInfo<ProgramStageRule> ruleInfo, @Nonnull FhirResourceMapping resourceMapping ) throws TransformerException { final Program program = programMetadataService.findMetadataByReference( ruleInfo.getRule().getProgramStage().getProgram().getProgramReference() ) .orElseThrow( () -> new TransformerMappingException( "Mapping " + ruleInfo + " requires program \"" + ruleInfo.getRule().getProgramStage().getProgram().getProgramReference() + "\" that does not exist." ) ); variables.put( ScriptVariable.PROGRAM.getVariableName(), program ); final ProgramStage programStage = program.getOptionalStage( ruleInfo.getRule().getProgramStage().getProgramStageReference() ).orElseThrow( () -> new TransformerMappingException( "Rule " + ruleInfo + " requires program stage \"" + ruleInfo.getRule().getProgramStage().getProgramStageReference() + "\" that is not included in program \"" + ruleInfo.getRule().getProgramStage().getProgram().getName() + "\"." ) ); variables.put( ScriptVariable.PROGRAM_STAGE.getVariableName(), programStage ); if ( !isApplicableProgramStageRef( context, ruleInfo, resourceMapping, programStage, variables ) ) { return false; } final TrackedEntityAttributes attributes = trackedEntityMetadataService.getAttributes(); final TrackedEntityType trackedEntityType = trackedEntityMetadataService .findTypeByReference( new Reference( program.getTrackedEntityTypeId(), ReferenceType.ID ) ) .orElseThrow( () -> new TransformerMappingException( "Program \"" + program.getName() + "\" references tracked entity type " + program.getTrackedEntityTypeId() + " that does not exist." ) ); variables.put( ScriptVariable.TRACKED_ENTITY_ATTRIBUTES.getVariableName(), attributes ); variables.put( ScriptVariable.TRACKED_ENTITY_TYPE.getVariableName(), trackedEntityType ); return true; }
FhirToProgramStageTransformer extends AbstractFhirToDhisTransformer<Event, ProgramStageRule> { protected boolean addBasicScriptVariables( @Nonnull Map<String, Object> variables, @Nonnull FhirToDhisTransformerContext context, @Nonnull RuleInfo<ProgramStageRule> ruleInfo, @Nonnull FhirResourceMapping resourceMapping ) throws TransformerException { final Program program = programMetadataService.findMetadataByReference( ruleInfo.getRule().getProgramStage().getProgram().getProgramReference() ) .orElseThrow( () -> new TransformerMappingException( "Mapping " + ruleInfo + " requires program \"" + ruleInfo.getRule().getProgramStage().getProgram().getProgramReference() + "\" that does not exist." ) ); variables.put( ScriptVariable.PROGRAM.getVariableName(), program ); final ProgramStage programStage = program.getOptionalStage( ruleInfo.getRule().getProgramStage().getProgramStageReference() ).orElseThrow( () -> new TransformerMappingException( "Rule " + ruleInfo + " requires program stage \"" + ruleInfo.getRule().getProgramStage().getProgramStageReference() + "\" that is not included in program \"" + ruleInfo.getRule().getProgramStage().getProgram().getName() + "\"." ) ); variables.put( ScriptVariable.PROGRAM_STAGE.getVariableName(), programStage ); if ( !isApplicableProgramStageRef( context, ruleInfo, resourceMapping, programStage, variables ) ) { return false; } final TrackedEntityAttributes attributes = trackedEntityMetadataService.getAttributes(); final TrackedEntityType trackedEntityType = trackedEntityMetadataService .findTypeByReference( new Reference( program.getTrackedEntityTypeId(), ReferenceType.ID ) ) .orElseThrow( () -> new TransformerMappingException( "Program \"" + program.getName() + "\" references tracked entity type " + program.getTrackedEntityTypeId() + " that does not exist." ) ); variables.put( ScriptVariable.TRACKED_ENTITY_ATTRIBUTES.getVariableName(), attributes ); variables.put( ScriptVariable.TRACKED_ENTITY_TYPE.getVariableName(), trackedEntityType ); return true; } }
FhirToProgramStageTransformer extends AbstractFhirToDhisTransformer<Event, ProgramStageRule> { protected boolean addBasicScriptVariables( @Nonnull Map<String, Object> variables, @Nonnull FhirToDhisTransformerContext context, @Nonnull RuleInfo<ProgramStageRule> ruleInfo, @Nonnull FhirResourceMapping resourceMapping ) throws TransformerException { final Program program = programMetadataService.findMetadataByReference( ruleInfo.getRule().getProgramStage().getProgram().getProgramReference() ) .orElseThrow( () -> new TransformerMappingException( "Mapping " + ruleInfo + " requires program \"" + ruleInfo.getRule().getProgramStage().getProgram().getProgramReference() + "\" that does not exist." ) ); variables.put( ScriptVariable.PROGRAM.getVariableName(), program ); final ProgramStage programStage = program.getOptionalStage( ruleInfo.getRule().getProgramStage().getProgramStageReference() ).orElseThrow( () -> new TransformerMappingException( "Rule " + ruleInfo + " requires program stage \"" + ruleInfo.getRule().getProgramStage().getProgramStageReference() + "\" that is not included in program \"" + ruleInfo.getRule().getProgramStage().getProgram().getName() + "\"." ) ); variables.put( ScriptVariable.PROGRAM_STAGE.getVariableName(), programStage ); if ( !isApplicableProgramStageRef( context, ruleInfo, resourceMapping, programStage, variables ) ) { return false; } final TrackedEntityAttributes attributes = trackedEntityMetadataService.getAttributes(); final TrackedEntityType trackedEntityType = trackedEntityMetadataService .findTypeByReference( new Reference( program.getTrackedEntityTypeId(), ReferenceType.ID ) ) .orElseThrow( () -> new TransformerMappingException( "Program \"" + program.getName() + "\" references tracked entity type " + program.getTrackedEntityTypeId() + " that does not exist." ) ); variables.put( ScriptVariable.TRACKED_ENTITY_ATTRIBUTES.getVariableName(), attributes ); variables.put( ScriptVariable.TRACKED_ENTITY_TYPE.getVariableName(), trackedEntityType ); return true; } FhirToProgramStageTransformer( @Nonnull ScriptExecutor scriptExecutor, @Nonnull OrganizationUnitService organizationUnitService, @Nonnull LockManager lockManager,
        @Nonnull TrackedEntityService trackedEntityService, @Nonnull TrackedEntityMetadataService trackedEntityMetadataService,
        @Nonnull ProgramMetadataService programMetadataService, @Nonnull EnrollmentService enrollmentService, @Nonnull EventService eventService,
        @Nonnull FhirResourceMappingRepository resourceMappingRepository, @Nonnull FhirDhisAssignmentRepository fhirDhisAssignmentRepository,
        @Nonnull ScriptExecutionContext scriptExecutionContext, @Nonnull ValueConverter valueConverter ); }
FhirToProgramStageTransformer extends AbstractFhirToDhisTransformer<Event, ProgramStageRule> { protected boolean addBasicScriptVariables( @Nonnull Map<String, Object> variables, @Nonnull FhirToDhisTransformerContext context, @Nonnull RuleInfo<ProgramStageRule> ruleInfo, @Nonnull FhirResourceMapping resourceMapping ) throws TransformerException { final Program program = programMetadataService.findMetadataByReference( ruleInfo.getRule().getProgramStage().getProgram().getProgramReference() ) .orElseThrow( () -> new TransformerMappingException( "Mapping " + ruleInfo + " requires program \"" + ruleInfo.getRule().getProgramStage().getProgram().getProgramReference() + "\" that does not exist." ) ); variables.put( ScriptVariable.PROGRAM.getVariableName(), program ); final ProgramStage programStage = program.getOptionalStage( ruleInfo.getRule().getProgramStage().getProgramStageReference() ).orElseThrow( () -> new TransformerMappingException( "Rule " + ruleInfo + " requires program stage \"" + ruleInfo.getRule().getProgramStage().getProgramStageReference() + "\" that is not included in program \"" + ruleInfo.getRule().getProgramStage().getProgram().getName() + "\"." ) ); variables.put( ScriptVariable.PROGRAM_STAGE.getVariableName(), programStage ); if ( !isApplicableProgramStageRef( context, ruleInfo, resourceMapping, programStage, variables ) ) { return false; } final TrackedEntityAttributes attributes = trackedEntityMetadataService.getAttributes(); final TrackedEntityType trackedEntityType = trackedEntityMetadataService .findTypeByReference( new Reference( program.getTrackedEntityTypeId(), ReferenceType.ID ) ) .orElseThrow( () -> new TransformerMappingException( "Program \"" + program.getName() + "\" references tracked entity type " + program.getTrackedEntityTypeId() + " that does not exist." ) ); variables.put( ScriptVariable.TRACKED_ENTITY_ATTRIBUTES.getVariableName(), attributes ); variables.put( ScriptVariable.TRACKED_ENTITY_TYPE.getVariableName(), trackedEntityType ); return true; } FhirToProgramStageTransformer( @Nonnull ScriptExecutor scriptExecutor, @Nonnull OrganizationUnitService organizationUnitService, @Nonnull LockManager lockManager,
        @Nonnull TrackedEntityService trackedEntityService, @Nonnull TrackedEntityMetadataService trackedEntityMetadataService,
        @Nonnull ProgramMetadataService programMetadataService, @Nonnull EnrollmentService enrollmentService, @Nonnull EventService eventService,
        @Nonnull FhirResourceMappingRepository resourceMappingRepository, @Nonnull FhirDhisAssignmentRepository fhirDhisAssignmentRepository,
        @Nonnull ScriptExecutionContext scriptExecutionContext, @Nonnull ValueConverter valueConverter ); @Nonnull @Override Set<FhirVersion> getFhirVersions(); @Nonnull @Override DhisResourceType getDhisResourceType(); @Nonnull @Override Class<Event> getDhisResourceClass(); @Nonnull @Override Class<ProgramStageRule> getRuleClass(); @Nullable @Override FhirToDhisTransformOutcome<Event> transform( @Nonnull FhirClientResource fhirClientResource, @Nonnull FhirToDhisTransformerContext context, @Nonnull IBaseResource input,
        @Nonnull RuleInfo<ProgramStageRule> ruleInfo, @Nonnull Map<String, Object> scriptVariables ); @Nullable @Override FhirToDhisDeleteTransformOutcome<Event> transformDeletion( @Nonnull FhirClientResource fhirClientResource, @Nonnull RuleInfo<ProgramStageRule> ruleInfo, @Nonnull DhisFhirResourceId dhisFhirResourceId ); }
FhirToProgramStageTransformer extends AbstractFhirToDhisTransformer<Event, ProgramStageRule> { protected boolean addBasicScriptVariables( @Nonnull Map<String, Object> variables, @Nonnull FhirToDhisTransformerContext context, @Nonnull RuleInfo<ProgramStageRule> ruleInfo, @Nonnull FhirResourceMapping resourceMapping ) throws TransformerException { final Program program = programMetadataService.findMetadataByReference( ruleInfo.getRule().getProgramStage().getProgram().getProgramReference() ) .orElseThrow( () -> new TransformerMappingException( "Mapping " + ruleInfo + " requires program \"" + ruleInfo.getRule().getProgramStage().getProgram().getProgramReference() + "\" that does not exist." ) ); variables.put( ScriptVariable.PROGRAM.getVariableName(), program ); final ProgramStage programStage = program.getOptionalStage( ruleInfo.getRule().getProgramStage().getProgramStageReference() ).orElseThrow( () -> new TransformerMappingException( "Rule " + ruleInfo + " requires program stage \"" + ruleInfo.getRule().getProgramStage().getProgramStageReference() + "\" that is not included in program \"" + ruleInfo.getRule().getProgramStage().getProgram().getName() + "\"." ) ); variables.put( ScriptVariable.PROGRAM_STAGE.getVariableName(), programStage ); if ( !isApplicableProgramStageRef( context, ruleInfo, resourceMapping, programStage, variables ) ) { return false; } final TrackedEntityAttributes attributes = trackedEntityMetadataService.getAttributes(); final TrackedEntityType trackedEntityType = trackedEntityMetadataService .findTypeByReference( new Reference( program.getTrackedEntityTypeId(), ReferenceType.ID ) ) .orElseThrow( () -> new TransformerMappingException( "Program \"" + program.getName() + "\" references tracked entity type " + program.getTrackedEntityTypeId() + " that does not exist." ) ); variables.put( ScriptVariable.TRACKED_ENTITY_ATTRIBUTES.getVariableName(), attributes ); variables.put( ScriptVariable.TRACKED_ENTITY_TYPE.getVariableName(), trackedEntityType ); return true; } FhirToProgramStageTransformer( @Nonnull ScriptExecutor scriptExecutor, @Nonnull OrganizationUnitService organizationUnitService, @Nonnull LockManager lockManager,
        @Nonnull TrackedEntityService trackedEntityService, @Nonnull TrackedEntityMetadataService trackedEntityMetadataService,
        @Nonnull ProgramMetadataService programMetadataService, @Nonnull EnrollmentService enrollmentService, @Nonnull EventService eventService,
        @Nonnull FhirResourceMappingRepository resourceMappingRepository, @Nonnull FhirDhisAssignmentRepository fhirDhisAssignmentRepository,
        @Nonnull ScriptExecutionContext scriptExecutionContext, @Nonnull ValueConverter valueConverter ); @Nonnull @Override Set<FhirVersion> getFhirVersions(); @Nonnull @Override DhisResourceType getDhisResourceType(); @Nonnull @Override Class<Event> getDhisResourceClass(); @Nonnull @Override Class<ProgramStageRule> getRuleClass(); @Nullable @Override FhirToDhisTransformOutcome<Event> transform( @Nonnull FhirClientResource fhirClientResource, @Nonnull FhirToDhisTransformerContext context, @Nonnull IBaseResource input,
        @Nonnull RuleInfo<ProgramStageRule> ruleInfo, @Nonnull Map<String, Object> scriptVariables ); @Nullable @Override FhirToDhisDeleteTransformOutcome<Event> transformDeletion( @Nonnull FhirClientResource fhirClientResource, @Nonnull RuleInfo<ProgramStageRule> ruleInfo, @Nonnull DhisFhirResourceId dhisFhirResourceId );  }