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