adab44df961d77f8c3afb9f0d34738559851ab26
[ccsdk/features.git] /
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.common.database.requests;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.json.JSONArray;
24 import org.json.JSONObject;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class UpdateRequest extends BaseRequest {
29
30         private static final Logger LOG = LoggerFactory.getLogger(UpdateRequest.class);
31         private JSONObject params;
32
33         public UpdateRequest(String alias, String dataType, String esId) {
34                 super("POST", String.format("/%s/%s/%s/_update", alias, dataType, BaseRequest.urlEncodeValue(esId)));
35                 this.params = null;
36         }
37
38         private UpdateRequest withParam(String key, JSONObject p) {
39                 if (this.params == null) {
40                         this.params = new JSONObject();
41                 }
42                 this.params.put(key, p);
43                 return this;
44         }
45
46         private UpdateRequest withParam(String key, JSONArray p) {
47                 if (this.params == null) {
48                         this.params = new JSONObject();
49                 }
50                 this.params.put(key, p);
51                 return this;
52         }
53         public void source(JSONObject map) {
54                 this.source(map,null);
55         }
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);
63                 }
64                 outer.put("script", script);
65                 outer.put("upsert", map);
66                 LOG.debug("update payload: " + outer.toString());
67                 super.setQuery(outer.toString());
68         }
69
70         private String createInline(JSONObject map, List<String> onlyForInsert) {
71                 if(onlyForInsert==null) {
72                         onlyForInsert = new ArrayList<String>();
73                 }
74                 String s = "",k="";
75                 Object value;
76                 String pkey;
77                 int i = 0;
78                 for (Object key : map.keySet()) {
79                         k=String.valueOf(key);
80                         if(onlyForInsert.contains(k)) {
81                                 continue;
82                         }
83                         value = map.get(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);
88                                 } else {
89                                         this.withParam(pkey, (JSONArray) value);
90                                 }
91
92                                 s += String.format("ctx._source['%s']=%s;", key, "params."+pkey);
93                         } else {
94                                 s += String.format("ctx._source['%s']=%s;", key, escpaped(value));
95                         }
96                 }
97                 return s;
98         }
99
100         private String escpaped(Object value) {
101                 String s = "";
102                 if (value instanceof Boolean || value instanceof Integer || value instanceof Long || value instanceof Float
103                                 || value instanceof Double) {
104                         s = String.valueOf(value);
105                 } else {
106                         s = "\"" + String.valueOf(value) + "\"";
107                 }
108                 return s;
109
110         }
111
112 }