1 /*******************************************************************************
2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.common.database.requests;
20 import org.json.JSONArray;
21 import org.json.JSONObject;
22 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder;
23 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders;
25 public class UpdateByQueryRequest extends BaseRequest {
27 private JSONObject params;
29 public UpdateByQueryRequest(String alias, String dataType) {
30 super("POST", String.format("/%s/%s/_update_by_query", alias, dataType));
34 public void source(String esId, JSONObject map) {
35 this.source(map, QueryBuilders.matchQuery("_id", esId));
38 public void source(JSONObject map, QueryBuilder query) {
39 JSONObject outer = new JSONObject();
40 outer.put("query", query.getInner());
41 JSONObject script = new JSONObject();
42 script.put("lang", "painless");
43 script.put("inline", this.createInline(map));
44 if(this.params!=null) {
45 script.put("params",this.params);
47 outer.put("script", script);
48 super.setQuery(outer.toString());
51 private String createInline(JSONObject map) {
52 String s = "", k, pkey;
55 for (Object key : map.keySet()) {
56 k = String.valueOf(key);
58 if (value instanceof JSONObject || value instanceof JSONArray) {
59 pkey = String.format("p%d", i++);
60 if (value instanceof JSONObject) {
61 this.withParam(pkey, (JSONObject) value);
63 this.withParam(pkey, (JSONArray) value);
66 s += String.format("ctx._source['%s']=%s;", key, "params." + pkey);
68 s += String.format("ctx._source['%s']=%s;", key, escpaped(value));
74 private UpdateByQueryRequest withParam(String key, JSONArray p) {
75 if (this.params == null) {
76 this.params = new JSONObject();
78 this.params.put(key, p);
82 private UpdateByQueryRequest withParam(String key, JSONObject p) {
83 if (this.params == null) {
84 this.params = new JSONObject();
86 this.params.put(key, p);
90 private String escpaped(Object value) {
92 if (value instanceof Boolean || value instanceof Integer || value instanceof Long || value instanceof Float
93 || value instanceof Double) {
94 s = String.valueOf(value);
96 s = "\"" + String.valueOf(value) + "\"";