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