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;
32 private final String alias;
34 public UpdateByQueryRequest(String alias, String dataType) {
35 this(alias, dataType, false);
38 public UpdateByQueryRequest(String alias, String dataType, boolean refresh) {
39 super("POST", String.format("/%s/%s/_update_by_query", alias, dataType), refresh);
44 public void source(String esId, JSONObject map) {
45 this.source(map, QueryBuilders.matchQuery("_id", esId));
48 public void source(JSONObject map, QueryBuilder query) {
49 JSONObject outer = new JSONObject();
50 outer.put("query", query.getInner());
51 JSONObject script = new JSONObject();
52 script.put("lang", "painless");
53 script.put("inline", this.createInline(map));
54 if(this.params!=null) {
55 script.put("params",this.params);
57 outer.put("script", script);
58 super.setQuery(outer.toString());
61 private String createInline(JSONObject map) {
62 String s = "", k, pkey;
65 for (Object key : map.keySet()) {
66 k = String.valueOf(key);
68 if (value instanceof JSONObject || value instanceof JSONArray) {
69 pkey = String.format("p%d", i++);
70 if (value instanceof JSONObject) {
71 this.withParam(pkey, (JSONObject) value);
73 this.withParam(pkey, (JSONArray) value);
76 s += String.format("ctx._source['%s']=%s;", key, "params." + pkey);
78 s += String.format("ctx._source['%s']=%s;", key, escpaped(value));
84 private UpdateByQueryRequest withParam(String key, JSONArray p) {
85 if (this.params == null) {
86 this.params = new JSONObject();
88 this.params.put(key, p);
92 private UpdateByQueryRequest withParam(String key, JSONObject p) {
93 if (this.params == null) {
94 this.params = new JSONObject();
96 this.params.put(key, p);
100 private String escpaped(Object value) {
102 if (value instanceof Boolean || value instanceof Integer || value instanceof Long || value instanceof Float
103 || value instanceof Double) {
104 s = String.valueOf(value);
106 s = "\"" + String.valueOf(value) + "\"";
112 protected String getAlias() {