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 java.util.ArrayList;
25 import java.util.List;
27 import org.json.JSONArray;
28 import org.json.JSONObject;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class UpdateRequest extends BaseRequest {
34 private static final Logger LOG = LoggerFactory.getLogger(UpdateRequest.class);
35 private JSONObject params;
40 public UpdateRequest(String alias, String dataType, String esId) {
41 this(alias, dataType, esId, BaseRequest.DEFAULT_RETRIES);
43 public UpdateRequest(String alias, String dataType, String esId, boolean refresh) {
44 this(alias, dataType, esId, BaseRequest.DEFAULT_RETRIES, refresh);
46 public UpdateRequest(String alias, String dataType, String esId, int retries) {
47 this(alias, dataType, esId, retries, false);
50 public UpdateRequest(String alias, String dataType, String esId, int retries, boolean refresh) {
51 this(String.format("/%s/%s/%s/_update", alias, dataType, BaseRequest.urlEncodeValue(esId)), refresh);
54 this.retries = retries;
57 public UpdateRequest(String uri, boolean refresh) {
58 super("POST", uri, refresh, BaseRequest.DEFAULT_RETRIES);
66 private UpdateRequest withParam(String key, JSONObject p) {
67 if (this.params == null) {
68 this.params = new JSONObject();
70 this.params.put(key, p);
74 private UpdateRequest withParam(String key, JSONArray p) {
75 if (this.params == null) {
76 this.params = new JSONObject();
78 this.params.put(key, p);
82 public void source(JSONObject map) {
83 this.source(map, null);
86 public void source(JSONObject map, List<String> onlyForInsert) {
87 JSONObject outer = new JSONObject();
88 JSONObject script = new JSONObject();
89 script.put("lang", "painless");
90 script.put("source", this.createInline(map, onlyForInsert));
91 if (this.params != null) {
92 script.put("params", this.params);
94 outer.put("script", script);
95 outer.put("upsert", map);
96 LOG.debug("update payload: " + outer.toString());
97 super.setQuery(outer.toString());
100 private String createInline(JSONObject map, List<String> onlyForInsert) {
101 if (onlyForInsert == null) {
102 onlyForInsert = new ArrayList<String>();
104 String s = "", k = "";
108 for (Object key : map.keySet()) {
109 k = String.valueOf(key);
110 if (onlyForInsert.contains(k)) {
114 if (value instanceof JSONObject || value instanceof JSONArray) {
115 pkey = String.format("p%d", i++);
116 if (value instanceof JSONObject) {
117 this.withParam(pkey, (JSONObject) value);
119 this.withParam(pkey, (JSONArray) value);
122 s += String.format("ctx._source['%s']=%s;", key, "params." + pkey);
124 s += String.format("ctx._source['%s']=%s;", key, escpaped(value));
130 private String escpaped(Object value) {
132 if (value instanceof Boolean || value instanceof Integer || value instanceof Long || value instanceof Float
133 || value instanceof Double) {
134 s = String.valueOf(value);
136 s = "\"" + String.valueOf(value) + "\"";
142 protected String getAlias() {
146 protected String getEsId() {
150 protected int getRetries() {