-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate validationRequest listener to unblock main thread
during questionnaire response submission
- Loading branch information
Showing
6 changed files
with
225 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 0 additions & 76 deletions
76
.../src/main/java/org/smartregister/fhircore/engine/util/extension/FhirValidatorExtension.kt
This file was deleted.
Oops, something went wrong.
94 changes: 94 additions & 0 deletions
94
.../main/java/org/smartregister/fhircore/engine/util/validation/ResourceValidationRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2021-2024 Ona Systems, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.smartregister.fhircore.engine.util.validation | ||
|
||
import ca.uhn.fhir.validation.FhirValidator | ||
import ca.uhn.fhir.validation.ResultSeverityEnum | ||
import ca.uhn.fhir.validation.ValidationResult | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import org.hl7.fhir.r4.model.Resource | ||
import org.smartregister.fhircore.engine.util.DispatcherProvider | ||
import org.smartregister.fhircore.engine.util.extension.referenceValue | ||
import timber.log.Timber | ||
|
||
data class ResourceValidationRequest(val resources: List<Resource>) { | ||
constructor(vararg resource: Resource) : this(resource.toList()) | ||
} | ||
|
||
class ResourceValidationRequestHandler( | ||
private val fhirValidator: FhirValidator, | ||
private val dispatcherProvider: DispatcherProvider, | ||
) { | ||
fun handleResourceValidationRequest(request: ResourceValidationRequest) { | ||
CoroutineScope(dispatcherProvider.io()).launch { | ||
val resources = request.resources | ||
fhirValidator.checkResources(resources).logErrorMessages() | ||
} | ||
} | ||
} | ||
|
||
internal data class ResourceValidationResult( | ||
val resource: Resource, | ||
val validationResult: ValidationResult, | ||
) { | ||
val errorMessages | ||
get() = buildString { | ||
val messages = | ||
validationResult.messages.filter { | ||
it.severity.ordinal >= ResultSeverityEnum.WARNING.ordinal | ||
} | ||
if (messages.isNotEmpty()) { | ||
appendLine(resource.referenceValue()) | ||
} | ||
for (validationMsg in messages) { | ||
appendLine( | ||
"${validationMsg.locationString} - ${validationMsg.message} -- (${validationMsg.severity})", | ||
) | ||
} | ||
} | ||
} | ||
|
||
internal class FhirValidatorResultsWrapper( | ||
val results: List<ResourceValidationResult> = emptyList(), | ||
) { | ||
val errorMessages = results.map { it.errorMessages } | ||
|
||
fun logErrorMessages() { | ||
results.forEach { | ||
if (it.errorMessages.isNotBlank()) { | ||
Timber.tag(TAG).e(it.errorMessages) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
private const val TAG = "FhirValidatorResult" | ||
} | ||
} | ||
|
||
internal fun FhirValidator.checkResources( | ||
resources: List<Resource>, | ||
): FhirValidatorResultsWrapper { | ||
return FhirValidatorResultsWrapper( | ||
results = | ||
resources.map { | ||
val result = this@checkResources.validateWithResult(it) | ||
ResourceValidationResult(it, result) | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.