Enhancements to Property class to support multiple flags (like nullable, isDate, etc)

pull/14/head
Sam Bosley 12 years ago
parent 2c147d178f
commit a3866b2270

@ -38,7 +38,13 @@ public abstract class Property<TYPE> extends Field implements Cloneable {
public final String name;
/** Can this field be null? */
public boolean nullable = false;
public static final int PROP_FLAG_NULLABLE = 1 << 0;
/** Is this field a date? */
public static final int PROP_FLAG_DATE = 1 << 1;
/** Is this field a user id? */
public static final int PROP_FLAG_USER_ID = 1 << 2;
public int flags = 0;
/**
* Create a property by table and column name. Uses the default property
@ -52,9 +58,9 @@ public abstract class Property<TYPE> extends Field implements Cloneable {
* Create a property by table and column name. Uses the default property
* expression which is derived from default table name
*/
protected Property(Table table, String columnName, boolean nullable) {
protected Property(Table table, String columnName, int flags) {
this(table, columnName, (table == null) ? (columnName) : (table.name() + "." + columnName));
this.nullable = nullable;
this.flags = flags;
}
/**
@ -117,8 +123,8 @@ public abstract class Property<TYPE> extends Field implements Cloneable {
super(table, name);
}
public IntegerProperty(Table table, String name, boolean nullable) {
super(table, name, nullable);
public IntegerProperty(Table table, String name, int flags) {
super(table, name, flags);
}
protected IntegerProperty(Table table, String name, String expression) {
@ -144,8 +150,8 @@ public abstract class Property<TYPE> extends Field implements Cloneable {
super(table, name);
}
public StringProperty(Table table, String name, boolean nullable) {
super(table, name, nullable);
public StringProperty(Table table, String name, int flags) {
super(table, name, flags);
}
protected StringProperty(Table table, String name, String expression) {
@ -191,8 +197,8 @@ public abstract class Property<TYPE> extends Field implements Cloneable {
super(table, name);
}
public DoubleProperty(Table table, String name, boolean nullable) {
super(table, name, nullable);
public DoubleProperty(Table table, String name, int flags) {
super(table, name, flags);
}
protected DoubleProperty(Table table, String name, String expression) {
@ -219,8 +225,8 @@ public abstract class Property<TYPE> extends Field implements Cloneable {
super(table, name);
}
public LongProperty(Table table, String name, boolean nullable) {
super(table, name, nullable);
public LongProperty(Table table, String name, int flags) {
super(table, name, flags);
}
protected LongProperty(Table table, String name, String expression) {
@ -239,6 +245,10 @@ public abstract class Property<TYPE> extends Field implements Cloneable {
}
}
public boolean checkFlag(int flag) {
return (flags & flag) > 0;
}
public String getColumnName() {
if (hasAlias())
return alias;

@ -101,7 +101,7 @@ public class TodorooCursor<TYPE extends AbstractModel> extends CursorWrapper {
public Object visitDouble(Property<Double> property,
TodorooCursor<?> cursor) {
int column = columnIndex(property, cursor);
if(property.nullable && cursor.isNull(column))
if(property.checkFlag(Property.PROP_FLAG_NULLABLE) && cursor.isNull(column))
return null;
return cursor.getDouble(column);
}
@ -109,14 +109,14 @@ public class TodorooCursor<TYPE extends AbstractModel> extends CursorWrapper {
public Object visitInteger(Property<Integer> property,
TodorooCursor<?> cursor) {
int column = columnIndex(property, cursor);
if(property.nullable && cursor.isNull(column))
if(property.checkFlag(Property.PROP_FLAG_NULLABLE) && cursor.isNull(column))
return null;
return cursor.getInt(column);
}
public Object visitLong(Property<Long> property, TodorooCursor<?> cursor) {
int column = columnIndex(property, cursor);
if(property.nullable && cursor.isNull(column))
if(property.checkFlag(Property.PROP_FLAG_NULLABLE) && cursor.isNull(column))
return null;
return cursor.getLong(column);
}
@ -124,7 +124,7 @@ public class TodorooCursor<TYPE extends AbstractModel> extends CursorWrapper {
public Object visitString(Property<String> property,
TodorooCursor<?> cursor) {
int column = columnIndex(property, cursor);
if(property.nullable && cursor.isNull(column))
if(property.checkFlag(Property.PROP_FLAG_NULLABLE) && cursor.isNull(column))
return null;
return cursor.getString(column);
}

@ -47,7 +47,7 @@ public final class TagData extends RemoteModel {
/** User id */
public static final LongProperty USER_ID = new LongProperty(
TABLE, USER_ID_PROPERTY_NAME);
TABLE, USER_ID_PROPERTY_NAME, Property.PROP_FLAG_USER_ID);
/** User Object (JSON) */
public static final StringProperty USER = new StringProperty(
@ -79,19 +79,19 @@ public final class TagData extends RemoteModel {
/** Unixtime Project was created */
public static final LongProperty CREATION_DATE = new LongProperty(
TABLE, "created");
TABLE, "created", Property.PROP_FLAG_DATE);
/** Unixtime Project was last touched */
public static final LongProperty MODIFICATION_DATE = new LongProperty(
TABLE, "modified");
TABLE, "modified", Property.PROP_FLAG_DATE);
/** Unixtime Project was completed. 0 means active */
public static final LongProperty COMPLETION_DATE = new LongProperty(
TABLE, "completed");
TABLE, "completed", Property.PROP_FLAG_DATE);
/** Unixtime Project was deleted. 0 means not deleted */
public static final LongProperty DELETION_DATE = new LongProperty(
TABLE, "deleted");
TABLE, "deleted", Property.PROP_FLAG_DATE);
/** Project picture thumbnail */
public static final StringProperty THUMB = new StringProperty(
@ -99,7 +99,7 @@ public final class TagData extends RemoteModel {
/** Project last activity date */
public static final LongProperty LAST_ACTIVITY_DATE = new LongProperty(
TABLE, "lastActivityDate");
TABLE, "lastActivityDate", Property.PROP_FLAG_DATE);
/** Whether user is part of Tag team */
public static final IntegerProperty IS_TEAM = new IntegerProperty(
@ -119,7 +119,7 @@ public final class TagData extends RemoteModel {
/** Pushed at date */
public static final LongProperty PUSHED_AT = new LongProperty(
TABLE, PUSHED_AT_PROPERTY_NAME);
TABLE, PUSHED_AT_PROPERTY_NAME, Property.PROP_FLAG_DATE);
/** UUID */
public static final StringProperty UUID = new StringProperty(

@ -60,27 +60,27 @@ public final class Task extends RemoteModel {
/** Unixtime Task is due, 0 if not set */
public static final LongProperty DUE_DATE = new LongProperty(
TABLE, "dueDate");
TABLE, "dueDate", Property.PROP_FLAG_DATE);
/** Unixtime Task should be hidden until, 0 if not set */
public static final LongProperty HIDE_UNTIL = new LongProperty(
TABLE, "hideUntil");
TABLE, "hideUntil", Property.PROP_FLAG_DATE);
/** Unixtime Task was created */
public static final LongProperty CREATION_DATE = new LongProperty(
TABLE, "created");
TABLE, "created", Property.PROP_FLAG_DATE);
/** Unixtime Task was last touched */
public static final LongProperty MODIFICATION_DATE = new LongProperty(
TABLE, "modified");
TABLE, "modified", Property.PROP_FLAG_DATE);
/** Unixtime Task was completed. 0 means active */
public static final LongProperty COMPLETION_DATE = new LongProperty(
TABLE, "completed");
TABLE, "completed", Property.PROP_FLAG_DATE);
/** Unixtime Task was deleted. 0 means not deleted */
public static final LongProperty DELETION_DATE = new LongProperty(
TABLE, "deleted");
TABLE, "deleted", Property.PROP_FLAG_DATE);
/** Cached Details Column - built from add-on detail exposers. A null
* value means there is no value in the cache and it needs to be
@ -90,7 +90,7 @@ public final class Task extends RemoteModel {
/** Date details were last updated */
public static final LongProperty DETAILS_DATE = new LongProperty(
TABLE, "detailsDate");
TABLE, "detailsDate", Property.PROP_FLAG_DATE);
public static final IntegerProperty FLAGS = new IntegerProperty(
TABLE, "flags");
@ -107,7 +107,7 @@ public final class Task extends RemoteModel {
TABLE, "elapsedSeconds");
public static final LongProperty TIMER_START = new LongProperty(
TABLE, "timerStart");
TABLE, "timerStart", Property.PROP_FLAG_DATE);
public static final IntegerProperty POSTPONE_COUNT = new IntegerProperty(
TABLE, "postponeCount");
@ -118,11 +118,11 @@ public final class Task extends RemoteModel {
/** Reminder period, in milliseconds. 0 means disabled */
public static final LongProperty REMINDER_PERIOD = new LongProperty(
TABLE, "notifications");
TABLE, "notifications", Property.PROP_FLAG_DATE);
/** Unixtime the last reminder was triggered */
public static final LongProperty REMINDER_LAST = new LongProperty(
TABLE, "lastNotified");
TABLE, "lastNotified", Property.PROP_FLAG_DATE);
/** What kind of reminder the last reminder was: private task,
* social with no faces, social with faces */
@ -131,13 +131,13 @@ public final class Task extends RemoteModel {
/** Unixtime snooze is set (0 -> no snooze) */
public static final LongProperty REMINDER_SNOOZE = new LongProperty(
TABLE, "snoozeTime");
TABLE, "snoozeTime", Property.PROP_FLAG_DATE);
public static final StringProperty RECURRENCE = new StringProperty(
TABLE, "recurrence");
public static final LongProperty REPEAT_UNTIL = new LongProperty(
TABLE, "repeatUntil");
TABLE, "repeatUntil", Property.PROP_FLAG_DATE);
public static final StringProperty CALENDAR_URI = new StringProperty(
TABLE, "calendarUri");
@ -146,11 +146,11 @@ public final class Task extends RemoteModel {
/** Remote id */
public static final LongProperty REMOTE_ID = new LongProperty(
TABLE, REMOTE_ID_PROPERTY_NAME, true);
TABLE, REMOTE_ID_PROPERTY_NAME, Property.PROP_FLAG_NULLABLE);
/** Assigned user id */
public static final LongProperty USER_ID = new LongProperty(
TABLE, USER_ID_PROPERTY_NAME);
TABLE, USER_ID_PROPERTY_NAME, Property.PROP_FLAG_USER_ID);
/** User Object (JSON) */
public static final StringProperty USER = new StringProperty(
@ -158,22 +158,22 @@ public final class Task extends RemoteModel {
/** Creator user id */
public static final LongProperty CREATOR_ID = new LongProperty(
TABLE, "creatorId");
TABLE, "creatorId", Property.PROP_FLAG_USER_ID);
public static final StringProperty SHARED_WITH = new StringProperty(
TABLE, "sharedWith");
/** Last Sync date */
public static final LongProperty LAST_SYNC = new LongProperty(
TABLE, "lastSync");
TABLE, "lastSync", Property.PROP_FLAG_DATE);
/** Pushed at date */
public static final LongProperty PUSHED_AT = new LongProperty(
TABLE, PUSHED_AT_PROPERTY_NAME);
TABLE, PUSHED_AT_PROPERTY_NAME, Property.PROP_FLAG_DATE);
/** UUID */
public static final StringProperty UUID = new StringProperty(
TABLE, UUID_PROPERTY_NAME, true);
TABLE, UUID_PROPERTY_NAME, Property.PROP_FLAG_NULLABLE);
/** List of all properties for this model */
public static final Property<?>[] PROPERTIES = generateProperties(Task.class);

