From e7e59f9d1bd6e3ebca8875e808ae28e889389fa3 Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Sun, 7 Oct 2012 01:17:05 -0400 Subject: [PATCH] Added time stamps to database. This allows topics to be displayed to the user in the proper oder. The order is descending dates Signed-off-by: Ricky Barrette --- OSJ Forum/AndroidManifest.xml | 2 +- .../osj/forum/database/ForumDatabase.java | 33 +++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/OSJ Forum/AndroidManifest.xml b/OSJ Forum/AndroidManifest.xml index ec8af10..10eaf36 100644 --- a/OSJ Forum/AndroidManifest.xml +++ b/OSJ Forum/AndroidManifest.xml @@ -1,5 +1,5 @@ getThreads(String id) { final ArrayList list = new ArrayList(); - final Cursor c = mDb.query(TABLE_THREADS, new String[] { KEY_ID }, KEY_TOPIC_ID +" = "+ DatabaseUtils.sqlEscapeString(id), null, null, null, null); + final Cursor c = mDb.query(TABLE_THREADS, new String[] { KEY_ID }, KEY_TOPIC_ID +" = "+ DatabaseUtils.sqlEscapeString(id), null, null, null, KEY_TIME_STAMP); if (c.moveToFirst()) do list.add(c.getString(0)); @@ -456,7 +467,7 @@ public class ForumDatabase { */ public List getTopics(String id) { final ArrayList list = new ArrayList(); - final Cursor c = mDb.query(TABLE_TOPICS, new String[] { KEY_ID }, KEY_FORUM_ID +" = "+ DatabaseUtils.sqlEscapeString(id) , null, null, null, null); + final Cursor c = mDb.query(TABLE_TOPICS, new String[] { KEY_ID }, KEY_FORUM_ID +" = "+ DatabaseUtils.sqlEscapeString(id) , null, null, null, KEY_TIME_STAMP + " DESC"); if (c.moveToFirst()) do list.add(c.getString(0)); @@ -548,7 +559,7 @@ public class ForumDatabase { if (result != null) for (final Object element : result) { final HashMap contentHash = (HashMap) element; - upsertThreadTable((String) forumId, (String) topicId, (String) contentHash.get("post_id"), convertHashMapToContentValues(contentHash)); + upsertThreadTable((String) forumId, (String) topicId, (String) contentHash.get("post_id"), (Date) contentHash.get("post_time"), convertHashMapToContentValues(contentHash)); } } @@ -572,7 +583,7 @@ public class ForumDatabase { if (result != null) for (final Object element : result) { final HashMap contentHash = (HashMap) element; - upsertTopicTable((String) forumId, (String) contentHash.get("topic_id"), convertHashMapToContentValues(contentHash)); + upsertTopicTable((String) forumId, (String) contentHash.get("topic_id"), (Date) contentHash.get("last_reply_time"), convertHashMapToContentValues(contentHash)); if (isRecursive) saveThreads(client, forumId, contentHash.get("topic_id")); } @@ -611,7 +622,7 @@ public class ForumDatabase { * @author ricky barrette */ private void upsertTable(final String table, final String where, final ContentValues item) { - Log.v(TAG, "upserting: " + item.getAsString(KEY)); + Log.v(TAG, "upserting: " + item.getAsString(KEY) + " : "+ item.getAsString(KEY_VALUE)); if (!(mDb.update(table, item, where, null) > 0)) mDb.insert(table, null, item); } @@ -626,7 +637,7 @@ public class ForumDatabase { * @throws NullPointerException * @author ricky barrette */ - private void upsertThreadTable(final String forumId, final String topicId, final String postId, final List values) throws NullPointerException { + private void upsertThreadTable(final String forumId, final String topicId, final String postId, final Date time, final List values) throws NullPointerException { if (values == null) throw new NullPointerException("values was null"); @@ -634,6 +645,7 @@ public class ForumDatabase { id.put(KEY_ID, postId); id.put(KEY_FORUM_ID, forumId); id.put(KEY_TOPIC_ID, topicId); + id.put(KEY_TIME_STAMP, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time)); upsertTable(TABLE_THREADS, KEY_ID + " = " + DatabaseUtils.sqlEscapeString(postId), id); for (final ContentValues item : values) { @@ -652,13 +664,14 @@ public class ForumDatabase { * @throws NullPointerException * @author ricky barrette */ - private void upsertTopicTable(final String forumId, final String topicId, final List values) throws NullPointerException { + private void upsertTopicTable(final String forumId, final String topicId, final Date time, final List values) throws NullPointerException { if (values == null) throw new NullPointerException("values was null"); final ContentValues id = new ContentValues(); id.put(KEY_ID, topicId); id.put(KEY_FORUM_ID, forumId); + id.put(KEY_TIME_STAMP, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time)); upsertTable(TABLE_TOPICS, KEY_ID + " = " + DatabaseUtils.sqlEscapeString(topicId), id); for (final ContentValues item : values) {