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.Date;\r
import java.util.List;\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
* The representation of a Subscription. Subscriptions can be retrieved from the DB, or stored/updated in the DB.\r
private static final String GROUP_ID_CONST = "groupid";\r
private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");\r
private static int nextGroupid = getMaxGroupID() + 1;\r
- private static final String SQLEXCEPTION = "SQLException: ";\r
\r
private int groupid;\r
private String authid;\r
} catch (InvalidObjectException e) {\r
throw e;\r
} catch (Exception e) {\r
- intlogger.warn("Invalid JSON: " + e.getMessage(), e);\r
+ intlogger.error("Invalid JSON: " + e.getMessage(), e);\r
throw new InvalidObjectException("Invalid JSON: " + e.getMessage());\r
}\r
}\r
*/\r
public static Group getGroupMatching(Group gup) {\r
String sql = String.format(\r
- "select * from GROUPS where NAME='%s'",\r
- gup.getName()\r
+ "select * from GROUPS where NAME='%s'",\r
+ gup.getName()\r
);\r
List<Group> list = getGroupsForSQL(sql);\r
return !list.isEmpty() ? list.get(0) : null;\r
*/\r
public static Group getGroupMatching(Group gup, int groupid) {\r
String sql = String.format(\r
- "select * from GROUPS where NAME = '%s' and GROUPID != %d ", gup.getName(), gup.getGroupid());\r
+ "select * from GROUPS where NAME = '%s' and GROUPID != %d ", gup.getName(), groupid);\r
List<Group> list = getGroupsForSQL(sql);\r
return !list.isEmpty() ? list.get(0) : null;\r
}\r
\r
private static List<Group> getGroupsForSQL(String sql) {\r
List<Group> list = new ArrayList<>();\r
- try {\r
- DB db = new DB();\r
- @SuppressWarnings("resource")\r
- Connection conn = db.getConnection();\r
- try (Statement stmt = conn.createStatement()) {\r
- try (ResultSet rs = stmt.executeQuery(sql)) {\r
- while (rs.next()) {\r
- Group group = new Group(rs);\r
- list.add(group);\r
- }\r
- }\r
+ try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
+ PreparedStatement ps = conn.prepareStatement(sql);\r
+ ResultSet rs = ps.executeQuery()) {\r
+ while (rs.next()) {\r
+ Group group = new Group(rs);\r
+ list.add(group);\r
}\r
- db.release(conn);\r
} catch (SQLException e) {\r
intlogger.error("PROV0009 getGroupsForSQL: " + e.getMessage(), e);\r
}\r
\r
private static int getMaxGroupID() {\r
int max = 0;\r
- try {\r
- DB db = new DB();\r
- @SuppressWarnings("resource")\r
- Connection conn = db.getConnection();\r
- try (Statement stmt = conn.createStatement()) {\r
- try (ResultSet rs = stmt.executeQuery("select MAX(groupid) from GROUPS")) {\r
- if (rs.next()) {\r
- max = rs.getInt(1);\r
- }\r
- }\r
+ try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
+ PreparedStatement ps = conn.prepareStatement("select MAX(groupid) from GROUPS");\r
+ ResultSet rs = ps.executeQuery()) {\r
+ if (rs.next()) {\r
+ max = rs.getInt(1);\r
}\r
- db.release(conn);\r
} catch (SQLException e) {\r
- intlogger.info("PROV0001 getMaxSubID: " + e.getMessage(), e);\r
+ intlogger.error("PROV0001 getMaxSubID: " + e.getMessage(), e);\r
}\r
return max;\r
}\r
@Override\r
public boolean doInsert(Connection conn) {\r
boolean rv = true;\r
- PreparedStatement ps = null;\r
- try {\r
+ try (PreparedStatement ps = conn.prepareStatement(\r
+ "insert into GROUPS(GROUPID, AUTHID, NAME, DESCRIPTION, CLASSIFICATION, MEMBERS) "\r
+ + "values (?, ?, ?, ?, ?, ?)", new String[]{"GROUPID"})) {\r
if (groupid == -1) {\r
// No feed ID assigned yet, so assign the next available one\r
setGroupid(nextGroupid++);\r
}\r
- // In case we insert a gropup from synchronization\r
+ // In case we insert a group from synchronization\r
if (groupid > nextGroupid) {\r
nextGroupid = groupid + 1;\r
}\r
-\r
// Create the GROUPS row\r
- String sql = "insert into GROUPS (GROUPID, AUTHID, NAME, DESCRIPTION, CLASSIFICATION, MEMBERS) "\r
- + "values (?, ?, ?, ?, ?, ?)";\r
- ps = conn.prepareStatement(sql, new String[]{"GROUPID"});\r
ps.setInt(1, groupid);\r
ps.setString(2, authid);\r
ps.setString(3, name);\r
ps.setString(5, classification);\r
ps.setString(6, members);\r
ps.execute();\r
- ps.close();\r
} catch (SQLException e) {\r
rv = false;\r
- intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e);\r
- } finally {\r
- try {\r
- if (ps != null) {\r
- ps.close();\r
- }\r
- } catch (SQLException e) {\r
- intlogger.error(SQLEXCEPTION + e.getMessage(), e);\r
- }\r
+ intlogger.error("PROV0005 doInsert: " + e.getMessage(), e);\r
}\r
return rv;\r
}\r
@Override\r
public boolean doUpdate(Connection conn) {\r
boolean rv = true;\r
- PreparedStatement ps = null;\r
- try {\r
- String sql = "update GROUPS set AUTHID = ?, NAME = ?, DESCRIPTION = ?, CLASSIFICATION = ? , MEMBERS = ? "\r
- + "where GROUPID = ?";\r
- ps = conn.prepareStatement(sql);\r
+ try (PreparedStatement ps = conn.prepareStatement(\r
+ "update GROUPS set AUTHID = ?, NAME = ?, DESCRIPTION = ?, CLASSIFICATION = ? , MEMBERS = ? where GROUPID = ?")) {\r
ps.setString(1, authid);\r
ps.setString(2, name);\r
ps.setString(3, description);\r
ps.executeUpdate();\r
} catch (SQLException e) {\r
rv = false;\r
- intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e);\r
- } finally {\r
- try {\r
- if (ps != null) {\r
- ps.close();\r
- }\r
- } catch (SQLException e) {\r
- intlogger.error(SQLEXCEPTION + e.getMessage(), e);\r
- }\r
+ intlogger.error("PROV0006 doUpdate: " + e.getMessage(), e);\r
}\r
return rv;\r
}\r
@Override\r
public boolean doDelete(Connection conn) {\r
boolean rv = true;\r
- PreparedStatement ps = null;\r
- try {\r
- String sql = "delete from GROUPS where GROUPID = ?";\r
- ps = conn.prepareStatement(sql);\r
+ try (PreparedStatement ps = conn.prepareStatement("delete from GROUPS where GROUPID = ?")) {\r
ps.setInt(1, groupid);\r
ps.execute();\r
} catch (SQLException e) {\r
rv = false;\r
- intlogger.warn("PROV0007 doDelete: " + e.getMessage(), e);\r
- } finally {\r
- try {\r
- if (ps != null) {\r
- ps.close();\r
- }\r
- } catch (SQLException e) {\r
- intlogger.error(SQLEXCEPTION + e.getMessage(), e);\r
- }\r
+ intlogger.error("PROV0007 doDelete: " + e.getMessage(), e);\r
}\r
return rv;\r
}\r
if (groupid != os.groupid) {\r
return false;\r
}\r
- if (authid != os.authid) {\r
+ if (!authid.equals(os.authid)) {\r
return false;\r
}\r
if (!name.equals(os.name)) {\r
return false;\r
}\r
- if (description != os.description) {\r
+ if (!description.equals(os.description)) {\r
return false;\r
}\r
if (!classification.equals(os.classification)) {\r
return false;\r
}\r
- if (!members.equals(os.members)) {\r
- return false;\r
- }\r
-\r
- return true;\r
+ return members.equals(os.members);\r
}\r
\r
@Override\r