From 992ccab36da6bd543bc39bce23d75143b6b1e54e Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Fri, 21 Sep 2012 14:27:56 -0400 Subject: [PATCH] Created a simple forum sync Finished creating a method that downloads and saves forums. Signed-off-by: Ricky Barrette --- OSJ Forum/AndroidManifest.xml | 2 +- .../osj/forum/ForumListActivity.java | 9 + .../osj/forum/database/ForumDatabase.java | 164 ++++++++++++++---- 3 files changed, 141 insertions(+), 34 deletions(-) diff --git a/OSJ Forum/AndroidManifest.xml b/OSJ Forum/AndroidManifest.xml index 9717115..8f37e9e 100644 --- a/OSJ Forum/AndroidManifest.xml +++ b/OSJ Forum/AndroidManifest.xml @@ -1,5 +1,5 @@ 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 + * Updates the database with online content + * + * @param context * @author ricky barrette */ - public void upsertTable(final String table, final ContentValues info) throws NullPointerException { - if (info == null) - throw new NullPointerException("info was null"); + public void updateDatabase() { + final XMLRPCClient client = XMLRPCClient.getClient(mContext); + /* - * here we wanr to update the information values in the info table + * download the forums */ - for (final Entry item : info.valueSet()) { - final ContentValues values = new ContentValues(); - values.put(KEY, item.getKey()); + Object[] result = null; + try { + result = (Object[]) client.call("get_forum", true); + } catch (final XMLRPCException e) { + e.printStackTrace(); + } + + /* + * save the forums + */ + 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 + } + + 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); + } + } + } + + /** + * 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 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) { - values.put(KEY_VALUE, (Integer) item.getValue()); + 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); + Log.v(TAG, (String) item.getKey()); + list.add(new ContentValues(values)); } + Log.v(TAG, "total list size "+ list.size()); + return list; } } \ No newline at end of file