Created a simple forum sync

Finished creating a method that downloads and saves forums.

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-09-21 14:27:56 -04:00
parent a2d60f98b1
commit 992ccab36d
3 changed files with 141 additions and 34 deletions

View File

@@ -1,5 +1,5 @@
<manifest package="org.RickBarrette.osj.forum"
android:versionCode="215"
android:versionCode="244"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
<uses-sdk

View File

@@ -20,9 +20,11 @@
package org.RickBarrette.osj.forum;
import org.RickBarrette.osj.forum.content.OnItemSelectedListener;
import org.RickBarrette.osj.forum.database.ForumDatabase;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
@@ -46,6 +48,13 @@ public class ForumListActivity extends FragmentActivity implements OnItemSelecte
setContentView(R.layout.activity_forum_list);
new Thread(new Runnable(){
public void run(){
Looper.prepare();
new ForumDatabase(ForumListActivity.this).updateDatabase();
}
}).start();
if (findViewById(R.id.forum_detail_container) != null) {
mTwoPane = true;
((ForumListFragment) getSupportFragmentManager().findFragmentById(R.id.forum_list)).setActivateOnItemClick(true);

View File

@@ -24,20 +24,24 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
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.xmlrpc.android.XMLRPCClient;
import org.xmlrpc.android.XMLRPCException;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.os.Handler;
@@ -46,6 +50,7 @@ import android.os.Message;
/**
* This class will be the main interface between osj forum and it's database
*
* @author ricky barrette
*/
public class ForumDatabase {
@@ -74,12 +79,12 @@ public class ForumDatabase {
* @author ricky barrette
*/
private void createDatabase(final SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE + "("+KEY+" TEXT PRIMARY KEY, " + KEY_VALUE + " TEXT)");
// db.execSQL("CREATE TABLE " + TABLE + "(" + KEY + " TEXT PRIMARY KEY, " + KEY_VALUE + " TEXT)");
}
/**
* called when the database is created for the first time. this will create our database
* (non-Javadoc)
* called when the database is created for the first time. this will
* create our database (non-Javadoc)
*
* @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
* @author ricky barrette
@@ -89,7 +94,7 @@ public class ForumDatabase {
if (Constraints.DROP_TABLES_EVERY_TIME)
db.execSQL("DROP TABLE IF EXISTS " + TABLE);
createDatabase(db);
if(mListener != null)
if (mListener != null)
mListener.onDatabaseCreate();
}
@@ -115,8 +120,8 @@ public class ForumDatabase {
public void run() {
Looper.prepare();
switch (oldVersion) {
case 1:
//Update stuff here, do not use breaks
case 1:
// Update stuff here, do not use breaks
}
}
}).start();
@@ -177,7 +182,6 @@ public class ForumDatabase {
private static final String KEY_TOPICM_ID = "topic_id";
private static final String KEY_THREAD_ID = "thread_id";
/**
* Parses a string boolean from the database
*
@@ -244,8 +248,10 @@ public class ForumDatabase {
/**
* Copies a file
*
* @param src file
* @param dst file
* @param src
* file
* @param dst
* file
* @throws IOException
* @author ricky barrette
*/
@@ -285,7 +291,7 @@ public class ForumDatabase {
public void run() {
Looper.prepare();
//Delete stuff here
// Delete stuff here
mHandler.sendEmptyMessage(DELETIONCOMPLETE);
progress.dismiss();
@@ -324,39 +330,131 @@ 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
* 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<String, Object> 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<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);
}
}
}
/**
* 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) {
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;
}
}