1eb6037f4f9bd811af2ad48ea693508e1b0ff08b
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.common.database.requests;
23
24 import org.json.JSONArray;
25 import org.json.JSONObject;
26 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder;
27 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders;
28
29 public class UpdateByQueryRequest extends BaseRequest {
30
31         private JSONObject params;
32
33         public UpdateByQueryRequest(String alias, String dataType) {
34                 super("POST", String.format("/%s/%s/_update_by_query", alias, dataType));
35                 this.params = null;
36         }
37
38         public void source(String esId, JSONObject map) {
39                 this.source(map, QueryBuilders.matchQuery("_id", esId));
40         }
41
42         public void source(JSONObject map, QueryBuilder query) {
43                 JSONObject outer = new JSONObject();
44                 outer.put("query", query.getInner());
45                 JSONObject script = new JSONObject();
46                 script.put("lang", "painless");
47                 script.put("inline", this.createInline(map));
48                 if(this.params!=null) {
49                         script.put("params",this.params);
50                 }
51                 outer.put("script", script);
52                 super.setQuery(outer.toString());
53         }
54
55         private String createInline(JSONObject map) {
56                 String s = "", k, pkey;
57                 int i = 1;
58                 Object value;
59                 for (Object key : map.keySet()) {
60                         k = String.valueOf(key);
61                         value = map.get(k);
62                         if (value instanceof JSONObject || value instanceof JSONArray) {
63                                 pkey = String.format("p%d", i++);
64                                 if (value instanceof JSONObject) {
65                                         this.withParam(pkey, (JSONObject) value);
66                                 } else {
67                                         this.withParam(pkey, (JSONArray) value);
68                                 }
69
70                                 s += String.format("ctx._source['%s']=%s;", key, "params." + pkey);
71                         } else {
72                                 s += String.format("ctx._source['%s']=%s;", key, escpaped(value));
73                         }
74                 }
75                 return s;
76         }
77
78         private UpdateByQueryRequest withParam(String key, JSONArray p) {
79                 if (this.params == null) {
80                         this.params = new JSONObject();
81                 }
82                 this.params.put(key, p);
83                 return this;
84         }
85
86         private UpdateByQueryRequest withParam(String key, JSONObject p) {
87                 if (this.params == null) {
88                         this.params = new JSONObject();
89                 }
90                 this.params.put(key, p);
91                 return this;
92         }
93
94         private String escpaped(Object value) {
95                 String s = "";
96                 if (value instanceof Boolean || value instanceof Integer || value instanceof Long || value instanceof Float
97                                 || value instanceof Double) {
98                         s = String.valueOf(value);
99                 } else {
100                         s = "\"" + String.valueOf(value) + "\"";
101                 }
102                 return s;
103
104         }
105
106 }