use of com.salesforce.androidsdk.rest.CompositeResponse.CompositeSubResponse in project SalesforceMobileSDK-Android by forcedotcom.
the class ParentChildrenSyncUpTarget method syncUpRecord.
private void syncUpRecord(SyncManager syncManager, JSONObject record, JSONArray children, List<String> fieldlist, SyncState.MergeMode mergeMode) throws JSONException, IOException {
boolean isCreate = isLocallyCreated(record);
boolean isDelete = isLocallyDeleted(record);
// Preparing request for parent
LinkedHashMap<String, RestRequest> refIdToRequests = new LinkedHashMap<>();
String parentId = record.getString(getIdFieldName());
RestRequest parentRequest = buildRequestForParentRecord(syncManager.apiVersion, record, fieldlist);
// Parent request goes first unless it's a delete
if (parentRequest != null && !isDelete)
refIdToRequests.put(parentId, parentRequest);
// Preparing requests for children
for (int i = 0; i < children.length(); i++) {
JSONObject childRecord = children.getJSONObject(i);
String childId = childRecord.getString(childrenInfo.idFieldName);
// Children need to be updated
if (isCreate) {
childRecord.put(LOCAL, true);
childRecord.put(LOCALLY_UPDATED, true);
}
RestRequest childRequest = buildRequestForChildRecord(syncManager.apiVersion, childRecord, isCreate, isDelete ? null : parentId);
if (childRequest != null)
refIdToRequests.put(childId, childRequest);
}
// Parent request goes last when it's a delete
if (parentRequest != null && isDelete)
refIdToRequests.put(parentId, parentRequest);
// Sending composite request
Map<String, CompositeSubResponse> refIdToResponses = CompositeRequestHelper.sendCompositeRequest(syncManager, false, refIdToRequests);
// Build refId to server id / status code / time stamp maps
Map<String, String> refIdToServerId = CompositeRequestHelper.parseIdsFromResponses(refIdToResponses.values());
// Will a re-run be required?
boolean needReRun = false;
// Update parent in local store
if (isDirty(record)) {
needReRun = updateParentRecordInLocalStore(syncManager, record, children, mergeMode, refIdToServerId, refIdToResponses.get(record.getString(getIdFieldName())));
}
// Update children local store
for (int i = 0; i < children.length(); i++) {
JSONObject childRecord = children.getJSONObject(i);
if (isDirty(childRecord) || isCreate) {
needReRun = needReRun || updateChildRecordInLocalStore(syncManager, childRecord, record, mergeMode, refIdToServerId, refIdToResponses.get(childRecord.getString(childrenInfo.idFieldName)));
}
}
// Re-run if required
if (needReRun) {
MobileSyncLogger.d(TAG, "syncUpOneRecord", record);
syncUpRecord(syncManager, record, children, fieldlist, mergeMode);
}
}
use of com.salesforce.androidsdk.rest.CompositeResponse.CompositeSubResponse in project SalesforceMobileSDK-Android by forcedotcom.
the class CompositeRequestHelper method sendCompositeRequest.
/**
* Build and send composite request
* @param syncManager
* @param allOrNone
* @param refIdToRequests
* @return map of ref id to composite sub response
* @throws JSONException
* @throws IOException
*/
public static Map<String, CompositeSubResponse> sendCompositeRequest(SyncManager syncManager, boolean allOrNone, LinkedHashMap<String, RestRequest> refIdToRequests) throws JSONException, IOException {
RestRequest compositeRequest = RestRequest.getCompositeRequest(syncManager.apiVersion, allOrNone, refIdToRequests);
RestResponse response = syncManager.sendSyncWithMobileSyncUserAgent(compositeRequest);
if (!response.isSuccess()) {
throw new SyncManager.MobileSyncException("sendCompositeRequest:" + response.toString());
}
CompositeResponse compositeResponse = new CompositeResponse(response.asJSONObject());
Map<String, CompositeSubResponse> refIdToResponses = new HashMap<>();
for (CompositeSubResponse subResponse : compositeResponse.subResponses) {
refIdToResponses.put(subResponse.referenceId, subResponse);
}
return refIdToResponses;
}
use of com.salesforce.androidsdk.rest.CompositeResponse.CompositeSubResponse in project SalesforceMobileSDK-Android by forcedotcom.
the class BatchSyncUpTarget method syncUpRecords.
private void syncUpRecords(SyncManager syncManager, List<JSONObject> records, List<String> fieldlist, SyncState.MergeMode mergeMode, String syncSoupName, boolean isReRun) throws JSONException, IOException {
if (records.size() > getMaxBatchSize()) {
throw new SyncManager.MobileSyncException(getClass().getSimpleName() + ":syncUpRecords can handle up to " + getMaxBatchSize() + " records");
}
if (records.isEmpty()) {
return;
}
LinkedHashMap<String, RestRequest> refIdToRequests = new LinkedHashMap<>();
for (int i = 0; i < records.size(); i++) {
JSONObject record = records.get(i);
String id = JSONObjectHelper.optString(record, getIdFieldName());
if (id == null) {
// create local id - needed for refId
id = createLocalId();
record.put(getIdFieldName(), id);
}
RestRequest request = buildRequestForRecord(syncManager.apiVersion, record, fieldlist);
if (request != null)
refIdToRequests.put(id, request);
}
// Sending composite request
Map<String, CompositeSubResponse> refIdToResponses = CompositeRequestHelper.sendCompositeRequest(syncManager, false, refIdToRequests);
// Build refId to server id / status code / time stamp maps
Map<String, String> refIdToServerId = CompositeRequestHelper.parseIdsFromResponses(refIdToResponses.values());
// Will a re-run be required?
boolean needReRun = false;
// Update local store
for (int i = 0; i < records.size(); i++) {
JSONObject record = records.get(i);
String id = record.getString(getIdFieldName());
if (isDirty(record)) {
needReRun = needReRun || updateRecordInLocalStore(syncManager, syncSoupName, record, mergeMode, refIdToServerId, refIdToResponses.get(id), isReRun);
}
}
// Re-run if required
if (needReRun && !isReRun) {
syncUpRecords(syncManager, records, fieldlist, mergeMode, syncSoupName, true);
}
}
Aggregations