mirror of https://github.com/tasks/tasks
Merge remote-tracking branch 'origin/130208_sb_task_attachments' into 130122_sb_new_sync
commit
ee0b5ddae4
@ -0,0 +1,193 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2012 Todoroo Inc
|
||||||
|
*
|
||||||
|
* See the file "LICENSE" for the full license governing this code.
|
||||||
|
*/
|
||||||
|
package com.todoroo.astrid.data;
|
||||||
|
|
||||||
|
|
||||||
|
import android.content.ContentValues;
|
||||||
|
import android.net.Uri;
|
||||||
|
|
||||||
|
import com.todoroo.andlib.data.AbstractModel;
|
||||||
|
import com.todoroo.andlib.data.Property;
|
||||||
|
import com.todoroo.andlib.data.Property.IntegerProperty;
|
||||||
|
import com.todoroo.andlib.data.Property.LongProperty;
|
||||||
|
import com.todoroo.andlib.data.Property.StringProperty;
|
||||||
|
import com.todoroo.andlib.data.Table;
|
||||||
|
import com.todoroo.andlib.data.TodorooCursor;
|
||||||
|
import com.todoroo.andlib.utility.DateUtilities;
|
||||||
|
import com.todoroo.astrid.api.AstridApiConstants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data Model which represents a user.
|
||||||
|
*
|
||||||
|
* @author Tim Su <tim@todoroo.com>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("nls")
|
||||||
|
public final class TaskAttachment extends RemoteModel {
|
||||||
|
|
||||||
|
// --- table and uri
|
||||||
|
|
||||||
|
/** table for this model */
|
||||||
|
public static final Table TABLE = new Table("task_attachments", TaskAttachment.class);
|
||||||
|
|
||||||
|
/** model class for entries in the outstanding table */
|
||||||
|
public static final Class<? extends OutstandingEntry<TaskAttachment>> OUTSTANDING_MODEL = TaskAttachmentOutstanding.class;
|
||||||
|
|
||||||
|
/** content uri for this model */
|
||||||
|
public static final Uri CONTENT_URI = Uri.parse("content://" + AstridApiConstants.API_PACKAGE + "/" +
|
||||||
|
TABLE.name);
|
||||||
|
|
||||||
|
// --- properties
|
||||||
|
|
||||||
|
/** ID */
|
||||||
|
public static final LongProperty ID = new LongProperty(
|
||||||
|
TABLE, ID_PROPERTY_NAME);
|
||||||
|
|
||||||
|
/** Remote id */
|
||||||
|
public static final StringProperty UUID = new StringProperty(
|
||||||
|
TABLE, UUID_PROPERTY_NAME);
|
||||||
|
|
||||||
|
/** Pushed at date */
|
||||||
|
public static final LongProperty PUSHED_AT = new LongProperty(
|
||||||
|
TABLE, PUSHED_AT_PROPERTY_NAME);
|
||||||
|
|
||||||
|
/** Creator user id */
|
||||||
|
public static final StringProperty USER_UUID = new StringProperty(
|
||||||
|
TABLE, "user_id");
|
||||||
|
|
||||||
|
/** Task uuid */
|
||||||
|
public static final StringProperty TASK_UUID = new StringProperty(
|
||||||
|
TABLE, "task_id");
|
||||||
|
|
||||||
|
/** File name */
|
||||||
|
public static final StringProperty NAME = new StringProperty(
|
||||||
|
TABLE, "name");
|
||||||
|
|
||||||
|
/** File url (for downloading) */
|
||||||
|
public static final StringProperty URL = new StringProperty(
|
||||||
|
TABLE, "url");
|
||||||
|
|
||||||
|
/** File path (on local storage) */
|
||||||
|
public static final StringProperty FILE_PATH = new StringProperty(
|
||||||
|
TABLE, "path");
|
||||||
|
|
||||||
|
/** File size (in bytes) */
|
||||||
|
public static final IntegerProperty SIZE = new IntegerProperty(
|
||||||
|
TABLE, "size");
|
||||||
|
|
||||||
|
/** File mimetype */
|
||||||
|
public static final StringProperty CONTENT_TYPE = new StringProperty(
|
||||||
|
TABLE, "content_type");
|
||||||
|
|
||||||
|
/** Attachment creation date */
|
||||||
|
public static final LongProperty CREATED_AT = new LongProperty(
|
||||||
|
TABLE, "created_at", Property.PROP_FLAG_DATE);
|
||||||
|
|
||||||
|
/** Attachment deletion date */
|
||||||
|
public static final LongProperty DELETED_AT = new LongProperty(
|
||||||
|
TABLE, "deleted_at", Property.PROP_FLAG_DATE);
|
||||||
|
|
||||||
|
/** List of all properties for this model */
|
||||||
|
public static final Property<?>[] PROPERTIES = generateProperties(TaskAttachment.class);
|
||||||
|
|
||||||
|
// --- defaults
|
||||||
|
|
||||||
|
/** Default values container */
|
||||||
|
private static final ContentValues defaultValues = new ContentValues();
|
||||||
|
|
||||||
|
static {
|
||||||
|
defaultValues.put(UUID.name, NO_UUID);
|
||||||
|
defaultValues.put(PUSHED_AT.name, 0);
|
||||||
|
defaultValues.put(USER_UUID.name, NO_UUID);
|
||||||
|
defaultValues.put(TASK_UUID.name, NO_UUID);
|
||||||
|
defaultValues.put(NAME.name, "");
|
||||||
|
defaultValues.put(URL.name, "");
|
||||||
|
defaultValues.put(FILE_PATH.name, "");
|
||||||
|
defaultValues.put(SIZE.name, 0);
|
||||||
|
defaultValues.put(CONTENT_TYPE.name, "");
|
||||||
|
defaultValues.put(CREATED_AT.name, 0);
|
||||||
|
defaultValues.put(DELETED_AT.name, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ContentValues getDefaultValues() {
|
||||||
|
return defaultValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -- Constants
|
||||||
|
/** default directory for files on external storage */
|
||||||
|
public static final String FILES_DIRECTORY_DEFAULT = "attachments"; //$NON-NLS-1$
|
||||||
|
|
||||||
|
/** preference key for some other download directory */
|
||||||
|
public static final String FILES_DIRECTORY_PREF = "custom_files_dir"; //$NON-NLS-1$
|
||||||
|
|
||||||
|
/** Constants for file types */
|
||||||
|
public static final String FILE_TYPE_AUDIO = "audio/"; //$NON-NLS-1$
|
||||||
|
public static final String FILE_TYPE_IMAGE = "image/"; //$NON-NLS-1$
|
||||||
|
public static final String FILE_TYPE_PDF = "application/pdf"; //$NON-NLS-1$
|
||||||
|
|
||||||
|
public static final String FILE_TYPE_DOC = "application/msword"; //$NON-NLS-1$
|
||||||
|
public static final String FILE_TYPE_DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; //$NON-NLS-1$
|
||||||
|
public static final String FILE_TYPE_PPT = "application/vnd.ms-powerpoint"; //$NON-NLS-1$
|
||||||
|
public static final String FILE_TYPE_PPTX = "application/vnd.openxmlformats-officedocument.presentationml.presentation"; //$NON-NLS-1$
|
||||||
|
public static final String FILE_TYPE_XLS = "application/vnd.ms-excel"; //$NON-NLS-1$
|
||||||
|
public static final String FILE_TYPE_XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; //$NON-NLS-1$
|
||||||
|
|
||||||
|
public static final String[] MS_FILETYPES = {
|
||||||
|
FILE_TYPE_DOC, FILE_TYPE_DOCX,
|
||||||
|
FILE_TYPE_XLS, FILE_TYPE_XLSX,
|
||||||
|
FILE_TYPE_PPT, FILE_TYPE_PPTX,
|
||||||
|
};
|
||||||
|
|
||||||
|
public static final String FILE_TYPE_OTHER = "application/octet-stream"; //$NON-NLS-1$
|
||||||
|
|
||||||
|
public static TaskAttachment createNewAttachment(String taskUuid, String filePath, String fileName, String fileType) {
|
||||||
|
TaskAttachment attachment = new TaskAttachment();
|
||||||
|
attachment.setValue(TaskAttachment.TASK_UUID, taskUuid);
|
||||||
|
attachment.setValue(NAME, fileName);
|
||||||
|
attachment.setValue(USER_UUID, Task.USER_ID_SELF);
|
||||||
|
attachment.setValue(FILE_PATH, filePath);
|
||||||
|
attachment.setValue(CONTENT_TYPE, fileType);
|
||||||
|
attachment.setValue(CREATED_AT, DateUtilities.now());
|
||||||
|
attachment.setValue(DELETED_AT, 0L);
|
||||||
|
return attachment;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- data access boilerplate
|
||||||
|
|
||||||
|
public TaskAttachment() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TaskAttachment(TodorooCursor<TaskAttachment> cursor) {
|
||||||
|
this();
|
||||||
|
readPropertiesFromCursor(cursor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void readFromCursor(TodorooCursor<TaskAttachment> cursor) {
|
||||||
|
super.readPropertiesFromCursor(cursor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getId() {
|
||||||
|
return getIdHelper(ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getUuid() {
|
||||||
|
return getUuidHelper(UUID);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- parcelable helpers
|
||||||
|
|
||||||
|
public static final Creator<TaskAttachment> CREATOR = new ModelCreator<TaskAttachment>(TaskAttachment.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Creator<? extends AbstractModel> getCreator() {
|
||||||
|
return CREATOR;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,63 @@
|
|||||||
|
package com.todoroo.astrid.data;
|
||||||
|
|
||||||
|
import android.content.ContentValues;
|
||||||
|
|
||||||
|
import com.todoroo.andlib.data.AbstractModel;
|
||||||
|
import com.todoroo.andlib.data.Property;
|
||||||
|
import com.todoroo.andlib.data.Property.LongProperty;
|
||||||
|
import com.todoroo.andlib.data.Property.StringProperty;
|
||||||
|
import com.todoroo.andlib.data.Table;
|
||||||
|
|
||||||
|
@SuppressWarnings("nls")
|
||||||
|
public class TaskAttachmentOutstanding extends OutstandingEntry<TaskAttachment> {
|
||||||
|
|
||||||
|
/** table for this model */
|
||||||
|
public static final Table TABLE = new Table("task_attachment_outstanding", TaskAttachmentOutstanding.class);
|
||||||
|
|
||||||
|
// --- properties
|
||||||
|
|
||||||
|
/** ID */
|
||||||
|
public static final LongProperty ID = new LongProperty(
|
||||||
|
TABLE, ID_PROPERTY_NAME);
|
||||||
|
|
||||||
|
public static final LongProperty TASK_ATTACHMENT_ID = new LongProperty(
|
||||||
|
TABLE, ENTITY_ID_PROPERTY_NAME);
|
||||||
|
|
||||||
|
public static final StringProperty COLUMN_STRING = new StringProperty(
|
||||||
|
TABLE, COLUMN_STRING_PROPERTY_NAME);
|
||||||
|
|
||||||
|
public static final StringProperty VALUE_STRING = new StringProperty(
|
||||||
|
TABLE, VALUE_STRING_PROPERTY_NAME);
|
||||||
|
|
||||||
|
public static final LongProperty CREATED_AT = new LongProperty(
|
||||||
|
TABLE, CREATED_AT_PROPERTY_NAME);
|
||||||
|
|
||||||
|
private static final ContentValues defaultValues = new ContentValues();
|
||||||
|
|
||||||
|
static {
|
||||||
|
defaultValues.put(TASK_ATTACHMENT_ID.name, 0);
|
||||||
|
defaultValues.put(COLUMN_STRING.name, "");
|
||||||
|
defaultValues.put(VALUE_STRING.name, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
/** List of all properties for this model */
|
||||||
|
public static final Property<?>[] PROPERTIES = generateProperties(TaskAttachmentOutstanding.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ContentValues getDefaultValues() {
|
||||||
|
return defaultValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getId() {
|
||||||
|
return getIdHelper(ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final Creator<TaskAttachmentOutstanding> CREATOR = new ModelCreator<TaskAttachmentOutstanding>(TaskAttachmentOutstanding.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Creator<? extends AbstractModel> getCreator() {
|
||||||
|
return CREATOR;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,107 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2012 Todoroo Inc
|
||||||
|
*
|
||||||
|
* See the file "LICENSE" for the full license governing this code.
|
||||||
|
*/
|
||||||
|
package com.todoroo.astrid.dao;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import android.content.ContentValues;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
|
||||||
|
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.DateUtilities;
|
||||||
|
import com.todoroo.astrid.actfm.sync.messages.NameMaps;
|
||||||
|
import com.todoroo.astrid.data.OutstandingEntry;
|
||||||
|
import com.todoroo.astrid.data.TagData;
|
||||||
|
import com.todoroo.astrid.data.TaskAttachment;
|
||||||
|
import com.todoroo.astrid.data.TaskAttachmentOutstanding;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data Access layer for {@link TagData}-related operations.
|
||||||
|
*
|
||||||
|
* @author Tim Su <tim@todoroo.com>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class TaskAttachmentDao extends RemoteModelDao<TaskAttachment> {
|
||||||
|
|
||||||
|
@Autowired Database database;
|
||||||
|
|
||||||
|
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value="UR_UNINIT_READ")
|
||||||
|
public TaskAttachmentDao() {
|
||||||
|
super(TaskAttachment.class);
|
||||||
|
DependencyInjectionService.getInstance().inject(this);
|
||||||
|
setDatabase(database);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean shouldRecordOutstandingEntry(String columnName) {
|
||||||
|
return NameMaps.shouldRecordOutstandingColumnForTable(NameMaps.TABLE_ID_ATTACHMENTS, columnName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int createOutstandingEntries(long modelId, ContentValues modelSetValues) {
|
||||||
|
// new attachment case -- only set by us when creating new attachments; when setting during sync outstanding entries suppressed
|
||||||
|
if (modelSetValues.containsKey(TaskAttachment.CONTENT_TYPE.name)) {
|
||||||
|
TaskAttachmentOutstanding m = new TaskAttachmentOutstanding();
|
||||||
|
String path = modelSetValues.getAsString(TaskAttachment.FILE_PATH.name);
|
||||||
|
if (TextUtils.isEmpty(path))
|
||||||
|
return -1;
|
||||||
|
try {
|
||||||
|
JSONObject newFileHash = new JSONObject();
|
||||||
|
newFileHash.put("name", modelSetValues.getAsString(TaskAttachment.NAME.name)); //$NON-NLS-1$
|
||||||
|
newFileHash.put("type", modelSetValues.getAsString(TaskAttachment.CONTENT_TYPE.name)); //$NON-NLS-1$
|
||||||
|
newFileHash.put("path", path); //$NON-NLS-1$
|
||||||
|
|
||||||
|
m.setValue(OutstandingEntry.ENTITY_ID_PROPERTY, modelId);
|
||||||
|
m.setValue(OutstandingEntry.COLUMN_STRING_PROPERTY, NameMaps.ATTACHMENT_ADDED_COLUMN);
|
||||||
|
m.setValue(OutstandingEntry.VALUE_STRING_PROPERTY, newFileHash.toString());
|
||||||
|
m.setValue(OutstandingEntry.CREATED_AT_PROPERTY, DateUtilities.now());
|
||||||
|
database.insert(outstandingTable.name, null, m.getSetValues());
|
||||||
|
} catch (JSONException e) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int result = super.createOutstandingEntries(modelId, modelSetValues);
|
||||||
|
if (result < 0) // Error
|
||||||
|
return result;
|
||||||
|
return 1 + result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean taskHasAttachments(String taskUuid) {
|
||||||
|
TodorooCursor<TaskAttachment> files = query(Query.select(TaskAttachment.TASK_UUID).where(
|
||||||
|
Criterion.and(TaskAttachment.TASK_UUID.eq(taskUuid),
|
||||||
|
TaskAttachment.DELETED_AT.eq(0))).limit(1));
|
||||||
|
try {
|
||||||
|
return files.getCount() > 0;
|
||||||
|
} finally {
|
||||||
|
files.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- SQL clause generators
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates SQL clauses
|
||||||
|
*/
|
||||||
|
public static class TagDataCriteria {
|
||||||
|
|
||||||
|
/** @returns tasks by id */
|
||||||
|
public static Criterion byId(long id) {
|
||||||
|
return TagData.ID.eq(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Criterion isTeam() {
|
||||||
|
return TagData.IS_TEAM.eq(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
package com.todoroo.astrid.dao;
|
||||||
|
|
||||||
|
import com.todoroo.astrid.data.TaskAttachmentOutstanding;
|
||||||
|
|
||||||
|
|
||||||
|
public class TaskAttachmentOutstandingDao extends OutstandingEntryDao<TaskAttachmentOutstanding> {
|
||||||
|
|
||||||
|
public TaskAttachmentOutstandingDao() {
|
||||||
|
super(TaskAttachmentOutstanding.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue