-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactAPI.java
More file actions
527 lines (445 loc) · 15.5 KB
/
ContactAPI.java
File metadata and controls
527 lines (445 loc) · 15.5 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package com.agilecrm.api;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONObject;
import com.agilecrm.stubs.Contact;
import com.agilecrm.stubs.ContactCollection;
import com.agilecrm.stubs.ContactField;
import com.agilecrm.stubs.ContactField.FieldName;
import com.agilecrm.stubs.Tag;
import com.agilecrm.utils.StringUtils;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.representation.Form;
import com.sun.jersey.core.util.MultivaluedMapImpl;
/**
* <code>ContactAPI</code> class contains methods to add, get, update and delete
* contacts from Agile CRM
*
* @author Ghanshyam
* @since April 2015
* @see APIManager
*/
public class ContactAPI
{
/**
* Holds a {@link WebResource} object
*/
private final WebResource resource;
/**
* Initializes the field resource with the configured resource from
* {@link APIManager}
*
* @param resource
* {@link WebResource}
*/
ContactAPI(WebResource resource)
{
this.resource = resource;
}
/**
* Adds a contact to Agile CRM with the given {@link Contact} object
*
* @param contact
* {@link Contact} to be added
* @return Added {@link Contact}
* @throws Exception
*/
public Contact addContact(Contact contact) throws Exception
{
System.out.println("Adding contact -------------------------");
if (contact == null)
{
throw new Exception("Cannot add a contact without a contact object");
}
contact = resource.path("/api/contacts")
.accept(MediaType.APPLICATION_XML).post(Contact.class, contact);
return contact;
}
/**
* Adds a contact to Agile CRM with the given parameters
*
* @param firstName
* First Name of the {@link Contact}
* @param lastName
* Last Name of the {@link Contact}
* @param organization
* Organization name of the {@link Contact}
* @param email
* Email of the {@link Contact}
* @param title
* Designation of the {@link Contact}
* @param phone
* Phone number of the {@link Contact}
* @param website
* Web URL of the {@link Contact}
* @return Added {@link Contact}
* @throws Exception
*/
public Contact addContact(String firstName, String lastName,
String organization, String email, String title, String phone,
String website) throws Exception
{
System.out.println("Adding contact -------------------------");
if (StringUtils
.isNullOrEmpty(new String[] { firstName, lastName, email }))
{
throw new Exception("Contact fileds empty");
}
Contact contact = new Contact();
contact = setContactFieldstoContact(contact, firstName, lastName,
organization, email, title, phone, website);
contact = resource.path("/api/contacts")
.accept(MediaType.APPLICATION_XML).post(Contact.class, contact);
return contact;
}
/**
* Retrieves all the contacts from agents Agile CRM account
*
* @return {@link List} of {@link Contact}
* @throws Exception
*/
public List<Contact> getContacts() throws Exception
{
System.out.println("Getting contacts -----------------------");
List<Contact> contactCollection = resource.path("/api/contacts")
.accept(MediaType.APPLICATION_XML).get(new GenericType<List<Contact>>() {});
return contactCollection;
}
/**
* Retrieves the {@link Contact} from Agile CRM based on its Id
*
* @param contactId
* {@link String} id of the {@link Contact}
* @return {@link Contact}
* @throws Exception
*/
public Contact getContact(String contactId) throws Exception
{
System.out.println("Getting contact by id ----------------");
if (StringUtils.isNullOrEmpty(new String[] { contactId }))
throw new Exception("Cannot get a contact without contact id");
Contact contact = resource.path("/api/contacts/" + contactId)
.accept(MediaType.APPLICATION_XML).get(Contact.class);
return contact;
}
/**
* Retrieves the {@link Contact} from Agile CRM based on its email
*
* @param email
* {@link String} email of the {@link Contact}
* @return {@link Contact}
* @throws Exception
*/
public Contact getContactFromEmail(String email) throws Exception
{
System.out.println("Getting contact by email ----------------");
if (StringUtils.isNullOrEmpty(new String[] { email }))
throw new Exception("Cannot get a contact without email");
Contact contact = resource.path("/api/contacts/search/email/" + email)
.accept(MediaType.APPLICATION_XML).get(Contact.class);
return contact;
}
/**
* Updates the {@link Contact} based on its id and given parameters
*
* @param contactId
* {@link String} id of the {@link Contact}
* @param contactFields
* {@link Map} which contact fields to be updated
* @param customFields
* {@link Map} which contains custom fields to be added to
* {@link Contact}
* @return Updated {@link Contact}
* @throws Exception
*/
public Contact updateContact(String contactId,
Map<FieldName, String> contactFields,
Map<String, String> customFields) throws Exception
{
System.out.println("updating contact -----------------------");
if (StringUtils.isNullOrEmpty(new String[] { contactId }))
throw new Exception("Cannot update a contact without contact id");
if (StringUtils.isNullOrEmpty(contactFields)
&& StringUtils.isNullOrEmpty(customFields))
{
throw new Exception(
"Specify contact field or custom field to update a contact");
}
Contact contact = getContact(contactId);
contact = setContactFieldstoContact(contact, contactFields);
contact = setCustomFieldstoContact(contact, customFields);
System.out.println(new JSONObject(new ObjectMapper()
.writeValueAsString(contact)));
contact = resource.path("/api/contacts").put(Contact.class, contact);
return contact;
}
/**
* Updates the {@link Contact} with the given {@link Contact} object
*
* @param contact
* {@link Contact} with the changes
* @return Updated {@link Contact}
* @throws Exception
*/
public Contact updateContact(Contact contact) throws Exception
{
System.out.println("updating contact -----------------------");
System.out.println(new JSONObject(new ObjectMapper()
.writeValueAsString(contact)));
if (contact == null)
{
throw new Exception(
"Cannot update a contact without a contact object");
}
contact = resource.path("/api/contacts").put(Contact.class, contact);
return contact;
}
/**
* Deletes a {@link Contact} in the Agile CRM based on its id
*
* @param contactId
* {@link String} id of the existing contact
* @throws Exception
*/
public void deleteContact(String contactId) throws Exception
{
System.out.println("deleting contact by id -----------------");
if (StringUtils.isNullOrEmpty(new String[] { contactId }))
throw new Exception("Cannot delete a contact without contact id");
resource.path("/api/contacts/" + contactId)
.accept(MediaType.TEXT_PLAIN).delete();
}
/**
* Deletes list of contacts in the Agile CRM based on their id's
*
* @param contactIds
* {@link List} of {@link String} contact id's to be deleted
* @throws Exception
*/
public void deleteBulkContacts(List<String> contactIds) throws Exception
{
System.out.println("Bulk delete contacts -----------------------"+contactIds);
if (StringUtils.isNullOrEmpty(contactIds))
throw new Exception("Specify contact ids to delete them");
Form form = new Form();
form.add("ids", contactIds);
resource.path("/api/bulk/update").queryParam("action_type", "DELETE")
.type(MediaType.APPLICATION_FORM_URLENCODED)
.post(ClientResponse.class, form);
}
/**
* Adds tags to the existing contacts in the Agile CRM
*
* @param tags
* {@link List} of {@link String} tags to be added
* @param contactIds
* {@link List} of {@link String} contact id's to which tags are
* added
* @throws Exception
*/
public void addTagsToContacts(List<String> tags, List<String> contactIds)
throws Exception
{
System.out
.println("Adding tags to contacts based on contact id's ----");
if (StringUtils.isNullOrEmpty(tags)
|| StringUtils.isNullOrEmpty(contactIds))
{
throw new Exception("Tags or contact ids are not provided");
}
Form form = new Form();
form.add("tags", tags);
form.add("contact_ids", contactIds);
resource.path("/api/bulk/update").queryParam("action_type", "ADD_TAG")
.post(ClientResponse.class, form);
}
/**
* Adds tag to the existing contact with email specified in the Agile CRM
*
* @param tag
* {@link Tag} to be added to {@link Contact}
* @param email
* {@link String} email of the {@link Contact}
* @throws Exception
*/
public void addTagsToEmail(Tag tag, String email) throws Exception
{
System.out
.println("Adding tags to contact based on email ------------------");
Form form = new Form();
form.add("tags", tag.getTags());
form.add("email", email);
resource.path("/api/contacts/email/tags/add")
.accept(MediaType.APPLICATION_XML)
.post(ClientResponse.class, form);
}
/**
* Sets the given parameters(contact fields) to the given contact by
* iterating through the map.
*
* This method is used internally while adding and updating contact
*
* @param contact
* {@link Contact}
* @param contactFields
* {@link Map} of contact fields to be set to contact
* @return {@link Contact} after setting fields
*/
private Contact setContactFieldstoContact(Contact contact,
Map<FieldName, String> contactFields)
{
@SuppressWarnings("rawtypes")
Iterator fields = contactFields.entrySet().iterator();
while (fields.hasNext())
{
@SuppressWarnings("unchecked")
Entry<FieldName, String> thisPair = (Entry<FieldName, String>) fields
.next();
FieldName contactFieldName = thisPair.getKey();
String contactFieldValue = thisPair.getValue();
contact.setContactField(contactFieldName, contactFieldValue);
}
return contact;
}
/**
* Sets the given parameters(custom fields) to the given contact by
* iterating through the map.
*
* This method is used internally while adding and updating contact
*
* @param contact
* {@link Contact}
* @param customFields
* {@link Map} of custom fields to be set to contact
* @return {@link Contact} after setting fields
*/
private Contact setCustomFieldstoContact(Contact contact,
Map<String, String> customFields)
{
@SuppressWarnings("rawtypes")
Iterator fields = customFields.entrySet().iterator();
while (fields.hasNext())
{
@SuppressWarnings("unchecked")
Entry<String, String> thisPair = (Entry<String, String>) fields
.next();
String customFieldName = thisPair.getKey();
String customFieldValue = thisPair.getValue();
contact.setCustomField(customFieldName, customFieldValue);
}
return contact;
}
/**
* Sets the given parameters(contact fields) to the given contact.
*
* This method is used internally while adding and updating contact
*
* @param contact
* {@link Contact}
* @param firstName
* First Name of the {@link Contact}
* @param lastName
* Last Name of the {@link Contact}
* @param organization
* Organization name of the {@link Contact}
* @param email
* Email of the {@link Contact}
* @param title
* Designation of the {@link Contact}
* @param phone
* Phone number of the {@link Contact}
* @param website
* Web URL of the {@link Contact}
* @return {@link Contact} after setting fields
*/
private Contact setContactFieldstoContact(Contact contact,
String firstName, String lastName, String organization,
String email, String title, String phone, String website)
{
contact.setContactField(FieldName.FIRST_NAME, firstName);
contact.setContactField(FieldName.LAST_NAME, lastName);
contact.setContactField(FieldName.ORGANIZATION, organization);
contact.setContactField(FieldName.EMAIL, email);
contact.setContactField(FieldName.TITLE, title);
contact.setContactField(FieldName.PHONE, phone);
contact.setContactField(FieldName.WEBSITE, website);
return contact;
}
/**
* Adds the given parameters (property name and property value) to the given contact,
* if the property already exists, it is updated.
*
* This method is used internally while adding and updating contact property
*
* @param name
* name of the property to be added
* @param value
* value of the property to be added
* @param email
* email of the contact
* @return
*/
public void addProperty(String name, String value, String email)
{
ContactField contactField = new ContactField();
contactField.setName(name);
contactField.setValue(value);
resource.path("api/contacts/add/property").queryParam("email", email).post(Contact.class, contactField);
}
public List<Contact> getContactsByPageCursor(String page,String cursor) {
System.out
.println("Getting contacts By page and cursor-----------------------");
MultivaluedMap queryParams = new MultivaluedMapImpl();
queryParams.add("page_size", page);
queryParams.add("cursor", cursor);
List<Contact> contactCollection = resource.path("/api/contacts")
.queryParams(queryParams)
.accept(MediaType.APPLICATION_XML).get(new GenericType<List<Contact>>() {});
return contactCollection;
}
/**
* Delete tag to the existing contact with email specified in the Agile CRM
*
* @param tag1
* {@link Tag} to be added to {@link Contact}
* @param email
* {@link String} email of the {@link Contact}
* @throws Exception
*/
public void deleteTagsToEmail(Tag tag1, String email) throws Exception {
System.out.println("Deleting tags to contact based on email ------------------");
Form form = new Form();
form.add("tags", tag1.getTags());
form.add("email", email);
ClientResponse clientResponse=resource.path("/api/contacts/email/tags/delete")
.accept(MediaType.APPLICATION_XML)
.post(ClientResponse.class, form);
System.out.println("Response code --"+clientResponse.getStatus());
}
/**
* Add score to the existing contact with email specified in the Agile CRM
*
* @param score
* {@link Tag} to be added to {@link Contact}
* @param email
* {@link String} email of the {@link Contact}
* @throws Exception
*/
public void addScoreToEmail(String score, String email) {
Form form = new Form();
form.add("score", score);
form.add("email", email);
ClientResponse clientResponse=resource.path("/api/contacts/add-score")
.accept(MediaType.APPLICATION_XML)
.post(ClientResponse.class, form);
System.out.println(" Response code = "+clientResponse.getStatus());
}
}