b47b03042f122ba4556812c793c8284de1b0a197
[ccsdk/features.git] / sdnr / wt / data-provider / dblib / src / main / java / org / onap / ccsdk / features / sdnr / wt / dataprovider / database / sqldb / query / UpdateQuery.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2021 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.dataprovider.database.sqldb.query;
23
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.util.ArrayList;
28 import java.util.List;
29 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBMapper;
30 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.filters.DBKeyValuePair;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class UpdateQuery<T extends DataObject> implements SqlQuery {
37
38     private final Logger LOG = LoggerFactory.getLogger(UpdateQuery.class);
39
40     private final Entity entity;
41     private final String controllerId;
42     private final T object;
43     private final boolean ignoreNull;
44     private String id;
45
46     public UpdateQuery(Entity e, T object) {
47         this(e, object, null);
48     }
49
50     public UpdateQuery(Entity e, T object, String controllerId) {
51         this.entity = e;
52         this.controllerId = controllerId;
53         this.object = object;
54         this.ignoreNull = true;
55         this.id = null;
56     }
57
58     @Override
59     public String toSql() {
60         try {
61             return this.toSqlWithError();
62         } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
63                 | JsonProcessingException e) {
64             LOG.warn("unable to create insert statement for table {} from object {}: ", this.entity, this.object, e);
65         }
66         return null;
67     }
68
69     private String toSqlWithError() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
70             JsonProcessingException {
71         Class<?> cls = this.object.getClass();
72         Object value;
73         String col;
74         StringBuilder sb = new StringBuilder();
75         List<String> args = new ArrayList<>();
76         sb.append("UPDATE `" + entity.getName() + "` SET ");
77         List<Method> methods = SqlDBMapper.getFilteredMethods(cls, true);
78         Method m;
79         for (int i = 0; i < methods.size(); i++) {
80             m = methods.get(i);
81             m.setAccessible(true);
82             value = m.invoke(this.object);
83             col = SqlDBMapper.getColumnName(m);
84             if (col.equals("id")) {
85                 if (this.id == null) {
86                     this.id = String.valueOf(value);
87                 }
88                 continue;
89             }
90             if (ignoreNull && value == null) {
91                 continue;
92             }
93             DBKeyValuePair<String> kvp = SqlDBMapper.getEscapedKeyValue(m, col, value);
94             args.add(String.format("%s=%s", kvp.getKey(), kvp.getValue()));
95         }
96         sb.append(String.join(",", args));
97         sb.append(String.format(" WHERE `id`='%s'", this.id));
98         if (this.controllerId != null) {
99             sb.append(String.format(" AND `%s`='%s'", SqlDBMapper.ODLID_DBCOL, this.controllerId));
100         }
101
102         return sb.toString();
103     }
104
105     public void setId(String id) {
106         this.id = id;
107     }
108
109
110
111 }