1+ package controller.conversion
2+
3+ import controller.validation.ValidationServiceFactory
4+ import model.CliContext
5+ import org.hl7.fhir.utilities.TimeTracker
6+ import org.hl7.fhir.utilities.VersionUtilities
7+ import org.hl7.fhir.validation.ValidationEngine
8+ import org.koin.core.component.KoinComponent
9+ import org.koin.core.component.inject
10+ import java.nio.file.Files
11+ import java.nio.file.Path
12+ import kotlin.io.path.deleteIfExists
13+
14+ class ConversionControllerImpl : ConversionController , KoinComponent {
15+
16+ private val validationServiceFactory by inject<ValidationServiceFactory >()
17+
18+ override suspend fun convertRequest (content : String , type : String? , version : String? , toType : String? ,
19+ toVersion : String? , sessionId : String? ): ConversionResponse {
20+ var fromType = type ? : " json"
21+ var fromVersion = version ? : " 5.0"
22+ var session = sessionId ? : " new"
23+
24+ val cliContext = CliContext ()
25+
26+ var validator: ValidationEngine ? = validationServiceFactory.getCachedValidator(session)
27+ if (validator == null || validator.version != fromVersion) {
28+ val definitions = VersionUtilities .packageForVersion(fromVersion) + " #" +
29+ VersionUtilities .getCurrentVersion(fromVersion)
30+ val timeTracker = TimeTracker ()
31+ session = validationServiceFactory.getValidationService()
32+ .initializeValidator(cliContext, definitions, timeTracker, " new" )
33+ validator = validationServiceFactory.getCachedValidator(session)
34+ validator?.version = fromVersion
35+ }
36+
37+ var input: Path ? = null
38+ var output: Path ? = null
39+ try {
40+ input = Files .createTempFile(" input" , " .$fromType " )
41+ Files .writeString(input.toAbsolutePath(), content)
42+ cliContext.addSource(input.toAbsolutePath().toString())
43+
44+ output = Files .createTempFile(" convert" , " .${toType ? : fromType} " )
45+
46+ cliContext.targetVer = toVersion ? : fromVersion
47+
48+ cliContext.output = output.toAbsolutePath().toString()
49+ validationServiceFactory.getValidationService().convertSources(cliContext, validator)
50+ return ConversionResponse (Files .readString(output.toAbsolutePath()), session)
51+ } finally {
52+ input?.deleteIfExists()
53+ output?.deleteIfExists()
54+ }
55+ }
56+ }
0 commit comments