001package org.hl7.fhir.common.hapi.validation.support;
002
003import ca.uhn.fhir.context.ConfigurationException;
004import ca.uhn.fhir.context.FhirContext;
005import ca.uhn.fhir.context.FhirVersionEnum;
006import ca.uhn.fhir.i18n.Msg;
007import ca.uhn.fhir.rest.api.Constants;
008import ca.uhn.fhir.util.FhirTerser;
009import ca.uhn.fhir.util.Logs;
010import ca.uhn.fhir.util.StopWatch;
011import jakarta.annotation.Nonnull;
012import org.apache.commons.lang3.Validate;
013import org.hl7.fhir.instance.model.api.IBaseResource;
014import org.slf4j.Logger;
015
016import java.io.IOException;
017
018// This is instantiated through reflection from DefaultProfileValidationSupport
019@SuppressWarnings("unused")
020public class DefaultProfileValidationSupportNpmStrategy extends NpmPackageValidationSupport {
021        private static final Logger ourLog = Logs.getTerminologyTroubleshootingLog();
022        private final FhirTerser myTerser;
023        private boolean mySkipSearchParameters;
024
025        /**
026         * Constructor
027         */
028        public DefaultProfileValidationSupportNpmStrategy(@Nonnull FhirContext theFhirContext) {
029                super(theFhirContext);
030
031                myTerser = theFhirContext.newTerser();
032
033                Validate.isTrue(theFhirContext.getVersion().getVersion() == FhirVersionEnum.R5);
034
035                ourLog.info("Loading R5 Core+Extension packages into memory");
036                StopWatch sw = new StopWatch();
037
038                try {
039                        mySkipSearchParameters = false;
040                        loadPackageFromClasspath("org/hl7/fhir/r5/packages/hl7.fhir.r5.core-5.0.0.tgz");
041
042                        // Don't load extended search parameters (these should be loaded manually if wanted)
043                        mySkipSearchParameters = true;
044                        loadPackageFromClasspath("org/hl7/fhir/r5/packages/hl7.fhir.uv.extensions.r5-1.0.0.tgz");
045                        loadPackageFromClasspath("org/hl7/fhir/r5/packages/hl7.terminology-5.1.0.tgz");
046                } catch (IOException e) {
047                        throw new ConfigurationException(
048                                        Msg.code(2333)
049                                                        + "Failed to load required validation resources. Make sure that the appropriate hapi-fhir-validation-resources-VER JAR is on the classpath",
050                                        e);
051                }
052
053                ourLog.info("Loaded {} Core+Extension resources in {}", countAll(), sw);
054        }
055
056        @Override
057        public String getName() {
058                return getFhirContext().getVersion().getVersion() + " FHIR Standard Profile NPM Validation Support";
059        }
060
061        @Override
062        public void addSearchParameter(IBaseResource theSearchParameter) {
063                if (mySkipSearchParameters) {
064                        return;
065                }
066
067                String code = myTerser.getSinglePrimitiveValueOrNull(theSearchParameter, "code");
068
069                // Not yet supported
070                if (Constants.PARAM_IN.equals(code)) {
071                        return;
072                }
073
074                super.addSearchParameter(theSearchParameter);
075        }
076}