diff --git a/OSJ Forum/AndroidManifest.xml b/OSJ Forum/AndroidManifest.xml index 8f37e9e..99fcb8f 100644 --- a/OSJ Forum/AndroidManifest.xml +++ b/OSJ Forum/AndroidManifest.xml @@ -1,5 +1,5 @@ convertHashMapToContentValues(final HashMap contentHash) { + final ArrayList list = new ArrayList(); + 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 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 convertHashMapToContentValues(final HashMap contentHash) { - final ArrayList list = new ArrayList(); - 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)); + public void saveTopics(final XMLRPCClient client, final Object forumId, final boolean isRecursive) { + 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 (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 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 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 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; } }