001/*
002 * #%L
003 * HAPI FHIR JPA - Search Parameters
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.searchparam.extractor;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.jpa.model.config.PartitionSettings;
024import ca.uhn.fhir.jpa.model.entity.StorageSettings;
025import ca.uhn.fhir.rest.server.util.ISearchParamRegistry;
026import ca.uhn.fhir.sl.cache.Cache;
027import ca.uhn.fhir.sl.cache.CacheFactory;
028import ca.uhn.fhir.util.BundleUtil;
029import com.google.common.annotations.VisibleForTesting;
030import jakarta.annotation.PostConstruct;
031import org.hl7.fhir.exceptions.FHIRException;
032import org.hl7.fhir.exceptions.PathEngineException;
033import org.hl7.fhir.instance.model.api.IBase;
034import org.hl7.fhir.r4.context.IWorkerContext;
035import org.hl7.fhir.r4.hapi.ctx.HapiWorkerContext;
036import org.hl7.fhir.r4.model.Base;
037import org.hl7.fhir.r4.model.ExpressionNode;
038import org.hl7.fhir.r4.model.IdType;
039import org.hl7.fhir.r4.model.Resource;
040import org.hl7.fhir.r4.model.ResourceType;
041import org.hl7.fhir.r4.model.TypeDetails;
042import org.hl7.fhir.r4.model.ValueSet;
043import org.hl7.fhir.r4.utils.FHIRPathEngine;
044import org.hl7.fhir.r4.utils.FHIRPathUtilityClasses.FunctionDetails;
045
046import java.util.Collections;
047import java.util.HashMap;
048import java.util.List;
049import java.util.Map;
050import java.util.concurrent.TimeUnit;
051
052import static org.apache.commons.lang3.StringUtils.isNotBlank;
053
054public class SearchParamExtractorR4 extends BaseSearchParamExtractor implements ISearchParamExtractor {
055
056        private Cache<String, ExpressionNode> myParsedFhirPathCache;
057        private FHIRPathEngine myFhirPathEngine;
058
059        /**
060         * Constructor
061         */
062        public SearchParamExtractorR4() {
063                super();
064        }
065
066        // This constructor is used by tests
067        @VisibleForTesting
068        public SearchParamExtractorR4(
069                        StorageSettings theStorageSettings,
070                        PartitionSettings thePartitionSettings,
071                        FhirContext theCtx,
072                        ISearchParamRegistry theSearchParamRegistry) {
073                super(theStorageSettings, thePartitionSettings, theCtx, theSearchParamRegistry);
074                initFhirPath();
075                start();
076        }
077
078        @Override
079        public IValueExtractor getPathValueExtractor(IBase theResource, String theSinglePath) {
080                return () -> {
081                        ExpressionNode parsed = myParsedFhirPathCache.get(theSinglePath, path -> myFhirPathEngine.parse(path));
082                        return myFhirPathEngine.evaluate(
083                                        theResource, (Base) theResource, (Base) theResource, (Base) theResource, parsed);
084                };
085        }
086
087        @Override
088        @PostConstruct
089        public void start() {
090                super.start();
091                if (myFhirPathEngine == null) {
092                        initFhirPath();
093                }
094        }
095
096        public void initFhirPath() {
097                IWorkerContext worker = new HapiWorkerContext(getContext(), getContext().getValidationSupport());
098                myFhirPathEngine = new FHIRPathEngine(worker);
099                myFhirPathEngine.setHostServices(new SearchParamExtractorR4HostServices());
100
101                myParsedFhirPathCache = CacheFactory.build(TimeUnit.MINUTES.toMillis(10));
102        }
103
104        private class SearchParamExtractorR4HostServices implements FHIRPathEngine.IEvaluationContext {
105
106                private final Map<String, Base> myResourceTypeToStub = Collections.synchronizedMap(new HashMap<>());
107
108                @Override
109                public List<Base> resolveConstant(Object appContext, String name, boolean beforeContext)
110                                throws PathEngineException {
111                        return Collections.emptyList();
112                }
113
114                @Override
115                public TypeDetails resolveConstantType(Object appContext, String name) throws PathEngineException {
116                        return null;
117                }
118
119                @Override
120                public boolean log(String argument, List<Base> focus) {
121                        return false;
122                }
123
124                @Override
125                public FunctionDetails resolveFunction(String functionName) {
126                        return null;
127                }
128
129                @Override
130                public TypeDetails checkFunction(Object appContext, String functionName, List<TypeDetails> parameters)
131                                throws PathEngineException {
132                        return null;
133                }
134
135                @Override
136                public List<Base> executeFunction(
137                                Object appContext, List<Base> focus, String functionName, List<List<Base>> parameters) {
138                        return null;
139                }
140
141                @Override
142                public Base resolveReference(Object theAppContext, String theUrl, Base theRefContext) throws FHIRException {
143                        Base retVal = (Base) BundleUtil.getReferenceInBundle(getContext(), theUrl, theAppContext);
144                        if (retVal != null) {
145                                return retVal;
146                        }
147
148                        /*
149                         * When we're doing resolution within the SearchParamExtractor, if we want
150                         * to do a resolve() it's just to check the type, so there is no point
151                         * going through the heavyweight test. We can just return a stub and
152                         * that's good enough since we're just doing something like
153                         *    Encounter.patient.where(resolve() is Patient)
154                         */
155                        IdType url = new IdType(theUrl);
156                        if (isNotBlank(url.getResourceType())) {
157
158                                retVal = myResourceTypeToStub.get(url.getResourceType());
159                                if (retVal != null) {
160                                        return retVal;
161                                }
162
163                                ResourceType resourceType = ResourceType.fromCode(url.getResourceType());
164                                if (resourceType != null) {
165                                        retVal = new Resource() {
166                                                private static final long serialVersionUID = -5303169871827706447L;
167
168                                                @Override
169                                                public Resource copy() {
170                                                        return this;
171                                                }
172
173                                                @Override
174                                                public ResourceType getResourceType() {
175                                                        return resourceType;
176                                                }
177
178                                                @Override
179                                                public String fhirType() {
180                                                        return url.getResourceType();
181                                                }
182                                        };
183                                        myResourceTypeToStub.put(url.getResourceType(), retVal);
184                                }
185                        }
186                        return retVal;
187                }
188
189                @Override
190                public boolean conformsToProfile(Object appContext, Base item, String url) throws FHIRException {
191                        return false;
192                }
193
194                @Override
195                public ValueSet resolveValueSet(Object appContext, String url) {
196                        return null;
197                }
198        }
199}