Implemented Initial database design.

probably going to scrap this design however.

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-09-22 01:56:07 -04:00
parent 992ccab36d
commit 7f3eb985f2
2 changed files with 81 additions and 61 deletions

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).updateDatabase();
new ForumDatabase(ForumListActivity.this).saveForums();
}
}).start();

View File

@@ -33,6 +33,7 @@ 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;
@@ -330,57 +331,40 @@ public class ForumDatabase {
mListener.onRestoreComplete();
}
// /**
// * upserts a database table
// *
// * @param table
// * @param info
// * @throws NullPointerException
// * @author ricky barrette
// */
// public void upsertTable(final String table, final ContentValues values) throws NullPointerException {
// if (values == null)
// throw new NullPointerException("info was null");
// /*
// * here we wanr to update the information values in the info table
// */
// for (final Entry<String, Object> item : values.valueSet()) {
// final ContentValues values = new ContentValues();
// values.put(KEY, 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();
// }
// }
// }
//
// /*
// * here we are going to try to update a row, if that fails we will
// * insert a row insead
// */
// if (!(mDb.update(table, values, null, null) > 0))
// mDb.insert(table, null, values);
// }
// }
// }
/**
* upserts a database table
*
* @param table
* @param info
* @throws NullPointerException
* @author ricky barrette
*/
public void upsertTable(final String table, final List<ContentValues> values) throws NullPointerException {
if (values == null)
throw new NullPointerException("info was null");
/*
* create a new table if needed
*/
try {
mDb.execSQL("CREATE TABLE " + table + " (" + KEY + " TEXT PRIMARY KEY, " + KEY_VALUE + " TEXT)");
} catch (SQLiteException e) {
// ignore exception, table is already created
}
for(ContentValues item : values){
Log.v(TAG, "upserting: "+ item.getAsString(KEY));
if (!(mDb.update(table, item, KEY +" = "+ DatabaseUtils.sqlEscapeString(item.getAsString(KEY)), null) > 0))
mDb.insert(table, null, item);
}
}
/**
* Updates the database with online content
* Downloads and saves forums into the database
*
* @param context
* @author ricky barrette
*/
public void updateDatabase() {
public void saveForums() {
final XMLRPCClient client = XMLRPCClient.getClient(mContext);
/*
@@ -399,25 +383,61 @@ public class ForumDatabase {
if (result != null)
for (int i = 0; i < result.length; i++) {
final HashMap<?, ?> contentHash = (HashMap<?, ?>) result[i];
/*
* create a new table if needed
*/
try {
mDb.execSQL("CREATE TABLE " + FORUM_TABLE + contentHash.get("forum_id") + " (" + KEY + " TEXT PRIMARY KEY, " + KEY_VALUE + " TEXT)");
} catch (SQLiteException e) {
// TODO: handle exception
}
upsertTable(FORUM_TABLE + contentHash.get("forum_id"), convertHashMapToContentValues(contentHash));
List<ContentValues> list = convertHashMapToContentValues(contentHash);
Log.v(TAG, "totsl converted list size "+ list.size());
for(ContentValues item : list){
Log.v(TAG, "upserting: "+ item.getAsString(KEY));
if (!(mDb.update(FORUM_TABLE + contentHash.get("forum_id"), item, KEY +" = "+ DatabaseUtils.sqlEscapeString(item.getAsString(KEY)), null) > 0))
mDb.insert(FORUM_TABLE + contentHash.get("forum_id"), null, item);
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
* @param client
* @param forumId
* @author ricky barrette
*/
private void saveThreads(XMLRPCClient client, Object topicId) {
Object[] result = null;
try {
result = (Object[]) ((HashMap<?, ?>) client.call("get_thread", topicId, 0, 0)).get("posts");
} catch (final XMLRPCException e) {
e.printStackTrace();
}
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]));
}
}
/**
* Converts a hash map into a list of content values ready to be entered