@ -159,8 +159,10 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
public Object visitLong(Property<Long> property, OE data) {
Long l = data.getMergedValues().getAsLong(OutstandingEntry.VALUE_STRING_PROPERTY.name);
if (l != null) {
if (l == 0 && (property.name.contains(RemoteModel.USER_ID_PROPERTY.name) || property.name.equals(Task.CREATOR_ID.name)))
if (l == 0 && property.checkFlag(Property.PROP_FLAG_USER_ID))
return ActFmPreferenceService.userId();
else if (property.checkFlag(Property.PROP_FLAG_DATE))
return l.longValue() / 1000L;
return l;
} else {
return getAsString(data);

@ -16,6 +16,7 @@ import com.todoroo.andlib.data.Property.LongProperty;
import com.todoroo.andlib.data.Property.PropertyVisitor;
import com.todoroo.andlib.data.Property.StringProperty;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.actfm.sync.ActFmPreferenceService;
import com.todoroo.astrid.dao.RemoteModelDao;
import com.todoroo.astrid.data.RemoteModel;
import com.todoroo.astrid.data.SyncFlags;
@ -111,6 +112,10 @@ public class MakeChanges<TYPE extends RemoteModel> extends ServerToClientMessage
public Void visitLong(Property<Long> property, String key) {
try {
long value = data.getLong(key);
if (property.checkFlag(Property.PROP_FLAG_USER_ID) && value == ActFmPreferenceService.userId())
value = 0;
else if (property.checkFlag(Property.PROP_FLAG_DATE))
value = value * 1000L;
model.setValue((LongProperty) property, value);
} catch (JSONException e) {
Log.e(ERROR_TAG, "Error reading long value with key " + key + " from JSON " + data, e);

@ -158,5 +158,4 @@ public class NameMaps {
return mapColumnName(table, serverColumn, TASK_PROPERTIES_SERVER_TO_LOCAL, TAG_DATA_PROPERTIES_SERVER_TO_LOCAL);
}
}

Loading…
Cancel
Save