108888ad05d3acc73abc4d2136de11a213f63461
[ccsdk/features.git] /
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 InsertQuery<T extends DataObject> implements SqlQuery {
37
38     private final Logger LOG = LoggerFactory.getLogger(InsertQuery.class);
39
40     protected 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 InsertQuery(Entity e, T object, String controllerId) {
47         this.entity = e;
48         this.controllerId = controllerId;
49         this.object = object;
50         this.ignoreNull = true;
51         this.id = null;
52
53     }
54
55     @Override
56     public String toSql() {
57         try {
58             return this.toSqlWithError();
59         } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
60                 | JsonProcessingException e) {
61             LOG.warn("unable to create insert statement for table {} from object {}: ", this.entity, this.object, e);
62         }
63         return null;
64     }
65
66     protected String toSqlWithError() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
67             JsonProcessingException {
68         Class<?> cls = this.object.getClass();
69         List<DBKeyValuePair<String>> kvps = new ArrayList<>();
70         List<String> cols = new ArrayList<>();
71         List<String> args = new ArrayList<>();
72         Object value;
73         String col;
74         StringBuilder sb = new StringBuilder();
75         sb.append("INSERT INTO `" + entity.getName() + "` (");
76         for (Method m : SqlDBMapper.getFilteredMethods(cls, true)) {
77
78             m.setAccessible(true);
79             value = m.invoke(this.object);
80             col = SqlDBMapper.getColumnName(m);
81             if (col.equals("id") && this.id != null) {
82                 value = this.id;
83             }
84             if (ignoreNull && value == null) {
85                 continue;
86             }
87             DBKeyValuePair<String> kvp = SqlDBMapper.getEscapedKeyValue(m,col, value);
88             cols.add(kvp.getKey());
89             args.add(kvp.getValue());
90             kvps.add(kvp);
91         }
92         if (this.id != null && !cols.contains("`id`")) {
93             cols.add("`id`");
94             args.add("'"+this.id+"'");
95         }
96         args.add("'"+this.controllerId+"'");
97         sb.append( String.join(",", cols));
98         sb.append(",`" + SqlDBMapper.ODLID_DBCOL + "`) VALUES (");
99         sb.append( String.join(",", args)+" )");
100         this.appendAdditionalToQuery(sb,kvps);
101         return sb.toString();
102     }
103
104     protected void appendAdditionalToQuery(StringBuilder sb, List<DBKeyValuePair<String>> keyValues) {
105
106     }
107
108     public void setId(String id) {
109         this.id = id;
110     }
111 }