forked from wufoo/j-woo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm.java
331 lines (305 loc) · 11.6 KB
/
Form.java
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package jWufoo;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.io.IOException;
import java.text.ParseException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Form {
String Name;
String Description;
String RedirectMessage;
String Url;
String Email;
Boolean IsPublic;
String Language;
Date StartDate;
Date EndDate;
int EntryLimit;
Date DateCreated;
Date DateUpdated;
String Hash;
String LinkEntries;
String LinkFields;
String LinkEntriesCount;
jWufooAPI api;
ArrayList<Field> fields;
ArrayList<Comment> comments;
public String getName() {return this.Name; }
public String getDescription() {return this.Description;}
public String getRedirectMessage() {return this.RedirectMessage;}
public String getUrl() {return this.Url;}
public String getEmail() {return this.Email;}
public Boolean getIsPublic() {return this.IsPublic;}
public String getLanguage() {return this.Language;}
public Date getStartDate() {return this.StartDate;}
public Date getEndDate() {return this.EndDate;}
public int getEntryLimit() {return this.EntryLimit;}
public Date getDateCreated() {return this.DateCreated;}
public Date getDateUpdated() {return this.DateUpdated;}
public String getHash() {return this.Hash;}
public Form(JSONObject json, jWufooAPI api) throws JSONException, ParseException {
this.api = api;
this.Name = json.getString("Name");
this.Description = json.getString("Description");
this.RedirectMessage = json.getString("RedirectMessage");
this.Url = json.getString("Url");
this.Email = json.getString("Email");
this.IsPublic = Utils.getBoolean(json.getString("IsPublic"));
this.Language = json.getString("Language");
this.StartDate = Utils.getDate(json.getString("StartDate"));
this.EndDate = Utils.getDate(json.getString("EndDate"));
this.EntryLimit = json.getInt("EntryLimit");
this.DateCreated = Utils.getDate(json.getString("DateCreated"));
this.DateUpdated = Utils.getDate(json.getString("DateUpdated"));
this.Hash = json.getString("Hash");
this.LinkEntries = json.getString("LinkEntries");
this.LinkFields = json.getString("LinkFields");
this.LinkEntriesCount = json.getString("LinkEntriesCount");
}
public Field getField(String title) {
ArrayList<Field> fields = new ArrayList<Field>();
fields = this.getFields();
for (Field field : fields) {
if (field.Title.contains(title)) {
return field;
}
else if (field.ID.contains(title)){
return field;
}
}
return null;
}
public ArrayList<Field> getFields() {
if (this.fields == null) {
this.fields = new ArrayList<Field>();
try {
JSONObject json = this.api.makeRequest(this.LinkFields);
JSONArray rawNodes = json.getJSONArray("Fields");
int rawCount = rawNodes.length();
for (int i = 0; i < rawCount; i++) {
this.fields.add(new Field(rawNodes.getJSONObject(i)));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return this.fields;
}
public ArrayList<Entry> getEntries(int pageStart, int pageSize, String sort) {
return getEntries(pageStart, pageSize, sort, "DESC");
}
public ArrayList<Entry> getEntries(int pageStart, int pageSize) {
return getEntries(pageStart, pageSize, "DateCreated", "DESC");
}
public ArrayList<Entry> getEntries(String sort, String sortDirection) {
return getEntries(0, 100, sort, sortDirection);
}
public ArrayList<Entry> getEntries(String sort) {
return getEntries(0, 100, sort, "DESC");
}
public ArrayList<Entry> getEntries() {
return getEntries(0, 100, "DateCreated", "DESC");
}
public ArrayList<Entry> getEntries(int pageStart, int pageSize, String sort, String sortDirection) {
ArrayList<Entry> entries = new ArrayList<Entry>();
String url = String.format("%s?system=true&pageStart=%d&pageSize=%d&sort=%s&sortDirection=%s",
this.LinkEntries,
pageStart,
pageSize,
sort,
sortDirection);
System.out.println(url);
JSONObject json = new JSONObject();
JSONArray rawNodes = new JSONArray();
int rawCount = 0;
try {
json = this.api.makeRequest(url);
rawNodes = json.getJSONArray("Entries");
rawCount = rawNodes.length();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < rawCount; i++) {
try {
entries.add(new Entry(rawNodes.getJSONObject(i), this));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return entries;
}
public ArrayList<Entry> searchEntries(ArrayList<Filter> filters, int pageStart, int pageSize, String sort) {
return searchEntries(filters, pageStart, pageSize, sort, "DESC");
}
public ArrayList<Entry> searchEntries(ArrayList<Filter> filters, int pageStart, int pageSize) {
return searchEntries(filters, pageStart, pageSize, "DateCreated", "DESC");
}
public ArrayList<Entry> searchEntries(ArrayList<Filter> filters, String sort, String sortDirection) {
return searchEntries(filters, 0, 100, sort, sortDirection);
}
public ArrayList<Entry> searchEntries(ArrayList<Filter> filters, String sort) {
return searchEntries(filters, 0, 100, sort, "DESC");
}
public ArrayList<Entry> searchEntries(ArrayList<Filter> filters) {
return searchEntries(filters, 0, 100, "DateCreated", "DESC");
}
public ArrayList<Entry> searchEntries(ArrayList<Filter> filters, int pageStart, int pageSize, String sort, String sortDirection) {
ArrayList<Entry> entries = new ArrayList<Entry>();
StringBuilder filtersBuilder = new StringBuilder();
for (int looper = 0; looper < filters.size(); looper++) {
Filter filter = filters.get(looper);
filtersBuilder.append(String.format("&Filter%s=%s+%s+%s", looper+1, filter.getField().ID, filter.getOperator(), filter.getValue()));
}
String filtersString = filtersBuilder.toString();
String url = String.format("%s?system=true&pageStart=%d&pageSize=%d&sort=%s&sortDirection=%s%s",
this.LinkEntries,
pageStart,
pageSize,
sort,
sortDirection,
filtersString);
System.out.println(url);
JSONObject json = new JSONObject();
JSONArray rawNodes = new JSONArray();
int rawCount = 0;
try {
json = this.api.makeRequest(url);
rawNodes = json.getJSONArray("Entries");
rawCount = rawNodes.length();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < rawCount; i++) {
try {
entries.add(new Entry(rawNodes.getJSONObject(i), this));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return entries;
}
public ArrayList<Comment> getComments() {
if (this.comments == null) {
String url = String.format("https://%s.wufoo.com/api/v3/forms/%s/comments.json", this.api.account, this.Hash);
this.comments = new ArrayList<Comment>();
try {
JSONObject json = this.api.makeRequest(url);
JSONArray rawNodes = json.getJSONArray("Comments");
int rawCount = rawNodes.length();
for (int i = 0; i < rawCount; i++) {
this.comments.add(new Comment(rawNodes.getJSONObject(i)));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return this.comments;
}
public Hashtable<String, String> addEntry(Entry entry) {
String url = String.format("https://%s.wufoo.com/api/v3/forms/%s/entries.json", this.api.account, this.getHash());
Hashtable<String, String> errors = new Hashtable<String, String>();
try {
JSONObject response = this.api.makeRequest(url, entry.getFields());
if (response.getInt("Success") == 1) {
entry.EntryId = response.getInt("EntryId");
}
else{
JSONArray jsonErrors = response.getJSONArray("FieldErrors");
for (int looper = 0; looper < jsonErrors.length(); looper++) {
JSONObject error = jsonErrors.getJSONObject(looper);
errors.put(error.getString("ID"), error.getString("ErrorText"));
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return errors;
}
public String getLinkUrl() {
return String.format("http://%s.wufoo.com/forms/%s/", this.api.getAccount(), this.Hash);
}
public String getEmbedUrl() {
String account = this.api.getAccount();
String hash = this.getHash();
return String.format("<script type=\"text/javascript\">var host = ((\"https:\" == document.location.protocol) ? \"https://secure.\" : \"http://\");document.write(unescape(\"%%3Cscript src='\" + host + \"wufoo.com/scripts/embed/form.js' type='text/javascript'%%3E%%3C/script%%3E\"));</script>\n<script type=\"text/javascript\">\nvar %s = new WufooForm();\n%s.initialize({\n'userName':'%s',\n'formHash':'%s', \n'autoResize':true,\n'height':'607'});\n%s.display();\n</script>", hash, hash, account, hash, hash);
}
public String getIframeEmbedUrl() {
String account = this.api.getAccount();
String hash = this.getHash();
String title = this.getName();
return String.format("<iframe height=\"607\" allowTransparency=\"true\" frameborder=\"0\" scrolling=\"no\" style=\"width:100%%;border:none\" src=\"http://%s.wufoo.com/embed/%s/\"><a href=\"http://%s.wufoo.com/forms/%s/\" title=\"%s\" rel=\"nofollow\">Fill out my Wufoo form!</a></iframe>", account, hash, account, hash, title);
}
public String addWebHook(String hookUrl, String handshakeKey, boolean returnMetadata) {
String url = String.format("https://%s.wufoo.com/api/v3/forms/%s/webhooks.json", this.api.account, this.getHash());
Hashtable<String, Object> params = new Hashtable<String, Object>();
params.put("url", hookUrl);
params.put("handshakeKey", handshakeKey);
params.put("metadata", returnMetadata);
try {
JSONObject response = this.api.makeRequest(url, params, "PUT");
response = response.getJSONObject("WebHookPutResult");
return response.getString("Hash");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
public String deleteWebHook(String webHookHash) {
String url = String.format("https://%s.wufoo.com/api/v3/forms/%s/webhooks/%s.json", this.api.account, this.getHash(), webHookHash);
Hashtable<String, Object> params = new Hashtable<String, Object>();
params.put("hash", webHookHash);
try {
JSONObject response = this.api.makeRequest(url, params, "DELETE");
response = response.getJSONObject("WebHookDeleteResult");
return response.getString("Hash");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
}