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