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 <rickbarrette@gmail.com>
This commit is contained in:
2012-10-07 01:17:05 -04:00
parent 42a34f5c5f
commit e7e59f9d1b
2 changed files with 24 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
<manifest package="org.RickBarrette.osj.forum"
android:versionCode="310"
android:versionCode="328"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
<uses-sdk

View File

@@ -24,7 +24,9 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
@@ -83,8 +85,8 @@ public class ForumDatabase {
* These tables will be used for storing forum, topic, and post id's
*/
db.execSQL("CREATE TABLE " + TABLE_FORUMS + "( id TEXT PRIMARY KEY)");
db.execSQL("CREATE TABLE " + TABLE_TOPICS + "( id TEXT PRIMARY KEY, " + KEY_FORUM_ID + " TEXT)");
db.execSQL("CREATE TABLE " + TABLE_THREADS + "( id TEXT PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY_TOPIC_ID + " TEXT)");
db.execSQL("CREATE TABLE " + TABLE_TOPICS + "( id TEXT PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, "+ KEY_TIME_STAMP + " DATE)");
db.execSQL("CREATE TABLE " + TABLE_THREADS + "( id TEXT PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY_TOPIC_ID + " TEXT, "+ KEY_TIME_STAMP + " DATE)");
/*
* These tables will be used for storing forum, topic, and post
@@ -205,6 +207,7 @@ public class ForumDatabase {
private static final String KEY_FORUM_ID = "forum_id";
private static final String KEY_TOPIC_ID = "topic_id";
private static final String KEY_POST_ID = "post_id";
private static final String KEY_TIME_STAMP = "time_stamp";
/**
* Parses a string boolean from the database
@@ -296,7 +299,15 @@ public class ForumDatabase {
try {
values.put(KEY_VALUE, new String((byte[]) item.getValue()));
} catch (final ClassCastException e3) {
e3.printStackTrace();
try {
values.put(KEY_VALUE, ((Date) item.getValue()).toString());
} catch (final ClassCastException e4) {
try {
values.put(KEY_VALUE, (Long) item.getValue());
} catch (final ClassCastException e5) {
Log.w(TAG, "convertHashMapToContentValues", e5);
}
}
}
}
}
@@ -427,7 +438,7 @@ public class ForumDatabase {
*/
public List<String> getThreads(String id) {
final ArrayList<String> list = new ArrayList<String>();
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<String> getTopics(String id) {
final ArrayList<String> list = new ArrayList<String>();
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<ContentValues> values) throws NullPointerException {
private void upsertThreadTable(final String forumId, final String topicId, final String postId, final Date time, final List<ContentValues> 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<ContentValues> values) throws NullPointerException {
private void upsertTopicTable(final String forumId, final String topicId, final Date time, final List<ContentValues> 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) {