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