|
|
|
|
@@ -78,19 +78,20 @@ public class ForumDatabase {
|
|
|
|
|
* @author ricky barrette
|
|
|
|
|
*/
|
|
|
|
|
private void createDatabase(final SQLiteDatabase db) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* These tables will be used for storing forum, topic, and post id's
|
|
|
|
|
*/
|
|
|
|
|
db.execSQL("CREATE TABLE " + TABLE_FORUMS + "( id TEXT PRIMARY KEY)");
|
|
|
|
|
db.execSQL("CREATE TABLE " + TABLE_TOPICS + "( id TEXT PRIMARY KEY, "+ KEY_FORUM_ID +" TEXT)");
|
|
|
|
|
db.execSQL("CREATE TABLE " + TABLE_THREADS + "( id TEXT PRIMARY KEY, "+ KEY_FORUM_ID + " TEXT, "+ KEY_TOPIC_ID + " TEXT)");
|
|
|
|
|
|
|
|
|
|
db.execSQL("CREATE TABLE " + TABLE_TOPICS + "( id TEXT PRIMARY KEY, " + KEY_FORUM_ID + " TEXT)");
|
|
|
|
|
db.execSQL("CREATE TABLE " + TABLE_THREADS + "( id TEXT PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY_TOPIC_ID + " TEXT)");
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* These tables will be used for storing forum, topic, and post information.
|
|
|
|
|
* These tables will be used for storing forum, topic, and post
|
|
|
|
|
* information.
|
|
|
|
|
*/
|
|
|
|
|
db.execSQL("CREATE TABLE " + TABLE_FORUM_INFO + "( id INTEGER PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE + " TEXT)");
|
|
|
|
|
db.execSQL("CREATE TABLE " + TABLE_TOPIC_INFO + "( id INTEGER PRIMARY KEY, " + KEY_TOPIC_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE+ " TEXT)");
|
|
|
|
|
db.execSQL("CREATE TABLE " + TABLE_TOPIC_INFO + "( id INTEGER PRIMARY KEY, " + KEY_TOPIC_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE + " TEXT)");
|
|
|
|
|
db.execSQL("CREATE TABLE " + TABLE_THREAD_INFO + "( id INTEGER PRIMARY KEY, " + KEY_POST_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE + " TEXT)");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -107,7 +108,7 @@ public class ForumDatabase {
|
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_FORUMS);
|
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TOPICS);
|
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_THREADS);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_FORUM_INFO);
|
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TOPIC_INFO);
|
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_THREAD_INFO);
|
|
|
|
|
@@ -337,6 +338,22 @@ public class ForumDatabase {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Converts a cursor with key-value pairs into a hash map
|
|
|
|
|
*
|
|
|
|
|
* @param c
|
|
|
|
|
* @return
|
|
|
|
|
* @author ricky barrette
|
|
|
|
|
*/
|
|
|
|
|
private HashMap<String, String> cursorToHashMap(final Cursor c) {
|
|
|
|
|
final HashMap<String, String> h = new HashMap<String, String>();
|
|
|
|
|
if (c.moveToFirst())
|
|
|
|
|
do
|
|
|
|
|
h.put(c.getString(c.getColumnIndex(KEY)), c.getString(c.getColumnIndex(KEY_VALUE)));
|
|
|
|
|
while (c.moveToNext());
|
|
|
|
|
return h;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* deletes a note by its row id
|
|
|
|
|
*
|
|
|
|
|
@@ -361,96 +378,90 @@ public class ForumDatabase {
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieves a list of forum ids
|
|
|
|
|
* @return
|
|
|
|
|
* @author ricky barrette
|
|
|
|
|
*/
|
|
|
|
|
public List<String> getForums(){
|
|
|
|
|
ArrayList<String> list = new ArrayList<String>();
|
|
|
|
|
Cursor c = mDb.query(TABLE_FORUMS, new String[] { KEY_ID }, null, null, null, null, null);
|
|
|
|
|
if(c.moveToFirst())
|
|
|
|
|
do{
|
|
|
|
|
list.add(c.getString(0));
|
|
|
|
|
} while(c.moveToNext());
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieves a list of topic ids
|
|
|
|
|
* @return
|
|
|
|
|
* @author ricky barrette
|
|
|
|
|
*/
|
|
|
|
|
public List<String> getTopics(){
|
|
|
|
|
ArrayList<String> list = new ArrayList<String>();
|
|
|
|
|
Cursor c = mDb.query(TABLE_TOPICS, new String[] { KEY_ID }, null, null, null, null, null);
|
|
|
|
|
if(c.moveToFirst())
|
|
|
|
|
do{
|
|
|
|
|
list.add(c.getString(0));
|
|
|
|
|
} while(c.moveToNext());
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieves a list of thread ids
|
|
|
|
|
* @return
|
|
|
|
|
* @author ricky barrette
|
|
|
|
|
*/
|
|
|
|
|
public List<String> getThreads(){
|
|
|
|
|
ArrayList<String> list = new ArrayList<String>();
|
|
|
|
|
Cursor c = mDb.query(TABLE_THREADS, new String[] { KEY_ID }, null, null, null, null, null);
|
|
|
|
|
if(c.moveToFirst())
|
|
|
|
|
do{
|
|
|
|
|
list.add(c.getString(0));
|
|
|
|
|
} while(c.moveToNext());
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieves a forum from the database
|
|
|
|
|
*
|
|
|
|
|
* @param id
|
|
|
|
|
* @return
|
|
|
|
|
* @author ricky barrette
|
|
|
|
|
*/
|
|
|
|
|
public HashMap<String, String> getForum(String id){
|
|
|
|
|
return cursorToHashMap(mDb.query(TABLE_FORUM_INFO, new String[] { KEY, KEY_VALUE }, KEY_FORUM_ID +" = "+ DatabaseUtils.sqlEscapeString(id), null, null, null, null));
|
|
|
|
|
public HashMap<String, String> getForum(final String id) {
|
|
|
|
|
return cursorToHashMap(mDb.query(TABLE_FORUM_INFO, new String[] { KEY, KEY_VALUE }, KEY_FORUM_ID + " = " + DatabaseUtils.sqlEscapeString(id), null, null, null,
|
|
|
|
|
null));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieves a topic from the database
|
|
|
|
|
* @param id
|
|
|
|
|
* Retrieves a list of forum ids
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
* @author ricky barrette
|
|
|
|
|
*/
|
|
|
|
|
public HashMap<String, String> getTopic(String id) {
|
|
|
|
|
return cursorToHashMap(mDb.query(TABLE_TOPIC_INFO, new String[] { KEY, KEY_VALUE }, KEY_TOPIC_ID +" = "+ DatabaseUtils.sqlEscapeString(id), null, null, null, null));
|
|
|
|
|
public List<String> getForums() {
|
|
|
|
|
final ArrayList<String> list = new ArrayList<String>();
|
|
|
|
|
final Cursor c = mDb.query(TABLE_FORUMS, new String[] { KEY_ID }, null, null, null, null, null);
|
|
|
|
|
if (c.moveToFirst())
|
|
|
|
|
do
|
|
|
|
|
list.add(c.getString(0));
|
|
|
|
|
while (c.moveToNext());
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieves a post from the threads table of the database
|
|
|
|
|
*
|
|
|
|
|
* @param id
|
|
|
|
|
* @return
|
|
|
|
|
* @author ricky barrette
|
|
|
|
|
*/
|
|
|
|
|
public HashMap<String, String> getThread(String id) {
|
|
|
|
|
return cursorToHashMap(mDb.query(TABLE_THREAD_INFO, new String[] { KEY, KEY_VALUE }, KEY_POST_ID +" = "+ DatabaseUtils.sqlEscapeString(id), null, null, null, null));
|
|
|
|
|
public HashMap<String, String> getThread(final String id) {
|
|
|
|
|
return cursorToHashMap(mDb.query(TABLE_THREAD_INFO, new String[] { KEY, KEY_VALUE }, KEY_POST_ID + " = " + DatabaseUtils.sqlEscapeString(id), null, null, null,
|
|
|
|
|
null));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Converts a cursor with key-value pairs into a hash map
|
|
|
|
|
* @param c
|
|
|
|
|
* Retrieves a list of thread ids
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
* @author ricky barrette
|
|
|
|
|
*/
|
|
|
|
|
private HashMap<String, String> cursorToHashMap(Cursor c){
|
|
|
|
|
HashMap<String, String> h = new HashMap<String, String>();
|
|
|
|
|
if(c.moveToFirst())
|
|
|
|
|
do{
|
|
|
|
|
h.put(c.getString(c.getColumnIndex(KEY)), c.getString(c.getColumnIndex(KEY_VALUE)));
|
|
|
|
|
} while(c.moveToNext());
|
|
|
|
|
return h;
|
|
|
|
|
public List<String> getThreads() {
|
|
|
|
|
final ArrayList<String> list = new ArrayList<String>();
|
|
|
|
|
final Cursor c = mDb.query(TABLE_THREADS, new String[] { KEY_ID }, null, null, null, null, null);
|
|
|
|
|
if (c.moveToFirst())
|
|
|
|
|
do
|
|
|
|
|
list.add(c.getString(0));
|
|
|
|
|
while (c.moveToNext());
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieves a topic from the database
|
|
|
|
|
*
|
|
|
|
|
* @param id
|
|
|
|
|
* @return
|
|
|
|
|
* @author ricky barrette
|
|
|
|
|
*/
|
|
|
|
|
public HashMap<String, String> getTopic(final String id) {
|
|
|
|
|
return cursorToHashMap(mDb.query(TABLE_TOPIC_INFO, new String[] { KEY, KEY_VALUE }, KEY_TOPIC_ID + " = " + DatabaseUtils.sqlEscapeString(id), null, null, null,
|
|
|
|
|
null));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieves a list of topic ids
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
* @author ricky barrette
|
|
|
|
|
*/
|
|
|
|
|
public List<String> getTopics() {
|
|
|
|
|
final ArrayList<String> list = new ArrayList<String>();
|
|
|
|
|
final Cursor c = mDb.query(TABLE_TOPICS, new String[] { KEY_ID }, null, null, null, null, null);
|
|
|
|
|
if (c.moveToFirst())
|
|
|
|
|
do
|
|
|
|
|
list.add(c.getString(0));
|
|
|
|
|
while (c.moveToNext());
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Restores the database from the sdcard
|
|
|
|
|
*
|
|
|
|
|
@@ -504,7 +515,7 @@ public class ForumDatabase {
|
|
|
|
|
/*
|
|
|
|
|
* save the forums
|
|
|
|
|
*/
|
|
|
|
|
if (result != null){
|
|
|
|
|
if (result != null) {
|
|
|
|
|
String id = null;
|
|
|
|
|
for (final Object element : result) {
|
|
|
|
|
final HashMap<?, ?> contentHash = (HashMap<?, ?>) element;
|
|
|
|
|
@@ -577,14 +588,15 @@ public class ForumDatabase {
|
|
|
|
|
private void upsertForumTable(final String forumId, final List<ContentValues> values) throws NullPointerException {
|
|
|
|
|
if (values == null)
|
|
|
|
|
throw new NullPointerException("values was null");
|
|
|
|
|
|
|
|
|
|
ContentValues id = new ContentValues();
|
|
|
|
|
|
|
|
|
|
final ContentValues id = new ContentValues();
|
|
|
|
|
id.put(KEY_ID, forumId);
|
|
|
|
|
upsertTable(TABLE_FORUMS, KEY_ID +" = "+ DatabaseUtils.sqlEscapeString(forumId), id);
|
|
|
|
|
upsertTable(TABLE_FORUMS, KEY_ID + " = " + DatabaseUtils.sqlEscapeString(forumId), id);
|
|
|
|
|
|
|
|
|
|
for (final ContentValues item : values) {
|
|
|
|
|
item.put(KEY_FORUM_ID, forumId);
|
|
|
|
|
upsertTable(TABLE_FORUM_INFO, KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY) + " AND " + KEY_FORUM_ID + " = " + DatabaseUtils.sqlEscapeString(forumId)), item);
|
|
|
|
|
upsertTable(TABLE_FORUM_INFO,
|
|
|
|
|
KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY) + " AND " + KEY_FORUM_ID + " = " + DatabaseUtils.sqlEscapeString(forumId)), item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -615,16 +627,17 @@ public class ForumDatabase {
|
|
|
|
|
private void upsertThreadTable(final String forumId, final String topicId, final String postId, final List<ContentValues> values) throws NullPointerException {
|
|
|
|
|
if (values == null)
|
|
|
|
|
throw new NullPointerException("values was null");
|
|
|
|
|
|
|
|
|
|
ContentValues id = new ContentValues();
|
|
|
|
|
|
|
|
|
|
final ContentValues id = new ContentValues();
|
|
|
|
|
id.put(KEY_ID, postId);
|
|
|
|
|
id.put(KEY_FORUM_ID, forumId);
|
|
|
|
|
id.put(KEY_TOPIC_ID, topicId);
|
|
|
|
|
upsertTable(TABLE_THREADS, KEY_ID +" = "+ DatabaseUtils.sqlEscapeString(postId), id);
|
|
|
|
|
upsertTable(TABLE_THREADS, KEY_ID + " = " + DatabaseUtils.sqlEscapeString(postId), id);
|
|
|
|
|
|
|
|
|
|
for (final ContentValues item : values) {
|
|
|
|
|
item.put(KEY_POST_ID, postId);
|
|
|
|
|
upsertTable( TABLE_THREAD_INFO, KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY)) + " AND " + KEY_POST_ID + " = " + DatabaseUtils.sqlEscapeString(postId), item);
|
|
|
|
|
upsertTable(TABLE_THREAD_INFO,
|
|
|
|
|
KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY)) + " AND " + KEY_POST_ID + " = " + DatabaseUtils.sqlEscapeString(postId), item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -640,11 +653,11 @@ public class ForumDatabase {
|
|
|
|
|
private void upsertTopicTable(final String forumId, final String topicId, final List<ContentValues> values) throws NullPointerException {
|
|
|
|
|
if (values == null)
|
|
|
|
|
throw new NullPointerException("values was null");
|
|
|
|
|
|
|
|
|
|
ContentValues id = new ContentValues();
|
|
|
|
|
|
|
|
|
|
final ContentValues id = new ContentValues();
|
|
|
|
|
id.put(KEY_ID, topicId);
|
|
|
|
|
id.put(KEY_FORUM_ID, forumId);
|
|
|
|
|
upsertTable(TABLE_TOPICS, KEY_ID +" = "+ DatabaseUtils.sqlEscapeString(topicId), id);
|
|
|
|
|
upsertTable(TABLE_TOPICS, KEY_ID + " = " + DatabaseUtils.sqlEscapeString(topicId), id);
|
|
|
|
|
|
|
|
|
|
for (final ContentValues item : values) {
|
|
|
|
|
item.put(KEY_TOPIC_ID, topicId);
|
|
|
|
|
|