aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/models/CommentList.java
blob: 6942e3f860d51ee482b9919a6b00c3764c97ad3a (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
package org.wordpress.android.models;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class CommentList extends ArrayList<Comment> {
    public int indexOfCommentId(long commentId) {
        for (int i=0; i < this.size(); i++) {
            if (commentId==this.get(i).commentID)
                return i;
        }
        return -1;
    }

    /*
     * replace comments in this list that match the passed list
     */
    public void replaceComments(final CommentList comments) {
        if (comments == null || comments.size() == 0)
            return;
        for (Comment comment: comments) {
            int index = indexOfCommentId(comment.commentID);
            if (index > -1)
                set(index, comment);
        }
    }

    /*
     * delete comments in this list that match the passed list
     */
    public void deleteComments(final CommentList comments) {
        if (comments == null || comments.size() == 0)
            return;
        for (Comment comment: comments) {
            int index = indexOfCommentId(comment.commentID);
            if (index > -1)
                remove(index);
        }
    }

    /*
     * returns true if any comments in this list have the passed status
     */
    public boolean hasAnyWithStatus(CommentStatus status) {
        for (Comment comment: this) {
            if (comment.getStatusEnum().equals(status))
                return true;
        }
        return false;
    }

    /*
     * returns true if any comments in this list do NOT have the passed status
     */
    public boolean hasAnyWithoutStatus(CommentStatus status) {
        for (Comment comment: this) {
            if (!comment.getStatusEnum().equals(status))
                return true;
        }
        return false;
    }

    /*
     * does passed list contain the same comments as this list?
     */
    public boolean isSameList(CommentList comments) {
        if (comments == null || comments.size() != this.size())
            return false;

        for (final Comment comment: comments) {
            int index = this.indexOfCommentId(comment.commentID);
            if (index == -1)
                return false;
            final Comment thisComment = this.get(index);
            if (!thisComment.getStatus().equals(comment.getStatus()))
                return false;
            if (!thisComment.getCommentText().equals(comment.getCommentText()))
                return false;
            if (!thisComment.getAuthorName().equals(comment.getAuthorName()))
                return false;
            if (!thisComment.getAuthorEmail().equals(comment.getAuthorEmail()))
                return false;
            if (!thisComment.getAuthorUrl().equals(comment.getAuthorUrl()))
                return false;

        }

        return true;
    }

    public static CommentList fromJSONV1_1(JSONObject object) throws JSONException {
        CommentList commentList = new CommentList();
        if (object == null) {
            return null;
        } else {
            JSONArray comments = object.getJSONArray("comments");
            for (int i=0; i < comments.length(); i++){
                JSONObject commentJson = comments.getJSONObject(i);
                commentList.add(Comment.fromJSON(commentJson));
            }
            return commentList;
        }
    }
}