\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
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
* @version $Id: Group.java,v 1.0 2016/07/19\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
private String members;\r
private Date last_mod;\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.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
public static Group getGroupMatching(Group gup) {\r
String sql = String.format(\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
public static Group getGroupMatching(Group gup, int groupid) {\r
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
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
+ 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
}\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
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
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
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.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
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
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
\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
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, last_mod);\r
+ }\r
}\r