@Test( expected = TransformerDataException.class ) public void getEnrollmentDateFutureNotAllowed() { final Map<String, Object> scriptVariables = new HashMap<>(); scriptVariables.put( ScriptVariable.INPUT.getVariableName(), baseResource ); program.setSelectEnrollmentDatesInFuture( false ); Mockito.when( baseResource.getMeta() ).thenReturn( meta ); Mockito.when( meta.getLastUpdated() ).thenReturn( Date.from( ZonedDateTime.of( ZonedDateTime.now().getYear() + 1, 10, 12, 0, 0, 0, 0, ZoneId.systemDefault() ).toInstant() ) ); transformer.getEnrollmentDate( context, new RuleInfo<>( enrollmentRule, Collections.emptyList() ), fhirResourceMapping, program, scriptVariables ); }
@Nullable protected ZonedDateTime getEnrollmentDate( @Nonnull FhirToDhisTransformerContext context, @Nonnull RuleInfo<EnrollmentRule> ruleInfo, @Nonnull FhirResourceMapping resourceMapping, @Nonnull Program program, @Nonnull Map<String, Object> scriptVariables ) { ZonedDateTime enrollmentDate = valueConverter.convert( executeScript( context, ruleInfo, resourceMapping.getImpEnrollmentDateLookupScript(), scriptVariables, Object.class ), ValueType.DATETIME, ZonedDateTime.class ); if ( enrollmentDate == null ) { final IAnyResource resource = TransformerUtils.getScriptVariable( scriptVariables, ScriptVariable.INPUT, IAnyResource.class ); if ( resource.getMeta() != null && resource.getMeta().getLastUpdated() != null ) { logger.info( "Enrollment date of program instance \"{}\" has not been returned by " + "enrollment date script (using last updated timestamp).", program.getName() ); enrollmentDate = ZonedDateTime.ofInstant( resource.getMeta().getLastUpdated().toInstant(), zoneId ); } } if ( enrollmentDate == null ) { logger.info( "Enrollment date of program instance \"{}\" has not been returned by " + "enrollment date script (using current timestamp).", program.getName() ); enrollmentDate = ZonedDateTime.now(); } if ( !program.isSelectEnrollmentDatesInFuture() && DateTimeUtils.isFutureDate( enrollmentDate ) ) { throw new TransformerDataException( "Enrollment date of of program instance \"" + program.getName() + "\" is in the future and program does not allow dates in the future." ); } return enrollmentDate; }
FhirToEnrollmentTransformer extends AbstractFhirToEnrollmentTransformer { @Nullable protected ZonedDateTime getEnrollmentDate( @Nonnull FhirToDhisTransformerContext context, @Nonnull RuleInfo<EnrollmentRule> ruleInfo, @Nonnull FhirResourceMapping resourceMapping, @Nonnull Program program, @Nonnull Map<String, Object> scriptVariables ) { ZonedDateTime enrollmentDate = valueConverter.convert( executeScript( context, ruleInfo, resourceMapping.getImpEnrollmentDateLookupScript(), scriptVariables, Object.class ), ValueType.DATETIME, ZonedDateTime.class ); if ( enrollmentDate == null ) { final IAnyResource resource = TransformerUtils.getScriptVariable( scriptVariables, ScriptVariable.INPUT, IAnyResource.class ); if ( resource.getMeta() != null && resource.getMeta().getLastUpdated() != null ) { logger.info( "Enrollment date of program instance \"{}\" has not been returned by " + "enrollment date script (using last updated timestamp).", program.getName() ); enrollmentDate = ZonedDateTime.ofInstant( resource.getMeta().getLastUpdated().toInstant(), zoneId ); } } if ( enrollmentDate == null ) { logger.info( "Enrollment date of program instance \"{}\" has not been returned by " + "enrollment date script (using current timestamp).", program.getName() ); enrollmentDate = ZonedDateTime.now(); } if ( !program.isSelectEnrollmentDatesInFuture() && DateTimeUtils.isFutureDate( enrollmentDate ) ) { throw new TransformerDataException( "Enrollment date of of program instance \"" + program.getName() + "\" is in the future and program does not allow dates in the future." ); } return enrollmentDate; } }
FhirToEnrollmentTransformer extends AbstractFhirToEnrollmentTransformer { @Nullable protected ZonedDateTime getEnrollmentDate( @Nonnull FhirToDhisTransformerContext context, @Nonnull RuleInfo<EnrollmentRule> ruleInfo, @Nonnull FhirResourceMapping resourceMapping, @Nonnull Program program, @Nonnull Map<String, Object> scriptVariables ) { ZonedDateTime enrollmentDate = valueConverter.convert( executeScript( context, ruleInfo, resourceMapping.getImpEnrollmentDateLookupScript(), scriptVariables, Object.class ), ValueType.DATETIME, ZonedDateTime.class ); if ( enrollmentDate == null ) { final IAnyResource resource = TransformerUtils.getScriptVariable( scriptVariables, ScriptVariable.INPUT, IAnyResource.class ); if ( resource.getMeta() != null && resource.getMeta().getLastUpdated() != null ) { logger.info( "Enrollment date of program instance \"{}\" has not been returned by " + "enrollment date script (using last updated timestamp).", program.getName() ); enrollmentDate = ZonedDateTime.ofInstant( resource.getMeta().getLastUpdated().toInstant(), zoneId ); } } if ( enrollmentDate == null ) { logger.info( "Enrollment date of program instance \"{}\" has not been returned by " + "enrollment date script (using current timestamp).", program.getName() ); enrollmentDate = ZonedDateTime.now(); } if ( !program.isSelectEnrollmentDatesInFuture() && DateTimeUtils.isFutureDate( enrollmentDate ) ) { throw new TransformerDataException( "Enrollment date of of program instance \"" + program.getName() + "\" is in the future and program does not allow dates in the future." ); } return enrollmentDate; } FhirToEnrollmentTransformer( @Nonnull ScriptExecutor scriptExecutor, @Nonnull TrackedEntityMetadataService trackedEntityMetadataService, @Nonnull TrackedEntityService trackedEntityService,
        @Nonnull TrackedEntityRuleRepository trackedEntityRuleRepository, @Nonnull OrganizationUnitService organizationUnitService, @Nonnull ProgramMetadataService programMetadataService, @Nonnull EnrollmentService enrollmentService,
        @Nonnull FhirResourceMappingRepository resourceMappingRepository, @Nonnull FhirDhisAssignmentRepository fhirDhisAssignmentRepository, @Nonnull ScriptExecutionContext scriptExecutionContext, @Nonnull ValueConverter valueConverter ); }
