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