001/*-
002 * #%L
003 * HAPI FHIR - Server Framework
004 * %%
005 * Copyright (C) 2014 - 2025 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.rest.server.interceptor.validation;
021
022import ca.uhn.fhir.context.support.IValidationSupport;
023import ca.uhn.fhir.interceptor.api.Interceptor;
024import ca.uhn.fhir.validation.ResultSeverityEnum;
025
026import java.util.List;
027
028import static java.lang.String.format;
029import static org.hl7.fhir.utilities.i18n.I18nConstants.TERMINOLOGY_PASSTHROUGH_TX_MESSAGE;
030import static org.hl7.fhir.utilities.i18n.I18nConstants.TERMINOLOGY_TX_NOVALID_12;
031import static org.hl7.fhir.utilities.i18n.I18nConstants.TERMINOLOGY_TX_SYSTEM_UNKNOWN;
032
033/**
034 * This interceptor is for changing the severity of the Unknown Code System validation issues to a desired severity
035 * after the validation has been performed.
036 * @since 8.6.0
037 */
038@Interceptor
039public class ValidationMessageUnknownCodeSystemPostProcessingInterceptor
040                extends ValidationMessagePostProcessingInterceptor {
041
042        public ValidationMessageUnknownCodeSystemPostProcessingInterceptor(
043                        IValidationSupport.IssueSeverity theDesiredSeverityForUnknownCodeSystem) {
044
045                ResultSeverityEnum desiredResultSeverity =
046                                mapIssueSeverityToResultSeverityEnum(theDesiredSeverityForUnknownCodeSystem);
047
048                super.addPostProcessingPatterns(
049                                // this is an unknown code system error produced by the core validator
050                                new ValidationPostProcessingRuleJson(
051                                                TERMINOLOGY_TX_SYSTEM_UNKNOWN,
052                                                null,
053                                                List.of(ResultSeverityEnum.ERROR, ResultSeverityEnum.WARNING, ResultSeverityEnum.INFORMATION),
054                                                List.of("Unknown Code System"),
055                                                desiredResultSeverity),
056                                // this is for the unknown code system error produced by HAPI-FHIR validation
057                                new ValidationPostProcessingRuleJson(
058                                                TERMINOLOGY_PASSTHROUGH_TX_MESSAGE,
059                                                null,
060                                                List.of(ResultSeverityEnum.ERROR, ResultSeverityEnum.WARNING, ResultSeverityEnum.INFORMATION),
061                                                List.of("CodeSystem is unknown and can't be validated"),
062                                                desiredResultSeverity),
063                                // This is a related error that is caused by unknown code systems, the issue message is produced by the
064                                // HAPI-FHIR validator.
065                                // In some cases, the core validator produces additional related issue with id Terminology_TX_NoValid_12
066                                // and including the HAPI-FHIR's issue message.
067                                // This rule covers both messages.
068                                new ValidationPostProcessingRuleJson(
069                                                null,
070                                                format("%s|%s", TERMINOLOGY_PASSTHROUGH_TX_MESSAGE, TERMINOLOGY_TX_NOVALID_12),
071                                                List.of(ResultSeverityEnum.ERROR, ResultSeverityEnum.WARNING, ResultSeverityEnum.INFORMATION),
072                                                List.of("Unable to expand ValueSet because CodeSystem could not be found"),
073                                                desiredResultSeverity));
074        }
075
076        private static ResultSeverityEnum mapIssueSeverityToResultSeverityEnum(
077                        IValidationSupport.IssueSeverity theIssueSeverity) {
078                return switch (theIssueSeverity) {
079                        case FATAL -> ResultSeverityEnum.FATAL;
080                        case ERROR -> ResultSeverityEnum.ERROR;
081                        case WARNING -> ResultSeverityEnum.WARNING;
082                                // treat success as information level
083                        case INFORMATION, SUCCESS -> ResultSeverityEnum.INFORMATION;
084                };
085        }
086}