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 java.util.ArrayList;
21 import java.util.List;
23 import org.json.JSONArray;
24 import org.json.JSONObject;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class UpdateRequest extends BaseRequest {
30 private static final Logger LOG = LoggerFactory.getLogger(UpdateRequest.class);
31 private JSONObject params;
33 public UpdateRequest(String alias, String dataType, String esId) {
34 super("POST", String.format("/%s/%s/%s/_update", alias, dataType, BaseRequest.urlEncodeValue(esId)));
38 private UpdateRequest withParam(String key, JSONObject p) {
39 if (this.params == null) {
40 this.params = new JSONObject();
42 this.params.put(key, p);
46 private UpdateRequest withParam(String key, JSONArray p) {
47 if (this.params == null) {
48 this.params = new JSONObject();
50 this.params.put(key, p);
53 public void source(JSONObject map) {
54 this.source(map,null);
56 public void source(JSONObject map, List<String> onlyForInsert) {
57 JSONObject outer = new JSONObject();
58 JSONObject script = new JSONObject();
59 script.put("lang", "painless");
60 script.put("source", this.createInline(map,onlyForInsert));
61 if(this.params!=null) {
62 script.put("params",this.params);
64 outer.put("script", script);
65 outer.put("upsert", map);
66 LOG.debug("update payload: " + outer.toString());
67 super.setQuery(outer.toString());
70 private String createInline(JSONObject map, List<String> onlyForInsert) {
71 if(onlyForInsert==null) {
72 onlyForInsert = new ArrayList<String>();
78 for (Object key : map.keySet()) {
79 k=String.valueOf(key);
80 if(onlyForInsert.contains(k)) {
84 if (value instanceof JSONObject || value instanceof JSONArray) {
85 pkey = String.format("p%d", i++);
86 if (value instanceof JSONObject) {
87 this.withParam(pkey, (JSONObject) value);
89 this.withParam(pkey, (JSONArray) value);
92 s += String.format("ctx._source['%s']=%s;", key, "params."+pkey);
94 s += String.format("ctx._source['%s']=%s;", key, escpaped(value));
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) + "\"";