SalesforceLeadSearch.java
7.45 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.crypto.CryptoException
* com.adobe.granite.crypto.CryptoSupport
* com.day.cq.wcm.webservicesupport.Configuration
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mcm.salesforce;
import com.adobe.cq.mcm.salesforce.SalesforceClient;
import com.adobe.cq.mcm.salesforce.SalesforceException;
import com.adobe.cq.mcm.salesforce.SalesforceResponse;
import com.adobe.cq.mcm.salesforce.SalesforceSearchParameters;
import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import com.day.cq.wcm.webservicesupport.Configuration;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service(value={SalesforceLeadSearch.class})
public class SalesforceLeadSearch {
private static final Logger log = LoggerFactory.getLogger(SalesforceLeadSearch.class);
private static final String INSTANCE_URL = "instanceurl";
public static final String CUSTOMER_KEY = "customerkey";
public static final String CUSTOMER_SECRET = "customersecret";
public static final String REFRESH_TOKEN = "refreshtoken";
private static final String QUERY_PATH = "/services/data/v20.0/query/";
public static final String APPLICATION_JSON = "application/json";
public static final String ACCESS_TOKEN = "accesstoken";
@Reference
private CryptoSupport cryptoSupport;
public JSONObject search(Configuration cloudConfig, SalesforceSearchParameters parameters, ResourceResolver resolver) throws SalesforceException {
if (cloudConfig != null) {
String instanceUrl = (String)cloudConfig.get("instanceurl", (Object)"");
String accessToken = (String)cloudConfig.get("accesstoken", (Object)"");
String clientId = (String)cloudConfig.get("customerkey", (Object)"");
String encryptedCustomerSecret = (String)cloudConfig.get("customersecret", (Object)"");
String encryptedRefereshToken = (String)cloudConfig.get("refreshtoken", (Object)"");
try {
String customerSecret = encryptedCustomerSecret;
String refreshToken = encryptedRefereshToken;
if (this.cryptoSupport.isProtected(encryptedCustomerSecret)) {
customerSecret = this.cryptoSupport.unprotect(encryptedCustomerSecret);
}
if (this.cryptoSupport.isProtected(encryptedRefereshToken)) {
refreshToken = this.cryptoSupport.unprotect(encryptedRefereshToken);
}
SalesforceClient client = new SalesforceClient();
client.setAccessToken(accessToken);
client.setInstanceURL(instanceUrl);
client.setRefreshToken(refreshToken);
client.setClientId(clientId);
client.setClientSecret(customerSecret);
client.setMethod(SalesforceClient.AvailableMethods.GET);
client.setContentType("application/json");
client.setPath("/services/data/v20.0/query/");
client.addParameter("q", this.buildSOSL(parameters));
SalesforceResponse response = client.executeRequest();
if (response.getAccessTokenUpdated().booleanValue()) {
String configPath = cloudConfig.getPath();
Resource configResource = resolver.getResource(configPath);
Node configNode = ((Node)configResource.adaptTo(Node.class)).getNode("jcr:content");
configNode.setProperty("accesstoken", client.getAccessToken());
configNode.getSession().save();
}
return response.getBodyAsJSON();
}
catch (RepositoryException e) {
log.error("Repository Exception in Searching SFDC Leads " + e.getMessage());
throw new SalesforceException("Repository Exception in Searching SFDC Leads " + e.getMessage());
}
catch (CryptoException e) {
log.error("Cryto Exception in searching SFDC Leads " + e.getMessage());
throw new SalesforceException("Crypto Exception in searching SFDC Leads " + e.getMessage());
}
catch (JSONException e) {
log.error("JSON Exception in searching SFDC Leads " + e.getMessage());
throw new SalesforceException("JSON Exception in searching SFDC Leads " + e.getMessage());
}
}
return null;
}
protected String buildSOSL(SalesforceSearchParameters parameters) throws SalesforceException {
StringBuilder query = new StringBuilder();
query.append("SELECT ");
if (parameters.getResultProperties() != null && parameters.getResultProperties().length > 0) {
for (int i = 0; i < parameters.getResultProperties().length; ++i) {
if (parameters.getResultProperties()[i] == null) continue;
query.append(parameters.getResultProperties()[i] + ", ");
}
query.deleteCharAt(query.lastIndexOf(","));
} else {
query.append("FirstName, LastName, Company, Status, Email ");
}
if (SalesforceSearchParameters.SalesforceObjectType.LEAD.equals((Object)parameters.getObjectType())) {
query.append("FROM LEAD ");
} else if (SalesforceSearchParameters.SalesforceObjectType.CONTACT.equals((Object)parameters.getObjectType())) {
query.append("FROM CONTACT ");
} else {
throw new SalesforceException("Invalid Search Query: Must search for either Leads or Contacts");
}
if (parameters.getSearchOperator() != null && parameters.getSearchType() != null && parameters.getSearchVal() != null) {
query.append("WHERE " + parameters.getSearchType() + " " + parameters.getSearchOperator() + " " + this.getEncodedSearchVal(parameters.getSearchOperator(), parameters.getSearchVal()));
}
return query.toString();
}
private String getEncodedSearchVal(String operator, String searchVal) {
Double searchValue = null;
try {
searchValue = Double.parseDouble(searchVal);
return searchValue.toString();
}
catch (NumberFormatException e) {
return "'" + searchVal + "'";
}
}
protected void bindCryptoSupport(CryptoSupport cryptoSupport) {
this.cryptoSupport = cryptoSupport;
}
protected void unbindCryptoSupport(CryptoSupport cryptoSupport) {
if (this.cryptoSupport == cryptoSupport) {
this.cryptoSupport = null;
}
}
}