-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathGroup.java
More file actions
202 lines (152 loc) · 3.93 KB
/
Copy pathGroup.java
File metadata and controls
202 lines (152 loc) · 3.93 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
package pl.smsapi.message;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import pl.smsapi.SmsapiException;
public class Group {
private Integer id;
private String name;
private String info;
private Integer numbers = 0;
private Object error;
private boolean deleted;
private Map<String, String> params;
private Phonebook.Action action = null;
public Group(String name){
this(name, "");
}
public Group(String name, String info){
this(name, info, null);
}
public Group(String name, String info, String numbers){
this.name = name;
this.info = info;
if(numbers != null && !numbers.isEmpty()){
this.numbers = Integer.parseInt(numbers);
}
}
public Group(Phonebook.Action action, String name, Integer id, Map<String, String> params){
this.params = params;
this.name = name;
this.id = id;
this.action = action;
initAction();
}
private void initAction(){
this.params.put(this.action.getName(), this.name);
if(id > 0){
switch(action)
{
case GROUP_EDIT:
params.put("group_id", id.toString());
break;
default:
params.put("id", id.toString());
}
params.put(action.getName(), "");
}
}
public void removeContacts(boolean delete){
if(action.equals(Phonebook.Action.GROUP_DELETE.getName())){
if(delete){
params.put("remove_contacts", "1");
}else{
params.remove("remove_contacts");
}
}
}
public static Group[] parseGroups(String str) throws SmsapiException
{
class Parser {
private int type;
private String str;
Group[] groups;
Parser(String str){
this.str = str;
String comp = str.substring(0,1);
type = (comp.equals("[") == true) ? 1 : 2;
}
public Group[] excecute()
{
try {
switch (type) {
case 1:
JSONArray aData = new JSONArray(str);
final int n = aData.length();
if (n > 0) {
groups = new Group[n];
for (int i = 0; i < n; i++) {
JSONObject tmp = aData.getJSONObject(i);
groups[i] = new Group(tmp.optString("name"), tmp.optString("info"), tmp.optString("numbers_count"));
}
}else{
groups = null;
}
break;
case 2:
JSONObject oData = new JSONObject(str);
groups = new Group[1];
groups[0] = parseGroup(oData);
break;
default:
groups = null;
}
} catch (JSONException ex) {
throw new SmsapiException("Problem with decode json format");
}
return groups;
}
}
return new Parser(str).excecute();
}
private static Group parseGroup(JSONObject data) throws JSONException {
if (!data.optString("error").isEmpty()) {
return new Group("", "", "").setError(SmsapiException.createError(data.optString("error"), data.optString("message")));
} else if (!data.optString("deleted").isEmpty()){
return new Group("", "", data.optString("removed_contacts")).setDeletedFlag(true);
} else {
return new Group(data.optString("name"), data.optString("info"), data.optString("numbers_count"));
}
}
private Group setError(Object error) {
this.error = error;
return this;
}
private Group setDeletedFlag(boolean val) {
deleted = val;
return this;
}
public boolean isDeleted(){
return deleted;
}
public boolean isError() {
return (error != null);
}
public Object getError() {
return error;
}
public String getName() {
return name;
}
public String getInfo() {
return info;
}
public String getNumbers() {
return (numbers > 0) ? numbers.toString() : "";
}
public Group setName(String name) {
if(params != null){
params.put("name",name);
}
this.name = name;
return this;
}
public Group setInfo(String info) {
if(params != null){
params.put("info",info);
}
this.info = info;
return this;
}
}