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