1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
24 package org.onap.dmaap.datarouter.provisioning.beans;
\r
26 import com.att.eelf.configuration.EELFLogger;
\r
27 import com.att.eelf.configuration.EELFManager;
\r
28 import java.io.InvalidObjectException;
\r
29 import java.sql.Connection;
\r
30 import java.sql.PreparedStatement;
\r
31 import java.sql.ResultSet;
\r
32 import java.sql.SQLException;
\r
33 import java.sql.Statement;
\r
34 import java.util.ArrayList;
\r
35 import java.util.Collection;
\r
36 import java.util.Date;
\r
37 import java.util.List;
\r
38 import java.util.Objects;
\r
39 import org.json.JSONObject;
\r
40 import org.onap.dmaap.datarouter.provisioning.utils.DB;
\r
43 * The representation of a Subscription. Subscriptions can be retrieved from the DB, or stored/updated in the DB.
\r
46 * @version $Id: Group.java,v 1.0 2016/07/19
\r
48 public class Group extends Syncable {
\r
50 private static final String GROUP_ID_CONST = "groupid";
\r
51 private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");
\r
52 private static int nextGroupid = getMaxGroupID() + 1;
\r
53 private static final String SQLEXCEPTION = "SQLException: ";
\r
55 private int groupid;
\r
56 private String authid;
\r
57 private String name;
\r
58 private String description;
\r
59 private String classification;
\r
60 private String members;
\r
61 private Date last_mod;
\r
67 public Group(String name, String desc, String members) {
\r
71 this.description = desc;
\r
72 this.members = members;
\r
73 this.classification = "";
\r
74 this.last_mod = new Date();
\r
78 public Group(ResultSet rs) throws SQLException {
\r
79 this.groupid = rs.getInt("GROUPID");
\r
80 this.authid = rs.getString("AUTHID");
\r
81 this.name = rs.getString("NAME");
\r
82 this.description = rs.getString("DESCRIPTION");
\r
83 this.classification = rs.getString("CLASSIFICATION");
\r
84 this.members = rs.getString("MEMBERS");
\r
85 this.last_mod = rs.getDate("LAST_MOD");
\r
89 public Group(JSONObject jo) throws InvalidObjectException {
\r
92 // The JSONObject is assumed to contain a vnd.dmaap-dr.group representation
\r
93 this.groupid = jo.optInt(GROUP_ID_CONST, -1);
\r
94 String gname = jo.getString("name");
\r
95 String gdescription = jo.getString("description");
\r
97 this.authid = jo.getString("authid");
\r
99 this.description = gdescription;
\r
100 this.classification = jo.getString("classification");
\r
101 this.members = jo.getString("members");
\r
103 if (gname.length() > 50) {
\r
104 throw new InvalidObjectException("Group name is too long");
\r
106 if (gdescription.length() > 256) {
\r
107 throw new InvalidObjectException("Group Description is too long");
\r
109 } catch (InvalidObjectException e) {
\r
111 } catch (Exception e) {
\r
112 intlogger.warn("Invalid JSON: " + e.getMessage(), e);
\r
113 throw new InvalidObjectException("Invalid JSON: " + e.getMessage());
\r
118 public static Group getGroupMatching(Group gup) {
\r
119 String sql = String.format(
\r
120 "select * from GROUPS where NAME='%s'",
\r
123 List<Group> list = getGroupsForSQL(sql);
\r
124 return list.size() > 0 ? list.get(0) : null;
\r
127 public static Group getGroupMatching(Group gup, int groupid) {
\r
128 String sql = String.format(
\r
129 "select * from GROUPS where NAME = '%s' and GROUPID != %d ",
\r
133 List<Group> list = getGroupsForSQL(sql);
\r
134 return list.size() > 0 ? list.get(0) : null;
\r
137 public static Group getGroupById(int id) {
\r
138 String sql = "select * from GROUPS where GROUPID = " + id;
\r
139 List<Group> list = getGroupsForSQL(sql);
\r
140 return list.size() > 0 ? list.get(0) : null;
\r
143 static Group getGroupByAuthId(String id) {
\r
144 String sql = "select * from GROUPS where AUTHID = '" + id + "'";
\r
145 List<Group> list = getGroupsForSQL(sql);
\r
146 return list.size() > 0 ? list.get(0) : null;
\r
149 public static Collection<Group> getAllgroups() {
\r
150 return getGroupsForSQL("select * from GROUPS");
\r
153 private static List<Group> getGroupsForSQL(String sql) {
\r
154 List<Group> list = new ArrayList<>();
\r
157 @SuppressWarnings("resource")
\r
158 Connection conn = db.getConnection();
\r
159 try (Statement stmt = conn.createStatement()) {
\r
160 try (ResultSet rs = stmt.executeQuery(sql)) {
\r
161 while (rs.next()) {
\r
162 Group group = new Group(rs);
\r
168 } catch (SQLException e) {
\r
169 intlogger.error("PROV0009 getGroupsForSQL: " + e.getMessage(), e);
\r
174 private static int getMaxGroupID() {
\r
178 @SuppressWarnings("resource")
\r
179 Connection conn = db.getConnection();
\r
180 try (Statement stmt = conn.createStatement()) {
\r
181 try (ResultSet rs = stmt.executeQuery("select MAX(groupid) from GROUPS")) {
\r
183 max = rs.getInt(1);
\r
188 } catch (SQLException e) {
\r
189 intlogger.info("PROV0001 getMaxSubID: " + e.getMessage(), e);
\r
194 public int getGroupid() {
\r
198 public static EELFLogger getIntlogger() {
\r
202 public void setGroupid(int groupid) {
\r
203 this.groupid = groupid;
\r
206 public static void setIntlogger(EELFLogger intlogger) {
\r
207 Group.intlogger = intlogger;
\r
210 public String getAuthid() {
\r
214 public void setAuthid(String authid) {
\r
215 this.authid = authid;
\r
218 public String getName() {
\r
222 public void setName(String name) {
\r
226 public String getDescription() {
\r
227 return description;
\r
230 public void setDescription(String description) {
\r
231 this.description = description;
\r
234 public String getClassification() {
\r
235 return classification;
\r
238 public void setClassification(String classification) {
\r
239 this.classification = classification;
\r
242 public String getMembers() {
\r
247 public JSONObject asJSONObject() {
\r
248 JSONObject jo = new JSONObject();
\r
249 jo.put(GROUP_ID_CONST, groupid);
\r
250 jo.put("authid", authid);
\r
251 jo.put("name", name);
\r
252 jo.put("description", description);
\r
253 jo.put("classification", classification);
\r
254 jo.put("members", members);
\r
255 jo.put("last_mod", last_mod.getTime());
\r
260 public boolean doInsert(Connection c) {
\r
262 PreparedStatement ps = null;
\r
264 if (groupid == -1) {
\r
265 // No feed ID assigned yet, so assign the next available one
\r
266 setGroupid(nextGroupid++);
\r
268 // In case we insert a gropup from synchronization
\r
269 if (groupid > nextGroupid) {
\r
270 nextGroupid = groupid + 1;
\r
273 // Create the GROUPS row
\r
274 String sql = "insert into GROUPS (GROUPID, AUTHID, NAME, DESCRIPTION, CLASSIFICATION, MEMBERS) values (?, ?, ?, ?, ?, ?)";
\r
275 ps = c.prepareStatement(sql, new String[]{"GROUPID"});
\r
276 ps.setInt(1, groupid);
\r
277 ps.setString(2, authid);
\r
278 ps.setString(3, name);
\r
279 ps.setString(4, description);
\r
280 ps.setString(5, classification);
\r
281 ps.setString(6, members);
\r
284 } catch (SQLException e) {
\r
286 intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e);
\r
292 } catch (SQLException e) {
\r
293 intlogger.error(SQLEXCEPTION + e.getMessage(), e);
\r
300 public boolean doUpdate(Connection c) {
\r
302 PreparedStatement ps = null;
\r
304 String sql = "update GROUPS set AUTHID = ?, NAME = ?, DESCRIPTION = ?, CLASSIFICATION = ? , MEMBERS = ? where GROUPID = ?";
\r
305 ps = c.prepareStatement(sql);
\r
306 ps.setString(1, authid);
\r
307 ps.setString(2, name);
\r
308 ps.setString(3, description);
\r
309 ps.setString(4, classification);
\r
310 ps.setString(5, members);
\r
311 ps.setInt(6, groupid);
\r
312 ps.executeUpdate();
\r
313 } catch (SQLException e) {
\r
315 intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e);
\r
321 } catch (SQLException e) {
\r
322 intlogger.error(SQLEXCEPTION + e.getMessage(), e);
\r
329 public boolean doDelete(Connection c) {
\r
331 PreparedStatement ps = null;
\r
333 String sql = "delete from GROUPS where GROUPID = ?";
\r
334 ps = c.prepareStatement(sql);
\r
335 ps.setInt(1, groupid);
\r
337 } catch (SQLException e) {
\r
339 intlogger.warn("PROV0007 doDelete: " + e.getMessage(), e);
\r
345 } catch (SQLException e) {
\r
346 intlogger.error(SQLEXCEPTION + e.getMessage(), e);
\r
353 public String getKey() {
\r
354 return "" + getGroupid();
\r
358 public boolean equals(Object obj) {
\r
359 if (!(obj instanceof Group)) {
\r
362 Group os = (Group) obj;
\r
363 if (groupid != os.groupid) {
\r
366 if (authid != os.authid) {
\r
369 if (!name.equals(os.name)) {
\r
372 if (description != os.description) {
\r
375 if (!classification.equals(os.classification)) {
\r
378 if (!members.equals(os.members)) {
\r
386 public String toString() {
\r
387 return "GROUP: groupid=" + groupid;
\r
391 public int hashCode() {
\r
392 return Objects.hash(groupid, authid, name, description, classification, members, last_mod);
\r