b2524f7db6017699df2844aa864d40a352d0f273
[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 java.util.ArrayList;
25 import java.util.List;
26
27 import org.json.JSONArray;
28 import org.json.JSONObject;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 @Deprecated
33 public class UpdateRequest extends BaseRequest {
34
35     private static final Logger LOG = LoggerFactory.getLogger(UpdateRequest.class);
36     private JSONObject params;
37     private String alias;
38     private String esId;
39     private int retries;
40
41     public UpdateRequest(String alias, String dataType, String esId) {
42         this(alias, dataType, esId, BaseRequest.DEFAULT_RETRIES);
43     }
44
45     public UpdateRequest(String alias, String dataType, String esId, boolean refresh) {
46         this(alias, dataType, esId, BaseRequest.DEFAULT_RETRIES, refresh);
47     }
48
49     public UpdateRequest(String alias, String dataType, String esId, int retries) {
50         this(alias, dataType, esId, retries, false);
51     }
52
53     public UpdateRequest(String alias, String dataType, String esId, int retries, boolean refresh) {
54         this(String.format("/%s/%s/%s/_update", alias, dataType, BaseRequest.urlEncodeValue(esId)), refresh);
55         this.alias = alias;
56         this.esId = esId;
57         this.retries = retries;
58     }
59
60     public UpdateRequest(String uri, boolean refresh) {
61         super("POST", uri, refresh, BaseRequest.DEFAULT_RETRIES);
62         this.params = null;
63         this.retries = 1;
64
65     }
66
67
68
69     private UpdateRequest withParam(String key, JSONObject p) {
70         if (this.params == null) {
71             this.params = new JSONObject();
72         }
73         this.params.put(key, p);
74         return this;
75     }
76
77     private UpdateRequest withParam(String key, JSONArray p) {
78         if (this.params == null) {
79             this.params = new JSONObject();
80         }
81         this.params.put(key, p);
82         return this;
83     }
84
85     public void source(JSONObject map) {
86         this.source(map, null);
87     }
88
89     public void source(JSONObject map, List<String> onlyForInsert) {
90         JSONObject outer = new JSONObject();
91         JSONObject script = new JSONObject();
92         script.put("lang", "painless");
93         script.put("source", this.createInline(map, onlyForInsert));
94         if (this.params != null) {
95             script.put("params", this.params);
96         }
97         outer.put("script", script);
98         outer.put("upsert", map);
99         LOG.debug("update payload: " + outer.toString());
100         super.setQuery(outer.toString());
101     }
102
103     private String createInline(JSONObject map, List<String> onlyForInsert) {
104         if (onlyForInsert == null) {
105             onlyForInsert = new ArrayList<String>();
106         }
107         String s = "", k = "";
108         Object value;
109         String pkey;
110         int i = 0;
111         for (Object key : map.keySet()) {
112             k = String.valueOf(key);
113             if (onlyForInsert.contains(k)) {
114                 continue;
115             }
116             value = map.get(k);
117             if (value instanceof JSONObject || value instanceof JSONArray) {
118                 pkey = String.format("p%d", i++);
119                 if (value instanceof JSONObject) {
120                     this.withParam(pkey, (JSONObject) value);
121                 } else {
122                     this.withParam(pkey, (JSONArray) value);
123                 }
124
125                 s += String.format("ctx._source['%s']=%s;", key, "params." + pkey);
126             } else {
127                 s += String.format("ctx._source['%s']=%s;", key, escpaped(value));
128             }
129         }
130         return s;
131     }
132
133     private String escpaped(Object value) {
134         String s = "";
135         if (value instanceof Boolean || value instanceof Integer || value instanceof Long || value instanceof Float
136                 || value instanceof Double) {
137             s = String.valueOf(value);
138         } else {
139             s = "\"" + String.valueOf(value) + "\"";
140         }
141         return s;
142
143     }
144
145     protected String getAlias() {
146         return this.alias;
147     }
148
149     protected String getEsId() {
150         return this.esId;
151     }
152
153     protected int getRetries() {
154         return this.retries;
155     }
156 }