001/*
002 * #%L
003 * HAPI FHIR Search Parameters
004 * %%
005 * Copyright (C) 2014 - 2023 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 com.google.common.annotations.VisibleForTesting;
027import org.hl7.fhir.dstu3.context.IWorkerContext;
028import org.hl7.fhir.dstu3.hapi.ctx.HapiWorkerContext;
029import org.hl7.fhir.dstu3.model.Base;
030import org.hl7.fhir.dstu3.utils.FHIRPathEngine;
031import org.hl7.fhir.instance.model.api.IBase;
032
033import javax.annotation.PostConstruct;
034import java.util.ArrayList;
035import java.util.List;
036
037public class SearchParamExtractorDstu3 extends BaseSearchParamExtractor implements ISearchParamExtractor {
038
039        private FHIRPathEngine myFhirPathEngine;
040
041        /**
042         * Constructor
043         */
044        public SearchParamExtractorDstu3() {
045                super();
046        }
047
048        // This constructor is used by tests
049        @VisibleForTesting
050        public SearchParamExtractorDstu3(StorageSettings theStorageSettings, PartitionSettings thePartitionSettings, FhirContext theCtx, ISearchParamRegistry theSearchParamRegistry) {
051                super(theStorageSettings, thePartitionSettings, theCtx, theSearchParamRegistry);
052                initFhirPathEngine();
053                start();
054        }
055
056        @Override
057        public IValueExtractor getPathValueExtractor(IBase theResource, String theSinglePath) {
058                return () -> {
059                        List<IBase> values = new ArrayList<>();
060                        List<Base> allValues = myFhirPathEngine.evaluate((Base) theResource, theSinglePath);
061                        if (allValues.isEmpty() == false) {
062                                values.addAll(allValues);
063                        }
064
065                        return values;
066                };
067        }
068
069
070        @Override
071        @PostConstruct
072        public void start() {
073                super.start();
074                if (myFhirPathEngine == null) {
075                        initFhirPathEngine();
076                }
077        }
078
079        public void initFhirPathEngine() {
080                IWorkerContext worker = new HapiWorkerContext(getContext(), getContext().getValidationSupport());
081                myFhirPathEngine = new FHIRPathEngine(worker);
082        }
083
084}