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