FhirToEnrollmentTransformer extends AbstractFhirToEnrollmentTransformer { @Nullable protected ZonedDateTime getEnrollmentDate( @Nonnull FhirToDhisTransformerContext context, @Nonnull RuleInfo<EnrollmentRule> ruleInfo, @Nonnull FhirResourceMapping resourceMapping, @Nonnull Program program, @Nonnull Map<String, Object> scriptVariables ) { ZonedDateTime enrollmentDate = valueConverter.convert( executeScript( context, ruleInfo, resourceMapping.getImpEnrollmentDateLookupScript(), scriptVariables, Object.class ), ValueType.DATETIME, ZonedDateTime.class ); if ( enrollmentDate == null ) { final IAnyResource resource = TransformerUtils.getScriptVariable( scriptVariables, ScriptVariable.INPUT, IAnyResource.class ); if ( resource.getMeta() != null && resource.getMeta().getLastUpdated() != null ) { logger.info( "Enrollment date of program instance \"{}\" has not been returned by " + "enrollment date script (using last updated timestamp).", program.getName() ); enrollmentDate = ZonedDateTime.ofInstant( resource.getMeta().getLastUpdated().toInstant(), zoneId ); } } if ( enrollmentDate == null ) { logger.info( "Enrollment date of program instance \"{}\" has not been returned by " + "enrollment date script (using current timestamp).", program.getName() ); enrollmentDate = ZonedDateTime.now(); } if ( !program.isSelectEnrollmentDatesInFuture() && DateTimeUtils.isFutureDate( enrollmentDate ) ) { throw new TransformerDataException( "Enrollment date of of program instance \"" + program.getName() + "\" is in the future and program does not allow dates in the future." ); } return enrollmentDate; } FhirToEnrollmentTransformer( @Nonnull ScriptExecutor scriptExecutor, @Nonnull TrackedEntityMetadataService trackedEntityMetadataService, @Nonnull TrackedEntityService trackedEntityService,
        @Nonnull TrackedEntityRuleRepository trackedEntityRuleRepository, @Nonnull OrganizationUnitService organizationUnitService, @Nonnull ProgramMetadataService programMetadataService, @Nonnull EnrollmentService enrollmentService,
        @Nonnull FhirResourceMappingRepository resourceMappingRepository, @Nonnull FhirDhisAssignmentRepository fhirDhisAssignmentRepository, @Nonnull ScriptExecutionContext scriptExecutionContext, @Nonnull ValueConverter valueConverter ); @Nonnull @Override Set<FhirVersion> getFhirVersions(); @Override FhirToDhisTransformOutcome<Enrollment> transform( @Nonnull FhirClientResource fhirClientResource, @Nonnull FhirToDhisTransformerContext context,
        @Nonnull IBaseResource input, @Nonnull RuleInfo<EnrollmentRule> ruleInfo, @Nonnull Map<String, Object> scriptVariables ); }
