-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDealAPI.java
More file actions
310 lines (252 loc) · 8.56 KB
/
DealAPI.java
File metadata and controls
310 lines (252 loc) · 8.56 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
package com.agilecrm.api;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import org.json.JSONArray;
import org.json.JSONObject;
import com.agilecrm.stubs.Contact;
import com.agilecrm.stubs.Deal;
import com.agilecrm.stubs.DealCollection;
import com.agilecrm.stubs.Note;
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>DealAPI</code> class contains methods to add, get, update and delete
* deals from Agile CRM
*
* @author Tejaswi
* @since March 2013
* @see APIManager
*/
public class DealAPI
{
/**
* Holds a {@link WebResource} object
*/
private final WebResource resource;
/**
* Initializes the field resource with the configured resource from
* {@link APIManager}
*
* @param resource
* {@link WebResource}
*/
DealAPI(WebResource resource)
{
this.resource = resource;
}
/**
* Adds a deal to Agile CRM with the given {@link Deal} object
*
* @param deal
* {@link Deal} to be added
* @return Added {@link Deal}
* @throws Exception
*/
public Deal addDeal(Deal deal) throws Exception
{
System.out.println("Adding deal ----------------------------------");
if (deal == null)
{
throw new Exception("Cannot create a deal without a Deal object");
}
deal = resource.path("/api/opportunity")
.accept(MediaType.APPLICATION_XML).post(Deal.class, deal);
return deal;
}
/**
* Adds a deal to Agile CRM with the given parameters
*
* @param dealName
* {@link String} name of the {@link Deal}
* @param probability
* {@link Integer} probability value of the {@link Deal}
* @param dealValue
* {@link String} value of the {@link Deal}
* @param mileStone
* {@link String} milestone of the {@link Deal}
* @return Added {@link Deal}
* @throws Exception
*/
public Deal addDeal(String dealName, Integer probability, Double dealValue,
String mileStone) throws Exception
{
System.out.println("Adding deal ----------------------------------");
if (StringUtils.isNullOrEmpty(new String[] { dealName }))
throw new Exception("Please specify name of the deal");
Deal deal = new Deal();
deal.setName(dealName);
deal.setProbability(probability);
deal.setExpected_value(dealValue);
if (mileStone != null)
deal.setMilestone(mileStone);
else
deal.setMilestone("open");
deal = resource.path("/api/opportunity")
.accept(MediaType.APPLICATION_XML).post(Deal.class, deal);
return deal;
}
/**
* Adds a deal to Agile CRM with the given parameters and relates it to the
* given list of contact id's.
*
* @param dealName
* {@link String} name of the {@link Deal}
* @param probability
* {@link Integer} probability value of the {@link Deal}
* @param dealValue
* {@link String} value of the {@link Deal}
* @param mileStone
* {@link String} milestone of the {@link Deal}
* @param contactIds
* {@link List} of {@link String} contact id's to be related to
* {@link Deal}
* @return Added {@link Deal}
* @throws Exception
*/
public Deal addDealToContacts(String dealName, Integer probability,
Double dealValue, String mileStone, List<String> contactIds)
throws Exception
{
System.out.println("Adding deal ----------------------------------");
if (StringUtils.isNullOrEmpty(contactIds))
throw new Exception("Please specify contact ids related to deal");
Deal deal = new Deal();
deal.setName(dealName);
deal.setProbability(probability);
deal.setExpected_value(dealValue);
deal.setContact_ids(contactIds);
if (mileStone != null)
deal.setMilestone(mileStone);
else
deal.setMilestone("open");
deal = resource.path("/api/opportunity")
.accept(MediaType.APPLICATION_XML).post(Deal.class, deal);
return deal;
}
/**
* Retrieves all deals from agents Agile CRM account
*
* @return {@link List} of {@link Deal}
* @throws Exception
*/
public List<Deal> getDeals()
{
System.out.println("Getting deals --------------------------------");
List<Deal> dealCollection = resource.path("/api/opportunity")
.accept(MediaType.APPLICATION_XML).get(new GenericType<List<Deal>>() {});
return dealCollection;
}
/**
* Retrieves the {@link Deal} from Agile CRM based on its Id
*
* @param dealId
* {@link String} id of the {@link Deal}
* @return {@link Deal}
* @throws Exception
*/
public Deal getDealByDealId(String dealId) throws Exception
{
System.out.println("Getting deal by id ---------------------------");
if (StringUtils.isNullOrEmpty(new String[] { dealId }))
throw new Exception("Please specify deal id to get the deal");
Deal deal = resource.path("api/opportunity/" + dealId)
.accept(MediaType.APPLICATION_JSON).get(Deal.class);
return deal;
}
/**
* Retrieves the {@link Deal} from Agile CRM based on its Id
*
* @param dealId
* {@link String} id of the {@link Deal}
* @return {@link Deal}
* @throws Exception
*/
public List<String> getNoteIds(String dealId) throws Exception
{
System.out.println("Getting deal by id ---------------------------");
if (StringUtils.isNullOrEmpty(new String[] { dealId }))
throw new Exception("Please specify deal id to get the deal");
ClientResponse deal = resource.path("api/opportunity/" + dealId)
.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
String output = deal.getEntity(String.class);
JSONObject root = new JSONObject(output);
JSONArray notesArray = root.getJSONArray("notes");
// now get the first element:
System.out.println(notesArray.length());
List<String> note_id = new ArrayList<String>();
for(int i=0;i<notesArray.length();i++){
JSONObject note = notesArray.getJSONObject(i);
String id = note.getString("id");
note_id.add(id);
}
//JSONObject firstSport = notesArray.getJSONObject(0);
// and so on
//String name = firstSport.getString("id"); // basketball
//System.out.println(name);
return note_id;
}
/**
* Updates the {@link Deal} with the given {@link Deal} object
*
* @param deal
* {@link Deal} with the changes
* @return Updated {@link Deal}
* @throws Exception
*/
public Deal updateDeal(Deal deal)
{
System.out.println("Updating deal --------------------------------");
System.out.println(deal);
deal = resource.path("/api/opportunity").accept(MediaType.APPLICATION_XML).put(Deal.class, deal);
return deal;
}
/**
* Deletes a {@link Deal} in the Agile CRM based on its id
*
* @param dealId
* {@link String} id of the existing deal
* @throws Exception
*/
public void deleteDealByDealId(String dealId) throws Exception
{
System.out.println("Deleting deal --------------------------------");
if (StringUtils.isNullOrEmpty(new String[] { dealId }))
throw new Exception("Please specify deal id to delete the deal");
resource.path("api/opportunity/" + dealId).delete();
}
/**
* Deletes list of deals in the Agile CRM based on their id's
*
* @param dealIds
* {@link List} of {@link String} deal id's to be deleted
* @throws Exception
*/
public void deleteDeals(List<String> dealIds) throws Exception
{
System.out.println("Bulk delete deals -------------------------");
if (StringUtils.isNullOrEmpty(dealIds))
throw new Exception("Please specify deal ids to be deleted");
Form form = new Form();
form.add("ids", dealIds);
resource.path("api/opportunity/bulk")
.type(MediaType.APPLICATION_FORM_URLENCODED)
.post(ClientResponse.class, form);
}
public List<Deal> getDealsByPageSizeAndCursor(String page,String cursor) {
System.out.println("Getting deals --------------------------------");
MultivaluedMap queryParams = new MultivaluedMapImpl();
queryParams.add("page_size", page);
queryParams.add("cursor", cursor);
List<Deal> dealCollection = resource.path("/api/opportunity")
.queryParams(queryParams).accept(MediaType.APPLICATION_XML)
.get(new GenericType<List<Deal>>() {});
return dealCollection;
}
}