8bca04ff6ae0a5e11b254140c342dfa90c41d54d
[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         private final String alias;
33         
34         public UpdateByQueryRequest(String alias, String dataType) {
35                 this(alias, dataType, false);
36         }
37
38         public UpdateByQueryRequest(String alias, String dataType, boolean refresh) {
39                 super("POST", String.format("/%s/%s/_update_by_query", alias, dataType), refresh);
40                 this.alias = alias;
41                 this.params = null;
42         }
43
44         public void source(String esId, JSONObject map) {
45                 this.source(map, QueryBuilders.matchQuery("_id", esId));
46         }
47
48         public void source(JSONObject map, QueryBuilder query) {
49                 JSONObject outer = new JSONObject();
50                 outer.put("query", query.getInner());
51                 JSONObject script = new JSONObject();
52                 script.put("lang", "painless");
53                 script.put("inline", this.createInline(map));
54                 if(this.params!=null) {
55                         script.put("params",this.params);
56                 }
57                 outer.put("script", script);
58                 super.setQuery(outer.toString());
59         }
60
61         private String createInline(JSONObject map) {
62                 String s = "", k, pkey;
63                 int i = 1;
64                 Object value;
65                 for (Object key : map.keySet()) {
66                         k = String.valueOf(key);
67                         value = map.get(k);
68                         if (value instanceof JSONObject || value instanceof JSONArray) {
69                                 pkey = String.format("p%d", i++);
70                                 if (value instanceof JSONObject) {
71                                         this.withParam(pkey, (JSONObject) value);
72                                 } else {
73                                         this.withParam(pkey, (JSONArray) value);
74                                 }
75
76                                 s += String.format("ctx._source['%s']=%s;", key, "params." + pkey);
77                         } else {
78                                 s += String.format("ctx._source['%s']=%s;", key, escpaped(value));
79                         }
80                 }
81                 return s;
82         }
83
84         private UpdateByQueryRequest withParam(String key, JSONArray p) {
85                 if (this.params == null) {
86                         this.params = new JSONObject();
87                 }
88                 this.params.put(key, p);
89                 return this;
90         }
91
92         private UpdateByQueryRequest withParam(String key, JSONObject p) {
93                 if (this.params == null) {
94                         this.params = new JSONObject();
95                 }
96                 this.params.put(key, p);
97                 return this;
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         protected String getAlias() {
113                 return this.alias;
114         }
115
116 }