001package ca.uhn.fhir.jpa.search;
002
003/*-
004 * #%L
005 * HAPI FHIR Storage api
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 com.google.gson.JsonArray;
024import com.google.gson.JsonObject;
025
026/**
027 * The whole purpose of his class is to ease construction of a non-trivial gson.JsonObject,
028 * which can't be done the easy way in this case (using a JSON string), because there are
029 * valid regex strings which break gson, as this: ".*\\^Donor$"
030 */
031public class ElasticsearchNestedQueryBuilderUtil {
032
033        private final String myNestedObjName;
034        private final String myNestedKeyPropName;
035        private final String myNestedKeyPropValue;
036        private final String myNestedValuePropName;
037        private final String myNestedValuePropValue;
038
039        private final String myNestedPropertyKeyPath;
040        private final String myNestedPropertyValuePath;
041
042        private JsonObject builtJsonObj = new JsonObject();
043
044        public ElasticsearchNestedQueryBuilderUtil(String theNestedObjName, String theNestedKeyPropName,
045                        String theNestedKeyPropValue, String theNestedValuePropName, String theNestedValuePropValue) {
046
047                myNestedObjName = theNestedObjName ;
048                myNestedKeyPropName = theNestedKeyPropName;
049                myNestedKeyPropValue = theNestedKeyPropValue;
050                myNestedValuePropName = theNestedValuePropName;
051                myNestedValuePropValue = theNestedValuePropValue;
052
053                myNestedPropertyKeyPath = myNestedObjName + "." + myNestedKeyPropName;
054                myNestedPropertyValuePath = myNestedObjName + "." + myNestedValuePropName;
055
056                buildJsonObj();
057        }
058
059
060        private void buildJsonObj() {
061                JsonObject matchPropJO = new JsonObject();
062                matchPropJO.addProperty(myNestedObjName + "." + myNestedKeyPropName, myNestedKeyPropValue);
063                JsonObject matchJO = new JsonObject();
064                matchJO.add("match", matchPropJO);
065
066                JsonObject regexpPropJO = new JsonObject();
067                regexpPropJO.addProperty(myNestedObjName + "." + myNestedValuePropName, myNestedValuePropValue);
068                JsonObject regexpJO = new JsonObject();
069                regexpJO.add("regexp", regexpPropJO);
070
071                JsonArray mustPropJA = new JsonArray();
072                mustPropJA.add(matchJO);
073                mustPropJA.add(regexpJO);
074
075                JsonObject mustPropJO = new JsonObject();
076                mustPropJO.add("must", mustPropJA);
077
078                JsonObject boolJO = new JsonObject();
079                boolJO.add("bool", mustPropJO);
080
081                JsonObject nestedJO = new JsonObject();
082                nestedJO.addProperty("path", myNestedObjName);
083                nestedJO.add("query", boolJO);
084
085                builtJsonObj.add("nested", nestedJO);
086        }
087
088        public JsonObject toGson() { return builtJsonObj; }
089
090        public String getNestedPropertyKeyPath() { return myNestedPropertyKeyPath; }
091
092        public String getNestedPropertyValuePath() { return myNestedPropertyValuePath; }
093
094}