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