001package ca.uhn.fhir.jpa.searchparam.extractor;
002
003/*
004 * #%L
005 * HAPI FHIR Search Parameters
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.jpa.model.config.PartitionSettings;
025import ca.uhn.fhir.jpa.model.entity.ModelConfig;
026import ca.uhn.fhir.rest.server.util.ISearchParamRegistry;
027import ca.uhn.fhir.model.dstu2.composite.ContactPointDt;
028import ca.uhn.fhir.util.FhirTerser;
029import org.hl7.fhir.instance.model.api.IBase;
030import org.hl7.fhir.instance.model.api.IBaseDatatype;
031import org.hl7.fhir.instance.model.api.IBaseExtension;
032import org.hl7.fhir.instance.model.api.IBaseResource;
033
034import java.util.ArrayList;
035import java.util.List;
036
037public class SearchParamExtractorDstu2 extends BaseSearchParamExtractor implements ISearchParamExtractor {
038
039        public SearchParamExtractorDstu2() {
040        }
041
042        /**
043         * Constructor for unit tests
044         */
045        SearchParamExtractorDstu2(ModelConfig theModelConfig, PartitionSettings thePartitionSettings, FhirContext theCtx, ISearchParamRegistry theSearchParamRegistry) {
046                super(theModelConfig, thePartitionSettings, theCtx, theSearchParamRegistry);
047                start();
048        }
049
050        @Override
051        public IValueExtractor getPathValueExtractor(IBaseResource theResource, String theSinglePath) {
052                return () -> {
053                        String path = theSinglePath;
054
055                        String needContactPointSystem = null;
056                        if (path.endsWith("(system=phone)")) {
057                                path = path.substring(0, path.length() - "(system=phone)".length());
058                                needContactPointSystem = "phone";
059                        }
060                        if (path.endsWith("(system=email)")) {
061                                path = path.substring(0, path.length() - "(system=email)".length());
062                                needContactPointSystem = "email";
063                        }
064
065                        List<IBase> values = new ArrayList<>();
066                        FhirTerser t = getContext().newTerser();
067                        List<IBase> allValues = t.getValues(theResource, path);
068                        for (IBase next : allValues) {
069                                if (next instanceof IBaseExtension) {
070                                        IBaseDatatype value = ((IBaseExtension) next).getValue();
071                                        if (value != null) {
072                                                values.add(value);
073                                        }
074                                } else {
075
076                                        if (needContactPointSystem != null) {
077                                                if (next instanceof ContactPointDt) {
078                                                        if (!needContactPointSystem.equals(((ContactPointDt) next).getSystem())) {
079                                                                continue;
080                                                        }
081                                                }
082                                        }
083
084                                        values.add(next);
085                                }
086                        }
087                        return values;
088                };
089        }
090
091}