Implemented Initial database design.
probably going to scrap this design however. Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
@@ -51,7 +51,7 @@ public class ForumListActivity extends FragmentActivity implements OnItemSelecte
|
|||||||
new Thread(new Runnable(){
|
new Thread(new Runnable(){
|
||||||
public void run(){
|
public void run(){
|
||||||
Looper.prepare();
|
Looper.prepare();
|
||||||
new ForumDatabase(ForumListActivity.this).updateDatabase();
|
new ForumDatabase(ForumListActivity.this).saveForums();
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ import java.util.Map.Entry;
|
|||||||
import org.RickBarrette.osj.forum.Constraints;
|
import org.RickBarrette.osj.forum.Constraints;
|
||||||
import org.RickBarrette.osj.forum.Log;
|
import org.RickBarrette.osj.forum.Log;
|
||||||
import org.RickBarrette.osj.forum.R;
|
import org.RickBarrette.osj.forum.R;
|
||||||
|
import org.RickBarrette.osj.forum.content.TopicContent.TopicItem;
|
||||||
import org.xmlrpc.android.XMLRPCClient;
|
import org.xmlrpc.android.XMLRPCClient;
|
||||||
import org.xmlrpc.android.XMLRPCException;
|
import org.xmlrpc.android.XMLRPCException;
|
||||||
|
|
||||||
@@ -330,57 +331,40 @@ public class ForumDatabase {
|
|||||||
mListener.onRestoreComplete();
|
mListener.onRestoreComplete();
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * upserts a database table
|
* upserts a database table
|
||||||
// *
|
*
|
||||||
// * @param table
|
* @param table
|
||||||
// * @param info
|
* @param info
|
||||||
// * @throws NullPointerException
|
* @throws NullPointerException
|
||||||
// * @author ricky barrette
|
* @author ricky barrette
|
||||||
// */
|
*/
|
||||||
// public void upsertTable(final String table, final ContentValues values) throws NullPointerException {
|
public void upsertTable(final String table, final List<ContentValues> values) throws NullPointerException {
|
||||||
// if (values == null)
|
if (values == null)
|
||||||
// throw new NullPointerException("info was null");
|
throw new NullPointerException("info was null");
|
||||||
// /*
|
/*
|
||||||
// * here we wanr to update the information values in the info table
|
* create a new table if needed
|
||||||
// */
|
*/
|
||||||
// for (final Entry<String, Object> item : values.valueSet()) {
|
try {
|
||||||
// final ContentValues values = new ContentValues();
|
mDb.execSQL("CREATE TABLE " + table + " (" + KEY + " TEXT PRIMARY KEY, " + KEY_VALUE + " TEXT)");
|
||||||
// values.put(KEY, item.getKey());
|
} catch (SQLiteException e) {
|
||||||
// try {
|
// ignore exception, table is already created
|
||||||
// values.put(KEY_VALUE, (String) item.getValue());
|
}
|
||||||
// } catch (final ClassCastException e) {
|
|
||||||
// try {
|
for(ContentValues item : values){
|
||||||
// values.put(KEY_VALUE, (Boolean) item.getValue() ? 1 : 0);
|
Log.v(TAG, "upserting: "+ item.getAsString(KEY));
|
||||||
// } catch (final ClassCastException e1) {
|
if (!(mDb.update(table, item, KEY +" = "+ DatabaseUtils.sqlEscapeString(item.getAsString(KEY)), null) > 0))
|
||||||
// try {
|
mDb.insert(table, null, item);
|
||||||
// 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);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the database with online content
|
* Downloads and saves forums into the database
|
||||||
*
|
*
|
||||||
* @param context
|
* @param context
|
||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
*/
|
*/
|
||||||
public void updateDatabase() {
|
public void saveForums() {
|
||||||
final XMLRPCClient client = XMLRPCClient.getClient(mContext);
|
final XMLRPCClient client = XMLRPCClient.getClient(mContext);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -399,26 +383,62 @@ public class ForumDatabase {
|
|||||||
if (result != null)
|
if (result != null)
|
||||||
for (int i = 0; i < result.length; i++) {
|
for (int i = 0; i < result.length; i++) {
|
||||||
final HashMap<?, ?> contentHash = (HashMap<?, ?>) result[i];
|
final HashMap<?, ?> contentHash = (HashMap<?, ?>) result[i];
|
||||||
/*
|
upsertTable(FORUM_TABLE + contentHash.get("forum_id"), convertHashMapToContentValues(contentHash));
|
||||||
* create a new table if needed
|
|
||||||
|
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 {
|
try {
|
||||||
mDb.execSQL("CREATE TABLE " + FORUM_TABLE + contentHash.get("forum_id") + " (" + KEY + " TEXT PRIMARY KEY, " + KEY_VALUE + " TEXT)");
|
result = (Object[]) ((HashMap<?, ?>) client.call("get_topic", forumId, 0, 0)).get("topics");
|
||||||
} catch (SQLiteException e) {
|
} catch (final XMLRPCException e) {
|
||||||
// TODO: handle exception
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<ContentValues> list = convertHashMapToContentValues(contentHash);
|
if (result != null)
|
||||||
Log.v(TAG, "totsl converted list size "+ list.size());
|
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]));
|
||||||
|
|
||||||
for(ContentValues item : list){
|
saveThreads(client, contentHash.get("topic_id"));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
* Converts a hash map into a list of content values ready to be entered
|
||||||
* into the database
|
* into the database
|
||||||
|
|||||||
Reference in New Issue
Block a user