Code style cleanup for prov authz and beans
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / beans / Group.java
index a021a60..e0756f9 100644 (file)
@@ -23,6 +23,8 @@
 \r
 package org.onap.dmaap.datarouter.provisioning.beans;\r
 \r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
 import java.io.InvalidObjectException;\r
 import java.sql.Connection;\r
 import java.sql.PreparedStatement;\r
@@ -33,11 +35,9 @@ import java.util.ArrayList;
 import java.util.Collection;\r
 import java.util.Date;\r
 import java.util.List;\r
-\r
-import org.apache.log4j.Logger;\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.URLUtilities;\r
 \r
 /**\r
  * The representation of a Subscription.  Subscriptions can be retrieved from the DB, or stored/updated in the DB.\r
@@ -45,9 +45,13 @@ import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities;
  * @author vikram\r
  * @version $Id: Group.java,v 1.0 2016/07/19\r
  */\r
+\r
 public class Group extends Syncable {\r
-    private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");\r
-    private static int next_groupid = getMaxGroupID() + 1;\r
+\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
@@ -55,18 +59,97 @@ public class Group extends Syncable {
     private String description;\r
     private String classification;\r
     private String members;\r
-    private Date last_mod;\r
+    private Date lastMod;\r
+\r
+    public Group() {\r
+        this("", "", "");\r
+    }\r
+\r
+    /**\r
+     * Group constructor.\r
+     * @param name group name\r
+     * @param desc group description\r
+     * @param members group members\r
+     */\r
+    public Group(String name, String desc, String members) {\r
+        this.groupid = -1;\r
+        this.authid = "";\r
+        this.name = name;\r
+        this.description = desc;\r
+        this.members = members;\r
+        this.classification = "";\r
+        this.lastMod = new Date();\r
+    }\r
+\r
+\r
+    /**\r
+     * Group constructor from ResultSet.\r
+     * @param rs ResultSet\r
+     * @throws SQLException in case of SQL statement error\r
+     */\r
+    public Group(ResultSet rs) throws SQLException {\r
+        this.groupid = rs.getInt("GROUPID");\r
+        this.authid = rs.getString("AUTHID");\r
+        this.name = rs.getString("NAME");\r
+        this.description = rs.getString("DESCRIPTION");\r
+        this.classification = rs.getString("CLASSIFICATION");\r
+        this.members = rs.getString("MEMBERS");\r
+        this.lastMod = rs.getDate("LAST_MOD");\r
+    }\r
+\r
+    /**\r
+     * Group constructor for JSONObject.\r
+     * @param jo JSONObject\r
+     * @throws InvalidObjectException in case of JSON error\r
+     */\r
+    public Group(JSONObject jo) throws InvalidObjectException {\r
+        this("", "", "");\r
+        try {\r
+            // The JSONObject is assumed to contain a vnd.dmaap-dr.group representation\r
+            this.groupid = jo.optInt(GROUP_ID_CONST, -1);\r
+            String gname = jo.getString("name");\r
+            String gdescription = jo.getString("description");\r
 \r
+            this.authid = jo.getString("authid");\r
+            this.name = gname;\r
+            this.description = gdescription;\r
+            this.classification = jo.getString("classification");\r
+            this.members = jo.getString("members");\r
 \r
+            if (gname.length() > 50) {\r
+                throw new InvalidObjectException("Group name is too long");\r
+            }\r
+            if (gdescription.length() > 256) {\r
+                throw new InvalidObjectException("Group Description is too long");\r
+            }\r
+        } catch (InvalidObjectException e) {\r
+            throw e;\r
+        } catch (Exception e) {\r
+            intlogger.warn("Invalid JSON: " + e.getMessage(), e);\r
+            throw new InvalidObjectException("Invalid JSON: " + e.getMessage());\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Get a group frpm DB.\r
+     * @param gup group object\r
+     * @return Group object\r
+     */\r
     public static Group getGroupMatching(Group gup) {\r
         String sql = String.format(\r
                 "select * from GROUPS where NAME='%s'",\r
                 gup.getName()\r
         );\r
         List<Group> list = getGroupsForSQL(sql);\r
-        return list.size() > 0 ? list.get(0) : null;\r
+        return !list.isEmpty() ? list.get(0) : null;\r
     }\r
 \r
+    /**\r
+     * Get a group from DB using name and groupid.\r
+     * @param gup group object\r
+     * @param groupid id of group\r
+     * @return group object\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 ",\r
@@ -74,19 +157,29 @@ public class Group extends Syncable {
                 gup.getGroupid()\r
         );\r
         List<Group> list = getGroupsForSQL(sql);\r
-        return list.size() > 0 ? list.get(0) : null;\r
+        return !list.isEmpty() ? list.get(0) : null;\r
     }\r
 \r
+    /**\r
+     * Get group from DB using groupid only.\r
+     * @param id id of group\r
+     * @return group object\r
+     */\r
     public static Group getGroupById(int id) {\r
         String sql = "select * from GROUPS where GROUPID = " + id;\r
         List<Group> list = getGroupsForSQL(sql);\r
-        return list.size() > 0 ? list.get(0) : null;\r
+        return !list.isEmpty() ? list.get(0) : null;\r
     }\r
 \r
-    public static Group getGroupByAuthId(String id) {\r
+    /**\r
+     * Get group from DB using AUTHID.\r
+     * @param id AUTHID\r
+     * @return group object\r
+     */\r
+    static Group getGroupByAuthId(String id) {\r
         String sql = "select * from GROUPS where AUTHID = '" + id + "'";\r
         List<Group> list = getGroupsForSQL(sql);\r
-        return list.size() > 0 ? list.get(0) : null;\r
+        return !list.isEmpty() ? list.get(0) : null;\r
     }\r
 \r
     public static Collection<Group> getAllgroups() {\r
@@ -94,151 +187,51 @@ public class Group extends Syncable {
     }\r
 \r
     private static List<Group> getGroupsForSQL(String sql) {\r
-        List<Group> list = new ArrayList<Group>();\r
+        List<Group> list = new ArrayList<>();\r
         try {\r
             DB db = new DB();\r
             @SuppressWarnings("resource")\r
             Connection conn = db.getConnection();\r
-            Statement stmt = conn.createStatement();\r
-            ResultSet rs = stmt.executeQuery(sql);\r
-            while (rs.next()) {\r
-                Group group = new Group(rs);\r
-                list.add(group);\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
             }\r
-            rs.close();\r
-            stmt.close();\r
             db.release(conn);\r
         } catch (SQLException e) {\r
-            e.printStackTrace();\r
+            intlogger.error("PROV0009 getGroupsForSQL: " + e.getMessage(), e);\r
         }\r
         return list;\r
     }\r
 \r
-    public static int getMaxGroupID() {\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
-            Statement stmt = conn.createStatement();\r
-            ResultSet rs = stmt.executeQuery("select MAX(groupid) from GROUPS");\r
-            if (rs.next()) {\r
-                max = rs.getInt(1);\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
             }\r
-            rs.close();\r
-            stmt.close();\r
             db.release(conn);\r
         } catch (SQLException e) {\r
-            intlogger.info("getMaxSubID: " + e.getMessage());\r
-            e.printStackTrace();\r
+            intlogger.info("PROV0001 getMaxSubID: " + e.getMessage(), e);\r
         }\r
         return max;\r
     }\r
 \r
-    public static Collection<String> getGroupsByClassfication(String classfication) {\r
-        List<String> list = new ArrayList<String>();\r
-        String sql = "select * from GROUPS where classification = '" + classfication + "'";\r
-        try {\r
-            DB db = new DB();\r
-            @SuppressWarnings("resource")\r
-            Connection conn = db.getConnection();\r
-            Statement stmt = conn.createStatement();\r
-            ResultSet rs = stmt.executeQuery(sql);\r
-            while (rs.next()) {\r
-                int groupid = rs.getInt("groupid");\r
-                //list.add(URLUtilities.generateSubscriptionURL(groupid));\r
-            }\r
-            rs.close();\r
-            stmt.close();\r
-            db.release(conn);\r
-        } catch (SQLException e) {\r
-            e.printStackTrace();\r
-        }\r
-        return list;\r
-    }\r
-\r
-    /**\r
-     * Return a count of the number of active subscriptions in the DB.\r
-     *\r
-     * @return the count\r
-     */\r
-    public static int countActiveSubscriptions() {\r
-        int count = 0;\r
-        try {\r
-            DB db = new DB();\r
-            @SuppressWarnings("resource")\r
-            Connection conn = db.getConnection();\r
-            Statement stmt = conn.createStatement();\r
-            ResultSet rs = stmt.executeQuery("select count(*) from SUBSCRIPTIONS");\r
-            if (rs.next()) {\r
-                count = rs.getInt(1);\r
-            }\r
-            rs.close();\r
-            stmt.close();\r
-            db.release(conn);\r
-        } catch (SQLException e) {\r
-            intlogger.warn("PROV0008 countActiveSubscriptions: " + e.getMessage());\r
-            e.printStackTrace();\r
-        }\r
-        return count;\r
-    }\r
-\r
-    public Group() {\r
-        this("", "", "");\r
-    }\r
-\r
-    public Group(String name, String desc, String members) {\r
-        this.groupid = -1;\r
-        this.authid = "";\r
-        this.name = name;\r
-        this.description = desc;\r
-        this.members = members;\r
-        this.classification = "";\r
-        this.last_mod = new Date();\r
-    }\r
-\r
-\r
-    public Group(ResultSet rs) throws SQLException {\r
-        this.groupid = rs.getInt("GROUPID");\r
-        this.authid = rs.getString("AUTHID");\r
-        this.name = rs.getString("NAME");\r
-        this.description = rs.getString("DESCRIPTION");\r
-        this.classification = rs.getString("CLASSIFICATION");\r
-        this.members = rs.getString("MEMBERS");\r
-        this.last_mod = rs.getDate("LAST_MOD");\r
-    }\r
-\r
-\r
-    public Group(JSONObject jo) throws InvalidObjectException {\r
-        this("", "", "");\r
-        try {\r
-            // The JSONObject is assumed to contain a vnd.att-dr.group representation\r
-            this.groupid = jo.optInt("groupid", -1);\r
-            String gname = jo.getString("name");\r
-            String gdescription = jo.getString("description");\r
-\r
-            this.authid = jo.getString("authid");\r
-            this.name = gname;\r
-            this.description = gdescription;\r
-            this.classification = jo.getString("classification");\r
-            this.members = jo.getString("members");\r
-\r
-            if (gname.length() > 50)\r
-                throw new InvalidObjectException("Group name is too long");\r
-            if (gdescription.length() > 256)\r
-                throw new InvalidObjectException("Group Description is too long");\r
-        } catch (InvalidObjectException e) {\r
-            throw e;\r
-        } catch (Exception e) {\r
-            throw new InvalidObjectException("invalid JSON: " + e.getMessage());\r
-        }\r
-    }\r
-\r
     public int getGroupid() {\r
         return groupid;\r
     }\r
 \r
-    public static Logger getIntlogger() {\r
+    public static EELFLogger getIntlogger() {\r
         return intlogger;\r
     }\r
 \r
@@ -246,18 +239,10 @@ public class Group extends Syncable {
         this.groupid = groupid;\r
     }\r
 \r
-    public static void setIntlogger(Logger intlogger) {\r
+    public static void setIntlogger(EELFLogger intlogger) {\r
         Group.intlogger = intlogger;\r
     }\r
 \r
-    public static int getNext_groupid() {\r
-        return next_groupid;\r
-    }\r
-\r
-    public static void setNext_groupid(int next_groupid) {\r
-        Group.next_groupid = next_groupid;\r
-    }\r
-\r
     public String getAuthid() {\r
         return authid;\r
     }\r
@@ -294,49 +279,37 @@ public class Group extends Syncable {
         return members;\r
     }\r
 \r
-    public void setMembers(String members) {\r
-        this.members = members;\r
-    }\r
-\r
-    public Date getLast_mod() {\r
-        return last_mod;\r
-    }\r
-\r
-    public void setLast_mod(Date last_mod) {\r
-        this.last_mod = last_mod;\r
-    }\r
-\r
-\r
     @Override\r
     public JSONObject asJSONObject() {\r
         JSONObject jo = new JSONObject();\r
-        jo.put("groupid", groupid);\r
+        jo.put(GROUP_ID_CONST, groupid);\r
         jo.put("authid", authid);\r
         jo.put("name", name);\r
         jo.put("description", description);\r
         jo.put("classification", classification);\r
         jo.put("members", members);\r
-        jo.put("last_mod", last_mod.getTime());\r
+        jo.put("last_mod", lastMod.getTime());\r
         return jo;\r
     }\r
 \r
     @Override\r
-    public boolean doInsert(Connection c) {\r
+    public boolean doInsert(Connection conn) {\r
         boolean rv = true;\r
         PreparedStatement ps = null;\r
         try {\r
             if (groupid == -1) {\r
                 // No feed ID assigned yet, so assign the next available one\r
-                setGroupid(next_groupid++);\r
+                setGroupid(nextGroupid++);\r
             }\r
             // In case we insert a gropup from synchronization\r
-            if (groupid > next_groupid)\r
-                next_groupid = groupid + 1;\r
-\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) values (?, ?, ?, ?, ?, ?)";\r
-            ps = c.prepareStatement(sql, new String[]{"GROUPID"});\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
@@ -347,25 +320,27 @@ public class Group extends Syncable {
             ps.close();\r
         } catch (SQLException e) {\r
             rv = false;\r
-            intlogger.warn("PROV0005 doInsert: " + e.getMessage());\r
-            e.printStackTrace();\r
+            intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e);\r
         } finally {\r
             try {\r
-                ps.close();\r
+                if (ps != null) {\r
+                    ps.close();\r
+                }\r
             } catch (SQLException e) {\r
-                e.printStackTrace();\r
+                intlogger.error(SQLEXCEPTION + e.getMessage(), e);\r
             }\r
         }\r
         return rv;\r
     }\r
 \r
     @Override\r
-    public boolean doUpdate(Connection c) {\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 = ? where GROUPID = ?";\r
-            ps = c.prepareStatement(sql);\r
+            String sql = "update GROUPS set AUTHID = ?, NAME = ?, DESCRIPTION = ?, CLASSIFICATION = ? ,  MEMBERS = ? "\r
+                                 + "where GROUPID = ?";\r
+            ps = conn.prepareStatement(sql);\r
             ps.setString(1, authid);\r
             ps.setString(2, name);\r
             ps.setString(3, description);\r
@@ -375,36 +350,38 @@ public class Group extends Syncable {
             ps.executeUpdate();\r
         } catch (SQLException e) {\r
             rv = false;\r
-            intlogger.warn("PROV0006 doUpdate: " + e.getMessage());\r
-            e.printStackTrace();\r
+            intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e);\r
         } finally {\r
             try {\r
-                ps.close();\r
+                if (ps != null) {\r
+                    ps.close();\r
+                }\r
             } catch (SQLException e) {\r
-                e.printStackTrace();\r
+                intlogger.error(SQLEXCEPTION + e.getMessage(), e);\r
             }\r
         }\r
         return rv;\r
     }\r
 \r
     @Override\r
-    public boolean doDelete(Connection c) {\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 = c.prepareStatement(sql);\r
+            ps = conn.prepareStatement(sql);\r
             ps.setInt(1, groupid);\r
             ps.execute();\r
         } catch (SQLException e) {\r
             rv = false;\r
-            intlogger.warn("PROV0007 doDelete: " + e.getMessage());\r
-            e.printStackTrace();\r
+            intlogger.warn("PROV0007 doDelete: " + e.getMessage(), e);\r
         } finally {\r
             try {\r
-                ps.close();\r
+                if (ps != null) {\r
+                    ps.close();\r
+                }\r
             } catch (SQLException e) {\r
-                e.printStackTrace();\r
+                intlogger.error(SQLEXCEPTION + e.getMessage(), e);\r
             }\r
         }\r
         return rv;\r
@@ -417,21 +394,28 @@ public class Group extends Syncable {
 \r
     @Override\r
     public boolean equals(Object obj) {\r
-        if (!(obj instanceof Group))\r
+        if (!(obj instanceof Group)) {\r
             return false;\r
+        }\r
         Group os = (Group) obj;\r
-        if (groupid != os.groupid)\r
+        if (groupid != os.groupid) {\r
             return false;\r
-        if (authid != os.authid)\r
+        }\r
+        if (authid != os.authid) {\r
             return false;\r
-        if (!name.equals(os.name))\r
+        }\r
+        if (!name.equals(os.name)) {\r
             return false;\r
-        if (description != os.description)\r
+        }\r
+        if (description != os.description) {\r
             return false;\r
-        if (!classification.equals(os.classification))\r
+        }\r
+        if (!classification.equals(os.classification)) {\r
             return false;\r
-        if (!members.equals(os.members))\r
+        }\r
+        if (!members.equals(os.members)) {\r
             return false;\r
+        }\r
 \r
         return true;\r
     }\r
@@ -440,4 +424,9 @@ public class Group extends Syncable {
     public String toString() {\r
         return "GROUP: groupid=" + groupid;\r
     }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return Objects.hash(groupid, authid, name, description, classification, members, lastMod);\r
+    }\r
 }\r