aboutsummaryrefslogtreecommitdiffstats
path: root/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Feed.java
blob: 3dccc02c77ac18cb41464d0db056fb0fd7fa7dd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
/*******************************************************************************
 * ============LICENSE_START==================================================
 * * org.onap.dmaap
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * *
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/


package org.onap.dmaap.datarouter.provisioning.beans;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import java.io.InvalidObjectException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.onap.dmaap.datarouter.provisioning.utils.DB;
import org.onap.dmaap.datarouter.provisioning.utils.JSONUtilities;
import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities;



/**
 * The representation of a Feed.  Feeds can be retrieved from the DB, or stored/updated in the DB.
 *
 * @author Robert Eby
 * @version $Id: Feed.java,v 1.13 2013/10/28 18:06:52 eby Exp $
 */
public class Feed extends Syncable {

    private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");
    private static int nextFeedID = getMaxFeedID() + 1;
    private static final String SQLEXCEPTION = "SQLException: ";
    private static final String FEED_ID_SQL = "FEEDID";
    private static final String FEED_ID = "feedid";
    private static final String DEL = "deleted";
    private static final String LAST_MOD = "last_mod";
    private static final String CREATED_DATE = "created_date";

    private int feedid;
    private int groupid; //New field is added - Groups feature Rally:US708115 - 1610
    private String name;
    private String version;
    private String description;
    private String businessDescription; // New field is added - Groups feature Rally:US708102 - 1610
    private FeedAuthorization authorization;
    private String publisher;
    private FeedLinks links;
    private boolean deleted;
    private boolean suspended;
    private Date lastMod;
    private Date createdDate;
    private String aafInstance;

    public Feed() {
        this("", "", "", "");
    }

    /**
     * Feed constructor.
     * @param name feed name
     * @param version feed version
     * @param desc feed description
     * @param businessDescription feed business description
     */
    public Feed(String name, String version, String desc, String businessDescription) {
        this.feedid = -1;
        this.groupid = -1; //New field is added - Groups feature Rally:US708115 - 1610
        this.name = name;
        this.version = version;
        this.description = desc;
        this.businessDescription = businessDescription; // New field is added - Groups feature Rally:US708102 - 1610
        this.authorization = new FeedAuthorization();
        this.publisher = "";
        this.links = new FeedLinks();
        this.deleted = false;
        this.suspended = false;
        this.lastMod = new Date();
        this.createdDate = new Date();
        this.aafInstance = "";
    }

    /**
     * Feed Constructor from ResultSet.
     * @param rs ResultSet
     * @throws SQLException in case of SQL statement error
     */
    public Feed(ResultSet rs) throws SQLException {
        this.feedid = rs.getInt(FEED_ID_SQL);
        //New field is added - Groups feature Rally:US708115 - 1610
        this.groupid = rs.getInt("GROUPID");
        this.name = rs.getString("NAME");
        this.version = rs.getString("VERSION");
        this.description = rs.getString("DESCRIPTION");
        // New field is added - Groups feature Rally:US708102 - 1610
        this.businessDescription = rs.getString("BUSINESS_DESCRIPTION");
        this.authorization = new FeedAuthorization();
        this.authorization.setClassification(rs.getString("AUTH_CLASS"));
        this.publisher = rs.getString("PUBLISHER");
        this.links = new FeedLinks();
        this.links.setSelf(rs.getString("SELF_LINK"));
        this.links.setPublish(rs.getString("PUBLISH_LINK"));
        this.links.setSubscribe(rs.getString("SUBSCRIBE_LINK"));
        this.links.setLog(rs.getString("LOG_LINK"));
        this.deleted = rs.getBoolean("DELETED");
        this.suspended = rs.getBoolean("SUSPENDED");
        this.lastMod = rs.getDate("LAST_MOD");
        this.createdDate = rs.getTimestamp("CREATED_DATE");
        this.aafInstance = rs.getString("AAF_INSTANCE");
    }

