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:
@@ -1,5 +1,5 @@
|
|||||||
<manifest package="org.RickBarrette.osj.forum"
|
<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">
|
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
|
||||||
|
|
||||||
<uses-sdk
|
<uses-sdk
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ public class ForumListActivity extends FragmentActivity implements OnItemSelecte
|
|||||||
new Thread(new Runnable(){
|
new Thread(new Runnable(){
|
||||||
public void run(){
|
public void run(){
|
||||||
Looper.prepare();
|
Looper.prepare();
|
||||||
new ForumDatabase(ForumListActivity.this).saveForums();
|
new ForumDatabase(ForumListActivity.this).saveForums(true);
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ import java.io.FileInputStream;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.channels.FileChannel;
|
import java.nio.channels.FileChannel;
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -33,7 +32,6 @@ import java.util.Map.Entry;
|
|||||||
import org.RickBarrette.osj.forum.Constraints;
|
import org.RickBarrette.osj.forum.Constraints;
|
||||||
import org.RickBarrette.osj.forum.Log;
|
import org.RickBarrette.osj.forum.Log;
|
||||||
import org.RickBarrette.osj.forum.R;
|
import org.RickBarrette.osj.forum.R;
|
||||||
import org.RickBarrette.osj.forum.content.TopicContent.TopicItem;
|
|
||||||
import org.xmlrpc.android.XMLRPCClient;
|
import org.xmlrpc.android.XMLRPCClient;
|
||||||
import org.xmlrpc.android.XMLRPCException;
|
import org.xmlrpc.android.XMLRPCException;
|
||||||
|
|
||||||
@@ -42,7 +40,6 @@ import android.content.ContentValues;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.database.DatabaseUtils;
|
import android.database.DatabaseUtils;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
import android.database.sqlite.SQLiteException;
|
|
||||||
import android.database.sqlite.SQLiteOpenHelper;
|
import android.database.sqlite.SQLiteOpenHelper;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
@@ -80,9 +77,11 @@ public class ForumDatabase {
|
|||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
*/
|
*/
|
||||||
private void createDatabase(final SQLiteDatabase db) {
|
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 " + 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 " + TOPIC_TABLE + "( id INTEGER PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY_TOPIC_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE
|
||||||
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)");
|
+ " 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
|
@Override
|
||||||
public void onCreate(final SQLiteDatabase db) {
|
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 " + FORUM_TABLE);
|
||||||
db.execSQL("DROP TABLE IF EXISTS " + TOPIC_TABLE);
|
db.execSQL("DROP TABLE IF EXISTS " + TOPIC_TABLE);
|
||||||
db.execSQL("DROP TABLE IF EXISTS " + THREAD_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 = "key";
|
||||||
private static final String KEY_VALUE = "value";
|
private static final String KEY_VALUE = "value";
|
||||||
private static final String KEY_FORUM_ID = "forum_id";
|
private static final String KEY_FORUM_ID = "forum_id";
|
||||||
private static final String KEY_TOPICM_ID = "topic_id";
|
private static final String KEY_TOPIC_ID = "topic_id";
|
||||||
private static final String KEY_THREAD_ID = "thread_id";
|
private static final String KEY_POST_ID = "post_id";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses a string boolean from the database
|
* 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
|
* Copies a file
|
||||||
*
|
*
|
||||||
@@ -336,33 +374,13 @@ public class ForumDatabase {
|
|||||||
mListener.onRestoreComplete();
|
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
|
* Downloads and saves forums into the database
|
||||||
*
|
*
|
||||||
* @param context
|
* @param isRecursive
|
||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
*/
|
*/
|
||||||
public void saveForums() {
|
public void saveForums(final boolean isRecursive) {
|
||||||
final XMLRPCClient client = XMLRPCClient.getClient(mContext);
|
final XMLRPCClient client = XMLRPCClient.getClient(mContext);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -379,48 +397,23 @@ public class ForumDatabase {
|
|||||||
* save the forums
|
* save the forums
|
||||||
*/
|
*/
|
||||||
if (result != null)
|
if (result != null)
|
||||||
for (int i = 0; i < result.length; i++) {
|
for (final Object element : result) {
|
||||||
final HashMap<?, ?> contentHash = (HashMap<?, ?>) result[i];
|
final HashMap<?, ?> contentHash = (HashMap<?, ?>) element;
|
||||||
upsertForumTable((String)contentHash.get("forum_id"), convertHashMapToContentValues(contentHash));
|
upsertForumTable((String) contentHash.get("forum_id"), convertHashMapToContentValues(contentHash));
|
||||||
|
if (isRecursive)
|
||||||
|
saveTopics(client, contentHash.get("forum_id"), isRecursive);
|
||||||
|
|
||||||
saveTopics(client, contentHash.get("forum_id"));
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
* Downloads and saves threads into the database
|
||||||
|
*
|
||||||
* @param client
|
* @param client
|
||||||
* @param forumId
|
* @param forumId
|
||||||
* @author ricky barrette
|
* @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;
|
Object[] result = null;
|
||||||
try {
|
try {
|
||||||
result = (Object[]) ((HashMap<?, ?>) client.call("get_thread", topicId, 0, 0)).get("posts");
|
result = (Object[]) ((HashMap<?, ?>) client.call("get_thread", topicId, 0, 0)).get("posts");
|
||||||
@@ -429,50 +422,117 @@ public class ForumDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (result != null)
|
if (result != null)
|
||||||
for (int i = 0; i < result.length; i++) {
|
for (final Object element : result) {
|
||||||
final HashMap<?, ?> contentHash = (HashMap<?, ?>) result[i];
|
final HashMap<?, ?> contentHash = (HashMap<?, ?>) element;
|
||||||
//upsertTable(THREAD_TABLE + contentHash.get("post_id"),convertHashMapToContentValues((HashMap<?, ?>) result[i]));
|
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
|
* Downloads and saves topics into the database
|
||||||
* into the database
|
|
||||||
*
|
*
|
||||||
* @param contentHash
|
* @param client
|
||||||
* @return list of ContentValues
|
* @param forumId
|
||||||
|
* @param isRecursive
|
||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
*/
|
*/
|
||||||
private List<ContentValues> convertHashMapToContentValues(final HashMap<?, ?> contentHash) {
|
public void saveTopics(final XMLRPCClient client, final Object forumId, final boolean isRecursive) {
|
||||||
final ArrayList<ContentValues> list = new ArrayList<ContentValues>();
|
Object[] result = null;
|
||||||
final ContentValues values = new ContentValues();
|
try {
|
||||||
Log.v(TAG, "total hash map size "+ contentHash.size());
|
result = (Object[]) ((HashMap<?, ?>) client.call("get_topic", forumId, 0, 0)).get("topics");
|
||||||
for (final Entry<?, ?> item : contentHash.entrySet()) {
|
} catch (final XMLRPCException e) {
|
||||||
values.clear();
|
e.printStackTrace();
|
||||||
values.put(KEY, (String) item.getKey());
|
}
|
||||||
try {
|
|
||||||
values.put(KEY_VALUE, (String) item.getValue());
|
if (result != null)
|
||||||
} catch (final ClassCastException e) {
|
for (final Object element : result) {
|
||||||
try {
|
final HashMap<?, ?> contentHash = (HashMap<?, ?>) element;
|
||||||
values.put(KEY_VALUE, (Boolean) item.getValue() ? 1 : 0);
|
upsertTopicTable((String) forumId, (String) contentHash.get("topic_id"), convertHashMapToContentValues(contentHash));
|
||||||
} catch (final ClassCastException e1) {
|
if (isRecursive)
|
||||||
try {
|
saveThreads(client, forumId, contentHash.get("topic_id"));
|
||||||
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();
|
* upserts the forum database table
|
||||||
}
|
*
|
||||||
}
|
* @param table
|
||||||
}
|
* @param info
|
||||||
}
|
* @throws NullPointerException
|
||||||
Log.v(TAG, (String) item.getKey());
|
* @author ricky barrette
|
||||||
list.add(new ContentValues(values));
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user