Limit number of topics per mmagent whitelist
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / database / DBFieldHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.database;
22
23 import java.lang.reflect.*;
24 import java.sql.*;
25 import java.util.*;
26
27 import org.onap.dmaap.dbcapi.model.*;
28
29
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
32
33 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
34
35
36 public class DBFieldHandler     {
37         static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();       
38
39         public DBFieldHandler(Class<?> c, String fieldname, int fieldnum) throws Exception {
40                 this(c, fieldname, fieldnum, null);
41         }
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));
47                 }
48                 String camelcase = sb.toString();
49                 try {
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);
54                 }
55                 objset = c.getMethod("set" + camelcase, objget.getReturnType());
56                 sqlop = op;
57                 if (sqlop != null) {
58                         return;
59                 }
60                 Class<?> x = objget.getReturnType();
61                 if (x.isEnum()) {
62                         sqlop = new EnumSql(x);
63                         return;
64                 }
65                 sqlop = sqltypes.get(x.getName());
66                 if (sqlop != null) {
67                         return;
68                 }
69                 errorLogger.error(DmaapbcLogMessageEnum.DB_NO_FIELD_HANDLER,  c.getName(),  fieldname,  Integer.toString(fieldnum),  x.getName());
70         }
71         
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;
75         }
76         private static class    AofString implements SqlOp {
77                 public Object get(ResultSet rs, int index) throws Exception {
78                         String val = rs.getString(index);
79                         if (val == null) {
80                                 return(null);
81                         }
82                         String[] ret = val.split(",");
83                         for (int i = 0; i < ret.length; i++) {
84                                 ret[i] = funesc(ret[i]);
85                         }
86                         return(ret);
87                 }
88                 public void set(PreparedStatement ps, int index, Object x) throws Exception {
89                         String[] val = (String[])x;
90                         if (val == null) {
91                                 ps.setString(index, null);
92                                 return;
93                         }
94                         StringBuffer sb = new StringBuffer();
95                         String sep = "";
96                         for (String s: val) {
97                                 sb.append(sep).append(fesc(s));
98                                 sep = ",";
99                         }
100                         ps.setString(index, sb.toString());
101                 }
102         }
103         private static class    EnumSql implements SqlOp {
104                 private Class   enclass;
105                 public EnumSql(Class enclass) {
106                         this.enclass = enclass;
107                 }
108                 @SuppressWarnings("unchecked")
109                 public Object get(ResultSet rs, int index) throws Exception {
110                         String val = rs.getString(index);
111                         if (val == null) {
112                                 return(null);
113                         } else {
114                                 return(Enum.valueOf(enclass, val));
115                         }
116                 }
117                 public void set(PreparedStatement ps, int index, Object value) throws Exception {
118                         if (value == null) {
119                                 ps.setString(index, null);
120                         } else {
121                                 ps.setString(index, value.toString());
122                         }
123                 }
124         }
125         private static class    SqlDate implements SqlOp {
126                 public Object get(ResultSet rs, int index) throws Exception {
127                         return(rs.getTimestamp(index));
128                 }
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());
132                         }
133                         ps.setTimestamp(index, (java.sql.Timestamp)val);
134                 }
135         }
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);
143                 }
144                 public Object get(ResultSet rs, int index) throws Exception {
145                         return(sqlget.invoke(rs, index));
146                 }
147                 public void set(PreparedStatement ps, int index, Object val) throws Exception {
148                         try {
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());
152                                 throw e;
153                         }
154                 }
155         }
156         private static Map<String, SqlOp> sqltypes;
157         static {
158                 sqltypes = new HashMap<>();
159                 sqltypes.put("[Ljava.lang.String;", new AofString());
160                 sqltypes.put("java.util.Date", new SqlDate());
161                 try {
162                         new SqlType("Boolean");
163                         new SqlType("Timestamp");
164                         new SqlType("Double");
165                         new SqlType("Float");
166                         new SqlType("Int");
167                         new SqlType("Long");
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() );
173                 }
174         }
175         private Method  objget;
176         private Method  objset;
177         private SqlOp   sqlop;
178         private int     fieldnum;
179         public void copy(Object from, Object to) throws Exception {
180                 objset.invoke(to, objget.invoke(from));
181         }
182         public void setKey(Object o, String key) throws Exception {
183                 objset.invoke(o, key);
184         }
185         public String getKey(Object o) throws Exception {
186                 return((String)objget.invoke(o));
187         }
188         public void toSQL(Object o, PreparedStatement ps) throws Exception {
189                 sqlop.set(ps, fieldnum, objget.invoke(o));
190         }
191         public void fromSQL(ResultSet r, Object o) throws Exception {
192                 objset.invoke(o, sqlop.get(r, fieldnum));
193         }
194         public static String fesc(String s) {
195                 if (s == null) {
196                         return(s);
197                 }
198                 return(s.replaceAll("@", "@a").replaceAll(";", "@s").replaceAll(",", "@c"));
199         }
200         public static String funesc(String s) {
201                 if (s == null) {
202                         return(s);
203                 }
204                 return(s.replaceAll("@c", ",").replaceAll("@s", ";").replaceAll("@a", "@"));
205         }
206 }