cab4d3d2c973575da616bc55b46c2e8fb7ebff81
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / beans / Feed.java
1 /*******************************************************************************\r
2  * ============LICENSE_START==================================================\r
3  * * org.onap.dmaap\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
10  * *\r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * *\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
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 \r
24 \r
25 package org.onap.dmaap.datarouter.provisioning.beans;\r
26 \r
27 import com.att.eelf.configuration.EELFLogger;\r
28 import com.att.eelf.configuration.EELFManager;\r
29 import org.json.JSONArray;\r
30 import org.json.JSONException;\r
31 import org.json.JSONObject;\r
32 import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
33 import org.onap.dmaap.datarouter.provisioning.utils.JSONUtilities;\r
34 import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities;\r
35 \r
36 import java.io.InvalidObjectException;\r
37 import java.sql.*;\r
38 import java.util.Date;\r
39 import java.util.*;\r
40 \r
41 /**\r
42  * The representation of a Feed.  Feeds can be retrieved from the DB, or stored/updated in the DB.\r
43  *\r
44  * @author Robert Eby\r
45  * @version $Id: Feed.java,v 1.13 2013/10/28 18:06:52 eby Exp $\r
46  */\r
47 public class Feed extends Syncable {\r
48     private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");\r
49     private static int next_feedid = getMaxFeedID() + 1;\r
50     private static final String SQLEXCEPTION = "SQLException: ";\r
51 \r
52     private int feedid;\r
53     private int groupid; //New field is added - Groups feature Rally:US708115 - 1610\r
54     private String name;\r
55     private String version;\r
56     private String description;\r
57     private String business_description; // New field is added - Groups feature Rally:US708102 - 1610\r
58     private FeedAuthorization authorization;\r
59     private String publisher;\r
60     private FeedLinks links;\r
61     private boolean deleted;\r
62     private boolean suspended;\r
63     private Date last_mod;\r
64     private Date created_date;\r
65     private String aaf_instance;\r
66 \r
67     /**\r
68      * Check if a feed ID is valid.\r
69      *\r
70      * @param id the Feed ID\r
71      * @return true if it is valid\r
72      */\r
73     @SuppressWarnings("resource")\r
74     public static boolean isFeedValid(int id) {\r
75         int count = 0;\r
76         try {\r
77             DB db = new DB();\r
78             Connection conn = db.getConnection();\r
79             try(PreparedStatement stmt = conn.prepareStatement("select COUNT(*) from FEEDS where FEEDID = ?")) {\r
80                 stmt.setInt(1, id);\r
81                 try(ResultSet rs = stmt.executeQuery()) {\r
82                     if (rs.next()) {\r
83                         count = rs.getInt(1);\r
84                     }\r
85                 }\r
86             }\r
87             db.release(conn);\r
88         } catch (SQLException e) {\r
89             intlogger.warn("PROV0024 Feed.isFeedValid: " + e.getMessage(), e);\r
90         }\r
91         return count != 0;\r
92     }\r
93 \r
94     /**\r
95      * Get a specific feed from the DB, based upon its ID.\r
96      *\r
97      * @param id the Feed ID\r
98      * @return the Feed object, or null if it does not exist\r
99      */\r
100     public static Feed getFeedById(int id) {\r
101         String sql = "select * from FEEDS where FEEDID = " + id;\r
102         return getFeedBySQL(sql);\r
103     }\r
104 \r
105     /**\r
106      * Get a specific feed from the DB, based upon its name and version.\r
107      *\r
108      * @param name    the name of the Feed\r
109      * @param version the version of the Feed\r
110      * @return the Feed object, or null if it does not exist\r
111      */\r
112     public static Feed getFeedByNameVersion(String name, String version) {\r
113         name = name.replaceAll("'", "''");\r
114         version = version.replaceAll("'", "''");\r
115         String sql = "select * from FEEDS where NAME = '" + name + "' and VERSION ='" + version + "'";\r
116         return getFeedBySQL(sql);\r
117     }\r
118 \r
119     /**\r
120      * Return a count of the number of active feeds in the DB.\r
121      *\r
122      * @return the count\r
123      */\r
124     public static int countActiveFeeds() {\r
125         int count = 0;\r
126         try {\r
127             DB db = new DB();\r
128             @SuppressWarnings("resource")\r
129             Connection conn = db.getConnection();\r
130             try(Statement stmt = conn.createStatement()) {\r
131                 try (ResultSet rs = stmt.executeQuery("select count(*) from FEEDS where DELETED = 0")) {\r
132                     if (rs.next()) {\r
133                         count = rs.getInt(1);\r
134                     }\r
135                 }\r
136             }\r
137             db.release(conn);\r
138         } catch (SQLException e) {\r
139             intlogger.warn("PROV0025 Feed.countActiveFeeds: " + e.getMessage(), e);\r
140         }\r
141         return count;\r
142     }\r
143 \r
144     public static int getMaxFeedID() {\r
145         int max = 0;\r
146         try {\r
147             DB db = new DB();\r
148             @SuppressWarnings("resource")\r
149             Connection conn = db.getConnection();\r
150             try(Statement stmt = conn.createStatement()) {\r
151                 try (ResultSet rs = stmt.executeQuery("select MAX(feedid) from FEEDS")) {\r
152                     if (rs.next()) {\r
153                         max = rs.getInt(1);\r
154                     }\r
155                 }\r
156             }\r
157             db.release(conn);\r
158         } catch (SQLException e) {\r
159             intlogger.warn("PROV0026 Feed.getMaxFeedID: " + e.getMessage(), e);\r
160         }\r
161         return max;\r
162     }\r
163 \r
164     public static Collection<Feed> getAllFeeds() {\r
165         Map<Integer, Feed> map = new HashMap<Integer, Feed>();\r
166         try {\r
167             DB db = new DB();\r
168             @SuppressWarnings("resource")\r
169             Connection conn = db.getConnection();\r
170             try(Statement stmt = conn.createStatement()) {\r
171                 try(ResultSet rs = stmt.executeQuery("select * from FEEDS")) {\r
172                     while (rs.next()) {\r
173                         Feed feed = new Feed(rs);\r
174                         map.put(feed.getFeedid(), feed);\r
175                     }\r
176                 }\r
177 \r
178                 String sql = "select * from FEED_ENDPOINT_IDS";\r
179                 try(ResultSet rs = stmt.executeQuery(sql)){\r
180                     while (rs.next()) {\r
181                         int id = rs.getInt("FEEDID");\r
182                         Feed feed = map.get(id);\r
183                         if (feed != null) {\r
184                             FeedEndpointID epi = new FeedEndpointID(rs);\r
185                             Collection<FeedEndpointID> ecoll = feed.getAuthorization().getEndpoint_ids();\r
186                             ecoll.add(epi);\r
187                         }\r
188                     }\r
189                 }\r
190 \r
191                 sql = "select * from FEED_ENDPOINT_ADDRS";\r
192                 try(ResultSet rs = stmt.executeQuery(sql)) {\r
193                     while (rs.next()) {\r
194                         int id = rs.getInt("FEEDID");\r
195                         Feed feed = map.get(id);\r
196                         if (feed != null) {\r
197                             Collection<String> acoll = feed.getAuthorization().getEndpoint_addrs();\r
198                             acoll.add(rs.getString("ADDR"));\r
199                         }\r
200                     }\r
201                 }\r
202             }\r
203             db.release(conn);\r
204         } catch (SQLException e) {\r
205             intlogger.warn("PROV0027 Feed.getAllFeeds: " + e.getMessage(), e);\r
206         }\r
207         return map.values();\r
208     }\r
209 \r
210     public static List<String> getFilteredFeedUrlList(final String name, final String val) {\r
211         List<String> list = new ArrayList<String>();\r
212         String sql = "select SELF_LINK from FEEDS where DELETED = 0";\r
213         if (name.equals("name")) {\r
214             sql += " and NAME = ?";\r
215         } else if (name.equals("publ")) {\r
216             sql += " and PUBLISHER = ?";\r
217         } else if (name.equals("subs")) {\r
218             sql = "select distinct FEEDS.SELF_LINK from FEEDS, SUBSCRIPTIONS " +\r
219                     "where DELETED = 0 " +\r
220                     "and FEEDS.FEEDID = SUBSCRIPTIONS.FEEDID " +\r
221                     "and SUBSCRIPTIONS.SUBSCRIBER = ?";\r
222         }\r
223         try {\r
224             DB db = new DB();\r
225             @SuppressWarnings("resource")\r
226             Connection conn = db.getConnection();\r
227             try(PreparedStatement ps = conn.prepareStatement(sql)) {\r
228                 if (sql.indexOf('?') >= 0)\r
229                     ps.setString(1, val);\r
230                 try(ResultSet rs = ps.executeQuery()) {\r
231                     while (rs.next()) {\r
232                         String t = rs.getString(1);\r
233                         list.add(t.trim());\r
234                     }\r
235                 }\r
236             }\r
237             db.release(conn);\r
238         } catch (SQLException e) {\r
239             intlogger.warn("PROV0028 Feed.getFilteredFeedUrlList: " + e.getMessage(), e);\r
240         }\r
241         return list;\r
242     }\r
243 \r
244     @SuppressWarnings("resource")\r
245     private static Feed getFeedBySQL(String sql) {\r
246         Feed feed = null;\r
247         try {\r
248             DB db = new DB();\r
249             Connection conn = db.getConnection();\r
250             try (Statement stmt = conn.createStatement()) {\r
251                 try (ResultSet rs = stmt.executeQuery(sql)) {\r
252                     if (rs.next()) {\r
253                         feed = new Feed(rs);\r
254                     }\r
255                 }\r
256                 if (feed != null) {\r
257                     sql = "select * from FEED_ENDPOINT_IDS where FEEDID = " + feed.feedid;\r
258                     try (ResultSet rs = stmt.executeQuery(sql)) {\r
259                         Collection<FeedEndpointID> ecoll = feed.getAuthorization().getEndpoint_ids();\r
260                         while (rs.next()) {\r
261                             FeedEndpointID epi = new FeedEndpointID(rs);\r
262                             ecoll.add(epi);\r
263                         }\r
264                     }\r
265                     sql = "select * from FEED_ENDPOINT_ADDRS where FEEDID = " + feed.feedid;\r
266                     try (ResultSet rs = stmt.executeQuery(sql)) {\r
267                         Collection<String> acoll = feed.getAuthorization().getEndpoint_addrs();\r
268                         while (rs.next()) {\r
269                             acoll.add(rs.getString("ADDR"));\r
270                         }\r
271                     }\r
272                 }\r
273             }\r
274             db.release(conn);\r
275         } catch (SQLException e) {\r
276             intlogger.warn("PROV0029 Feed.getFeedBySQL: " + e.getMessage(), e);\r
277         }\r
278         return feed;\r
279     }\r
280 \r
281     public Feed() {\r
282         this("", "", "", "");\r
283     }\r
284 \r
285     public Feed(String name, String version, String desc, String business_description) {\r
286         this.feedid = -1;\r
287         this.groupid = -1; //New field is added - Groups feature Rally:US708115 - 1610\r
288         this.name = name;\r
289         this.version = version;\r
290         this.description = desc;\r
291         this.business_description = business_description; // New field is added - Groups feature Rally:US708102 - 1610\r
292         this.authorization = new FeedAuthorization();\r
293         this.publisher = "";\r
294         this.links = new FeedLinks();\r
295         this.deleted = false;\r
296         this.suspended = false;\r
297         this.last_mod = new Date();\r
298         this.created_date = new Date();\r
299         this.aaf_instance = "";\r
300     }\r
301 \r
302     public Feed(ResultSet rs) throws SQLException {\r
303         this.feedid = rs.getInt("FEEDID");\r
304         this.groupid = rs.getInt("GROUPID"); //New field is added - Groups feature Rally:US708115 - 1610\r
305         this.name = rs.getString("NAME");\r
306         this.version = rs.getString("VERSION");\r
307         this.description = rs.getString("DESCRIPTION");\r
308         this.business_description = rs.getString("BUSINESS_DESCRIPTION"); // New field is added - Groups feature Rally:US708102 - 1610\r
309         this.authorization = new FeedAuthorization();\r
310         this.authorization.setClassification(rs.getString("AUTH_CLASS"));\r
311         this.publisher = rs.getString("PUBLISHER");\r
312         this.links = new FeedLinks();\r
313         this.links.setSelf(rs.getString("SELF_LINK"));\r
314         this.links.setPublish(rs.getString("PUBLISH_LINK"));\r
315         this.links.setSubscribe(rs.getString("SUBSCRIBE_LINK"));\r
316         this.links.setLog(rs.getString("LOG_LINK"));\r
317         this.deleted = rs.getBoolean("DELETED");\r
318         this.suspended = rs.getBoolean("SUSPENDED");\r
319         this.last_mod = rs.getDate("LAST_MOD");\r
320         this.created_date = rs.getTimestamp("CREATED_DATE");\r
321         this.aaf_instance = rs.getString("AAF_INSTANCE");\r
322     }\r
323 \r
324     public Feed(JSONObject jo) throws InvalidObjectException {\r
325         this("", "", "", "");\r
326         try {\r
327             // The JSONObject is assumed to contain a vnd.dmaap-dr.feed representation\r
328             this.feedid = jo.optInt("feedid", -1);\r
329             this.groupid = jo.optInt("groupid");\r
330             this.name = jo.getString("name");\r
331             this.aaf_instance = jo.optString("aaf_instance", "legacy");\r
332             if(!(aaf_instance.equalsIgnoreCase("legacy"))){\r
333                 if (aaf_instance.length() > 255){\r
334                     throw new InvalidObjectException("aaf_instance field is too long");\r
335                 }\r
336             }\r
337             if (name.length() > 255)\r
338                 throw new InvalidObjectException("name field is too long");\r
339             try {\r
340                 this.version = jo.getString("version");\r
341             } catch (JSONException e) {\r
342                 intlogger.warn("PROV0023 Feed.Feed: " + e.getMessage(), e);\r
343                 this.version = null;\r
344             }\r
345             if(version != null && version.length() > 20)\r
346                 throw new InvalidObjectException("version field is too long");\r
347             this.description = jo.optString("description");\r
348             this.business_description = jo.optString("business_description");\r
349             if (description.length() > 1000)\r
350                 throw new InvalidObjectException("technical description field is too long");\r
351             if (business_description.length() > 1000)\r
352                 throw new InvalidObjectException("business description field is too long");\r
353             this.authorization = new FeedAuthorization();\r
354             JSONObject jauth = jo.getJSONObject("authorization");\r
355             this.authorization.setClassification(jauth.getString("classification"));\r
356             if (this.authorization.getClassification().length() > 32)\r
357                 throw new InvalidObjectException("classification field is too long");\r
358             JSONArray endPointIds = jauth.getJSONArray("endpoint_ids");\r
359             for (int i = 0; i < endPointIds.length(); i++) {\r
360                 JSONObject id = endPointIds.getJSONObject(i);\r
361                 FeedEndpointID fid = new FeedEndpointID(id.getString("id"), id.getString("password"));\r
362                 if (fid.getId().length() > 60)\r
363                     throw new InvalidObjectException("id field is too long (" + fid.getId() + ")");\r
364                 if (fid.getPassword().length() > 32)\r
365                     throw new InvalidObjectException("password field is too long ("+ fid.getPassword()+")");  //Fortify scan fixes - Privacy Violation\r
366                 this.authorization.getEndpoint_ids().add(fid);\r
367             }\r
368             if (this.authorization.getEndpoint_ids().size() < 1)\r
369                 throw new InvalidObjectException("need to specify at least one endpoint_id");\r
370             endPointIds = jauth.getJSONArray("endpoint_addrs");\r
371             for (int i = 0; i < endPointIds.length(); i++) {\r
372                 String addr = endPointIds.getString(i);\r
373                 if (!JSONUtilities.validIPAddrOrSubnet(addr))\r
374                     throw new InvalidObjectException("bad IP addr or subnet mask: " + addr);\r
375                 this.authorization.getEndpoint_addrs().add(addr);\r
376             }\r
377 \r
378             this.publisher = jo.optString("publisher", "");\r
379             this.deleted = jo.optBoolean("deleted", false);\r
380             this.suspended = jo.optBoolean("suspend", false);\r
381             JSONObject jol = jo.optJSONObject("links");\r
382             this.links = (jol == null) ? (new FeedLinks()) : (new FeedLinks(jol));\r
383         } catch (InvalidObjectException e) {\r
384             throw e;\r
385         } catch (Exception e) {\r
386             intlogger.warn("Invalid JSON: " + e.getMessage(), e);\r
387             throw new InvalidObjectException("Invalid JSON: " + e.getMessage());\r
388         }\r
389     }\r
390 \r
391     public int getFeedid() {\r
392         return feedid;\r
393     }\r
394 \r
395     public void setFeedid(int feedid) {\r
396         this.feedid = feedid;\r
397 \r
398         // Create link URLs\r
399         FeedLinks fl = getLinks();\r
400         fl.setSelf(URLUtilities.generateFeedURL(feedid));\r
401         fl.setPublish(URLUtilities.generatePublishURL(feedid));\r
402         fl.setSubscribe(URLUtilities.generateSubscribeURL(feedid));\r
403         fl.setLog(URLUtilities.generateFeedLogURL(feedid));\r
404     }\r
405 \r
406     public String getAafInstance() {\r
407         return aaf_instance;\r
408     }\r
409 \r
410     public void setAaf_instance(String aaf_instance) {\r
411         this.aaf_instance = aaf_instance;\r
412     }\r
413 \r
414     //new getter setters for groups- Rally:US708115 - 1610\r
415     public int getGroupid() {\r
416         return groupid;\r
417     }\r
418 \r
419     public void setGroupid(int groupid) {\r
420         this.groupid = groupid;\r
421     }\r
422 \r
423     public String getName() {\r
424         return name;\r
425     }\r
426 \r
427     public void setName(String name) {\r
428         this.name = name;\r
429     }\r
430 \r
431     public String getVersion() {\r
432         return version;\r
433     }\r
434 \r
435     public void setVersion(String version) {\r
436         this.version = version;\r
437     }\r
438 \r
439     public String getDescription() {\r
440         return description;\r
441     }\r
442 \r
443     public void setDescription(String description) {\r
444         this.description = description;\r
445     }\r
446 \r
447     // New field is added - Groups feature Rally:US708102 - 1610\r
448     public String getBusiness_description() {\r
449         return business_description;\r
450     }\r
451 \r
452     public void setBusiness_description(String business_description) {\r
453         this.business_description = business_description;\r
454     }\r
455 \r
456     public FeedAuthorization getAuthorization() {\r
457         return authorization;\r
458     }\r
459 \r
460     public void setAuthorization(FeedAuthorization authorization) {\r
461         this.authorization = authorization;\r
462     }\r
463 \r
464     public String getPublisher() {\r
465         return publisher;\r
466     }\r
467 \r
468     public void setPublisher(String publisher) {\r
469         if (publisher != null) {\r
470             if (publisher.length() > 8)\r
471                 publisher = publisher.substring(0, 8);\r
472             this.publisher = publisher;\r
473         }\r
474     }\r
475 \r
476     public FeedLinks getLinks() {\r
477         return links;\r
478     }\r
479 \r
480     public void setLinks(FeedLinks links) {\r
481         this.links = links;\r
482     }\r
483 \r
484     public boolean isDeleted() {\r
485         return deleted;\r
486     }\r
487 \r
488     public void setDeleted(boolean deleted) {\r
489         this.deleted = deleted;\r
490     }\r
491 \r
492     public boolean isSuspended() {\r
493         return suspended;\r
494     }\r
495 \r
496     public void setSuspended(boolean suspended) {\r
497         this.suspended = suspended;\r
498     }\r
499 \r
500     public Date getLast_mod() {\r
501         return last_mod;\r
502     }\r
503 \r
504     public Date getCreated_date() {\r
505         return created_date;\r
506     }\r
507 \r
508     @Override\r
509     public JSONObject asJSONObject() {\r
510         JSONObject jo = new JSONObject();\r
511         jo.put("feedid", feedid);\r
512         jo.put("groupid", groupid); //New field is added - Groups feature Rally:US708115 - 1610\r
513         jo.put("name", name);\r
514         jo.put("version", version);\r
515         jo.put("description", description);\r
516         jo.put("business_description", business_description); // New field is added - Groups feature Rally:US708102 - 1610\r
517         jo.put("authorization", authorization.asJSONObject());\r
518         jo.put("publisher", publisher);\r
519         jo.put("links", links.asJSONObject());\r
520         jo.put("deleted", deleted);\r
521         jo.put("suspend", suspended);\r
522         jo.put("last_mod", last_mod.getTime());\r
523         jo.put("created_date", created_date.getTime());\r
524         jo.put("aaf_instance", aaf_instance);\r
525         return jo;\r
526     }\r
527 \r
528     public JSONObject asLimitedJSONObject() {\r
529         JSONObject jo = asJSONObject();\r
530         jo.remove("deleted");\r
531         jo.remove("feedid");\r
532         jo.remove("last_mod");\r
533         jo.remove("created_date");\r
534         return jo;\r
535     }\r
536 \r
537     public JSONObject asJSONObject(boolean hidepasswords) {\r
538         JSONObject jo = asJSONObject();\r
539         if (hidepasswords) {\r
540             jo.remove("feedid");    // we no longer hide passwords, however we do hide these\r
541             jo.remove("deleted");\r
542             jo.remove("last_mod");\r
543             jo.remove("created_date");\r
544         }\r
545         return jo;\r
546     }\r
547 \r
548     @Override\r
549     public boolean doDelete(Connection c) {\r
550         boolean rv = true;\r
551         PreparedStatement ps = null;\r
552         try {\r
553             String sql = "delete from FEEDS where FEEDID = ?";\r
554             ps = c.prepareStatement(sql);\r
555             ps.setInt(1, feedid);\r
556             ps.execute();\r
557         } catch (SQLException e) {\r
558             rv = false;\r
559             intlogger.error("PROV0007 doDelete: " + e.getMessage(), e);\r
560         } finally {\r
561             try {\r
562                 if(ps!=null) {\r
563                     ps.close();\r
564                 }\r
565             } catch (SQLException e) {\r
566                 intlogger.error(SQLEXCEPTION + e.getMessage(), e);\r
567             }\r
568         }\r
569         return rv;\r
570     }\r
571 \r
572     @Override\r
573     public synchronized boolean doInsert(Connection c) {\r
574         boolean rv = true;\r
575         try {\r
576             if (feedid == -1) {\r
577                 setFeedid(next_feedid++);\r
578             }\r
579             // In case we insert a feed from synchronization\r
580             if (feedid > next_feedid)\r
581                 next_feedid = feedid + 1;\r
582 \r
583             // Create FEED_ENDPOINT_IDS rows\r
584             FeedAuthorization auth = getAuthorization();\r
585             String sql = "insert into FEED_ENDPOINT_IDS values (?, ?, ?)";\r
586             try(PreparedStatement ps2 = c.prepareStatement(sql)) {\r
587                 for (FeedEndpointID fid : auth.getEndpoint_ids()) {\r
588                     ps2.setInt(1, feedid);\r
589                     ps2.setString(2, fid.getId());\r
590                     ps2.setString(3, fid.getPassword());\r
591                     ps2.executeUpdate();\r
592                 }\r
593             }\r
594 \r
595             // Create FEED_ENDPOINT_ADDRS rows\r
596             sql = "insert into FEED_ENDPOINT_ADDRS values (?, ?)";\r
597             try(PreparedStatement ps2 = c.prepareStatement(sql)) {\r
598                 for (String t : auth.getEndpoint_addrs()) {\r
599                     ps2.setInt(1, feedid);\r
600                     ps2.setString(2, t);\r
601                     ps2.executeUpdate();\r
602                 }\r
603             }\r
604 \r
605             // Finally, create the FEEDS row\r
606             sql = "insert into FEEDS (FEEDID, NAME, VERSION, DESCRIPTION, AUTH_CLASS, PUBLISHER, SELF_LINK, PUBLISH_LINK, SUBSCRIBE_LINK, LOG_LINK, DELETED, SUSPENDED,BUSINESS_DESCRIPTION, GROUPID, AAF_INSTANCE) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";\r
607             try(PreparedStatement ps2 = c.prepareStatement(sql)) {\r
608                 ps2.setInt(1, feedid);\r
609                 ps2.setString(2, getName());\r
610                 ps2.setString(3, getVersion());\r
611                 ps2.setString(4, getDescription());\r
612                 ps2.setString(5, getAuthorization().getClassification());\r
613                 ps2.setString(6, getPublisher());\r
614                 ps2.setString(7, getLinks().getSelf());\r
615                 ps2.setString(8, getLinks().getPublish());\r
616                 ps2.setString(9, getLinks().getSubscribe());\r
617                 ps2.setString(10, getLinks().getLog());\r
618                 ps2.setBoolean(11, isDeleted());\r
619                 ps2.setBoolean(12, isSuspended());\r
620                 ps2.setString(13, getBusiness_description());\r
621                 ps2.setInt(14, groupid);\r
622                 ps2.setString(15, getAafInstance());\r
623                 ps2.executeUpdate();\r
624             }\r
625         } catch (SQLException e) {\r
626             rv = false;\r
627             intlogger.error("PROV0005 doInsert: " + e.getMessage(), e);\r
628         }\r
629         return rv;\r
630     }\r
631 \r
632     @Override\r
633     public boolean doUpdate(Connection c) {\r
634         boolean rv = true;\r
635         Feed oldobj = getFeedById(feedid);\r
636         PreparedStatement ps = null;\r
637         try {\r
638             Set<FeedEndpointID> newset = getAuthorization().getEndpoint_ids();\r
639             Set<FeedEndpointID> oldset = oldobj.getAuthorization().getEndpoint_ids();\r
640 \r
641             // Insert new FEED_ENDPOINT_IDS rows\r
642             String sql = "insert into FEED_ENDPOINT_IDS values (?, ?, ?)";\r
643             ps = c.prepareStatement(sql);\r
644             for (FeedEndpointID fid : newset) {\r
645                 if (!oldset.contains(fid)) {\r
646                     ps.setInt(1, feedid);\r
647                     ps.setString(2, fid.getId());\r
648                     ps.setString(3, fid.getPassword());\r
649                     ps.executeUpdate();\r
650                 }\r
651             }\r
652             ps.close();\r
653 \r
654             // Delete old FEED_ENDPOINT_IDS rows\r
655             sql = "delete from FEED_ENDPOINT_IDS where FEEDID = ? AND USERID = ? AND PASSWORD = ?";\r
656             ps = c.prepareStatement(sql);\r
657             for (FeedEndpointID fid : oldset) {\r
658                 if (!newset.contains(fid)) {\r
659                     ps.setInt(1, feedid);\r
660                     ps.setString(2, fid.getId());\r
661                     ps.setString(3, fid.getPassword());\r
662                     ps.executeUpdate();\r
663                 }\r
664             }\r
665             ps.close();\r
666 \r
667             // Insert new FEED_ENDPOINT_ADDRS rows\r
668             Set<String> newset2 = getAuthorization().getEndpoint_addrs();\r
669             Set<String> oldset2 = oldobj.getAuthorization().getEndpoint_addrs();\r
670             sql = "insert into FEED_ENDPOINT_ADDRS values (?, ?)";\r
671             ps = c.prepareStatement(sql);\r
672             for (String t : newset2) {\r
673                 if (!oldset2.contains(t)) {\r
674                     ps.setInt(1, feedid);\r
675                     ps.setString(2, t);\r
676                     ps.executeUpdate();\r
677                 }\r
678             }\r
679             ps.close();\r
680 \r
681             // Delete old FEED_ENDPOINT_ADDRS rows\r
682             sql = "delete from FEED_ENDPOINT_ADDRS where FEEDID = ? AND ADDR = ?";\r
683             ps = c.prepareStatement(sql);\r
684             for (String t : oldset2) {\r
685                 if (!newset2.contains(t)) {\r
686                     ps.setInt(1, feedid);\r
687                     ps.setString(2, t);\r
688                     ps.executeUpdate();\r
689                 }\r
690             }\r
691             ps.close();\r
692 \r
693             // Finally, update the FEEDS row\r
694             sql = "update FEEDS set DESCRIPTION = ?, AUTH_CLASS = ?, DELETED = ?, SUSPENDED = ?, BUSINESS_DESCRIPTION=?, GROUPID=? where FEEDID = ?";\r
695             ps = c.prepareStatement(sql);\r
696             ps.setString(1, getDescription());\r
697             ps.setString(2, getAuthorization().getClassification());\r
698             ps.setInt(3, deleted ? 1 : 0);\r
699             ps.setInt(4, suspended ? 1 : 0);\r
700             ps.setString(5, getBusiness_description());\r
701             ps.setInt(6, groupid);\r
702             ps.setInt(7, feedid);\r
703             ps.executeUpdate();\r
704             ps.close();\r
705         } catch (SQLException e) {\r
706             rv = false;\r
707             intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e);\r
708         } finally {\r
709             try {\r
710                 if (ps != null)\r
711                     ps.close();\r
712             } catch (SQLException e) {\r
713                 intlogger.error(SQLEXCEPTION + e.getMessage(), e);\r
714             }\r
715         }\r
716         return rv;\r
717     }\r
718 \r
719     /**\r
720      * Rally US708115\r
721      * Change Ownership of FEED - 1610\r
722      */\r
723     public boolean changeOwnerShip() {\r
724         boolean rv = true;\r
725         PreparedStatement ps = null;\r
726         try {\r
727 \r
728             DB db = new DB();\r
729             @SuppressWarnings("resource")\r
730             Connection c = db.getConnection();\r
731             String sql = "update FEEDS set PUBLISHER = ? where FEEDID = ?";\r
732             ps = c.prepareStatement(sql);\r
733             ps.setString(1, this.publisher);\r
734             ps.setInt(2, feedid);\r
735             ps.execute();\r
736             ps.close();\r
737         } catch (SQLException e) {\r
738             rv = false;\r
739             intlogger.warn("PROV0008 changeOwnerShip: " + e.getMessage(), e);\r
740         } finally {\r
741             try {\r
742                 if(ps!=null) {\r
743                     ps.close();\r
744                 }\r
745             } catch (SQLException e) {\r
746                 intlogger.error(SQLEXCEPTION + e.getMessage(), e);\r
747             }\r
748         }\r
749         return rv;\r
750     }\r
751 \r
752 \r
753     @Override\r
754     public String getKey() {\r
755         return "" + getFeedid();\r
756     }\r
757 \r
758     @Override\r
759     public boolean equals(Object obj) {\r
760         if (!(obj instanceof Feed))\r
761             return false;\r
762         Feed of = (Feed) obj;\r
763         if (feedid != of.feedid)\r
764             return false;\r
765         if (groupid != of.groupid) //New field is added - Groups feature Rally:US708115 - 1610\r
766             return false;\r
767         if (!name.equals(of.name))\r
768             return false;\r
769         if (!version.equals(of.version))\r
770             return false;\r
771         if (!description.equals(of.description))\r
772             return false;\r
773         if (!business_description.equals(of.business_description)) // New field is added - Groups feature Rally:US708102 - 1610\r
774             return false;\r
775         if (!publisher.equals(of.publisher))\r
776             return false;\r
777         if (!authorization.equals(of.authorization))\r
778             return false;\r
779         if (!links.equals(of.links))\r
780             return false;\r
781         if (deleted != of.deleted)\r
782             return false;\r
783         if (suspended != of.suspended)\r
784             return false;\r
785         if (!aaf_instance.equals(of.aaf_instance))\r
786             return false;\r
787         return true;\r
788     }\r
789 \r
790     @Override\r
791     public String toString() {\r
792         return "FEED: feedid=" + feedid + ", name=" + name + ", version=" + version;\r
793     }\r
794 \r
795     @Override\r
796     public int hashCode() {\r
797         return super.hashCode();\r
798     }\r
799 }