FhirToEnrollmentTransformer extends AbstractFhirToEnrollmentTransformer { @Nullable protected ZonedDateTime getEnrollmentDate( @Nonnull FhirToDhisTransformerContext context, @Nonnull RuleInfo<EnrollmentRule> ruleInfo, @Nonnull FhirResourceMapping resourceMapping, @Nonnull Program program, @Nonnull Map<String, Object> scriptVariables ) { ZonedDateTime enrollmentDate = valueConverter.convert( executeScript( context, ruleInfo, resourceMapping.getImpEnrollmentDateLookupScript(), scriptVariables, Object.class ), ValueType.DATETIME, ZonedDateTime.class ); if ( enrollmentDate == null ) { final IAnyResource resource = TransformerUtils.getScriptVariable( scriptVariables, ScriptVariable.INPUT, IAnyResource.class ); if ( resource.getMeta() != null && resource.getMeta().getLastUpdated() != null ) { logger.info( "Enrollment date of program instance \"{}\" has not been returned by " + "enrollment date script (using last updated timestamp).", program.getName() ); enrollmentDate = ZonedDateTime.ofInstant( resource.getMeta().getLastUpdated().toInstant(), zoneId ); } } if ( enrollmentDate == null ) { logger.info( "Enrollment date of program instance \"{}\" has not been returned by " + "enrollment date script (using current timestamp).", program.getName() ); enrollmentDate = ZonedDateTime.now(); } if ( !program.isSelectEnrollmentDatesInFuture() && DateTimeUtils.isFutureDate( enrollmentDate ) ) { throw new TransformerDataException( "Enrollment date of of program instance \"" + program.getName() + "\" is in the future and program does not allow dates in the future." ); } return enrollmentDate; } FhirToEnrollmentTransformer( @Nonnull ScriptExecutor scriptExecutor, @Nonnull TrackedEntityMetadataService trackedEntityMetadataService, @Nonnull TrackedEntityService trackedEntityService,
        @Nonnull TrackedEntityRuleRepository trackedEntityRuleRepository, @Nonnull OrganizationUnitService organizationUnitService, @Nonnull ProgramMetadataService programMetadataService, @Nonnull EnrollmentService enrollmentService,
        @Nonnull FhirResourceMappingRepository resourceMappingRepository, @Nonnull FhirDhisAssignmentRepository fhirDhisAssignmentRepository, @Nonnull ScriptExecutionContext scriptExecutionContext, @Nonnull ValueConverter valueConverter ); @Nonnull @Override Set<FhirVersion> getFhirVersions(); @Override FhirToDhisTransformOutcome<Enrollment> transform( @Nonnull FhirClientResource fhirClientResource, @Nonnull FhirToDhisTransformerContext context,
        @Nonnull IBaseResource input, @Nonnull RuleInfo<EnrollmentRule> ruleInfo, @Nonnull Map<String, Object> scriptVariables );  }