Sync deletion of files to the server

pull/14/head
Sam Bosley 12 years ago
parent 8bb5f2d405
commit 005271be06

@ -624,6 +624,27 @@ public final class ActFmSyncService {
}
}
public void deleteAttachment(Metadata fileMetadata) {
long attachmentId = fileMetadata.getValue(FileMetadata.REMOTE_ID);
if (attachmentId <= 0)
return;
ArrayList<Object> params = new ArrayList<Object>();
params.add("id"); params.add(attachmentId);
params.add("token"); params.add(token);
try {
JSONObject result = actFmInvoker.post("task_attachment_remove", null, params.toArray(new Object[params.size()]));
if (result.optString("status").equals("success")) {
metadataService.delete(fileMetadata);
}
} catch (ActFmServiceException e) {
handleException("push-attacgment-error", e);
} catch (IOException e) {
handleException("push-attacgment-error", e);
}
}
// --- data fetch methods
/**

@ -92,10 +92,11 @@ public class ActFmSyncV2Provider extends SyncV2Provider {
long taskId = model.getValue(Metadata.TASK);
Task localTask = taskService.fetchById(taskId, Task.REMOTE_ID);
long remoteTaskId = localTask.getValue(Task.REMOTE_ID);
if (remoteTaskId <= 0)
return;
actFmSyncService.pushAttachment(remoteTaskId, model);
if (model.getValue(FileMetadata.DELETION_DATE) > 0)
actFmSyncService.deleteAttachment(model);
else if (remoteTaskId > 0)
actFmSyncService.pushAttachment(remoteTaskId, model);
};
public Metadata getRemoteModelInstance(TodorooCursor<Metadata> cursor) {
@ -306,7 +307,7 @@ public class ActFmSyncV2Provider extends SyncV2Provider {
TodorooCursor<Metadata> filesCursor = metadataService.query(Query.select(Metadata.PROPERTIES)
.where(Criterion.and(
MetadataCriteria.withKey(FileMetadata.METADATA_KEY),
FileMetadata.REMOTE_ID.eq(0))));
Criterion.or(FileMetadata.REMOTE_ID.eq(0), FileMetadata.DELETION_DATE.gt(0)))));
try {
pushQueued(callback, finisher, filesCursor, false, filesPusher);
} finally {

@ -2,8 +2,11 @@ package com.todoroo.astrid.files;
import com.todoroo.andlib.data.Property.LongProperty;
import com.todoroo.andlib.data.Property.StringProperty;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Query;
import com.todoroo.astrid.core.PluginServices;
import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria;
import com.todoroo.astrid.data.Metadata;
public class FileMetadata {
@ -27,7 +30,7 @@ public class FileMetadata {
public static final StringProperty FILE_TYPE = new StringProperty(Metadata.TABLE,
Metadata.VALUE2.name);
public static final LongProperty ATTACH_DATE = new LongProperty(Metadata.TABLE,
public static final LongProperty DELETION_DATE = new LongProperty(Metadata.TABLE,
Metadata.VALUE3.name);
public static final LongProperty REMOTE_ID = new LongProperty(Metadata.TABLE,
@ -48,12 +51,20 @@ public class FileMetadata {
metadata.setValue(FILE_PATH, filePath);
metadata.setValue(FILE_TYPE, fileType);
metadata.setValue(REMOTE_ID, 0L);
metadata.setValue(ATTACH_DATE, DateUtilities.now());
metadata.setValue(DELETION_DATE, 0L);
return metadata;
}
public static boolean taskHasAttachments(long taskId) {
return PluginServices.getMetadataByTaskAndWithKey(taskId, METADATA_KEY) != null;
TodorooCursor<Metadata> files = PluginServices.getMetadataService()
.query(Query.select(Metadata.TASK).where(
Criterion.and(MetadataCriteria.byTaskAndwithKey(taskId, METADATA_KEY),
DELETION_DATE.eq(0))).limit(1));
try {
return files.getCount() > 0;
} finally {
files.close();
}
}
}

@ -28,8 +28,10 @@ import com.todoroo.aacenc.RecognizerApi;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.astrid.actfm.sync.ActFmPreferenceService;
import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria;
@ -80,7 +82,8 @@ public class FilesControlSet extends PopupControlSet {
if (model != null) {
TodorooCursor<Metadata> cursor = metadataService.query(
Query.select(Metadata.PROPERTIES)
.where(MetadataCriteria.byTaskAndwithKey(model.getId(), FileMetadata.METADATA_KEY)));
.where(Criterion.and(MetadataCriteria.byTaskAndwithKey(model.getId(), FileMetadata.METADATA_KEY),
FileMetadata.DELETION_DATE.eq(0))));
try {
files.clear();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
@ -91,11 +94,32 @@ public class FilesControlSet extends PopupControlSet {
} finally {
cursor.close();
}
validateFiles();
if (initialized)
afterInflate();
}
}
private void validateFiles() {
for (int i = 0; i < files.size(); i++) {
Metadata m = files.get(i);
if (m.containsNonNullValue(FileMetadata.FILE_PATH)) {
File f = new File(m.getValue(FileMetadata.FILE_PATH));
if (!f.exists()) {
m.setValue(FileMetadata.FILE_PATH, null);
if (m.containsNonNullValue(FileMetadata.URL)) { // We're ok, just the local file was deleted
metadataService.save(m);
} else { // No local file and no url -- delete the metadata
metadataService.delete(m);
files.remove(i);
i--;
}
}
}
}
}
@Override
protected void readFromTaskOnInitialize() {
// TODO Auto-generated method stub
@ -131,15 +155,20 @@ public class FilesControlSet extends PopupControlSet {
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int which) {
if (m.getValue(FileMetadata.REMOTE_ID) > 0) {
m.setValue(FileMetadata.DELETION_DATE, DateUtilities.now());
metadataService.save(m);
} else {
metadataService.delete(m);
}
if (m.containsNonNullValue(FileMetadata.FILE_PATH)) {
File f = new File(m.getValue(FileMetadata.FILE_PATH));
if (f.delete()) {
metadataService.delete(m);
files.remove(m);
refreshDisplayView();
finalList.removeView(fileRow);
}
f.delete();
}
files.remove(m);
refreshDisplayView();
finalList.removeView(fileRow);
}
}, null);
}

Loading…
Cancel
Save