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