    /**
     * Feed constructor from JSONObject.
     * @param jo JSONObject
     * @throws InvalidObjectException in case of JSON error
     */
    public Feed(JSONObject jo) throws InvalidObjectException {
        this("", "", "", "");
        try {
            // The JSONObject is assumed to contain a vnd.dmaap-dr.feed representation
            this.feedid = jo.optInt(FEED_ID, -1);
            this.groupid = jo.optInt("groupid");
            this.name = jo.getString("name");
            this.aafInstance = jo.optString("aaf_instance", "legacy");
            if (!("legacy".equalsIgnoreCase(aafInstance)) && aafInstance.length() > 255) {
                throw new InvalidObjectException("aaf_instance field is too long");
            }
            if (name.length() > 255) {
                throw new InvalidObjectException("name field is too long");
            }
            try {
                this.version = jo.getString("version");
            } catch (JSONException e) {
                intlogger.warn("PROV0023 Feed.Feed: " + e.getMessage(), e);
                this.version = null;
            }
            if (version != null && version.length() > 20) {
                throw new InvalidObjectException("version field is too long");
            }
            this.description = jo.optString("description");
            this.businessDescription = jo.optString("business_description");
            if (description.length() > 1000) {
                throw new InvalidObjectException("technical description field is too long");
            }
            if (businessDescription.length() > 1000) {
                throw new InvalidObjectException("business description field is too long");
            }
            this.authorization = new FeedAuthorization();
            JSONObject jauth = jo.getJSONObject("authorization");
            this.authorization.setClassification(jauth.getString("classification"));
            if (this.authorization.getClassification().length() > 32) {
                throw new InvalidObjectException("classification field is too long");
            }
            JSONArray endPointIds = jauth.getJSONArray("endpoint_ids");
            for (int i = 0; i < endPointIds.length(); i++) {
                JSONObject id = endPointIds.getJSONObject(i);
                FeedEndpointID fid = new FeedEndpointID(id.getString("id"), id.getString("password"));
                if (fid.getId().length() > 60) {
                    throw new InvalidObjectException("id field is too long (" + fid.getId() + ")");
                }
                if (fid.getPassword().length() > 32) {
                    //Fortify scan fixes - Privacy Violation
                    throw new InvalidObjectException("password field is too long (" + fid.getPassword() + ")");
                }
                this.authorization.getEndpointIDS().add(fid);
            }
            if (this.authorization.getEndpointIDS().isEmpty()) {
                throw new InvalidObjectException("need to specify at least one endpoint_id");
            }
            endPointIds = jauth.getJSONArray("endpoint_addrs");
            for (int i = 0; i < endPointIds.length(); i++) {
                String addr = endPointIds.getString(i);
                if (!JSONUtilities.validIPAddrOrSubnet(addr)) {
                    throw new InvalidObjectException("bad IP addr or subnet mask: " + addr);
                }
                this.authorization.getEndpointAddrs().add(addr);
            }

            this.publisher = jo.optString("publisher", "");
            this.deleted = jo.optBoolean(DEL, false);
            this.suspended = jo.optBoolean("suspend", false);
            JSONObject jol = jo.optJSONObject("links");
            this.links = (jol == null) ? (new FeedLinks()) : (new FeedLinks(jol));
        } catch (InvalidObjectException e) {
            throw e;
        } catch (Exception e) {
            intlogger.warn("Invalid JSON: " + e.getMessage(), e);
            throw new InvalidObjectException("Invalid JSON: " + e.getMessage());
        }
    }

    /**
     * Check if a feed ID is valid.
     *
     * @param id the Feed ID
     * @return true if it is valid
     */
    @SuppressWarnings("resource")
    public static boolean isFeedValid(int id) {
        int count = 0;
        try {
            DB db = new DB();
            Connection conn = db.getConnection();
            try (PreparedStatement stmt = conn.prepareStatement("select COUNT(*) from FEEDS where FEEDID = ?")) {
                stmt.setInt(1, id);
                try (ResultSet rs = stmt.executeQuery()) {
                    if (rs.next()) {
                        count = rs.getInt(1);
                    }
                }
            }
            db.release(conn);
        } catch (SQLException e) {
            intlogger.warn("PROV0024 Feed.isFeedValid: " + e.getMessage(), e);
        }
        return count != 0;
    }

    /**
     * Get a specific feed from the DB, based upon its ID.
     *
     * @param id the Feed ID
     * @return the Feed object, or null if it does not exist
     */
    public static Feed getFeedById(int id) {
        String sql = "select * from FEEDS where FEEDID = " + id;
        return getFeedBySQL(sql);
    }

    /**
     * Get a specific feed from the DB, based upon its name and version.
     *
     * @param name    the name of the Feed
     * @param version the version of the Feed
     * @return the Feed object, or null if it does not exist
     */
    public static Feed getFeedByNameVersion(String name, String version) {
        name = name.replaceAll("'", "''");
        version = version.replaceAll("'", "''");
        String sql = "select * from FEEDS where NAME = '" + name + "' and VERSION ='" + version + "'";
        return getFeedBySQL(sql);
    }

