aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/stats/models/AuthorsModel.java
blob: 92b5f6a4715a48111c54208338195ed1b58e84a5 (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
package org.wordpress.android.ui.stats.models;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.wordpress.android.util.AppLog;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class AuthorsModel extends BaseStatsModel {
    private String mPeriod;
    private String mDate;
    private String mBlogID;
    private int mOtherViews;
    private List<AuthorModel> mAuthors;

    public AuthorsModel(String blogID, JSONObject response) throws JSONException {
        this.mBlogID = blogID;
        this.mPeriod = response.getString("period");
        this.mDate = response.getString("date");

        JSONObject jDaysObject = response.getJSONObject("days");
        if (jDaysObject.length() == 0) {
            throw new JSONException("Invalid document returned from the REST API");
        }

        JSONArray authorsJSONArray;
        // Read the first day
        Iterator<String> keys = jDaysObject.keys();
        String key = keys.next();
        JSONObject firstDayObject = jDaysObject.getJSONObject(key);
        this.mOtherViews = firstDayObject.optInt("other_views");
        authorsJSONArray = firstDayObject.optJSONArray("authors");

        if (authorsJSONArray != null) {
            mAuthors = new ArrayList<>(authorsJSONArray.length());
            for (int i = 0; i < authorsJSONArray.length(); i++) {
                try {
                    JSONObject currentAuthorJSON = authorsJSONArray.getJSONObject(i);
                    AuthorModel currentAuthor = new AuthorModel(blogID, mDate, currentAuthorJSON);
                    mAuthors.add(currentAuthor);
                } catch (JSONException e) {
                    AppLog.e(AppLog.T.STATS, "Unexpected Author object " +
                            "at position " + i + " Response: " + response.toString(), e);
                }
            }
        }
    }

    public String getBlogID() {
        return mBlogID;
    }

    public void setBlogID(String blogID) {
        this.mBlogID = blogID;
    }

    public String getDate() {
        return mDate;
    }

    public void setDate(String date) {
        this.mDate = date;
    }

    public String getPeriod() {
        return mPeriod;
    }

    public void setPeriod(String period) {
        this.mPeriod = period;
    }

    public List<AuthorModel> getAuthors() {
        return this.mAuthors;
    }

    public int getOtherViews() {
        return mOtherViews;
    }
}