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 com.att.research.datarouter.provisioning.beans;
\r
26 import java.io.InvalidObjectException;
\r
27 import java.sql.Connection;
\r
28 import java.sql.PreparedStatement;
\r
29 import java.sql.ResultSet;
\r
30 import java.sql.SQLException;
\r
31 import java.sql.Statement;
\r
32 import java.util.ArrayList;
\r
33 import java.util.Collection;
\r
34 import java.util.Date;
\r
35 import java.util.List;
\r
37 import org.apache.log4j.Logger;
\r
38 import org.json.JSONObject;
\r
40 import com.att.research.datarouter.provisioning.utils.DB;
\r
41 import com.att.research.datarouter.provisioning.utils.URLUtilities;
\r
44 * 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
49 private static Logger intlogger = Logger.getLogger("com.att.research.datarouter.provisioning.internal");
\r
50 private static int next_groupid = getMaxGroupID() + 1;
\r
52 private int groupid;
\r
53 private String authid;
\r
54 private String name;
\r
55 private String description;
\r
56 private String classification;
\r
57 private String members;
\r
58 private Date last_mod;
\r
61 public static Group getGroupMatching(Group gup) {
\r
62 String sql = String.format(
\r
63 "select * from GROUPS where NAME = \"%s\"",
\r
66 List<Group> list = getGroupsForSQL(sql);
\r
67 return list.size() > 0 ? list.get(0) : null;
\r
70 public static Group getGroupMatching(Group gup, int groupid) {
\r
71 String sql = String.format(
\r
72 "select * from GROUPS where NAME = \"%s\" and GROUPID != %d ",
\r
76 List<Group> list = getGroupsForSQL(sql);
\r
77 return list.size() > 0 ? list.get(0) : null;
\r
80 public static Group getGroupById(int id) {
\r
81 String sql = "select * from GROUPS where GROUPID = " + id;
\r
82 List<Group> list = getGroupsForSQL(sql);
\r
83 return list.size() > 0 ? list.get(0) : null;
\r
86 public static Group getGroupByAuthId(String id) {
\r
87 String sql = "select * from GROUPS where AUTHID = '" + id +"'";
\r
88 List<Group> list = getGroupsForSQL(sql);
\r
89 return list.size() > 0 ? list.get(0) : null;
\r
92 public static Collection<Group> getAllgroups() {
\r
93 return getGroupsForSQL("select * from GROUPS");
\r
95 private static List<Group> getGroupsForSQL(String sql) {
\r
96 List<Group> list = new ArrayList<Group>();
\r
99 @SuppressWarnings("resource")
\r
100 Connection conn = db.getConnection();
\r
101 Statement stmt = conn.createStatement();
\r
102 ResultSet rs = stmt.executeQuery(sql);
\r
103 while (rs.next()) {
\r
104 Group group = new Group(rs);
\r
110 } catch (SQLException e) {
\r
111 e.printStackTrace();
\r
115 public static int getMaxGroupID() {
\r
119 @SuppressWarnings("resource")
\r
120 Connection conn = db.getConnection();
\r
121 Statement stmt = conn.createStatement();
\r
122 ResultSet rs = stmt.executeQuery("select MAX(groupid) from GROUPS");
\r
124 max = rs.getInt(1);
\r
129 } catch (SQLException e) {
\r
130 intlogger.info("getMaxSubID: "+e.getMessage());
\r
131 e.printStackTrace();
\r
135 public static Collection<String> getGroupsByClassfication(String classfication) {
\r
136 List<String> list = new ArrayList<String>();
\r
137 String sql = "select * from GROUPS where classification = '"+classfication+"'";
\r
140 @SuppressWarnings("resource")
\r
141 Connection conn = db.getConnection();
\r
142 Statement stmt = conn.createStatement();
\r
143 ResultSet rs = stmt.executeQuery(sql);
\r
144 while (rs.next()) {
\r
145 int groupid = rs.getInt("groupid");
\r
146 //list.add(URLUtilities.generateSubscriptionURL(groupid));
\r
151 } catch (SQLException e) {
\r
152 e.printStackTrace();
\r
157 * Return a count of the number of active subscriptions in the DB.
\r
158 * @return the count
\r
160 public static int countActiveSubscriptions() {
\r
164 @SuppressWarnings("resource")
\r
165 Connection conn = db.getConnection();
\r
166 Statement stmt = conn.createStatement();
\r
167 ResultSet rs = stmt.executeQuery("select count(*) from SUBSCRIPTIONS");
\r
169 count = rs.getInt(1);
\r
174 } catch (SQLException e) {
\r
175 intlogger.warn("PROV0008 countActiveSubscriptions: "+e.getMessage());
\r
176 e.printStackTrace();
\r
184 public Group(String name, String desc, String members) {
\r
188 this.description = desc;
\r
189 this.members = members;
\r
190 this.classification = "";
\r
191 this.last_mod = new Date();
\r
195 public Group(ResultSet rs) throws SQLException {
\r
196 this.groupid = rs.getInt("GROUPID");
\r
197 this.authid = rs.getString("AUTHID");
\r
198 this.name = rs.getString("NAME");
\r
199 this.description = rs.getString("DESCRIPTION");
\r
200 this.classification = rs.getString("CLASSIFICATION");
\r
201 this.members = rs.getString("MEMBERS");
\r
202 this.last_mod = rs.getDate("LAST_MOD");
\r
207 public Group(JSONObject jo) throws InvalidObjectException {
\r
210 // The JSONObject is assumed to contain a vnd.att-dr.group representation
\r
211 this.groupid = jo.optInt("groupid", -1);
\r
212 String gname = jo.getString("name");
\r
213 String gdescription = jo.getString("description");
\r
215 this.authid = jo.getString("authid");
\r
217 this.description = gdescription;
\r
218 this.classification = jo.getString("classification");
\r
219 this.members = jo.getString("members");
\r
221 if (gname.length() > 50)
\r
222 throw new InvalidObjectException("Group name is too long");
\r
223 if (gdescription.length() > 256)
\r
224 throw new InvalidObjectException("Group Description is too long");
\r
225 } catch (InvalidObjectException e) {
\r
227 } catch (Exception e) {
\r
228 throw new InvalidObjectException("invalid JSON: "+e.getMessage());
\r
231 public int getGroupid() {
\r
235 public static Logger getIntlogger() {
\r
238 public void setGroupid(int groupid) {
\r
239 this.groupid = groupid;
\r
242 public static void setIntlogger(Logger intlogger) {
\r
243 Group.intlogger = intlogger;
\r
245 public static int getNext_groupid() {
\r
246 return next_groupid;
\r
248 public static void setNext_groupid(int next_groupid) {
\r
249 Group.next_groupid = next_groupid;
\r
251 public String getAuthid() {
\r
254 public void setAuthid(String authid) {
\r
255 this.authid = authid;
\r
257 public String getName() {
\r
260 public void setName(String name) {
\r
263 public String getDescription() {
\r
264 return description;
\r
266 public void setDescription(String description) {
\r
267 this.description = description;
\r
269 public String getClassification() {
\r
270 return classification;
\r
272 public void setClassification(String classification) {
\r
273 this.classification = classification;
\r
275 public String getMembers() {
\r
278 public void setMembers(String members) {
\r
279 this.members = members;
\r
281 public Date getLast_mod() {
\r
284 public void setLast_mod(Date last_mod) {
\r
285 this.last_mod = last_mod;
\r
290 public JSONObject asJSONObject() {
\r
291 JSONObject jo = new JSONObject();
\r
292 jo.put("groupid", groupid);
\r
293 jo.put("authid", authid);
\r
294 jo.put("name", name);
\r
295 jo.put("description", description);
\r
296 jo.put("classification", classification);
\r
297 jo.put("members", members);
\r
298 jo.put("last_mod", last_mod.getTime());
\r
302 public boolean doInsert(Connection c) {
\r
304 PreparedStatement ps = null;
\r
306 if (groupid == -1) {
\r
307 // No feed ID assigned yet, so assign the next available one
\r
308 setGroupid(next_groupid++);
\r
310 // In case we insert a gropup from synchronization
\r
311 if (groupid > next_groupid)
\r
312 next_groupid = groupid+1;
\r
315 // Create the GROUPS row
\r
316 String sql = "insert into GROUPS (GROUPID, AUTHID, NAME, DESCRIPTION, CLASSIFICATION, MEMBERS) values (?, ?, ?, ?, ?, ?)";
\r
317 ps = c.prepareStatement(sql, new String[] { "GROUPID" });
\r
318 ps.setInt(1, groupid);
\r
319 ps.setString(2, authid);
\r
320 ps.setString(3, name);
\r
321 ps.setString(4, description);
\r
322 ps.setString(5, classification);
\r
323 ps.setString(6, members);
\r
326 } catch (SQLException e) {
\r
328 intlogger.warn("PROV0005 doInsert: "+e.getMessage());
\r
329 e.printStackTrace();
\r
333 } catch (SQLException e) {
\r
334 e.printStackTrace();
\r
340 public boolean doUpdate(Connection c) {
\r
342 PreparedStatement ps = null;
\r
344 String sql = "update GROUPS set AUTHID = ?, NAME = ?, DESCRIPTION = ?, CLASSIFICATION = ? , MEMBERS = ? where GROUPID = ?";
\r
345 ps = c.prepareStatement(sql);
\r
346 ps.setString(1, authid);
\r
347 ps.setString(2, name);
\r
348 ps.setString(3, description);
\r
349 ps.setString(4, classification);
\r
350 ps.setString(5, members);
\r
351 ps.setInt(6, groupid);
\r
352 ps.executeUpdate();
\r
353 } catch (SQLException e) {
\r
355 intlogger.warn("PROV0006 doUpdate: "+e.getMessage());
\r
356 e.printStackTrace();
\r
360 } catch (SQLException e) {
\r
361 e.printStackTrace();
\r
367 public boolean doDelete(Connection c) {
\r
369 PreparedStatement ps = null;
\r
371 String sql = "delete from GROUPS where GROUPID = ?";
\r
372 ps = c.prepareStatement(sql);
\r
373 ps.setInt(1, groupid);
\r
375 } catch (SQLException e) {
\r
377 intlogger.warn("PROV0007 doDelete: "+e.getMessage());
\r
378 e.printStackTrace();
\r
382 } catch (SQLException e) {
\r
383 e.printStackTrace();
\r
389 public String getKey() {
\r
390 return ""+getGroupid();
\r
393 public boolean equals(Object obj) {
\r
394 if (!(obj instanceof Group))
\r
396 Group os = (Group) obj;
\r
397 if (groupid != os.groupid)
\r
399 if (authid != os.authid)
\r
401 if (!name.equals(os.name))
\r
403 if (description != os.description)
\r
405 if (!classification.equals(os.classification))
\r
407 if (!members.equals(os.members))
\r
414 public String toString() {
\r
415 return "GROUP: groupid=" + groupid;
\r