/
SObjectsTemplate.java
90 lines (77 loc) · 3.37 KB
/
SObjectsTemplate.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
package org.springframework.social.salesforce.api.impl;
import org.codehaus.jackson.JsonNode;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.social.salesforce.api.SObjectDetail;
import org.springframework.social.salesforce.api.SObjectOperations;
import org.springframework.social.salesforce.api.SObjectSummary;
import org.springframework.social.salesforce.api.Salesforce;
import org.springframework.social.support.URIBuilder;
import org.springframework.util.StringUtils;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
/**
* Default implementation of SObjectOperations.
*
* @author Umut Utkan
*/
public class SObjectsTemplate extends AbstractSalesForceOperations<Salesforce> implements SObjectOperations {
private RestTemplate restTemplate;
public SObjectsTemplate(Salesforce api, RestTemplate restTemplate) {
super(api);
this.restTemplate = restTemplate;
}
@Override
public List<Map> getSObjects() {
requireAuthorization();
JsonNode dataNode = restTemplate.getForObject(api.getBaseUrl() + "/v23.0/sobjects", JsonNode.class);
return api.readList(dataNode.get("sobjects"), Map.class);
}
@Override
public SObjectSummary getSObject(String name) {
requireAuthorization();
JsonNode node = restTemplate.getForObject(api.getBaseUrl() + "/v23.0/sobjects/{name}", JsonNode.class, name);
return api.readObject(node.get("objectDescribe"), SObjectSummary.class);
}
@Override
public SObjectDetail describeSObject(String name) {
requireAuthorization();
return restTemplate.getForObject(api.getBaseUrl() + "/v23.0/sobjects/{name}/describe", SObjectDetail.class, name);
}
@Override
public Map getRow(String name, String id, String... fields) {
requireAuthorization();
URIBuilder builder = URIBuilder.fromUri(api.getBaseUrl() + "/v23.0/sobjects/" + name + "/" + id);
if (fields.length > 0) {
builder.queryParam("fields", StringUtils.arrayToCommaDelimitedString(fields));
}
return restTemplate.getForObject(builder.build(), Map.class);
}
@Override
public InputStream getBlob(String name, String id, String field) {
requireAuthorization();
return restTemplate.execute(api.getBaseUrl() + "/v23.0/sobjects/{name}/{id}/{field}",
HttpMethod.GET, null, new ResponseExtractor<InputStream>() {
@Override
public InputStream extractData(ClientHttpResponse response) throws IOException {
return response.getBody();
}
}, name, id, field);
}
@Override
@SuppressWarnings("rawtypes")
public Map<?, ?> create(String name, Map<String, String> fields) {
requireAuthorization();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Map> entity = new HttpEntity<Map>(fields, headers);
return restTemplate.postForObject(api.getBaseUrl() + "/v23.0/sobjects/{name}", entity, Map.class, name);
}
}