Finished new database structure

The database now uses 3 tables. forums, topics, and threads.

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-09-24 11:19:50 -04:00
parent 4e0cd45852
commit f06bd3e94c
3 changed files with 164 additions and 104 deletions

View File

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

View File

@@ -51,7 +51,7 @@ public class ForumListActivity extends FragmentActivity implements OnItemSelecte
new Thread(new Runnable(){
public void run(){
Looper.prepare();
new ForumDatabase(ForumListActivity.this).saveForums();
new ForumDatabase(ForumListActivity.this).saveForums(true);
}
}).start();

View File

@@ -24,7 +24,6 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -33,7 +32,6 @@ import java.util.Map.Entry;
import org.RickBarrette.osj.forum.Constraints;
import org.RickBarrette.osj.forum.Log;
import org.RickBarrette.osj.forum.R;
import org.RickBarrette.osj.forum.content.TopicContent.TopicItem;
import org.xmlrpc.android.XMLRPCClient;
import org.xmlrpc.android.XMLRPCException;
@@ -42,7 +40,6 @@ import android.content.ContentValues;
import android.content.Context;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.os.Handler;
@@ -80,9 +77,11 @@ public class ForumDatabase {
* @author ricky barrette
*/
private void createDatabase(final SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + FORUM_TABLE + "( id INTEGER PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY + " TEXT, "+ KEY_VALUE + " TEXT)");
db.execSQL("CREATE TABLE " + TOPIC_TABLE + "( id INTEGER PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY_TOPICM_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE + " TEXT)");
db.execSQL("CREATE TABLE " + THREAD_TABLE + "( id INTEGER PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY_TOPICM_ID + " TEXT, " + KEY_THREAD_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE + " TEXT)");
db.execSQL("CREATE TABLE " + FORUM_TABLE + "( id INTEGER PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE + " TEXT)");
db.execSQL("CREATE TABLE " + TOPIC_TABLE + "( id INTEGER PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY_TOPIC_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE
+ " TEXT)");
db.execSQL("CREATE TABLE " + THREAD_TABLE + "( id INTEGER PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY_TOPIC_ID + " TEXT, " + KEY_POST_ID + " TEXT, "
+ KEY + " TEXT, " + KEY_VALUE + " TEXT)");
}
/**
@@ -94,7 +93,7 @@ public class ForumDatabase {
*/
@Override
public void onCreate(final SQLiteDatabase db) {
if (Constraints.DROP_TABLES_EVERY_TIME){
if (Constraints.DROP_TABLES_EVERY_TIME) {
db.execSQL("DROP TABLE IF EXISTS " + FORUM_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + TOPIC_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + THREAD_TABLE);
@@ -185,8 +184,8 @@ public class ForumDatabase {
private static final String KEY = "key";
private static final String KEY_VALUE = "value";
private static final String KEY_FORUM_ID = "forum_id";
private static final String KEY_TOPICM_ID = "topic_id";
private static final String KEY_THREAD_ID = "thread_id";
private static final String KEY_TOPIC_ID = "topic_id";
private static final String KEY_POST_ID = "post_id";
/**
* Parses a string boolean from the database
@@ -251,6 +250,45 @@ public class ForumDatabase {
}
}
/**
* Converts a hash map into a list of content values ready to be entered
* into the database
*
* @param contentHash
* @return list of ContentValues
* @author ricky barrette
*/
private List<ContentValues> convertHashMapToContentValues(final HashMap<?, ?> contentHash) {
final ArrayList<ContentValues> list = new ArrayList<ContentValues>();
final ContentValues values = new ContentValues();
Log.v(TAG, "total hash map size " + contentHash.size());
for (final Entry<?, ?> item : contentHash.entrySet()) {
values.clear();
values.put(KEY, (String) item.getKey());
try {
values.put(KEY_VALUE, (String) item.getValue());
} catch (final ClassCastException e) {
try {
values.put(KEY_VALUE, (Boolean) item.getValue() ? 1 : 0);
} catch (final ClassCastException e1) {
try {
values.put(KEY_VALUE, (Integer) item.getValue());
} catch (final ClassCastException e2) {
try {
values.put(KEY_VALUE, new String((byte[]) item.getValue()));
} catch (final ClassCastException e3) {
e3.printStackTrace();
}
}
}
}
Log.v(TAG, (String) item.getKey());
list.add(new ContentValues(values));
}
Log.v(TAG, "total list size " + list.size());
return list;
}
/**
* Copies a file
*
@@ -336,33 +374,13 @@ public class ForumDatabase {
mListener.onRestoreComplete();
}
/**
* upserts a database table
*
* @param table
* @param info
* @throws NullPointerException
* @author ricky barrette
*/
public void upsertForumTable(final String forumId, final List<ContentValues> values) throws NullPointerException {
if (values == null)
throw new NullPointerException("info was null");
for(ContentValues item : values){
item.put(KEY_FORUM_ID, forumId);
Log.v(TAG, "upserting: "+ item.getAsString(KEY));
if (!(mDb.update(FORUM_TABLE, item, KEY +" = "+ DatabaseUtils.sqlEscapeString(item.getAsString(KEY)), null) > 0))
mDb.insert(FORUM_TABLE, null, item);
}
}
/**
* Downloads and saves forums into the database
*
* @param context
* @param isRecursive
* @author ricky barrette
*/
public void saveForums() {
public void saveForums(final boolean isRecursive) {
final XMLRPCClient client = XMLRPCClient.getClient(mContext);
/*
@@ -379,48 +397,23 @@ public class ForumDatabase {
* save the forums
*/
if (result != null)
for (int i = 0; i < result.length; i++) {
final HashMap<?, ?> contentHash = (HashMap<?, ?>) result[i];
upsertForumTable((String)contentHash.get("forum_id"), convertHashMapToContentValues(contentHash));
saveTopics(client, contentHash.get("forum_id"));
for (final Object element : result) {
final HashMap<?, ?> contentHash = (HashMap<?, ?>) element;
upsertForumTable((String) contentHash.get("forum_id"), convertHashMapToContentValues(contentHash));
if (isRecursive)
saveTopics(client, contentHash.get("forum_id"), isRecursive);
}
}
/**
* Downloads and saves topics into the database
* @param client
* @param forumId
* @author ricky barrette
*/
private void saveTopics(XMLRPCClient client, Object forumId) {
Object[] result = null;
try {
result = (Object[]) ((HashMap<?, ?>) client.call("get_topic", forumId, 0, 0)).get("topics");
} catch (final XMLRPCException e) {
e.printStackTrace();
}
if (result != null)
for (int i = 0; i < result.length; i++) {
final HashMap<?, ?> contentHash = (HashMap<?, ?>) result[i];
//upsertTable(TOPIC_TABLE + contentHash.get("topic_id"),convertHashMapToContentValues((HashMap<?, ?>) result[i]));
saveThreads(client, contentHash.get("topic_id"));
}
}
/**
* Downloads and saves threads into the database
*
* @param client
* @param forumId
* @author ricky barrette
*/
private void saveThreads(XMLRPCClient client, Object topicId) {
public void saveThreads(final XMLRPCClient client, final Object forumId, final Object topicId) {
Object[] result = null;
try {
result = (Object[]) ((HashMap<?, ?>) client.call("get_thread", topicId, 0, 0)).get("posts");
@@ -429,50 +422,117 @@ public class ForumDatabase {
}
if (result != null)
for (int i = 0; i < result.length; i++) {
final HashMap<?, ?> contentHash = (HashMap<?, ?>) result[i];
//upsertTable(THREAD_TABLE + contentHash.get("post_id"),convertHashMapToContentValues((HashMap<?, ?>) result[i]));
for (final Object element : result) {
final HashMap<?, ?> contentHash = (HashMap<?, ?>) element;
upsertThreadTable((String) forumId, (String) topicId, (String) contentHash.get("post_id"), convertHashMapToContentValues(contentHash));
}
}
/**
* Converts a hash map into a list of content values ready to be entered
* into the database
* Downloads and saves topics into the database
*
* @param contentHash
* @return list of ContentValues
* @param client
* @param forumId
* @param isRecursive
* @author ricky barrette
*/
private List<ContentValues> convertHashMapToContentValues(final HashMap<?, ?> contentHash) {
final ArrayList<ContentValues> list = new ArrayList<ContentValues>();
final ContentValues values = new ContentValues();
Log.v(TAG, "total hash map size "+ contentHash.size());
for (final Entry<?, ?> item : contentHash.entrySet()) {
values.clear();
values.put(KEY, (String) item.getKey());
public void saveTopics(final XMLRPCClient client, final Object forumId, final boolean isRecursive) {
Object[] result = null;
try {
values.put(KEY_VALUE, (String) item.getValue());
} catch (final ClassCastException e) {
try {
values.put(KEY_VALUE, (Boolean) item.getValue() ? 1 : 0);
} catch (final ClassCastException e1) {
try {
values.put(KEY_VALUE, (Integer) item.getValue());
} catch (final ClassCastException e2) {
try {
values.put(KEY_VALUE, new String((byte[]) item.getValue()));
} catch (final ClassCastException e3) {
e3.printStackTrace();
result = (Object[]) ((HashMap<?, ?>) client.call("get_topic", forumId, 0, 0)).get("topics");
} catch (final XMLRPCException e) {
e.printStackTrace();
}
if (result != null)
for (final Object element : result) {
final HashMap<?, ?> contentHash = (HashMap<?, ?>) element;
upsertTopicTable((String) forumId, (String) contentHash.get("topic_id"), convertHashMapToContentValues(contentHash));
if (isRecursive)
saveThreads(client, forumId, contentHash.get("topic_id"));
}
}
/**
* upserts the forum database table
*
* @param table
* @param info
* @throws NullPointerException
* @author ricky barrette
*/
private void upsertForumTable(final String forumId, final List<ContentValues> values) throws NullPointerException {
if (values == null)
throw new NullPointerException("values was null");
for (final ContentValues item : values) {
item.put(KEY_FORUM_ID, forumId);
upsertTable(FORUM_TABLE,
KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY) + " AND " + KEY_FORUM_ID + " = " + DatabaseUtils.sqlEscapeString(forumId)), item);
}
}
/**
* upserts a database table
*
* @param table
* @param where
* @param item
* @author ricky barrette
*/
private void upsertTable(final String table, final String where, final ContentValues item) {
Log.v(TAG, "upserting: " + item.getAsString(KEY));
if (!(mDb.update(table, item, where, null) > 0))
mDb.insert(table, null, item);
}
/**
* Upserts the thread table
*
* @param forumId
* @param topicId
* @param postId
* @param values
* @throws NullPointerException
* @author ricky barrette
*/
private void upsertThreadTable(final String forumId, final String topicId, final String postId, final List<ContentValues> values) throws NullPointerException {
if (values == null)
throw new NullPointerException("values was null");
for (final ContentValues item : values) {
item.put(KEY_FORUM_ID, forumId);
item.put(KEY_TOPIC_ID, topicId);
item.put(KEY_POST_ID, postId);
upsertTable(
THREAD_TABLE,
KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY)) + " AND " + KEY_FORUM_ID + " = " + DatabaseUtils.sqlEscapeString(forumId)
+ " AND " + KEY_TOPIC_ID + " = " + DatabaseUtils.sqlEscapeString(topicId) + " AND " + KEY_POST_ID + " = "
+ DatabaseUtils.sqlEscapeString(postId), item);
}
}
Log.v(TAG, (String) item.getKey());
list.add(new ContentValues(values));
/**
* Upserts the topic table
*
* @param forumId
* @param topicId
* @param values
* @throws NullPointerException
* @author ricky barrette
*/
private void upsertTopicTable(final String forumId, final String topicId, final List<ContentValues> values) throws NullPointerException {
if (values == null)
throw new NullPointerException("values was null");
for (final ContentValues item : values) {
item.put(KEY_FORUM_ID, forumId);
item.put(KEY_TOPIC_ID, topicId);
upsertTable(TOPIC_TABLE,
KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY)) + " AND " + KEY_FORUM_ID + " = " + DatabaseUtils.sqlEscapeString(forumId)
+ " AND " + KEY_TOPIC_ID + " = " + DatabaseUtils.sqlEscapeString(topicId), item);
}
Log.v(TAG, "total list size "+ list.size());
return list;
}
}