    /**
     * Return a count of the number of active feeds in the DB.
     *
     * @return the count
     */
    public static int countActiveFeeds() {
        int count = 0;
        try {
            DB db = new DB();
            @SuppressWarnings("resource") Connection conn = db.getConnection();
            try (Statement stmt = conn.createStatement()) {
                try (ResultSet rs = stmt.executeQuery("select count(*) from FEEDS where DELETED = 0")) {
                    if (rs.next()) {
                        count = rs.getInt(1);
                    }
                }
            }
            db.release(conn);
        } catch (SQLException e) {
            intlogger.warn("PROV0025 Feed.countActiveFeeds: " + e.getMessage(), e);
        }
        return count;
    }

    /**
     * Method to get max feed id.
     * @return int max feed id
     */
    public static int getMaxFeedID() {
        int max = 0;
        try {
            DB db = new DB();
            @SuppressWarnings("resource")
            Connection conn = db.getConnection();
            try (Statement stmt = conn.createStatement()) {
                try (ResultSet rs = stmt.executeQuery("select MAX(feedid) from FEEDS")) {
                    if (rs.next()) {
                        max = rs.getInt(1);
                    }
                }
            }
            db.release(conn);
        } catch (SQLException e) {
            intlogger.warn("PROV0026 Feed.getMaxFeedID: " + e.getMessage(), e);
        }
        return max;
    }

    /**
     * Gets all feeds.
     * @return Collection of feeds
     */
    public static Collection<Feed> getAllFeeds() {
        Map<Integer, Feed> map = new HashMap<>();
        try {
            DB db = new DB();
            @SuppressWarnings("resource")
            Connection conn = db.getConnection();
            try (Statement stmt = conn.createStatement()) {
                try (ResultSet rs = stmt.executeQuery("select * from FEEDS")) {
                    while (rs.next()) {
                        Feed feed = new Feed(rs);
                        map.put(feed.getFeedid(), feed);
                    }
                }

                String sql = "select * from FEED_ENDPOINT_IDS";
                try (ResultSet rs = stmt.executeQuery(sql)) {
                    while (rs.next()) {
                        int id = rs.getInt(FEED_ID_SQL);
                        Feed feed = map.get(id);
                        if (feed != null) {
                            FeedEndpointID epi = new FeedEndpointID(rs);
                            Collection<FeedEndpointID> ecoll = feed.getAuthorization().getEndpointIDS();
                            ecoll.add(epi);
                        }
                    }
                }

                sql = "select * from FEED_ENDPOINT_ADDRS";
                try (ResultSet rs = stmt.executeQuery(sql)) {
                    while (rs.next()) {
                        int id = rs.getInt(FEED_ID_SQL);
                        Feed feed = map.get(id);
                        if (feed != null) {
                            Collection<String> acoll = feed.getAuthorization().getEndpointAddrs();
                            acoll.add(rs.getString("ADDR"));
                        }
                    }
                }
            }
            db.release(conn);
        } catch (SQLException e) {
            intlogger.warn("PROV0027 Feed.getAllFeeds: " + e.getMessage(), e);
        }
        return map.values();
    }

