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;
29 public class UpdateByQueryRequest extends BaseRequest {
31 private JSONObject params;
33 public UpdateByQueryRequest(String alias, String dataType) {
34 super("POST", String.format("/%s/%s/_update_by_query", alias, dataType));
38 public void source(String esId, JSONObject map) {
39 this.source(map, QueryBuilders.matchQuery("_id", esId));
42 public void source(JSONObject map, QueryBuilder query) {
43 JSONObject outer = new JSONObject();
44 outer.put("query", query.getInner());
45 JSONObject script = new JSONObject();
46 script.put("lang", "painless");
47 script.put("inline", this.createInline(map));
48 if(this.params!=null) {
49 script.put("params",this.params);
51 outer.put("script", script);
52 super.setQuery(outer.toString());
55 private String createInline(JSONObject map) {
56 String s = "", k, pkey;
59 for (Object key : map.keySet()) {
60 k = String.valueOf(key);
62 if (value instanceof JSONObject || value instanceof JSONArray) {
63 pkey = String.format("p%d", i++);
64 if (value instanceof JSONObject) {
65 this.withParam(pkey, (JSONObject) value);
67 this.withParam(pkey, (JSONArray) value);
70 s += String.format("ctx._source['%s']=%s;", key, "params." + pkey);
72 s += String.format("ctx._source['%s']=%s;", key, escpaped(value));
78 private UpdateByQueryRequest withParam(String key, JSONArray p) {
79 if (this.params == null) {
80 this.params = new JSONObject();
82 this.params.put(key, p);
86 private UpdateByQueryRequest withParam(String key, JSONObject p) {
87 if (this.params == null) {
88 this.params = new JSONObject();
90 this.params.put(key, p);
94 private String escpaped(Object value) {
96 if (value instanceof Boolean || value instanceof Integer || value instanceof Long || value instanceof Float
97 || value instanceof Double) {
98 s = String.valueOf(value);
100 s = "\"" + String.valueOf(value) + "\"";