Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/main/java/com/force/api/ApiResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.force.api;

public class ApiResponse {

private String id;
private ApiError[] errors;
private boolean success;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public ApiError[] getErrors() {
return errors;
}

public void setErrors(ApiError[] errors) {
this.errors = errors;
}

public boolean isSuccess() {
return success;
}

public void setSuccess(boolean success) {
this.success = success;
}

}
24 changes: 1 addition & 23 deletions src/main/java/com/force/api/CreateResponse.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
package com.force.api;

public class CreateResponse {
String id;
ApiError[] errors;
boolean success;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public ApiError[] getErrors() {
return errors;
}
public void setErrors(ApiError[] errors) {
this.errors = errors;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public class CreateResponse extends ApiResponse {


}
64 changes: 51 additions & 13 deletions src/main/java/com/force/api/ForceApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,44 @@ public CreateOrUpdateResult createOrUpdateSObject(String type, String externalId
}
}

public UpsertResult upsertSObject(String type, String externalIdField, String externalIdValue, Object sObject) {
try {
String method = externalIdValue != null ? "?_HttpMethod=PATCH" : "";
String idValue = externalIdValue != null ? "/" + URLEncoder.encode(externalIdValue, "UTF-8") : "";
HttpResponse res =
apiRequest(new HttpRequest()
.url(uriBase() + "/sobjects/" + type + "/" + externalIdField + idValue + method)
.method("POST")
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.content(jsonMapper.writeValueAsBytes(sObject))
);
if (res.getResponseCode() == 201) {
UpsertResult upsertResult = jsonMapper.readValue(res.getStream(), UpsertResult.class);
upsertResult.setStatus(UpsertStatus.INSERTED);
return upsertResult;
} else if (res.getResponseCode() == 204) {
UpsertResult upsertResult = new UpsertResult();
if ("Id".equals(externalIdField)) {
upsertResult.setId(externalIdValue);
}
upsertResult.setStatus(UpsertStatus.UPDATED);
return upsertResult;
} else {
logger.debug("Code: {}", res.getResponseCode());
logger.debug("Message: {}", res.getString());
throw new RuntimeException();
}

} catch (JsonGenerationException e) {
throw new ResourceException(e);
} catch (JsonMappingException e) {
throw new ResourceException(e);
} catch (IOException e) {
throw new ResourceException(e);
}
}

public <T> QueryResult<T> query(String query, Class<T> clazz) {
try {
return queryAny(uriBase() + "/query/?q=" + URLEncoder.encode(query, "UTF-8"), clazz);
Expand Down Expand Up @@ -416,11 +454,11 @@ public DescribeSObject describeSObject(String sobject) {
throw new ResourceException(e);
}
}

private final String uriBase() {
return(session.getApiEndpoint()+"/services/data/"+config.getApiVersionString());
}

private final HttpResponse apiRequest(HttpRequest req) {
req.setAuthorization("Bearer "+session.getAccessToken());
req.setRequestTimeout(this.config.getRequestTimeout());
Expand Down Expand Up @@ -454,17 +492,17 @@ private final HttpResponse apiRequest(HttpRequest req) {
return res;
}
}

/**
* Normalizes the JSON response in case it contains responses from
* relationship queries. For e.g.
*
* <code>
* Query:
* select Id,Name,(select Id,Email,FirstName from Contacts) from Account
*
*
* Json Response Returned:
*
*
* {
* "totalSize" : 1,
* "done" : true,
Expand All @@ -491,9 +529,9 @@ private final HttpResponse apiRequest(HttpRequest req) {
* } ]
* }
* </code>
*
*
* Will get normalized to:
*
*
* <code>
* {
* "totalSize" : 1,
Expand All @@ -515,12 +553,12 @@ private final HttpResponse apiRequest(HttpRequest req) {
* "FirstName" : "John"
* } ]
* } ]
* }
* }
* </code
*
*
* This allows Jackson to deserialize the response into it's corresponding Object representation
*
* @param node
*
* @param node
* @return
*/
private final JsonNode normalizeCompositeResponse(JsonNode node){
Expand All @@ -531,14 +569,14 @@ private final JsonNode normalizeCompositeResponse(JsonNode node){
currNode = elements.next();

newNode.set(currNode.getKey(),
( currNode.getValue().isObject() &&
( currNode.getValue().isObject() &&
currNode.getValue().get("records")!=null
)?
currNode.getValue().get("records"):
currNode.getValue()
);
}
return newNode;

}
}
15 changes: 15 additions & 0 deletions src/main/java/com/force/api/UpsertResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.force.api;


public class UpsertResult extends ApiResponse {

private UpsertStatus status;

public UpsertStatus getStatus() {
return status;
}

public void setStatus(UpsertStatus status) {
this.status = status;
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/force/api/UpsertStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.force.api;

public enum UpsertStatus {

INSERTED,
UPDATED

}