2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dmaap.dbcapi.database;
23 import java.lang.reflect.*;
27 import org.onap.dmaap.dbcapi.model.*;
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
33 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
36 public class DBFieldHandler {
37 static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
39 public DBFieldHandler(Class<?> c, String fieldname, int fieldnum) throws Exception {
40 this(c, fieldname, fieldnum, null);
42 public DBFieldHandler(Class<?> c, String fieldname, int fieldnum, SqlOp op) throws Exception {
43 this.fieldnum = fieldnum;
44 StringBuilder sb = new StringBuilder();
45 for (String s: fieldname.split("_")) {
46 sb.append(s.substring(0, 1).toUpperCase()).append(s.substring(1));
48 String camelcase = sb.toString();
50 objget = c.getMethod("is" + camelcase);
51 } catch (Exception e) {
52 errorLogger.warn("No 'is' method for " + c.getName() + " so trying 'get' method");
53 objget = c.getMethod("get" + camelcase);
55 objset = c.getMethod("set" + camelcase, objget.getReturnType());
60 Class<?> x = objget.getReturnType();
62 sqlop = new EnumSql(x);
65 sqlop = sqltypes.get(x.getName());
69 errorLogger.error(DmaapbcLogMessageEnum.DB_NO_FIELD_HANDLER, c.getName(), fieldname, Integer.toString(fieldnum), x.getName());
72 public static interface SqlOp {
73 public Object get(ResultSet rs, int index) throws Exception;
74 public void set(PreparedStatement ps, int index, Object value) throws Exception;
76 private static class AofString implements SqlOp {
77 public Object get(ResultSet rs, int index) throws Exception {
78 String val = rs.getString(index);
82 String[] ret = val.split(",");
83 for (int i = 0; i < ret.length; i++) {
84 ret[i] = funesc(ret[i]);
88 public void set(PreparedStatement ps, int index, Object x) throws Exception {
89 String[] val = (String[])x;
91 ps.setString(index, null);
94 StringBuffer sb = new StringBuffer();
97 sb.append(sep).append(fesc(s));
100 ps.setString(index, sb.toString());
103 private static class EnumSql implements SqlOp {
104 private Class enclass;
105 public EnumSql(Class enclass) {
106 this.enclass = enclass;
108 @SuppressWarnings("unchecked")
109 public Object get(ResultSet rs, int index) throws Exception {
110 String val = rs.getString(index);
114 return(Enum.valueOf(enclass, val));
117 public void set(PreparedStatement ps, int index, Object value) throws Exception {
119 ps.setString(index, null);
121 ps.setString(index, value.toString());
125 private static class SqlDate implements SqlOp {
126 public Object get(ResultSet rs, int index) throws Exception {
127 return(rs.getTimestamp(index));
129 public void set(PreparedStatement ps, int index, Object val) throws Exception {
130 if (val instanceof java.util.Date && !(val instanceof java.sql.Timestamp)) {
131 val = new java.sql.Timestamp(((java.util.Date)val).getTime());
133 ps.setTimestamp(index, (java.sql.Timestamp)val);
136 private static class SqlType implements SqlOp {
137 private Method sqlget;
138 private Method sqlset;
139 private SqlType(String tag) throws Exception {
140 sqlget = ResultSet.class.getMethod("get" + tag, Integer.TYPE);
141 sqlset = PreparedStatement.class.getMethod("set" + tag, Integer.TYPE, sqlget.getReturnType());
142 sqltypes.put(sqlget.getReturnType().getName(), this);
144 public Object get(ResultSet rs, int index) throws Exception {
145 return(sqlget.invoke(rs, index));
147 public void set(PreparedStatement ps, int index, Object val) throws Exception {
149 sqlset.invoke(ps, index, val);
150 } catch (Exception e) {
151 errorLogger.error(DmaapbcLogMessageEnum.DB_FIELD_INIT_ERROR, Integer.toString(index), val.toString(), ps.toString());
156 private static Map<String, SqlOp> sqltypes;
158 sqltypes = new HashMap<>();
159 sqltypes.put("[Ljava.lang.String;", new AofString());
160 sqltypes.put("java.util.Date", new SqlDate());
162 new SqlType("Boolean");
163 new SqlType("Timestamp");
164 new SqlType("Double");
165 new SqlType("Float");
168 new SqlType("Short");
169 new SqlType("String");
170 } catch (Exception e) {
171 errorLogger.error("Error", e);
172 errorLogger.error(DmaapbcLogMessageEnum.DB_ACCESS_INIT_ERROR, e.getMessage() );
175 private Method objget;
176 private Method objset;
178 private int fieldnum;
179 public void copy(Object from, Object to) throws Exception {
180 objset.invoke(to, objget.invoke(from));
182 public void setKey(Object o, String key) throws Exception {
183 objset.invoke(o, key);
185 public String getKey(Object o) throws Exception {
186 return((String)objget.invoke(o));
188 public void toSQL(Object o, PreparedStatement ps) throws Exception {
189 sqlop.set(ps, fieldnum, objget.invoke(o));
191 public void fromSQL(ResultSet r, Object o) throws Exception {
192 objset.invoke(o, sqlop.get(r, fieldnum));
194 public static String fesc(String s) {
198 return(s.replaceAll("@", "@a").replaceAll(";", "@s").replaceAll(",", "@c"));
200 public static String funesc(String s) {
204 return(s.replaceAll("@c", ",").replaceAll("@s", ";").replaceAll("@a", "@"));