b1469a57e6ec5e3ea77b9aa4a0771f95d13cf58f
[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 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;
24
25 public class UpdateByQueryRequest extends BaseRequest {
26
27         private JSONObject params;
28
29         public UpdateByQueryRequest(String alias, String dataType) {
30                 super("POST", String.format("/%s/%s/_update_by_query", alias, dataType));
31                 this.params = null;
32         }
33
34         public void source(String esId, JSONObject map) {
35                 this.source(map, QueryBuilders.matchQuery("_id", esId));
36         }
37
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);
46                 }
47                 outer.put("script", script);
48                 super.setQuery(outer.toString());
49         }
50
51         private String createInline(JSONObject map) {
52                 String s = "", k, pkey;
53                 int i = 1;
54                 Object value;
55                 for (Object key : map.keySet()) {
56                         k = String.valueOf(key);
57                         value = map.get(k);
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);
62                                 } else {
63                                         this.withParam(pkey, (JSONArray) value);
64                                 }
65
66                                 s += String.format("ctx._source['%s']=%s;", key, "params." + pkey);
67                         } else {
68                                 s += String.format("ctx._source['%s']=%s;", key, escpaped(value));
69                         }
70                 }
71                 return s;
72         }
73
74         private UpdateByQueryRequest withParam(String key, JSONArray p) {
75                 if (this.params == null) {
76                         this.params = new JSONObject();
77                 }
78                 this.params.put(key, p);
79                 return this;
80         }
81
82         private UpdateByQueryRequest withParam(String key, JSONObject p) {
83                 if (this.params == null) {
84                         this.params = new JSONObject();
85                 }
86                 this.params.put(key, p);
87                 return this;
88         }
89
90         private String escpaped(Object value) {
91                 String s = "";
92                 if (value instanceof Boolean || value instanceof Integer || value instanceof Long || value instanceof Float
93                                 || value instanceof Double) {
94                         s = String.valueOf(value);
95                 } else {
96                         s = "\"" + String.valueOf(value) + "\"";
97                 }
98                 return s;
99
100         }
101
102 }