    /**
     * Get Feed URL list.
     * @param name of Feed
     * @param val of feed
     * @return List of feed names
     */
    public static List<String> getFilteredFeedUrlList(final String name, final String val) {
        List<String> list = new ArrayList<>();
        String sql = "select SELF_LINK from FEEDS where DELETED = 0";
        if (name.equals("name")) {
            sql += " and NAME = ?";
        } else if (name.equals("publ")) {
            sql += " and PUBLISHER = ?";
        } else if (name.equals("subs")) {
            sql = "select distinct FEEDS.SELF_LINK from FEEDS, SUBSCRIPTIONS "
                          + "where DELETED = 0 "
                          + "and FEEDS.FEEDID = SUBSCRIPTIONS.FEEDID "
                          + "and SUBSCRIPTIONS.SUBSCRIBER = ?";
        }
        try {
            DB db = new DB();
            @SuppressWarnings("resource")
            Connection conn = db.getConnection();
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                if (sql.indexOf('?') >= 0) {
                    ps.setString(1, val);
                }
                try (ResultSet rs = ps.executeQuery()) {
                    while (rs.next()) {
                        String str = rs.getString(1);
                        list.add(str.trim());
                    }
                }
            }
            db.release(conn);
        } catch (SQLException e) {
            intlogger.warn("PROV0028 Feed.getFilteredFeedUrlList: " + e.getMessage(), e);
        }
        return list;
    }

    @SuppressWarnings("resource")
    private static Feed getFeedBySQL(String sql) {
        Feed feed = null;
        try {
            DB db = new DB();
            Connection conn = db.getConnection();
            try (Statement stmt = conn.createStatement()) {
                try (ResultSet rs = stmt.executeQuery(sql)) {
                    if (rs.next()) {
                        feed = new Feed(rs);
                    }
                }
                if (feed != null) {
                    sql = "select * from FEED_ENDPOINT_IDS where FEEDID = " + feed.feedid;
                    try (ResultSet rs = stmt.executeQuery(sql)) {
                        Collection<FeedEndpointID> ecoll = feed.getAuthorization().getEndpointIDS();
                        while (rs.next()) {
                            FeedEndpointID epi = new FeedEndpointID(rs);
                            ecoll.add(epi);
                        }
                    }
                    sql = "select * from FEED_ENDPOINT_ADDRS where FEEDID = " + feed.feedid;
                    try (ResultSet rs = stmt.executeQuery(sql)) {
                        Collection<String> acoll = feed.getAuthorization().getEndpointAddrs();
                        while (rs.next()) {
                            acoll.add(rs.getString("ADDR"));
                        }
                    }
                }
            }
            db.release(conn);
        } catch (SQLException e) {
            intlogger.warn("PROV0029 Feed.getFeedBySQL: " + e.getMessage(), e);
        }
        return feed;
    }



    public int getFeedid() {
        return feedid;
    }

    /**
     *  Set feedid with FeedLinks.
      * @param feedid Feedid to set to
     */
    public void setFeedid(int feedid) {
        this.feedid = feedid;

        // Create link URLs
        FeedLinks fl = getLinks();
        fl.setSelf(URLUtilities.generateFeedURL(feedid));
        fl.setPublish(URLUtilities.generatePublishURL(feedid));
        fl.setSubscribe(URLUtilities.generateSubscribeURL(feedid));
        fl.setLog(URLUtilities.generateFeedLogURL(feedid));
    }

    public String getAafInstance() {
        return aafInstance;
    }

    public void setAafInstance(String aafInstance) {
        this.aafInstance = aafInstance;
    }

    //new getter setters for groups- Rally:US708115 - 1610
    public int getGroupid() {
        return groupid;
    }

    public void setGroupid(int groupid) {
        this.groupid = groupid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    // New field is added - Groups feature Rally:US708102 - 1610
    public String getBusinessDescription() {
        return businessDescription;
    }

    public void setBusinessDescription(String businessDescription) {
        this.businessDescription = businessDescription;
    }

    public FeedAuthorization getAuthorization() {
        return authorization;
    }

    public void setAuthorization(FeedAuthorization authorization) {
        this.authorization = authorization;
    }

    public String getPublisher() {
        return publisher;
    }

    /**
     * Set publisher.
     * @param publisher Publisher name
     */
    public void setPublisher(String publisher) {
        if (publisher != null) {
            if (publisher.length() > 8) {
                publisher = publisher.substring(0, 8);
            }
            this.publisher = publisher;
        }
    }

    public FeedLinks getLinks() {
        return links;
    }

    public void setLinks(FeedLinks links) {
        this.links = links;
    }

    public boolean isDeleted() {
        return deleted;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

    public boolean isSuspended() {
        return suspended;
    }

    public void setSuspended(boolean suspended) {
        this.suspended = suspended;
    }

    @Override
    public JSONObject asJSONObject() {
        JSONObject jo = new JSONObject();
        jo.put(FEED_ID, feedid);
        //New field is added - Groups feature Rally:US708115 - 1610
        jo.put("groupid", groupid);
        jo.put("name", name);
        jo.put("version", version);
        jo.put("description", description);
        // New field is added - Groups feature Rally:US708102 - 1610
        jo.put("business_description", businessDescription);
        jo.put("authorization", authorization.asJSONObject());
        jo.put("publisher", publisher);
        jo.put("links", links.asJSONObject());
        jo.put(DEL, deleted);
        jo.put("suspend", suspended);
        jo.put(LAST_MOD, lastMod.getTime());
        jo.put(CREATED_DATE, createdDate.getTime());
        jo.put("aaf_instance", aafInstance);
        return jo;
    }

    /**
     * Method to hide some attributes.
     * @param hidepasswords true/false
     * @return JSONObject
     */
    public JSONObject asJSONObject(boolean hidepasswords) {
        JSONObject jo = asJSONObject();
        if (hidepasswords) {
            jo.remove(FEED_ID);    // we no longer hide passwords, however we do hide these
            jo.remove(DEL);
            jo.remove(LAST_MOD);
            jo.remove(CREATED_DATE);
        }
        return jo;
    }

    /**
     * Method to limit JSONObject.
     * @return JSONObject
     */
    public JSONObject asLimitedJSONObject() {
        JSONObject jo = asJSONObject();
        jo.remove(DEL);
        jo.remove(FEED_ID);
        jo.remove(LAST_MOD);
        jo.remove(CREATED_DATE);
        return jo;
    }



    @Override
    public boolean doDelete(Connection conn) {
        boolean rv = true;
        PreparedStatement ps = null;
        try {
            String sql = "delete from FEEDS where FEEDID = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, feedid);
            ps.execute();
        } catch (SQLException e) {
            rv = false;
            intlogger.error("PROV0007 doDelete: " + e.getMessage(), e);
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException e) {
                intlogger.error(SQLEXCEPTION + e.getMessage(), e);
            }
        }
        return rv;
    }

    @Override
    public synchronized boolean doInsert(Connection conn) {
        boolean rv = true;
        try {
            if (feedid == -1) {
                setFeedid(nextFeedID++);
            }
            // In case we insert a feed from synchronization
            if (feedid > nextFeedID) {
                nextFeedID = feedid + 1;
            }

            // Create FEED_ENDPOINT_IDS rows
            FeedAuthorization auth = getAuthorization();
            String sql = "insert into FEED_ENDPOINT_IDS values (?, ?, ?)";
            try (PreparedStatement ps2 = conn.prepareStatement(sql)) {
                for (FeedEndpointID fid : auth.getEndpointIDS()) {
                    ps2.setInt(1, feedid);
                    ps2.setString(2, fid.getId());
                    ps2.setString(3, fid.getPassword());
                    ps2.executeUpdate();
                }
            }

            // Create FEED_ENDPOINT_ADDRS rows
            sql = "insert into FEED_ENDPOINT_ADDRS values (?, ?)";
            try (PreparedStatement ps2 = conn.prepareStatement(sql)) {
                for (String t : auth.getEndpointAddrs()) {
                    ps2.setInt(1, feedid);
                    ps2.setString(2, t);
                    ps2.executeUpdate();
                }
            }

            // Finally, create the FEEDS row
            sql = "insert into FEEDS (FEEDID, NAME, VERSION, DESCRIPTION, AUTH_CLASS, PUBLISHER, SELF_LINK, "
                          + "PUBLISH_LINK, SUBSCRIBE_LINK, LOG_LINK, DELETED, SUSPENDED,"
                          + "BUSINESS_DESCRIPTION, GROUPID, AAF_INSTANCE) "
                          + "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            try (PreparedStatement ps2 = conn.prepareStatement(sql)) {
                ps2.setInt(1, feedid);
                ps2.setString(2, getName());
                ps2.setString(3, getVersion());
                ps2.setString(4, getDescription());
                ps2.setString(5, getAuthorization().getClassification());
                ps2.setString(6, getPublisher());
                ps2.setString(7, getLinks().getSelf());
                ps2.setString(8, getLinks().getPublish());
                ps2.setString(9, getLinks().getSubscribe());
                ps2.setString(10, getLinks().getLog());
                ps2.setBoolean(11, isDeleted());
                ps2.setBoolean(12, isSuspended());
                ps2.setString(13, getBusinessDescription());
                ps2.setInt(14, groupid);
                ps2.setString(15, getAafInstance());
                ps2.executeUpdate();
            }
        } catch (SQLException e) {
            rv = false;
            intlogger.error("PROV0005 doInsert: " + e.getMessage(), e);
        }
        return rv;
    }

    @Override
    public boolean doUpdate(Connection conn) {
        boolean rv = true;
        Feed oldobj = getFeedById(feedid);
        PreparedStatement ps = null;
        try {
            Set<FeedEndpointID> newset = getAuthorization().getEndpointIDS();
            Set<FeedEndpointID> oldset = oldobj.getAuthorization().getEndpointIDS();

            // Insert new FEED_ENDPOINT_IDS rows
            String sql = "insert into FEED_ENDPOINT_IDS values (?, ?, ?)";
            ps = conn.prepareStatement(sql);
            for (FeedEndpointID fid : newset) {
                if (!oldset.contains(fid)) {
                    ps.setInt(1, feedid);
                    ps.setString(2, fid.getId());
                    ps.setString(3, fid.getPassword());
                    ps.executeUpdate();
                }
            }
            ps.close();

            // Delete old FEED_ENDPOINT_IDS rows
            sql = "delete from FEED_ENDPOINT_IDS where FEEDID = ? AND USERID = ? AND PASSWORD = ?";
            ps = conn.prepareStatement(sql);
            for (FeedEndpointID fid : oldset) {
                if (!newset.contains(fid)) {
                    ps.setInt(1, feedid);
                    ps.setString(2, fid.getId());
                    ps.setString(3, fid.getPassword());
                    ps.executeUpdate();
                }
            }
            ps.close();

            // Insert new FEED_ENDPOINT_ADDRS rows
            Set<String> newset2 = getAuthorization().getEndpointAddrs();
            Set<String> oldset2 = oldobj.getAuthorization().getEndpointAddrs();
            sql = "insert into FEED_ENDPOINT_ADDRS values (?, ?)";
            ps = conn.prepareStatement(sql);
            for (String t : newset2) {
                if (!oldset2.contains(t)) {
                    ps.setInt(1, feedid);
                    ps.setString(2, t);
                    ps.executeUpdate();
                }
            }
            ps.close();

            // Delete old FEED_ENDPOINT_ADDRS rows
            sql = "delete from FEED_ENDPOINT_ADDRS where FEEDID = ? AND ADDR = ?";
            ps = conn.prepareStatement(sql);
            for (String t : oldset2) {
                if (!newset2.contains(t)) {
                    ps.setInt(1, feedid);
                    ps.setString(2, t);
                    ps.executeUpdate();
                }
            }
            ps.close();

            // Finally, update the FEEDS row
            sql = "update FEEDS set DESCRIPTION = ?, AUTH_CLASS = ?, DELETED = ?, SUSPENDED = ?, "
                          + "BUSINESS_DESCRIPTION=?, GROUPID=? where FEEDID = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, getDescription());
            ps.setString(2, getAuthorization().getClassification());
            ps.setInt(3, deleted ? 1 : 0);
            ps.setInt(4, suspended ? 1 : 0);
            ps.setString(5, getBusinessDescription());
            ps.setInt(6, groupid);
            ps.setInt(7, feedid);
            ps.executeUpdate();
            ps.close();
        } catch (SQLException e) {
            rv = false;
            intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e);
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException e) {
                intlogger.error(SQLEXCEPTION + e.getMessage(), e);
            }
        }
        return rv;
    }

    /**
     * Rally US708115.
     * Change Ownership of FEED - 1610
     */
    public boolean changeOwnerShip() {
        boolean rv = true;
        PreparedStatement ps = null;
        try {

            DB db = new DB();
            @SuppressWarnings("resource")
            Connection conn = db.getConnection();
            String sql = "update FEEDS set PUBLISHER = ? where FEEDID = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, this.publisher);
            ps.setInt(2, feedid);
            ps.execute();
            ps.close();
        } catch (SQLException e) {
            rv = false;
            intlogger.warn("PROV0008 changeOwnerShip: " + e.getMessage(), e);
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException e) {
                intlogger.error(SQLEXCEPTION + e.getMessage(), e);
            }
        }
        return rv;
    }


    @Override
    public String getKey() {
        return "" + getFeedid();
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Feed)) {
            return false;
        }
        Feed of = (Feed) obj;
        if (feedid != of.feedid) {
            return false;
        }
        if (groupid != of.groupid) {
            //New field is added - Groups feature Rally:US708115 - 1610
            return false;
        }
        if (!name.equals(of.name)) {
            return false;
        }
        if (!version.equals(of.version)) {
            return false;
        }
        if (!description.equals(of.description)) {
            return false;
        }
        if (!businessDescription.equals(of.businessDescription)) {
            // New field is added - Groups feature Rally:US708102 - 1610
            return false;
        }
        if (!publisher.equals(of.publisher)) {
            return false;
        }
        if (!authorization.equals(of.authorization)) {
            return false;
        }
        if (!links.equals(of.links)) {
            return false;
        }
        if (deleted != of.deleted) {
            return false;
        }
        if (suspended != of.suspended) {
            return false;
        }
        if (!aafInstance.equals(of.aafInstance)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "FEED: feedid=" + feedid + ", name=" + name + ", version=" + version;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }
}