Improvements to NameMaps, AcknowledgeChange, ChangesHappened

pull/14/head
Sam Bosley 13 years ago
parent e9ab0a4859
commit 0f308db6aa

@ -64,6 +64,10 @@ public class DatabaseDao<TYPE extends AbstractModel> {
return table; return table;
} }
public Class<TYPE> getModelClass() {
return modelClass;
}
/** /**
* Sets database accessed by this DAO. Used for dependency-injected * Sets database accessed by this DAO. Used for dependency-injected
* initialization by child classes and unit tests * initialization by child classes and unit tests

@ -6,43 +6,29 @@ import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import android.text.TextUtils;
import com.todoroo.andlib.data.AbstractModel; import com.todoroo.andlib.data.AbstractModel;
import com.todoroo.andlib.service.Autowired; import com.todoroo.astrid.core.PluginServices;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.astrid.dao.OutstandingEntryDao; import com.todoroo.astrid.dao.OutstandingEntryDao;
import com.todoroo.astrid.dao.TagOutstandingDao;
import com.todoroo.astrid.dao.TaskOutstandingDao;
public class AcknowledgeChange extends ServerToClientMessage { public class AcknowledgeChange extends ServerToClientMessage {
@Autowired private final OutstandingEntryDao<?> dao;
private TaskOutstandingDao taskOutstandingDao;
@Autowired
private TagOutstandingDao tagOutstandingDao;
public AcknowledgeChange(JSONObject json) { public AcknowledgeChange(JSONObject json) {
super(json); super(json);
DependencyInjectionService.getInstance().inject(this); String table = json.optString("table"); //$NON-NLS-1$
if (NameMaps.SERVER_TABLE_TASKS.equals(table))
dao = PluginServices.getTaskOutstandingDao();
else if (NameMaps.SERVER_TABLE_TAGS.equals(table))
dao = PluginServices.getTagOutstandingDao();
else
dao = null;
} }
@Override @Override
@SuppressWarnings("nls")
public void processMessage() { public void processMessage() {
JSONArray idsArray = json.optJSONArray("ids"); JSONArray idsArray = json.optJSONArray("ids"); //$NON-NLS-1$
String table = json.optString("table"); if (idsArray != null && dao != null) {
if (idsArray != null && !TextUtils.isEmpty(table)) {
OutstandingEntryDao<?> dao = null;
if (NameMaps.SERVER_TABLE_TASKS.equals(table))
dao = taskOutstandingDao;
else if (NameMaps.SERVER_TABLE_TAGS.equals(table))
dao = tagOutstandingDao;
if (dao == null)
return;
ArrayList<Long> idsList = new ArrayList<Long>(); ArrayList<Long> idsList = new ArrayList<Long>();
for (int i = 0; i < idsArray.length(); i++) { for (int i = 0; i < idsArray.length(); i++) {
try { try {

@ -44,11 +44,12 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
// Process changes list and serialize to JSON // Process changes list and serialize to JSON
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
try { try {
String serverTable = NameMaps.getServerNameForTable(table);
json.put(TYPE_KEY, "ChangesHappened"); json.put(TYPE_KEY, "ChangesHappened");
json.put(TABLE_KEY, NameMaps.getServerNameForTable(table)); json.put(TABLE_KEY, serverTable);
json.put(UUID_KEY, uuid); json.put(UUID_KEY, uuid);
json.put(PUSHED_AT_KEY, pushedAt); json.put(PUSHED_AT_KEY, pushedAt);
json.put(CHANGES_KEY, changesToJSON()); json.put(CHANGES_KEY, changesToJSON(serverTable));
} catch (JSONException e) { } catch (JSONException e) {
return null; return null;
} }
@ -63,14 +64,14 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
return changes.size(); return changes.size();
} }
private JSONArray changesToJSON() { private JSONArray changesToJSON(String tableString) {
JSONArray array = new JSONArray(); JSONArray array = new JSONArray();
for (OE change : changes) { for (OE change : changes) {
try { try {
String localColumn = change.getValue(OutstandingEntry.COLUMN_STRING_PROPERTY); String localColumn = change.getValue(OutstandingEntry.COLUMN_STRING_PROPERTY);
String serverColumn = NameMaps.serverColumnNameToLocalColumnName(table, localColumn); String serverColumn = NameMaps.localColumnNameToServerColumnName(tableString, localColumn);
if (serverColumn == null) if (serverColumn == null)
throw new RuntimeException("No server column found for local column " + localColumn + " in table " + table.name); throw new RuntimeException("No server column found for local column " + localColumn + " in table " + tableString);
JSONObject changeJson = new JSONObject(); JSONObject changeJson = new JSONObject();
changeJson.put("id", change.getId()); changeJson.put("id", change.getId());

@ -3,6 +3,7 @@ package com.todoroo.astrid.actfm.sync.messages;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.Table; import com.todoroo.andlib.data.Table;
import com.todoroo.andlib.utility.AndroidUtilities; import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.astrid.data.TagData; import com.todoroo.astrid.data.TagData;
@ -45,56 +46,67 @@ public class NameMaps {
// -------------------------------- // --------------------------------
// ---- Column name mappings ------- // ---- Column name mappings -------
// -------------------------------- // --------------------------------
private static final Map<Property<?>, String> TASK_PROPERTIES_LOCAL_TO_SERVER;
private static final Map<String, String> TASK_COLUMNS_LOCAL_TO_SERVER; private static final Map<String, String> TASK_COLUMNS_LOCAL_TO_SERVER;
private static final Map<String, String> TASK_COLUMNS_SERVER_TO_LOCAL; private static final Map<String, Property<?>> TASK_PROPERTIES_SERVER_TO_LOCAL;
private static void putPropertyToServerName(Property<?> property, String serverName,
Map<Property<?>, String> propertyMap, Map<String, String> nameMap) {
propertyMap.put(property, serverName);
nameMap.put(property.name, serverName);
}
static { static {
// Hardcoded local columns mapped to corresponding server names // Hardcoded local columns mapped to corresponding server names
TASK_PROPERTIES_LOCAL_TO_SERVER = new HashMap<Property<?>, String>();
TASK_COLUMNS_LOCAL_TO_SERVER = new HashMap<String, String>(); TASK_COLUMNS_LOCAL_TO_SERVER = new HashMap<String, String>();
TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.TITLE.name, "title"); putPropertyToServerName(Task.TITLE, "title", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER);
TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.IMPORTANCE.name, "importance"); putPropertyToServerName(Task.IMPORTANCE, "importance", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER);
TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.DUE_DATE.name, "due"); putPropertyToServerName(Task.DUE_DATE, "due", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER);
TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.HIDE_UNTIL.name, "hide_until"); putPropertyToServerName(Task.HIDE_UNTIL, "hide_until", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER);
TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.COMPLETION_DATE.name, "completed_at"); putPropertyToServerName(Task.COMPLETION_DATE, "completed_at", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER);
TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.DELETION_DATE.name, "deleted_at"); putPropertyToServerName(Task.DELETION_DATE, "deleted_at", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER);
TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.NOTES.name, "notes"); putPropertyToServerName(Task.NOTES, "notes", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER);
TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.RECURRENCE.name, "repeat"); putPropertyToServerName(Task.RECURRENCE, "repeat", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER);
TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.USER_ID.name, "user_id"); putPropertyToServerName(Task.USER_ID, "user_id", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER);
TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.USER.name, "user"); putPropertyToServerName(Task.USER, "user", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER);
TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.CREATOR_ID.name, "creator_id"); putPropertyToServerName(Task.CREATOR_ID, "creator_id", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER);
TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.UUID.name, "uuid"); putPropertyToServerName(Task.UUID, "uuid", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER);
TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.PROOF_TEXT.name, "proof_text"); putPropertyToServerName(Task.PROOF_TEXT, "proof_text", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER);
// Reverse the mapping to construct the server to local map TASK_PROPERTIES_SERVER_TO_LOCAL = AndroidUtilities.reverseMap(TASK_PROPERTIES_LOCAL_TO_SERVER);
TASK_COLUMNS_SERVER_TO_LOCAL = AndroidUtilities.reverseMap(TASK_COLUMNS_LOCAL_TO_SERVER);
} }
private static final Map<Property<?>, String> TAG_DATA_PROPERTIES_LOCAL_TO_SERVER;
private static final Map<String, String> TAG_DATA_COLUMNS_LOCAL_TO_SERVER; private static final Map<String, String> TAG_DATA_COLUMNS_LOCAL_TO_SERVER;
private static final Map<String, String> TAG_DATA_COLUMNS_SERVER_TO_LOCAL; private static final Map<String, Property<?>> TAG_DATA_PROPERTIES_SERVER_TO_LOCAL;
static { static {
// Hardcoded local columns mapped to corresponding server names // Hardcoded local columns mapped to corresponding server names
TAG_DATA_PROPERTIES_LOCAL_TO_SERVER = new HashMap<Property<?>, String>();
TAG_DATA_COLUMNS_LOCAL_TO_SERVER = new HashMap<String, String>(); TAG_DATA_COLUMNS_LOCAL_TO_SERVER = new HashMap<String, String>();
TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.USER_ID.name, "user_id");
TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.USER.name, "user"); //TODO: NOT CORRECT putPropertyToServerName(TagData.USER_ID, "user_id", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER);
TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.NAME.name, "name"); putPropertyToServerName(TagData.USER, "user", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); //TODO: NOT CORRECT
TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.PICTURE.name, "picture_id"); //TODO: NOT CORRECT putPropertyToServerName(TagData.NAME, "name", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER);
TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.MEMBERS.name, "members"); //TODO: NOT CORRECT putPropertyToServerName(TagData.PICTURE, "picture_id", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); //TODO: NOT CORRECT
TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.CREATION_DATE.name, "created_at"); putPropertyToServerName(TagData.MEMBERS, "members", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); //TODO: NOT CORRECT
TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.DELETION_DATE.name, "deleted_at"); putPropertyToServerName(TagData.CREATION_DATE, "created_at", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER);
TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.UUID.name, "uuid"); putPropertyToServerName(TagData.DELETION_DATE, "deleted_at", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER);
TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.PROOF_TEXT.name, "proof_text"); putPropertyToServerName(TagData.UUID, "uuid", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER);
TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.TAG_ORDERING.name, "tag_ordering"); //TODO: NOT CORRECT putPropertyToServerName(TagData.PROOF_TEXT, "proof_text", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER);
putPropertyToServerName(TagData.TAG_ORDERING, "tag_ordering", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); //TODO: NOT CORRECT
// Reverse the mapping to construct the server to local map // Reverse the mapping to construct the server to local map
TAG_DATA_COLUMNS_SERVER_TO_LOCAL = AndroidUtilities.reverseMap(TAG_DATA_COLUMNS_LOCAL_TO_SERVER); TAG_DATA_PROPERTIES_SERVER_TO_LOCAL = AndroidUtilities.reverseMap(TAG_DATA_PROPERTIES_LOCAL_TO_SERVER);
} }
public static String localColumnNameToServerColumnName(Table table, String localColumn) { public static String localColumnNameToServerColumnName(String table, String localColumn) {
Map<String, String> map = null; Map<String, String> map = null;
if (table == Task.TABLE) if (SERVER_TABLE_TASKS.equals(table))
map = TASK_COLUMNS_LOCAL_TO_SERVER; map = TASK_COLUMNS_LOCAL_TO_SERVER;
else if (table == TagData.TABLE) else if (SERVER_TABLE_TAGS.equals(table))
map = TAG_DATA_COLUMNS_LOCAL_TO_SERVER; map = TAG_DATA_COLUMNS_LOCAL_TO_SERVER;
if (map == null) if (map == null)
@ -103,12 +115,12 @@ public class NameMaps {
return map.get(localColumn); return map.get(localColumn);
} }
public static String serverColumnNameToLocalColumnName(Table table, String serverColumn) { public static Property<?> serverColumnNameToLocalProperty(String table, String serverColumn) {
Map<String, String> map = null; Map<String, Property<?>> map = null;
if (table == Task.TABLE) if (SERVER_TABLE_TASKS.equals(table))
map = TASK_COLUMNS_SERVER_TO_LOCAL; map = TASK_PROPERTIES_SERVER_TO_LOCAL;
else if (table == TagData.TABLE) else if (SERVER_TABLE_TAGS.equals(table))
map = TAG_DATA_COLUMNS_SERVER_TO_LOCAL; map = TAG_DATA_PROPERTIES_SERVER_TO_LOCAL;
if (map == null) if (map == null)
return null; return null;

@ -19,7 +19,6 @@ public class RequestDoubleCheck<TYPE extends RemoteModel> extends ClientToServer
json.put(TYPE_KEY, "RequestDoubleCheck"); //$NON-NLS-1$ json.put(TYPE_KEY, "RequestDoubleCheck"); //$NON-NLS-1$
json.put(TABLE_KEY, NameMaps.getServerNameForTable(table)); json.put(TABLE_KEY, NameMaps.getServerNameForTable(table));
json.put(UUID_KEY, uuid); json.put(UUID_KEY, uuid);
json.put(PUSHED_AT_KEY, pushedAt);
} catch (JSONException e) { } catch (JSONException e) {
return null; return null;
} }

@ -13,6 +13,10 @@ import com.todoroo.andlib.sql.Query;
import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria; import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria;
import com.todoroo.astrid.dao.StoreObjectDao; import com.todoroo.astrid.dao.StoreObjectDao;
import com.todoroo.astrid.dao.TagDataDao;
import com.todoroo.astrid.dao.TagOutstandingDao;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.dao.TaskOutstandingDao;
import com.todoroo.astrid.dao.UserDao; import com.todoroo.astrid.dao.UserDao;
import com.todoroo.astrid.data.Metadata; import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.service.AddOnService; import com.todoroo.astrid.service.AddOnService;
@ -32,6 +36,9 @@ public final class PluginServices {
@Autowired @Autowired
TaskService taskService; TaskService taskService;
@Autowired
TaskDao taskDao;
@Autowired @Autowired
Database database; Database database;
@ -47,12 +54,21 @@ public final class PluginServices {
@Autowired @Autowired
TagDataService tagDataService; TagDataService tagDataService;
@Autowired
TagDataDao tagDataDao;
@Autowired @Autowired
StoreObjectDao storeObjectDao; StoreObjectDao storeObjectDao;
@Autowired @Autowired
UserDao userDao; UserDao userDao;
@Autowired
TaskOutstandingDao taskOutstandingDao;
@Autowired
TagOutstandingDao tagOutstandingDao;
private static PluginServices instance; private static PluginServices instance;
static { static {
@ -79,10 +95,26 @@ public final class PluginServices {
return getInstance().taskService; return getInstance().taskService;
} }
public static TaskDao getTaskDao() {
return getInstance().taskDao;
}
public static TagDataService getTagDataService() { public static TagDataService getTagDataService() {
return getInstance().tagDataService; return getInstance().tagDataService;
} }
public static TagDataDao getTagDataDao() {
return getInstance().tagDataDao;
}
public static TaskOutstandingDao getTaskOutstandingDao() {
return getInstance().taskOutstandingDao;
}
public static TagOutstandingDao getTagOutstandingDao() {
return getInstance().tagOutstandingDao;
}
public static ExceptionService getExceptionService() { public static ExceptionService getExceptionService() {
return getInstance().exceptionService; return getInstance().exceptionService;
} }

Loading…
Cancel
Save