2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright (C) 2019 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.dmaap.dbcapi.database;
25 import java.lang.reflect.*;
29 import org.onap.dmaap.dbcapi.model.*;
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
35 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
38 public class DBFieldHandler {
39 static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
41 public DBFieldHandler(Class<?> c, String fieldname, int fieldnum) throws Exception {
42 this(c, fieldname, fieldnum, null);
44 public DBFieldHandler(Class<?> c, String fieldname, int fieldnum, SqlOp op) throws Exception {
45 this.fieldnum = fieldnum;
46 StringBuilder sb = new StringBuilder();
47 for (String s: fieldname.split("_")) {
48 sb.append(s.substring(0, 1).toUpperCase()).append(s.substring(1));
50 String camelcase = sb.toString();
52 objget = c.getMethod("is" + camelcase);
53 } catch (Exception e) {
54 errorLogger.warn("No 'is' method for " + c.getName() + " so trying 'get' method");
55 objget = c.getMethod("get" + camelcase);
57 objset = c.getMethod("set" + camelcase, objget.getReturnType());
62 Class<?> x = objget.getReturnType();
64 sqlop = new EnumSql(x);
67 sqlop = sqltypes.get(x.getName());
71 errorLogger.error(DmaapbcLogMessageEnum.DB_NO_FIELD_HANDLER, c.getName(), fieldname, Integer.toString(fieldnum), x.getName());
74 public static interface SqlOp {
75 public Object get(ResultSet rs, int index) throws Exception;
76 public void set(PreparedStatement ps, int index, Object value) throws Exception;
78 private static class AofString implements SqlOp {
79 public Object get(ResultSet rs, int index) throws Exception {
80 String val = rs.getString(index);
84 String[] ret = val.split(",");
85 for (int i = 0; i < ret.length; i++) {
86 ret[i] = funesc(ret[i]);
90 public void set(PreparedStatement ps, int index, Object x) throws Exception {
91 String[] val = (String[])x;
93 ps.setString(index, null);
96 StringBuilder sb = new StringBuilder();
99 sb.append(sep).append(fesc(s));
102 ps.setString(index, sb.toString());
105 private static class EnumSql implements SqlOp {
106 private Class enclass;
107 public EnumSql(Class enclass) {
108 this.enclass = enclass;
110 @SuppressWarnings("unchecked")
111 public Object get(ResultSet rs, int index) throws Exception {
112 String val = rs.getString(index);
116 return(Enum.valueOf(enclass, val));
119 public void set(PreparedStatement ps, int index, Object value) throws Exception {
121 ps.setString(index, null);
123 ps.setString(index, value.toString());
127 private static class SqlDate implements SqlOp {
128 public Object get(ResultSet rs, int index) throws Exception {
129 return(rs.getTimestamp(index));
131 public void set(PreparedStatement ps, int index, Object val) throws Exception {
132 if (val instanceof java.util.Date && !(val instanceof java.sql.Timestamp)) {
133 val = new java.sql.Timestamp(((java.util.Date)val).getTime());
135 ps.setTimestamp(index, (java.sql.Timestamp)val);
138 private static class SqlType implements SqlOp {
139 private Method sqlget;
140 private Method sqlset;
141 private SqlType(String tag) throws Exception {
142 sqlget = ResultSet.class.getMethod("get" + tag, Integer.TYPE);
143 sqlset = PreparedStatement.class.getMethod("set" + tag, Integer.TYPE, sqlget.getReturnType());
144 sqltypes.put(sqlget.getReturnType().getName(), this);
146 public Object get(ResultSet rs, int index) throws Exception {
147 return(sqlget.invoke(rs, index));
149 public void set(PreparedStatement ps, int index, Object val) throws Exception {
151 sqlset.invoke(ps, index, val);
152 } catch (Exception e) {
153 errorLogger.error(DmaapbcLogMessageEnum.DB_FIELD_INIT_ERROR, Integer.toString(index), val.toString(), ps.toString());
158 private static Map<String, SqlOp> sqltypes;
160 sqltypes = new HashMap<>();
161 sqltypes.put("[Ljava.lang.String;", new AofString());
162 sqltypes.put("java.util.Date", new SqlDate());
164 new SqlType("Boolean");
165 new SqlType("Timestamp");
166 new SqlType("Double");
167 new SqlType("Float");
170 new SqlType("Short");
171 new SqlType("String");
172 } catch (Exception e) {
173 errorLogger.error("Error", e);
174 errorLogger.error(DmaapbcLogMessageEnum.DB_ACCESS_INIT_ERROR, e.getMessage() );
177 private Method objget;
178 private Method objset;
180 private int fieldnum;
181 public void copy(Object from, Object to) throws Exception {
182 objset.invoke(to, objget.invoke(from));
184 public void setKey(Object o, String key) throws Exception {
185 objset.invoke(o, key);
187 public String getKey(Object o) throws Exception {
188 return((String)objget.invoke(o));
190 public void toSQL(Object o, PreparedStatement ps) throws Exception {
191 sqlop.set(ps, fieldnum, objget.invoke(o));
193 public void fromSQL(ResultSet r, Object o) throws Exception {
194 objset.invoke(o, sqlop.get(r, fieldnum));
196 public static String fesc(String s) {
200 return(s.replaceAll("@", "@a").replaceAll(";", "@s").replaceAll(",", "@c"));
202 public static String funesc(String s) {
206 return(s.replaceAll("@c", ",").replaceAll("@s", ";").replaceAll("@a", "@"));