Fixed bugs with syncing task attachments

pull/14/head
Sam Bosley 14 years ago
parent 598384480f
commit 6867c26978

@ -709,6 +709,9 @@ public final class ActFmSyncService {
if (attachmentId <= 0) if (attachmentId <= 0)
return; return;
if (!checkForToken())
return;
ArrayList<Object> params = new ArrayList<Object>(); ArrayList<Object> params = new ArrayList<Object>();
params.add("id"); params.add(attachmentId); params.add("id"); params.add(attachmentId);
params.add("token"); params.add(token); params.add("token"); params.add(token);
@ -809,6 +812,7 @@ public final class ActFmSyncService {
task.putTransitory(SyncFlags.ACTFM_SUPPRESS_SYNC, true); task.putTransitory(SyncFlags.ACTFM_SUPPRESS_SYNC, true);
taskService.save(task); taskService.save(task);
metadataService.synchronizeMetadata(task.getId(), metadata, Metadata.KEY.eq(TagService.KEY)); metadataService.synchronizeMetadata(task.getId(), metadata, Metadata.KEY.eq(TagService.KEY));
synchronizeAttachments(result, task);
} }
/** /**
@ -1243,58 +1247,63 @@ public final class ActFmSyncService {
protected Class<Task> typeClass() { protected Class<Task> typeClass() {
return Task.class; return Task.class;
} }
}
private void synchronizeAttachments(JSONObject item, Task model) { private void synchronizeAttachments(JSONObject item, Task model) {
TodorooCursor<Metadata> attachments = metadataService.query(Query.select(Metadata.PROPERTIES) TodorooCursor<Metadata> attachments = metadataService.query(Query.select(Metadata.PROPERTIES)
.where(Criterion.and(MetadataCriteria.byTaskAndwithKey(model.getId(), .where(Criterion.and(MetadataCriteria.byTaskAndwithKey(model.getId(),
FileMetadata.METADATA_KEY), FileMetadata.REMOTE_ID.gt(0)))); FileMetadata.METADATA_KEY), FileMetadata.REMOTE_ID.gt(0))));
try { try {
HashMap<Long, Metadata> currentFiles = new HashMap<Long, Metadata>(); HashMap<Long, Metadata> currentFiles = new HashMap<Long, Metadata>();
for (attachments.moveToFirst(); !attachments.isAfterLast(); attachments.moveToNext()) { for (attachments.moveToFirst(); !attachments.isAfterLast(); attachments.moveToNext()) {
Metadata m = new Metadata(attachments); Metadata m = new Metadata(attachments);
currentFiles.put(m.getValue(FileMetadata.REMOTE_ID), m); currentFiles.put(m.getValue(FileMetadata.REMOTE_ID), m);
} }
JSONArray remoteFiles = item.getJSONArray("attachments"); JSONArray remoteFiles = item.getJSONArray("attachments");
for (int i = 0; i < remoteFiles.length(); i++) { for (int i = 0; i < remoteFiles.length(); i++) {
JSONObject file = remoteFiles.getJSONObject(i); JSONObject file = remoteFiles.getJSONObject(i);
long id = file.optLong("id"); long id = file.optLong("id");
if (currentFiles.containsKey(id)) { if (currentFiles.containsKey(id)) {
// Match, make sure name and url are up to date, then remove from map // Match, make sure name and url are up to date, then remove from map
Metadata fileMetadata = currentFiles.get(id); Metadata fileMetadata = currentFiles.get(id);
fileMetadata.setValue(FileMetadata.URL, file.getString("url")); fileMetadata.setValue(FileMetadata.URL, file.getString("url"));
fileMetadata.setValue(FileMetadata.NAME, file.getString("name")); fileMetadata.setValue(FileMetadata.NAME, file.getString("name"));
metadataService.save(fileMetadata); metadataService.save(fileMetadata);
currentFiles.remove(id); currentFiles.remove(id);
} else { } else {
// Create new file attachment // Create new file attachment
Metadata newAttachment = FileMetadata.createNewFileMetadata(model.getId(), "", Metadata newAttachment = FileMetadata.createNewFileMetadata(model.getId(), "",
file.getString("name"), file.getString("content_type")); file.getString("name"), file.getString("content_type"));
String url = file.getString("url"); String url = file.getString("url");
newAttachment.setValue(FileMetadata.URL, url); newAttachment.setValue(FileMetadata.URL, url);
newAttachment.setValue(FileMetadata.REMOTE_ID, id); newAttachment.setValue(FileMetadata.REMOTE_ID, id);
metadataService.save(newAttachment); metadataService.save(newAttachment);
}
} }
}
// Remove all the leftovers // Remove all the leftovers
Set<Long> attachmentsToDelete = currentFiles.keySet(); Set<Long> attachmentsToDelete = currentFiles.keySet();
for (Long remoteId : attachmentsToDelete) { for (Long remoteId : attachmentsToDelete) {
Metadata toDelete = currentFiles.get(remoteId); Metadata toDelete = currentFiles.get(remoteId);
String path = toDelete.getValue(FileMetadata.FILE_PATH);
if (TextUtils.isEmpty(path))
metadataService.delete(toDelete);
else {
File f = new File(toDelete.getValue(FileMetadata.FILE_PATH)); File f = new File(toDelete.getValue(FileMetadata.FILE_PATH));
if (f.delete()) { if (!f.exists() || f.delete()) {
metadataService.delete(toDelete); metadataService.delete(toDelete);
} }
}
} catch (JSONException e) { }
e.printStackTrace();
} finally {
attachments.close();
} }
}
} catch (JSONException e) {
e.printStackTrace();
} finally {
attachments.close();
}
} }
/** Call sync method */ /** Call sync method */

Loading…
Cancel
Save