More bug fix and refactoring
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / beans / Parameters.java
index 78ed96d..79fc91b 100644 (file)
@@ -29,14 +29,13 @@ import java.sql.Connection;
 import java.sql.PreparedStatement;\r
 import java.sql.ResultSet;\r
 import java.sql.SQLException;\r
-import java.sql.Statement;\r
 import java.util.ArrayList;\r
 import java.util.Collection;\r
 import java.util.HashMap;\r
 import java.util.Map;\r
 import java.util.Objects;\r
 import org.json.JSONObject;\r
-import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
+import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;\r
 \r
 /**\r
  * Methods to provide access to Provisioning parameters in the DB. This class also provides constants of the standard\r
@@ -45,6 +44,7 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB;
  * @author Robert Eby\r
  * @version $Id: Parameters.java,v 1.11 2014/03/12 19:45:41 eby Exp $\r
  */\r
+\r
 public class Parameters extends Syncable {\r
 \r
     public static final String PROV_REQUIRE_SECURE = "PROV_REQUIRE_SECURE";\r
@@ -53,7 +53,7 @@ public class Parameters extends Syncable {
     public static final String PROV_AUTH_SUBJECTS = "PROV_AUTH_SUBJECTS";\r
     public static final String PROV_NAME = "PROV_NAME";\r
     public static final String PROV_ACTIVE_NAME = "PROV_ACTIVE_NAME";\r
-    public static final String PROV_DOMAIN = "PROV_DOMAIN";\r
+    static final String PROV_DOMAIN = "PROV_DOMAIN";\r
     public static final String PROV_MAXFEED_COUNT = "PROV_MAXFEED_COUNT";\r
     public static final String PROV_MAXSUB_COUNT = "PROV_MAXSUB_COUNT";\r
     public static final String PROV_POKETIMER1 = "PROV_POKETIMER1";\r
@@ -78,9 +78,9 @@ public class Parameters extends Syncable {
     private String keyname;\r
     private String value;\r
 \r
-    public Parameters(String k, String v) {\r
-        this.keyname = k;\r
-        this.value = v;\r
+    public Parameters(String key, String val) {\r
+        this.keyname = key;\r
+        this.value = val;\r
     }\r
 \r
     public Parameters(ResultSet rs) throws SQLException {\r
@@ -101,19 +101,19 @@ public class Parameters extends Syncable {
         return props;\r
     }\r
 \r
+    /**\r
+     * Method to get parameters.\r
+     * @return collection of parameters\r
+     */\r
     public static Collection<Parameters> getParameterCollection() {\r
         Collection<Parameters> coll = new ArrayList<>();\r
-        DB db = new DB();\r
-        String sql = "select * from PARAMETERS";\r
-        try (Connection conn = db.getConnection();\r
-                Statement stmt = conn.createStatement()) {\r
-            try (ResultSet rs = stmt.executeQuery(sql)) {\r
-                while (rs.next()) {\r
-                    Parameters p = new Parameters(rs);\r
-                    coll.add(p);\r
-                }\r
+        try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
+            PreparedStatement ps = conn.prepareStatement("select * from PARAMETERS");\r
+            ResultSet rs = ps.executeQuery()) {\r
+            while (rs.next()) {\r
+                Parameters param = new Parameters(rs);\r
+                coll.add(param);\r
             }\r
-            db.release(conn);\r
         } catch (SQLException e) {\r
             intlogger.error(SQLEXCEPTION + e.getMessage(), e);\r
         }\r
@@ -123,26 +123,24 @@ public class Parameters extends Syncable {
     /**\r
      * Get a specific parameter value from the DB.\r
      *\r
-     * @param k the key to lookup\r
+     * @param key the key to lookup\r
      * @return the value, or null if non-existant\r
      */\r
-    public static Parameters getParameter(String k) {\r
-        Parameters v = null;\r
-        DB db = new DB();\r
-        String sql = "select KEYNAME, VALUE from PARAMETERS where KEYNAME = ?";\r
-        try (Connection conn = db.getConnection();\r
-                PreparedStatement stmt = conn.prepareStatement(sql)) {\r
-            stmt.setString(1, k);\r
+    public static Parameters getParameter(String key) {\r
+        Parameters val = null;\r
+        try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
+            PreparedStatement stmt = conn.prepareStatement(\r
+                "select KEYNAME, VALUE from PARAMETERS where KEYNAME = ?")) {\r
+            stmt.setString(1, key);\r
             try (ResultSet rs = stmt.executeQuery()) {\r
                 if (rs.next()) {\r
-                    v = new Parameters(rs);\r
+                    val = new Parameters(rs);\r
                 }\r
             }\r
-            db.release(conn);\r
         } catch (SQLException e) {\r
             intlogger.error(SQLEXCEPTION + e.getMessage(), e);\r
         }\r
-        return v;\r
+        return val;\r
     }\r
 \r
     public String getKeyname() {\r
@@ -166,10 +164,9 @@ public class Parameters extends Syncable {
     }\r
 \r
     @Override\r
-    public boolean doInsert(Connection c) {\r
+    public boolean doInsert(Connection conn) {\r
         boolean rv = true;\r
-        String sql = "insert into PARAMETERS values (?, ?)";\r
-        try (PreparedStatement ps = c.prepareStatement(sql)) {\r
+        try (PreparedStatement ps = conn.prepareStatement("insert into PARAMETERS values (?, ?)")) {\r
             ps.setString(1, getKeyname());\r
             ps.setString(2, getValue());\r
             ps.execute();\r
@@ -181,10 +178,9 @@ public class Parameters extends Syncable {
     }\r
 \r
     @Override\r
-    public boolean doUpdate(Connection c) {\r
+    public boolean doUpdate(Connection conn) {\r
         boolean rv = true;\r
-        String sql = "update PARAMETERS set VALUE = ? where KEYNAME = ?";\r
-        try (PreparedStatement ps = c.prepareStatement(sql)) {\r
+        try (PreparedStatement ps = conn.prepareStatement("update PARAMETERS set VALUE = ? where KEYNAME = ?")) {\r
             ps.setString(1, getValue());\r
             ps.setString(2, getKeyname());\r
             ps.executeUpdate();\r
@@ -196,10 +192,9 @@ public class Parameters extends Syncable {
     }\r
 \r
     @Override\r
-    public boolean doDelete(Connection c) {\r
+    public boolean doDelete(Connection conn) {\r
         boolean rv = true;\r
-        String sql = "delete from PARAMETERS where KEYNAME = ?";\r
-        try (PreparedStatement ps = c.prepareStatement(sql)) {\r
+        try (PreparedStatement ps = conn.prepareStatement("delete from PARAMETERS where KEYNAME = ?")) {\r
             ps.setString(1, getKeyname());\r
             ps.execute();\r
         } catch (SQLException e) {\r