Removing clumsy code smells and adding missing JettyFilter test
[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<>();\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<>();\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")) && aaf_instance.length() > 255){\r
333                     throw new InvalidObjectException("aaf_instance field is too long");\r
334             }\r
335             if (name.length() > 255)\r
336                 throw new InvalidObjectException("name field is too long");\r
337             try {\r
338                 this.version = jo.getString("version");\r
339             } catch (JSONException e) {\r
340                 intlogger.warn("PROV0023 Feed.Feed: " + e.getMessage(), 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().isEmpty())\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             throw e;\r
383         } catch (Exception e) {\r
384             intlogger.warn("Invalid JSON: " + e.getMessage(), e);\r
385             throw new InvalidObjectException("Invalid JSON: " + e.getMessage());\r
386         }\r
387     }\r
388 \r
389     public int getFeedid() {\r
390         return feedid;\r
391     }\r
392 \r
393     public void setFeedid(int feedid) {\r
394         this.feedid = feedid;\r
395 \r
396         // Create link URLs\r
397         FeedLinks fl = getLinks();\r
398         fl.setSelf(URLUtilities.generateFeedURL(feedid));\r
399         fl.setPublish(URLUtilities.generatePublishURL(feedid));\r
400         fl.setSubscribe(URLUtilities.generateSubscribeURL(feedid));\r
401         fl.setLog(URLUtilities.generateFeedLogURL(feedid));\r
402     }\r
403 \r
404     public String getAafInstance() {\r
405         return aaf_instance;\r
406     }\r
407 \r
408     public void setAaf_instance(String aaf_instance) {\r
409         this.aaf_instance = aaf_instance;\r
410     }\r
411 \r
412     //new getter setters for groups- Rally:US708115 - 1610\r
413     public int getGroupid() {\r
414         return groupid;\r
415     }\r
416 \r
417     public void setGroupid(int groupid) {\r
418         this.groupid = groupid;\r
419     }\r
420 \r
421     public String getName() {\r
422         return name;\r
423     }\r
424 \r
425     public void setName(String name) {\r
426         this.name = name;\r
427     }\r
428 \r
429     public String getVersion() {\r
430         return version;\r
431     }\r
432 \r
433     public void setVersion(String version) {\r
434         this.version = version;\r
435     }\r
436 \r
437     public String getDescription() {\r
438         return description;\r
439     }\r
440 \r
441     public void setDescription(String description) {\r
442         this.description = description;\r
443     }\r
444 \r
445     // New field is added - Groups feature Rally:US708102 - 1610\r
446     public String getBusiness_description() {\r
447         return business_description;\r
448     }\r
449 \r
450     public void setBusiness_description(String business_description) {\r
451         this.business_description = business_description;\r
452     }\r
453 \r
454     public FeedAuthorization getAuthorization() {\r
455         return authorization;\r
456     }\r
457 \r
458     public void setAuthorization(FeedAuthorization authorization) {\r
459         this.authorization = authorization;\r
460     }\r
461 \r
462     public String getPublisher() {\r
463         return publisher;\r
464     }\r
465 \r
466     public void setPublisher(String publisher) {\r
467         if (publisher != null) {\r
468             if (publisher.length() > 8)\r
469                 publisher = publisher.substring(0, 8);\r
470             this.publisher = publisher;\r
471         }\r
472     }\r
473 \r
474     public FeedLinks getLinks() {\r
475         return links;\r
476     }\r
477 \r
478     public void setLinks(FeedLinks links) {\r
479         this.links = links;\r
480     }\r
481 \r
482     public boolean isDeleted() {\r
483         return deleted;\r
484     }\r
485 \r
486     public void setDeleted(boolean deleted) {\r
487         this.deleted = deleted;\r
488     }\r
489 \r
490     public boolean isSuspended() {\r
491         return suspended;\r
492     }\r
493 \r
494     public void setSuspended(boolean suspended) {\r
495         this.suspended = suspended;\r
496     }\r
497 \r
498     @Override\r
499     public JSONObject asJSONObject() {\r
500         JSONObject jo = new JSONObject();\r
501         jo.put("feedid", feedid);\r
502         jo.put("groupid", groupid); //New field is added - Groups feature Rally:US708115 - 1610\r
503         jo.put("name", name);\r
504         jo.put("version", version);\r
505         jo.put("description", description);\r
506         jo.put("business_description", business_description); // New field is added - Groups feature Rally:US708102 - 1610\r
507         jo.put("authorization", authorization.asJSONObject());\r
508         jo.put("publisher", publisher);\r
509         jo.put("links", links.asJSONObject());\r
510         jo.put("deleted", deleted);\r
511         jo.put("suspend", suspended);\r
512         jo.put("last_mod", last_mod.getTime());\r
513         jo.put("created_date", created_date.getTime());\r
514         jo.put("aaf_instance", aaf_instance);\r
515         return jo;\r
516     }\r
517 \r
518     public JSONObject asLimitedJSONObject() {\r
519         JSONObject jo = asJSONObject();\r
520         jo.remove("deleted");\r
521         jo.remove("feedid");\r
522         jo.remove("last_mod");\r
523         jo.remove("created_date");\r
524         return jo;\r
525     }\r
526 \r
527     public JSONObject asJSONObject(boolean hidepasswords) {\r
528         JSONObject jo = asJSONObject();\r
529         if (hidepasswords) {\r
530             jo.remove("feedid");    // we no longer hide passwords, however we do hide these\r
531             jo.remove("deleted");\r
532             jo.remove("last_mod");\r
533             jo.remove("created_date");\r
534         }\r
535         return jo;\r
536     }\r
537 \r
538     @Override\r
539     public boolean doDelete(Connection c) {\r
540         boolean rv = true;\r
541         PreparedStatement ps = null;\r
542         try {\r
543             String sql = "delete from FEEDS where FEEDID = ?";\r
544             ps = c.prepareStatement(sql);\r
545             ps.setInt(1, feedid);\r
546             ps.execute();\r
547         } catch (SQLException e) {\r
548             rv = false;\r
549             intlogger.error("PROV0007 doDelete: " + e.getMessage(), e);\r
550         } finally {\r
551             try {\r
552                 if(ps!=null) {\r
553                     ps.close();\r
554                 }\r
555             } catch (SQLException e) {\r
556                 intlogger.error(SQLEXCEPTION + e.getMessage(), e);\r
557             }\r
558         }\r
559         return rv;\r
560     }\r
561 \r
562     @Override\r
563     public synchronized boolean doInsert(Connection c) {\r
564         boolean rv = true;\r
565         try {\r
566             if (feedid == -1) {\r
567                 setFeedid(next_feedid++);\r
568             }\r
569             // In case we insert a feed from synchronization\r
570             if (feedid > next_feedid)\r
571                 next_feedid = feedid + 1;\r
572 \r
573             // Create FEED_ENDPOINT_IDS rows\r
574             FeedAuthorization auth = getAuthorization();\r
575             String sql = "insert into FEED_ENDPOINT_IDS values (?, ?, ?)";\r
576             try(PreparedStatement ps2 = c.prepareStatement(sql)) {\r
577                 for (FeedEndpointID fid : auth.getEndpoint_ids()) {\r
578                     ps2.setInt(1, feedid);\r
579                     ps2.setString(2, fid.getId());\r
580                     ps2.setString(3, fid.getPassword());\r
581                     ps2.executeUpdate();\r
582                 }\r
583             }\r
584 \r
585             // Create FEED_ENDPOINT_ADDRS rows\r
586             sql = "insert into FEED_ENDPOINT_ADDRS values (?, ?)";\r
587             try(PreparedStatement ps2 = c.prepareStatement(sql)) {\r
588                 for (String t : auth.getEndpoint_addrs()) {\r
589                     ps2.setInt(1, feedid);\r
590                     ps2.setString(2, t);\r
591                     ps2.executeUpdate();\r
592                 }\r
593             }\r
594 \r
595             // Finally, create the FEEDS row\r
596             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
597             try(PreparedStatement ps2 = c.prepareStatement(sql)) {\r
598                 ps2.setInt(1, feedid);\r
599                 ps2.setString(2, getName());\r
600                 ps2.setString(3, getVersion());\r
601                 ps2.setString(4, getDescription());\r
602                 ps2.setString(5, getAuthorization().getClassification());\r
603                 ps2.setString(6, getPublisher());\r
604                 ps2.setString(7, getLinks().getSelf());\r
605                 ps2.setString(8, getLinks().getPublish());\r
606                 ps2.setString(9, getLinks().getSubscribe());\r
607                 ps2.setString(10, getLinks().getLog());\r
608                 ps2.setBoolean(11, isDeleted());\r
609                 ps2.setBoolean(12, isSuspended());\r
610                 ps2.setString(13, getBusiness_description());\r
611                 ps2.setInt(14, groupid);\r
612                 ps2.setString(15, getAafInstance());\r
613                 ps2.executeUpdate();\r
614             }\r
615         } catch (SQLException e) {\r
616             rv = false;\r
617             intlogger.error("PROV0005 doInsert: " + e.getMessage(), e);\r
618         }\r
619         return rv;\r
620     }\r
621 \r
622     @Override\r
623     public boolean doUpdate(Connection c) {\r
624         boolean rv = true;\r
625         Feed oldobj = getFeedById(feedid);\r
626         PreparedStatement ps = null;\r
627         try {\r
628             Set<FeedEndpointID> newset = getAuthorization().getEndpoint_ids();\r
629             Set<FeedEndpointID> oldset = oldobj.getAuthorization().getEndpoint_ids();\r
630 \r
631             // Insert new FEED_ENDPOINT_IDS rows\r
632             String sql = "insert into FEED_ENDPOINT_IDS values (?, ?, ?)";\r
633             ps = c.prepareStatement(sql);\r
634             for (FeedEndpointID fid : newset) {\r
635                 if (!oldset.contains(fid)) {\r
636                     ps.setInt(1, feedid);\r
637                     ps.setString(2, fid.getId());\r
638                     ps.setString(3, fid.getPassword());\r
639                     ps.executeUpdate();\r
640                 }\r
641             }\r
642             ps.close();\r
643 \r
644             // Delete old FEED_ENDPOINT_IDS rows\r
645             sql = "delete from FEED_ENDPOINT_IDS where FEEDID = ? AND USERID = ? AND PASSWORD = ?";\r
646             ps = c.prepareStatement(sql);\r
647             for (FeedEndpointID fid : oldset) {\r
648                 if (!newset.contains(fid)) {\r
649                     ps.setInt(1, feedid);\r
650                     ps.setString(2, fid.getId());\r
651                     ps.setString(3, fid.getPassword());\r
652                     ps.executeUpdate();\r
653                 }\r
654             }\r
655             ps.close();\r
656 \r
657             // Insert new FEED_ENDPOINT_ADDRS rows\r
658             Set<String> newset2 = getAuthorization().getEndpoint_addrs();\r
659             Set<String> oldset2 = oldobj.getAuthorization().getEndpoint_addrs();\r
660             sql = "insert into FEED_ENDPOINT_ADDRS values (?, ?)";\r
661             ps = c.prepareStatement(sql);\r
662             for (String t : newset2) {\r
663                 if (!oldset2.contains(t)) {\r
664                     ps.setInt(1, feedid);\r
665                     ps.setString(2, t);\r
666                     ps.executeUpdate();\r
667                 }\r
668             }\r
669             ps.close();\r
670 \r
671             // Delete old FEED_ENDPOINT_ADDRS rows\r
672             sql = "delete from FEED_ENDPOINT_ADDRS where FEEDID = ? AND ADDR = ?";\r
673             ps = c.prepareStatement(sql);\r
674             for (String t : oldset2) {\r
675                 if (!newset2.contains(t)) {\r
676                     ps.setInt(1, feedid);\r
677                     ps.setString(2, t);\r
678                     ps.executeUpdate();\r
679                 }\r
680             }\r
681             ps.close();\r
682 \r
683             // Finally, update the FEEDS row\r
684             sql = "update FEEDS set DESCRIPTION = ?, AUTH_CLASS = ?, DELETED = ?, SUSPENDED = ?, BUSINESS_DESCRIPTION=?, GROUPID=? where FEEDID = ?";\r
685             ps = c.prepareStatement(sql);\r
686             ps.setString(1, getDescription());\r
687             ps.setString(2, getAuthorization().getClassification());\r
688             ps.setInt(3, deleted ? 1 : 0);\r
689             ps.setInt(4, suspended ? 1 : 0);\r
690             ps.setString(5, getBusiness_description());\r
691             ps.setInt(6, groupid);\r
692             ps.setInt(7, feedid);\r
693             ps.executeUpdate();\r
694             ps.close();\r
695         } catch (SQLException e) {\r
696             rv = false;\r
697             intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e);\r
698         } finally {\r
699             try {\r
700                 if (ps != null)\r
701                     ps.close();\r
702             } catch (SQLException e) {\r
703                 intlogger.error(SQLEXCEPTION + e.getMessage(), e);\r
704             }\r
705         }\r
706         return rv;\r
707     }\r
708 \r
709     /**\r
710      * Rally US708115\r
711      * Change Ownership of FEED - 1610\r
712      */\r
713     public boolean changeOwnerShip() {\r
714         boolean rv = true;\r
715         PreparedStatement ps = null;\r
716         try {\r
717 \r
718             DB db = new DB();\r
719             @SuppressWarnings("resource")\r
720             Connection c = db.getConnection();\r
721             String sql = "update FEEDS set PUBLISHER = ? where FEEDID = ?";\r
722             ps = c.prepareStatement(sql);\r
723             ps.setString(1, this.publisher);\r
724             ps.setInt(2, feedid);\r
725             ps.execute();\r
726             ps.close();\r
727         } catch (SQLException e) {\r
728             rv = false;\r
729             intlogger.warn("PROV0008 changeOwnerShip: " + e.getMessage(), e);\r
730         } finally {\r
731             try {\r
732                 if(ps!=null) {\r
733                     ps.close();\r
734                 }\r
735             } catch (SQLException e) {\r
736                 intlogger.error(SQLEXCEPTION + e.getMessage(), e);\r
737             }\r
738         }\r
739         return rv;\r
740     }\r
741 \r
742 \r
743     @Override\r
744     public String getKey() {\r
745         return "" + getFeedid();\r
746     }\r
747 \r
748     @Override\r
749     public boolean equals(Object obj) {\r
750         if (!(obj instanceof Feed))\r
751             return false;\r
752         Feed of = (Feed) obj;\r
753         if (feedid != of.feedid)\r
754             return false;\r
755         if (groupid != of.groupid) //New field is added - Groups feature Rally:US708115 - 1610\r
756             return false;\r
757         if (!name.equals(of.name))\r
758             return false;\r
759         if (!version.equals(of.version))\r
760             return false;\r
761         if (!description.equals(of.description))\r
762             return false;\r
763         if (!business_description.equals(of.business_description)) // New field is added - Groups feature Rally:US708102 - 1610\r
764             return false;\r
765         if (!publisher.equals(of.publisher))\r
766             return false;\r
767         if (!authorization.equals(of.authorization))\r
768             return false;\r
769         if (!links.equals(of.links))\r
770             return false;\r
771         if (deleted != of.deleted)\r
772             return false;\r
773         if (suspended != of.suspended)\r
774             return false;\r
775         if (!aaf_instance.equals(of.aaf_instance))\r
776             return false;\r
777         return true;\r
778     }\r
779 \r
780     @Override\r
781     public String toString() {\r
782         return "FEED: feedid=" + feedid + ", name=" + name + ", version=" + version;\r
783     }\r
784 \r
785     @Override\r
786     public int hashCode() {\r
787         return super.hashCode();\r
788     }\r
789 }