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

import android.text.TextUtils;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class VisitsModel extends BaseStatsModel {
    private String mFields; // Holds a JSON Object
    private String mUnit;
    private String mDate;
    private String mBlogID;
    private List<VisitModel> mVisits;

    public VisitsModel(String blogID, JSONObject response) throws JSONException {
        this.setBlogID(blogID);
        this.setDate(response.getString("date"));
        this.setUnit(response.getString("unit"));
        this.setFields(response.getJSONArray("fields").toString());

        JSONArray dataJSON;
        try {
            dataJSON = response.getJSONArray("data");
        } catch (JSONException e) {
            AppLog.e(AppLog.T.STATS, this.getClass().getName() + " cannot convert the data field to a JSON array", e);
            dataJSON = new JSONArray();
        }

        if (dataJSON == null || dataJSON.length() == 0) {
            mVisits =  new ArrayList<>(0);
        } else {
            // Read the position/index of each field in the response
            HashMap<String, Integer> columnsMapping = new HashMap<>(6);
            final JSONArray fieldsJSON = getFieldsJSON();
            if (fieldsJSON == null || fieldsJSON.length() == 0) {
                mVisits =  new ArrayList<>(0);
            } else {
                try {
                    for (int i = 0; i < fieldsJSON.length(); i++) {
                        final String field = fieldsJSON.getString(i);
                        columnsMapping.put(field, i);
                    }
                } catch (JSONException e) {
                    AppLog.e(AppLog.T.STATS, "Cannot read the parameter fields from the JSON response." +
                            "Response: " + response.toString(), e);
                    mVisits = new ArrayList<>(0);
                }
            }

            int viewsColumnIndex = columnsMapping.get("views");
            int visitorsColumnIndex = columnsMapping.get("visitors");
            int likesColumnIndex = columnsMapping.get("likes");
            int commentsColumnIndex = columnsMapping.get("comments");
            int periodColumnIndex = columnsMapping.get("period");

            int numPoints = dataJSON.length();
            mVisits = new ArrayList<>(numPoints);

            for (int i = 0; i < numPoints; i++) {
                try {
                    JSONArray currentDayData = dataJSON.getJSONArray(i);
                    VisitModel currentVisitModel = new VisitModel();
                    currentVisitModel.setBlogID(getBlogID());
                    currentVisitModel.setPeriod(currentDayData.getString(periodColumnIndex));
                    currentVisitModel.setViews(currentDayData.getInt(viewsColumnIndex));
                    currentVisitModel.setVisitors(currentDayData.getInt(visitorsColumnIndex));
                    currentVisitModel.setComments(currentDayData.getInt(commentsColumnIndex));
                    currentVisitModel.setLikes(currentDayData.getInt(likesColumnIndex));
                    mVisits.add(currentVisitModel);
                } catch (JSONException e) {
                    AppLog.e(AppLog.T.STATS, "Cannot read the Visit item at index " + i
                            + " Response: " + response.toString(), e);
                }
            }
        }
    }

    public List<VisitModel> getVisits() {
        return mVisits;
    }

    public String getBlogID() {
        return mBlogID;
    }

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

    public String getDate() {
        return mDate;
    }

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

    public String getUnit() {
        return mUnit;
    }

    private void setUnit(String unit) {
        this.mUnit = unit;
    }

    private JSONArray getFieldsJSON() {
        JSONArray jArray;
        try {
            String categories = StringUtils.unescapeHTML(this.getFields() != null ? this.getFields() : "[]");
            if (TextUtils.isEmpty(categories)) {
                jArray = new JSONArray();
            } else {
                jArray = new JSONArray(categories);
            }
        } catch (JSONException e) {
            AppLog.e(AppLog.T.STATS, this.getClass().getName() + " cannot convert the string to JSON", e);
            return null;
        }
        return jArray;
    }

    private void setFields(String fields) {
        this.mFields = fields;
    }

    private String getFields() {
        return mFields;
    }
}