001/*-
002 * #%L
003 * HAPI FHIR Storage api
004 * %%
005 * Copyright (C) 2014 - 2024 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 ReferenceValidationPolicy policyForReference(
054                        IResourceValidator validator, Object appContext, String path, String url) {
055                int slashIdx = url.indexOf("/");
056                if (slashIdx > 0 && myFhirContext.getResourceTypes().contains(url.substring(0, slashIdx))) {
057                        return myValidationSettings.getLocalReferenceValidationDefaultPolicy();
058                }
059
060                return ReferenceValidationPolicy.IGNORE;
061        }
062
063        @Override
064        public EnumSet<ResourceValidationAction> policyForResource(
065                        IResourceValidator validator, Object appContext, StructureDefinition type, String path) {
066                return EnumSet.allOf(ResourceValidationAction.class);
067        }
068
069        @Override
070        public EnumSet<ElementValidationAction> policyForElement(
071                        IResourceValidator validator,
072                        Object appContext,
073                        StructureDefinition structure,
074                        ElementDefinition element,
075                        String path) {
076                return EnumSet.allOf(ElementValidationAction.class);
077        }
078
079        @Override
080        public EnumSet<CodedContentValidationAction> policyForCodedContent(
081                        IResourceValidator validator,
082                        Object appContext,
083                        String stackPath,
084                        ElementDefinition definition,
085                        StructureDefinition structure,
086                        BindingKind kind,
087                        AdditionalBindingPurpose purpose,
088                        ValueSet valueSet,
089                        List<String> systems) {
090                return EnumSet.allOf(CodedContentValidationAction.class);
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 getReferencePolicy() {
128                return ReferenceValidationPolicy.IGNORE;
129        }
130}