From 7f3eb985f247423353fb02ca6b42748981b818fc Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Sat, 22 Sep 2012 01:56:07 -0400 Subject: [PATCH] Implemented Initial database design. probably going to scrap this design however. Signed-off-by: Ricky Barrette --- .../osj/forum/ForumListActivity.java | 2 +- .../osj/forum/database/ForumDatabase.java | 140 ++++++++++-------- 2 files changed, 81 insertions(+), 61 deletions(-) diff --git a/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListActivity.java b/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListActivity.java index 5ded465..fd336f2 100644 --- a/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListActivity.java +++ b/OSJ Forum/src/org/RickBarrette/osj/forum/ForumListActivity.java @@ -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(); diff --git a/OSJ Forum/src/org/RickBarrette/osj/forum/database/ForumDatabase.java b/OSJ Forum/src/org/RickBarrette/osj/forum/database/ForumDatabase.java index 6ccc542..416ea0c 100644 --- a/OSJ Forum/src/org/RickBarrette/osj/forum/database/ForumDatabase.java +++ b/OSJ Forum/src/org/RickBarrette/osj/forum/database/ForumDatabase.java @@ -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 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 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 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