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