X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdbcapi%2Fdatabase%2FDBFieldHandler.java;h=5cad7ece972f43574c959822659bbccb6a610486;hb=HEAD;hp=072c5484a7e00f67cc39cfab0875049860d3510c;hpb=0bff051a842b164b680bc938f4a56db435dd5841;p=dmaap%2Fdbcapi.git diff --git a/src/main/java/org/onap/dmaap/dbcapi/database/DBFieldHandler.java b/src/main/java/org/onap/dmaap/dbcapi/database/DBFieldHandler.java index 072c548..5cad7ec 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/database/DBFieldHandler.java +++ b/src/main/java/org/onap/dmaap/dbcapi/database/DBFieldHandler.java @@ -3,6 +3,8 @@ * 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. @@ -24,7 +26,8 @@ import java.lang.reflect.*; import java.sql.*; import java.util.*; -import org.apache.log4j.Logger; +import org.onap.dmaap.dbcapi.model.*; + import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; @@ -33,7 +36,40 @@ import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum; public class DBFieldHandler { - static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger(); + 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; @@ -57,7 +93,7 @@ public class DBFieldHandler { ps.setString(index, null); return; } - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); String sep = ""; for (String s: val) { sb.append(sep).append(fesc(s)); @@ -121,7 +157,7 @@ public class DBFieldHandler { } private static Map sqltypes; static { - sqltypes = new HashMap(); + sqltypes = new HashMap<>(); sqltypes.put("[Ljava.lang.String;", new AofString()); sqltypes.put("java.util.Date", new SqlDate()); try { @@ -134,6 +170,7 @@ public class DBFieldHandler { new SqlType("Short"); new SqlType("String"); } catch (Exception e) { + errorLogger.error("Error", e); errorLogger.error(DmaapbcLogMessageEnum.DB_ACCESS_INIT_ERROR, e.getMessage() ); } } @@ -156,37 +193,6 @@ public class DBFieldHandler { public void fromSQL(ResultSet r, Object o) throws Exception { objset.invoke(o, sqlop.get(r, fieldnum)); } - 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; - StringBuffer sb = new StringBuffer(); - 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) { - 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 String fesc(String s) { if (s == null) { return(s);