private int pushNew() throws URISyntaxException, LocalStorageException, IOException, HttpException { int count = 0; long[] newIDs = local.findNew(); Log.i(TAG, "Uploading " + newIDs.length + " new resource(s) (if not existing)"); try { for (long id : newIDs) try { Resource res = local.findById(id, true); String eTag = remote.add(res); if (eTag != null) local.updateETag(res, eTag); local.clearDirty(res); count++; } catch (PreconditionFailedException e) { Log.i(TAG, "Didn't overwrite existing resource with other content"); } catch (ValidationException e) { Log.e(TAG, "Couldn't create entity for adding: " + e.toString()); } catch (RecordNotFoundException e) { Log.wtf(TAG, "Couldn't read new record", e); } } finally { local.commit(); } return count; }
private int pushDirty() throws URISyntaxException, LocalStorageException, IOException, HttpException { int count = 0; long[] dirtyIDs = local.findUpdated(); Log.i(TAG, "Uploading " + dirtyIDs.length + " modified resource(s) (if not changed)"); try { for (long id : dirtyIDs) { try { Resource res = local.findById(id, true); String eTag = remote.update(res); if (eTag != null) local.updateETag(res, eTag); local.clearDirty(res); count++; } catch (PreconditionFailedException e) { Log.i(TAG, "Locally changed resource has been changed on the server in the meanwhile"); } catch (ValidationException e) { Log.e(TAG, "Couldn't create entity for updating: " + e.toString()); } catch (RecordNotFoundException e) { Log.e(TAG, "Couldn't read dirty record", e); } } } finally { local.commit(); } return count; }
private int pushDeleted() throws URISyntaxException, LocalStorageException, IOException, HttpException { int count = 0; long[] deletedIDs = local.findDeleted(); try { Log.i( TAG, "Remotely removing " + deletedIDs.length + " deleted resource(s) (if not changed)"); for (long id : deletedIDs) try { Resource res = local.findById(id, false); if (res.getName() != null) // is this resource even present remotely? try { remote.delete(res); } catch (NotFoundException e) { Log.i(TAG, "Locally-deleted resource has already been removed from server"); } catch (PreconditionFailedException e) { Log.i( TAG, "Locally-deleted resource has been changed on the server in the meanwhile"); } // always delete locally so that the record with the DELETED flag doesn't cause another // deletion attempt local.delete(res); count++; } catch (RecordNotFoundException e) { Log.wtf(TAG, "Couldn't read locally-deleted record", e); } } finally { local.commit(); } return count; }