X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2Fbeans%2FGroup.java;h=30fc87c4b438f5e00b44cb5d48d2da718b781b42;hb=ee6fa61e2cd7df99891092709765235b6166a041;hp=a021a60e6e3c6cda9d8674ee98e1b77844c5c2d6;hpb=a1cc0279360b09663c1b59fd0b89a1c2e081dfbd;p=dmaap%2Fdatarouter.git diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Group.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Group.java index a021a60e..30fc87c4 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Group.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Group.java @@ -29,10 +29,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Date; -import java.util.List; +import java.util.*; import org.apache.log4j.Logger; import org.json.JSONObject; @@ -46,6 +43,7 @@ import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities; * @version $Id: Group.java,v 1.0 2016/07/19 */ public class Group extends Syncable { + private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal"); private static int next_groupid = getMaxGroupID() + 1; @@ -99,17 +97,17 @@ public class Group extends Syncable { DB db = new DB(); @SuppressWarnings("resource") Connection conn = db.getConnection(); - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery(sql); - while (rs.next()) { - Group group = new Group(rs); - list.add(group); + try (Statement stmt = conn.createStatement()) { + try (ResultSet rs = stmt.executeQuery(sql)) { + while (rs.next()) { + Group group = new Group(rs); + list.add(group); + } + } } - rs.close(); - stmt.close(); db.release(conn); } catch (SQLException e) { - e.printStackTrace(); + intlogger.error("SQLException " + e.getMessage()); } return list; } @@ -120,39 +118,39 @@ public class Group extends Syncable { DB db = new DB(); @SuppressWarnings("resource") Connection conn = db.getConnection(); - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery("select MAX(groupid) from GROUPS"); - if (rs.next()) { - max = rs.getInt(1); + try (Statement stmt = conn.createStatement()) { + try (ResultSet rs = stmt.executeQuery("select MAX(groupid) from GROUPS")) { + if (rs.next()) { + max = rs.getInt(1); + } + } } - rs.close(); - stmt.close(); db.release(conn); } catch (SQLException e) { intlogger.info("getMaxSubID: " + e.getMessage()); - e.printStackTrace(); } return max; } public static Collection getGroupsByClassfication(String classfication) { - List list = new ArrayList(); - String sql = "select * from GROUPS where classification = '" + classfication + "'"; + List list = new ArrayList<>(); + String sql = "select * from GROUPS where classification = ?"; try { DB db = new DB(); @SuppressWarnings("resource") Connection conn = db.getConnection(); - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery(sql); - while (rs.next()) { - int groupid = rs.getInt("groupid"); - //list.add(URLUtilities.generateSubscriptionURL(groupid)); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, classfication); + try (ResultSet rs = stmt.executeQuery()) { + while (rs.next()) { + int groupid = rs.getInt("groupid"); + + } + } } - rs.close(); - stmt.close(); db.release(conn); } catch (SQLException e) { - e.printStackTrace(); + intlogger.error("SQLException " + e.getMessage()); } return list; } @@ -168,17 +166,16 @@ public class Group extends Syncable { DB db = new DB(); @SuppressWarnings("resource") Connection conn = db.getConnection(); - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery("select count(*) from SUBSCRIPTIONS"); - if (rs.next()) { - count = rs.getInt(1); + try (Statement stmt = conn.createStatement()) { + try (ResultSet rs = stmt.executeQuery("select count(*) from SUBSCRIPTIONS")) { + if (rs.next()) { + count = rs.getInt(1); + } + } } - rs.close(); - stmt.close(); db.release(conn); } catch (SQLException e) { intlogger.warn("PROV0008 countActiveSubscriptions: " + e.getMessage()); - e.printStackTrace(); } return count; } @@ -212,7 +209,7 @@ public class Group extends Syncable { public Group(JSONObject jo) throws InvalidObjectException { this("", "", ""); try { - // The JSONObject is assumed to contain a vnd.att-dr.group representation + // The JSONObject is assumed to contain a vnd.dmaap-dr.group representation this.groupid = jo.optInt("groupid", -1); String gname = jo.getString("name"); String gdescription = jo.getString("description"); @@ -223,10 +220,12 @@ public class Group extends Syncable { this.classification = jo.getString("classification"); this.members = jo.getString("members"); - if (gname.length() > 50) + if (gname.length() > 50) { throw new InvalidObjectException("Group name is too long"); - if (gdescription.length() > 256) + } + if (gdescription.length() > 256) { throw new InvalidObjectException("Group Description is too long"); + } } catch (InvalidObjectException e) { throw e; } catch (Exception e) { @@ -330,9 +329,9 @@ public class Group extends Syncable { setGroupid(next_groupid++); } // In case we insert a gropup from synchronization - if (groupid > next_groupid) + if (groupid > next_groupid) { next_groupid = groupid + 1; - + } // Create the GROUPS row String sql = "insert into GROUPS (GROUPID, AUTHID, NAME, DESCRIPTION, CLASSIFICATION, MEMBERS) values (?, ?, ?, ?, ?, ?)"; @@ -348,12 +347,13 @@ public class Group extends Syncable { } catch (SQLException e) { rv = false; intlogger.warn("PROV0005 doInsert: " + e.getMessage()); - e.printStackTrace(); } finally { try { - ps.close(); + if (ps != null) { + ps.close(); + } } catch (SQLException e) { - e.printStackTrace(); + intlogger.error("SQLException " + e.getMessage()); } } return rv; @@ -376,12 +376,13 @@ public class Group extends Syncable { } catch (SQLException e) { rv = false; intlogger.warn("PROV0006 doUpdate: " + e.getMessage()); - e.printStackTrace(); } finally { try { - ps.close(); + if (ps != null) { + ps.close(); + } } catch (SQLException e) { - e.printStackTrace(); + intlogger.error("SQLException " + e.getMessage()); } } return rv; @@ -399,12 +400,13 @@ public class Group extends Syncable { } catch (SQLException e) { rv = false; intlogger.warn("PROV0007 doDelete: " + e.getMessage()); - e.printStackTrace(); } finally { try { - ps.close(); + if (ps != null) { + ps.close(); + } } catch (SQLException e) { - e.printStackTrace(); + intlogger.error("SQLException " + e.getMessage()); } } return rv; @@ -417,21 +419,28 @@ public class Group extends Syncable { @Override public boolean equals(Object obj) { - if (!(obj instanceof Group)) + if (!(obj instanceof Group)) { return false; + } Group os = (Group) obj; - if (groupid != os.groupid) + if (groupid != os.groupid) { return false; - if (authid != os.authid) + } + if (authid != os.authid) { return false; - if (!name.equals(os.name)) + } + if (!name.equals(os.name)) { return false; - if (description != os.description) + } + if (description != os.description) { return false; - if (!classification.equals(os.classification)) + } + if (!classification.equals(os.classification)) { return false; - if (!members.equals(os.members)) + } + if (!members.equals(os.members)) { return false; + } return true; } @@ -440,4 +449,9 @@ public class Group extends Syncable { public String toString() { return "GROUP: groupid=" + groupid; } + + @Override + public int hashCode() { + return Objects.hash(groupid, authid, name, description, classification, members, last_mod); + } }