throw new JobExecutionException("Task " + taskId + " isn't a SyncTask");
}
final SyncTask syncTask = (SyncTask) this.task;
ConnectorFacadeProxy connector;
try {
connector = connInstanceLoader.getConnector(syncTask.getResource());
} catch (Exception e) {
final String msg = String.format("Connector instance bean for resource %s and connInstance %s not found",
syncTask.getResource(), syncTask.getResource().getConnector());
throw new JobExecutionException(msg, e);
}
final SchemaMapping accountIdMap = SchemaMappingUtil.getAccountIdMapping(syncTask.getResource().getMappings());
if (accountIdMap == null) {
throw new JobExecutionException("Invalid account id mapping for resource " + syncTask.getResource());
}
LOG.debug("Execute synchronization with token {}", syncTask.getResource().getSyncToken() != null
? syncTask.getResource().getSyncToken().getValue()
: null);
final List<SyncResult> results = new ArrayList<SyncResult>();
actions.beforeAll(syncTask);
try {
final SyncPolicy syncPolicy = syncTask.getResource().getSyncPolicy();
final ConflictResolutionAction conflictResolutionAction = syncPolicy != null
&& syncPolicy.getSpecification() != null
? ((SyncPolicySpec) syncPolicy.getSpecification()).getConflictResolutionAction()
: ConflictResolutionAction.IGNORE;
if (syncTask.isFullReconciliation()) {
connector.getAllObjects(ObjectClass.ACCOUNT, new SyncResultsHandler() {
@Override
public boolean handle(final SyncDelta delta) {
try {
results.addAll(handleDelta(syncTask, delta, conflictResolutionAction, dryRun));
return true;
} catch (JobExecutionException e) {
LOG.error("Reconciliation failed", e);
return false;
}
}
}, connector.getOperationOptions(syncTask.getResource()));
} else {
connector.sync(syncTask.getResource().getSyncToken(), new SyncResultsHandler() {
@Override
public boolean handle(final SyncDelta delta) {
try {
results.addAll(handleDelta(syncTask, delta, conflictResolutionAction, dryRun));
return true;
} catch (JobExecutionException e) {
LOG.error("Synchronization failed", e);
return false;
}
}
}, connector.getOperationOptions(syncTask.getResource()));
}
if (!dryRun && !syncTask.isFullReconciliation()) {
try {
ExternalResource resource = resourceDAO.find(syncTask.getResource().getName());
resource.setSyncToken(connector.getLatestSyncToken());
resourceDAO.save(resource);
} catch (Exception e) {
throw new JobExecutionException("While updating SyncToken", e);
}