-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSubuser.java
More file actions
207 lines (159 loc) · 3.86 KB
/
Copy pathSubuser.java
File metadata and controls
207 lines (159 loc) · 3.86 KB
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
package pl.smsapi.message;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import pl.smsapi.SmsapiException;
public class Subuser {
private String name;
private String info;
private String accessPhones;
private Float limit;
private Float limitMonth;
private boolean senders;
private boolean active;
private Object error;
public Subuser(String name)
{
this.name = name;
}
public Subuser(Map<String, String> data) {
setName(data.get("username"));
setLimit(data.get("limit"));
setLimitMonth(data.get("month_limit"));
setSenders(data.get("senders"));
setAccessPhones(data.get("phonebook"));
setActive(data.get("active"));
setInfo(data.get("info"));
}
private Subuser setError(Object error) {
this.error = error;
return this;
}
public Object getError() {
return error;
}
public boolean isError() {
return (error != null);
}
public String getName() {
return name;
}
public Subuser setName(String name) {
this.name = name;
return this;
}
public String getInfo() {
return info;
}
public Subuser setInfo(String info) {
this.info = info;
return this;
}
public String getLimit() {
return limit.toString();
}
public Subuser setLimit(String limit) {
this.limit = Float.parseFloat(limit);
return this;
}
public String getLimitMonth() {
return limitMonth.toString();
}
public Subuser setLimitMonth(String limitMonth) {
this.limitMonth = Float.parseFloat(limitMonth);
return this;
}
public boolean isSenders() {
return senders;
}
public Subuser setSenders(boolean senders) {
this.senders = senders;
return this;
}
public Subuser setSenders(String senders) {
this.senders = senders.equals("1") ? true : false;
return this;
}
public String getPhones() {
return accessPhones;
}
public Subuser setAccessPhones(String access) {
this.accessPhones = access;
return this;
}
public boolean isActive() {
return active;
}
public Subuser setActive(boolean active) {
this.active = active;
return this;
}
public Subuser setActive(String active) {
this.active = active.equals("1") ? true : false;
return this;
}
public static Subuser[] parseSubusers(String str) throws SmsapiException
{
class Parser {
private int type;
private String str;
Subuser[] subusers;
Parser(String str){
this.str = str;
String comp = str.substring(0,1);
type = (comp.equals("[") == true) ? 1 : 2;
if(str.contains("ERROR")){
type = 3;
}
}
public Subuser[] excecute()
{
try {
switch (type) {
case 1:
JSONArray aData = new JSONArray(str);
final int n = aData.length();
if (n > 0) {
subusers = new Subuser[n];
for (int i = 0; i < n; i++) {
JSONObject tmp = aData.getJSONObject(i);
subusers[i] = new Subuser(bildValues(tmp));
}
}else{
subusers = null;
}
break;
case 2:
JSONObject oData = new JSONObject(str);
subusers = new Subuser[1];
subusers[0] = new Subuser(bildValues(oData));
break;
case 3:
subusers = new Subuser[1];
subusers[0] = new Subuser("").setError(SmsapiException.createError(str.split(":")));
break;
default:
subusers = null;
}
} catch (JSONException ex) {
throw new SmsapiException("Problem with decode json format");
}
return subusers;
}
}
return new Parser(str).excecute();
}
private static HashMap<String, String> bildValues(JSONObject val) {
String key;
Iterator it = val.keys();
HashMap<String, String> data = new HashMap<String, String>();
while (it.hasNext()) {
key = (String) it.next();
data.put(key, val.optString(key));
}
return data;
}
}