Fixed bugs with syncing task attachments

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

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

Loading…
Cancel
Save