001/*-
002 * #%L
003 * HAPI FHIR Storage api
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.jpa.validation;
021
022import ca.uhn.fhir.context.FhirContext;
023import org.hl7.fhir.r5.elementmodel.Element;
024import org.hl7.fhir.r5.model.ElementDefinition;
025import org.hl7.fhir.r5.model.StructureDefinition;
026import org.hl7.fhir.r5.model.ValueSet;
027import org.hl7.fhir.r5.utils.validation.IMessagingServices;
028import org.hl7.fhir.r5.utils.validation.IResourceValidator;
029import org.hl7.fhir.r5.utils.validation.IValidationPolicyAdvisor;
030import org.hl7.fhir.r5.utils.validation.constants.BindingKind;
031import org.hl7.fhir.r5.utils.validation.constants.ContainedReferenceValidationPolicy;
032import org.hl7.fhir.r5.utils.validation.constants.ReferenceValidationPolicy;
033import org.hl7.fhir.utilities.validation.ValidationMessage;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036import org.springframework.beans.factory.annotation.Autowired;
037
038import java.util.Arrays;
039import java.util.EnumSet;
040import java.util.List;
041
042public class ValidatorPolicyAdvisor implements IValidationPolicyAdvisor {
043
044        private static final Logger ourLog = LoggerFactory.getLogger(ValidatorPolicyAdvisor.class);
045
046        @Autowired
047        private ValidationSettings myValidationSettings;
048
049        @Autowired
050        private FhirContext myFhirContext;
051
052        @Override
053        public EnumSet<ResourceValidationAction> policyForResource(
054                        IResourceValidator validator, Object appContext, StructureDefinition type, String path) {
055                return EnumSet.allOf(ResourceValidationAction.class);
056        }
057
058        @Override
059        public EnumSet<ElementValidationAction> policyForElement(
060                        IResourceValidator validator,
061                        Object appContext,
062                        StructureDefinition structure,
063                        ElementDefinition element,
064                        String path) {
065                return EnumSet.allOf(ElementValidationAction.class);
066        }
067
068        @Override
069        public EnumSet<CodedContentValidationAction> policyForCodedContent(
070                        IResourceValidator validator,
071                        Object appContext,
072                        String stackPath,
073                        ElementDefinition definition,
074                        StructureDefinition structure,
075                        BindingKind kind,
076                        AdditionalBindingPurpose purpose,
077                        ValueSet valueSet,
078                        List<String> systems) {
079                return EnumSet.allOf(CodedContentValidationAction.class);
080        }
081
082        @Override
083        public SpecialValidationAction policyForSpecialValidation(
084                        IResourceValidator validator,
085                        Object appContext,
086                        SpecialValidationRule rule,
087                        String stackPath,
088                        Element resource,
089                        Element element) {
090                return null;
091        }
092
093        @Override
094        public ContainedReferenceValidationPolicy policyForContained(
095                        IResourceValidator validator,
096                        Object appContext,
097                        StructureDefinition structure,
098                        ElementDefinition element,
099                        String containerType,
100                        String containerId,
101                        Element.SpecialElement containingResourceType,
102                        String path,
103                        String url) {
104                return ContainedReferenceValidationPolicy.CHECK_VALID;
105        }
106
107        @Override
108        public List<StructureDefinition> getImpliedProfilesForResource(
109                        IResourceValidator validator,
110                        Object appContext,
111                        String stackPath,
112                        ElementDefinition definition,
113                        StructureDefinition structure,
114                        Element resource,
115                        boolean valid,
116                        IMessagingServices msgServices,
117                        List<ValidationMessage> messages) {
118                return Arrays.asList();
119        }
120
121        @Override
122        public boolean isSuppressMessageId(String path, String messageId) {
123                return false;
124        }
125
126        @Override
127        public ReferenceValidationPolicy policyForReference(
128                        IResourceValidator validator,
129                        Object appContext,
130                        String path,
131                        String url,
132                        ReferenceDestinationType destinationType) {
133                int slashIdx = url.indexOf("/");
134                if (slashIdx > 0 && myFhirContext.getResourceTypes().contains(url.substring(0, slashIdx))) {
135                        return myValidationSettings.getLocalReferenceValidationDefaultPolicy();
136                }
137                return ReferenceValidationPolicy.IGNORE;
138        }
139
140        @Override
141        public IValidationPolicyAdvisor getPolicyAdvisor() {
142                return this;
143        }
144
145        @Override
146        public IValidationPolicyAdvisor setPolicyAdvisor(IValidationPolicyAdvisor policyAdvisor) {
147                return this;
148        }
149
150        @Override
151        public ReferenceValidationPolicy getReferencePolicy() {
152                return ReferenceValidationPolicy.IGNORE;
153        }
154}