2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.common.database.requests;
24 import org.json.JSONArray;
25 import org.json.JSONObject;
26 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder;
27 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders;
30 public class UpdateByQueryRequest extends BaseRequest {
32 private JSONObject params;
33 private final String alias;
35 public UpdateByQueryRequest(String alias, String dataType) {
36 this(alias, dataType, false);
39 public UpdateByQueryRequest(String alias, String dataType, boolean refresh) {
40 super("POST", String.format("/%s/%s/_update_by_query", alias, dataType), refresh);
45 public void source(String esId, JSONObject map) {
46 this.source(map, QueryBuilders.matchQuery("_id", esId));
49 public void source(JSONObject map, QueryBuilder query) {
50 JSONObject outer = new JSONObject();
51 outer.put("query", query.getInner());
52 JSONObject script = new JSONObject();
53 script.put("lang", "painless");
54 script.put("inline", this.createInline(map));
55 if (this.params != null) {
56 script.put("params", this.params);
58 outer.put("script", script);
59 super.setQuery(outer.toString());
62 private String createInline(JSONObject map) {
63 String s = "", k, pkey;
66 for (Object key : map.keySet()) {
67 k = String.valueOf(key);
69 if (value instanceof JSONObject || value instanceof JSONArray) {
70 pkey = String.format("p%d", i++);
71 if (value instanceof JSONObject) {
72 this.withParam(pkey, (JSONObject) value);
74 this.withParam(pkey, (JSONArray) value);
77 s += String.format("ctx._source['%s']=%s;", key, "params." + pkey);
79 s += String.format("ctx._source['%s']=%s;", key, escpaped(value));
85 private UpdateByQueryRequest withParam(String key, JSONArray p) {
86 if (this.params == null) {
87 this.params = new JSONObject();
89 this.params.put(key, p);
93 private UpdateByQueryRequest withParam(String key, JSONObject p) {
94 if (this.params == null) {
95 this.params = new JSONObject();
97 this.params.put(key, p);
101 private String escpaped(Object value) {
103 if (value instanceof Boolean || value instanceof Integer || value instanceof Long || value instanceof Float
104 || value instanceof Double) {
105 s = String.valueOf(value);
107 s = "\"" + String.valueOf(value) + "\"";
113 protected String getAlias() {