aboutsummaryrefslogtreecommitdiffstats
path: root/dmaap-bc/src/main/java/org/onap/dmaap/dbcapi/database/DBFieldHandler.java
blob: 618932ec9a60d6a668b9b46ea6ee42d8243ccac0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*-
 * ============LICENSE_START=======================================================
 * org.onap.dmaap
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 *
 * Modifications Copyright (C) 2019 IBM.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.dmaap.dbcapi.database;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;


public class DBFieldHandler	{
	static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();	

	public DBFieldHandler(Class<?> c, String fieldname, int fieldnum) throws Exception {
		this(c, fieldname, fieldnum, null);
	}
	public DBFieldHandler(Class<?> c, String fieldname, int fieldnum, SqlOp op) throws Exception {
		this.fieldnum = fieldnum;
		StringBuilder sb = new StringBuilder();
		for (String s: fieldname.split("_")) {	
			sb.append(s.substring(0, 1).toUpperCase()).append(s.substring(1));
		}
		String camelcase = sb.toString();
		try {
			objget = c.getMethod("is" + camelcase);
		} catch (Exception e) {
			errorLogger.warn("No 'is' method for " + c.getName() + " so trying 'get' method");
			objget = c.getMethod("get" + camelcase);
		}
		objset = c.getMethod("set" + camelcase, objget.getReturnType());
		sqlop = op;
		if (sqlop != null) {
			return;
		}
		Class<?> x = objget.getReturnType();
		if (x.isEnum()) {
			sqlop = new EnumSql(x);
			return;
		}
		sqlop = sqltypes.get(x.getName());
		if (sqlop != null) {
			return;
		}
		errorLogger.error(DmaapbcLogMessageEnum.DB_NO_FIELD_HANDLER,  c.getName(),  fieldname,  Integer.toString(fieldnum),  x.getName());
	}
	
	public static interface	SqlOp	{
		public Object get(ResultSet rs, int index) throws Exception;
		public void set(PreparedStatement ps, int index, Object value) throws Exception;
	}
	private static class	AofString implements SqlOp {
		public Object get(ResultSet rs, int index) throws Exception {
			String val = rs.getString(index);
			if (val == null) {
				return(null);
			}
			String[] ret = val.split(",");
			for (int i = 0; i < ret.length; i++) {
				ret[i] = funesc(ret[i]);
			}
			return(ret);
		}
		public void set(PreparedStatement ps, int index, Object x) throws Exception {
			String[] val = (String[])x;
			if (val == null) {
				ps.setString(index, null);
				return;
			}
			StringBuilder sb = new StringBuilder();
			String sep = "";
			for (String s: val) {
				sb.append(sep).append(fesc(s));
				sep = ",";
			}
			ps.setString(index, sb.toString());
		}
	}
	private static class	EnumSql implements SqlOp {
		private Class	enclass;
		public EnumSql(Class enclass) {
			this.enclass = enclass;
		}
		@SuppressWarnings("unchecked")
		public Object get(ResultSet rs, int index) throws Exception {
			String val = rs.getString(index);
			if (val == null) {
				return(null);
			} else {
				return(Enum.valueOf(enclass, val));
			}
		}
		public void set(PreparedStatement ps, int index, Object value) throws Exception {
			if (value == null) {
				ps.setString(index, null);
			} else {
				ps.setString(index, value.toString());
			}
		}
	}
	private static class	SqlDate implements SqlOp {
		public Object get(ResultSet rs, int index) throws Exception {
			return(rs.getTimestamp(index));
		}
		public void set(PreparedStatement ps, int index, Object val) throws Exception {
			if (val instanceof java.util.Date && !(val instanceof java.sql.Timestamp)) {
				val = new java.sql.Timestamp(((java.util.Date)val).getTime());
			}
			ps.setTimestamp(index, (java.sql.Timestamp)val);
		}
	}
        private static class    SqlType implements SqlOp {
                private Method   sqlget;
                private Method   sqlset;
                private SqlType(String tag) throws Exception {
			sqlget = ResultSet.class.getMethod("get" + tag, Integer.TYPE);
                        sqlset = PreparedStatement.class.getMethod("set" + tag, Integer.TYPE, sqlget.getReturnType());
                        sqltypes.put(sqlget.getReturnType().getName(), this);
                }
		public Object get(ResultSet rs, int index) throws Exception {
			return(sqlget.invoke(rs, index));
		}
		public void set(PreparedStatement ps, int index, Object val) throws Exception {
			try {
				sqlset.invoke(ps, index, val);
			} catch (Exception e) {
				errorLogger.error(DmaapbcLogMessageEnum.DB_FIELD_INIT_ERROR,  Integer.toString(index), val.toString(), ps.toString());
				throw e;
			}
		}
        }
        private static Map<String, SqlOp> sqltypes;
        static {
                sqltypes = new HashMap<>();
		sqltypes.put("[Ljava.lang.String;", new AofString());
		sqltypes.put("java.util.Date", new SqlDate());
                try {
                        new SqlType("Boolean");
                        new SqlType("Timestamp");
                        new SqlType("Double");
                        new SqlType("Float");
                        new SqlType("Int");
                        new SqlType("Long");
                        new SqlType("Short");
                        new SqlType("String");
                } catch (Exception e) {
                	errorLogger.error("Error", e);
                	errorLogger.error(DmaapbcLogMessageEnum.DB_ACCESS_INIT_ERROR,  e.getMessage() );
                }
        }
	private Method	objget;
	private Method	objset;
	private SqlOp	sqlop;
	private int	fieldnum;
	public void copy(Object from, Object to) throws Exception {
		objset.invoke(to, objget.invoke(from));
	}
	public void setKey(Object o, String key) throws Exception {
		objset.invoke(o, key);
	}
	public String getKey(Object o) throws Exception {
		return((String)objget.invoke(o));
	}
	public void toSQL(Object o, PreparedStatement ps) throws Exception {
		sqlop.set(ps, fieldnum, objget.invoke(o));
	}
	public void fromSQL(ResultSet r, Object o) throws Exception {
		objset.invoke(o, sqlop.get(r, fieldnum));
	}
	public static String fesc(String s) {
		if (s == null) {
			return(s);
		}
		return(s.replaceAll("@", "@a").replaceAll(";", "@s").replaceAll(",", "@c"));
	}
	public static String funesc(String s) {
		if (s == null) {
			return(s);
		}
		return(s.replaceAll("@c", ",").replaceAll("@s", ";").replaceAll("@a", "@"));
	}
}