1
0
mirror of https://github.com/etesync/android synced 2025-07-26 00:18:06 +00:00

Small refactoring

This commit is contained in:
Ricki Hirner 2015-07-18 15:15:36 +02:00
parent 92966a5c57
commit dcd86c7d86
3 changed files with 27 additions and 33 deletions

View File

@ -132,25 +132,7 @@ public class LocalAddressBook extends LocalCollection<Contact> {
c.setVCardVersion(accountSettings.getAddressBookVCardVersion()); c.setVCardVersion(accountSettings.getAddressBookVCardVersion());
return c; return c;
} }
public int deleteAllExceptRemoteNames(Resource[] remoteResources) throws LocalStorageException {
String where;
if (remoteResources.length != 0) {
List<String> sqlFileNames = new LinkedList<>();
for (Resource res : remoteResources)
sqlFileNames.add(DatabaseUtils.sqlEscapeString(res.getName()));
where = entryColumnRemoteName() + " NOT IN (" + StringUtils.join(sqlFileNames, ",") + ")";
} else
where = entryColumnRemoteName() + " IS NOT NULL";
try {
return providerClient.delete(entriesURI(), where, null);
} catch (RemoteException e) {
throw new LocalStorageException("Couldn't delete contacts locally", e);
}
}
@Override @Override
public int commit() throws LocalStorageException { public int commit() throws LocalStorageException {
int affected = super.commit(); int affected = super.commit();

View File

@ -269,7 +269,9 @@ public abstract class LocalCollection<T extends Resource> {
* @return the new resource object */ * @return the new resource object */
abstract public T newResource(long localID, String resourceName, String eTag); abstract public T newResource(long localID, String resourceName, String eTag);
/** Enqueues adding the resource (including all data) to the local collection. */ /** Adds the resource (including all data) to the local collection.
* @param resource Resource to be added
*/
public void add(Resource resource) throws LocalStorageException { public void add(Resource resource) throws LocalStorageException {
int idx = pendingOperations.size(); int idx = pendingOperations.size();
pendingOperations.add( pendingOperations.add(
@ -281,8 +283,8 @@ public abstract class LocalCollection<T extends Resource> {
commit(); commit();
} }
/** Enqueues updating an existing resource in the local collection. The resource will be found by /** Updates an existing resource in the local collection. The resource will be found by
* the remote file name and all data will be updated. Requires commit(). */ * the remote file name and all data will be updated. */
public void updateByRemoteName(Resource remoteResource) throws LocalStorageException { public void updateByRemoteName(Resource remoteResource) throws LocalStorageException {
T localResource = findByRemoteName(remoteResource.getName(), false); T localResource = findByRemoteName(remoteResource.getName(), false);
pendingOperations.add( pendingOperations.add(
@ -296,7 +298,7 @@ public abstract class LocalCollection<T extends Resource> {
commit(); commit();
} }
/** Enqueues deleting a resource from the local collection. Requires commit(). */ /** Enqueues deleting a resource from the local collection. Requires commit() to be effective! */
public void delete(Resource resource) { public void delete(Resource resource) {
pendingOperations.add(ContentProviderOperation pendingOperations.add(ContentProviderOperation
.newDelete(ContentUris.withAppendedId(entriesURI(), resource.getLocalID())) .newDelete(ContentUris.withAppendedId(entriesURI(), resource.getLocalID()))
@ -309,7 +311,8 @@ public abstract class LocalCollection<T extends Resource> {
* @param remoteResources resources with these remote file names will be kept * @param remoteResources resources with these remote file names will be kept
* @return number of deleted resources * @return number of deleted resources
*/ */
public int deleteAllExceptRemoteNames(Resource[] remoteResources) throws LocalStorageException { public int deleteAllExceptRemoteNames(Resource[] remoteResources) throws LocalStorageException
{
final String where; final String where;
if (remoteResources.length != 0) { if (remoteResources.length != 0) {
@ -322,13 +325,19 @@ public abstract class LocalCollection<T extends Resource> {
// delete all entries // delete all entries
where = entryColumnRemoteName() + " IS NOT NULL"; where = entryColumnRemoteName() + " IS NOT NULL";
Log.d(TAG, "deleteAllExceptRemoteNames: " + where);
try { try {
return providerClient.delete( if (entryColumnParentID() != null)
entriesURI(), // entries have a parent collection (for instance, events which have a calendar)
// restrict deletion to parent collection return providerClient.delete(
entryColumnParentID() + "=? AND (" + where + ')', entriesURI(),
new String[] { String.valueOf(getId()) } entryColumnParentID() + "=? AND (" + where + ')', // restrict deletion to parent collection
); new String[] { String.valueOf(getId()) }
);
else
// entries don't have a parent collection (contacts are stored directly and not within an address book)
return providerClient.delete(entriesURI(), null, null);
} catch (RemoteException e) { } catch (RemoteException e) {
throw new LocalStorageException("Couldn't delete local resources", e); throw new LocalStorageException("Couldn't delete local resources", e);
} }
@ -348,7 +357,7 @@ public abstract class LocalCollection<T extends Resource> {
} }
} }
/** Enqueues removing the dirty flag from a locally-stored resource. Requires commit(). */ /** Enqueues removing the dirty flag from a locally-stored resource. Requires commit() to be effective! */
public void clearDirty(Resource resource) { public void clearDirty(Resource resource) {
pendingOperations.add(ContentProviderOperation pendingOperations.add(ContentProviderOperation
.newUpdate(ContentUris.withAppendedId(entriesURI(), resource.getLocalID())) .newUpdate(ContentUris.withAppendedId(entriesURI(), resource.getLocalID()))
@ -364,8 +373,11 @@ public abstract class LocalCollection<T extends Resource> {
Log.d(TAG, "Committing " + pendingOperations.size() + " operations ..."); Log.d(TAG, "Committing " + pendingOperations.size() + " operations ...");
ContentProviderResult[] results = providerClient.applyBatch(pendingOperations); ContentProviderResult[] results = providerClient.applyBatch(pendingOperations);
for (ContentProviderResult result : results) for (ContentProviderResult result : results)
if (result != null && result.count != null) if (result != null) // will have either .uri or .count set
affected += result.count; if (result.count != null)
affected += result.count;
else if (result.uri != null)
affected = 1;
Log.d(TAG, "... " + affected + " row(s) affected"); Log.d(TAG, "... " + affected + " row(s) affected");
pendingOperations.clear(); pendingOperations.clear();
} catch(OperationApplicationException | RemoteException ex) { } catch(OperationApplicationException | RemoteException ex) {

View File

@ -87,7 +87,7 @@ public class SyncManager {
syncResult.stats.numInserts = pullNew(remotelyAdded.toArray(new Resource[remotelyAdded.size()])); syncResult.stats.numInserts = pullNew(remotelyAdded.toArray(new Resource[remotelyAdded.size()]));
syncResult.stats.numUpdates = pullChanged(remotelyUpdated.toArray(new Resource[remotelyUpdated.size()])); syncResult.stats.numUpdates = pullChanged(remotelyUpdated.toArray(new Resource[remotelyUpdated.size()]));
Log.i(TAG, "Removing non-dirty resources that are not present remotely anymore"); Log.i(TAG, "Removing entries that are not present remotely anymore (retaining " + remoteResources.length + " entries)");
syncResult.stats.numDeletes = local.deleteAllExceptRemoteNames(remoteResources); syncResult.stats.numDeletes = local.deleteAllExceptRemoteNames(remoteResources);
syncResult.stats.numEntries = syncResult.stats.numInserts + syncResult.stats.numUpdates + syncResult.stats.numDeletes; syncResult.stats.numEntries = syncResult.stats.numInserts + syncResult.stats.numUpdates + syncResult.stats.numDeletes;