package com.gaiagps.iburn.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.gaiagps.iburn.PlayaClient;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;


/**
 *
 * This class handles bootstrapping the application database
 * from a pre-populated SQL database stored in assets.
 *
 * To test bootstrapping the application database from JSON, comment
 * out the entirety of this class.
 *
 * @author davidbrodsky
 * @description SQLiteWrapper is written to be application agnostic.
 * requires Strings: DATABASE_NAME, DATABASE_VERSION,
 * CREATE_TABLE_STATEMENT, TABLE_NAME
 */
public class DBWrapper extends SQLiteAssetHelper {

    private static final String DATABASE_NAME = "playaDatabase.db";
    private static final int DATABASE_VERSION = PlayaClient.DATABASE_VERSION;

    static Context c;

    public DBWrapper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        c = context;
    }

    /**
     * Invoked if a DB upgrade (version change) has been detected
     */
    @Override
    public void onUpgrade(SQLiteDatabase db,
       int oldVersion, int newVersion) {
        // Drop old table and re-create
    	db.execSQL("DROP TABLE IF EXISTS " + PlayaDatabase.CAMPS);
    	db.execSQL("DROP TABLE IF EXISTS " + PlayaDatabase.EVENTS);
    	db.execSQL("DROP TABLE IF EXISTS " + PlayaDatabase.ART);
		onCreate(db);
    }
}
@@ -58,18 +58,88 @@
 nt.Conte
-xt
+ntValues;%0Aimport android.content.Context;%0Aimport android.database.Cursor
 ;%0Aimport
@@ -280,16 +280,45 @@
 elper;%0A%0A
+import java.util.ArrayList;%0A%0A
 %0A/**%0A *%0A
@@ -1271,53 +1271,1134 @@
 ) %7B%0A
-        // Drop old table and re-create%0A    %09
+%0A        if (oldVersion == 2 && newVersion == 3) %7B%0A            // Add UserPoiTable%0A            // Restore all favorites from camps, art, events%0A            ArrayList%3CInteger%3E favoriteArtPlayaIds          = new ArrayList%3C%3E();%0A            recordAllFavoriteIds(db, PlayaDatabase.ART,     favoriteArtPlayaIds);%0A%0A            ArrayList%3CInteger%3E favoriteCampPlayaIds         = new ArrayList%3C%3E();%0A            recordAllFavoriteIds(db, PlayaDatabase.CAMPS,   favoriteCampPlayaIds);%0A%0A            ArrayList%3CInteger%3E favoriteEventPlayaIds        = new ArrayList%3C%3E();%0A            recordAllFavoriteIds(db, PlayaDatabase.EVENTS,  favoriteEventPlayaIds);%0A%0A            recreateAllTables(db);%0A%0A            db.beginTransaction();%0A            commitAllFavorites(db, PlayaDatabase.ART,       favoriteArtPlayaIds);%0A            commitAllFavorites(db, PlayaDatabase.CAMPS,     favoriteCampPlayaIds);%0A            commitAllFavorites(db, PlayaDatabase.EVENTS,    favoriteEventPlayaIds);%0A            db.endTransaction();%0A        %7D else %7B%0A            recreateAllTables(db);%0A        %7D%0A%0A    %7D%0A%0A    private void recreateAllTables(SQLiteDatabase db) %7B%0A        
 db.e
@@ -2448,33 +2448,36 @@
 ase.CAMPS);%0A    
-%09
+    
 db.execSQL(%22DROP
@@ -2524,17 +2524,20 @@
 S);%0A    
-%09
+    
 db.execS
@@ -2589,10 +2589,16 @@
 T);%0A
-%09%09
+        
 onCr
@@ -2612,10 +2612,939 @@
 );%0A    %7D
+%0A%0A    private void recordAllFavoriteIds(SQLiteDatabase db, String table, ArrayList%3CInteger%3E favoritePlayaIds) %7B%0A        Cursor favoriteArt = db.query(table, new String%5B%5D %7B PlayaItemTable.playaId %7D, PlayaItemTable.favorite + %22 = ?%22, new String%5B%5D %7B%221%22%7D, null, null, null);%0A        if (favoriteArt != null) %7B%0A            while (favoriteArt.moveToNext()) %7B%0A                favoritePlayaIds.add(favoriteArt.getInt(favoriteArt.getColumnIndex(PlayaItemTable.playaId)));%0A            %7D%0A            favoriteArt.close();%0A        %7D%0A    %7D%0A%0A    private void commitAllFavorites(SQLiteDatabase db, String table, ArrayList%3CInteger%3E favoritePlayaIds) %7B%0A        ContentValues values = new ContentValues();%0A        values.put(PlayaItemTable.favorite, 1);%0A        for (Integer favoritePlayaId : favoritePlayaIds) %7B%0A            db.update(table, values, PlayaItemTable.playaId + %22 = ?%22, new String%5B%5D %7B String.valueOf(favoritePlayaId)%7D);%0A        %7D%0A    %7D
 %0A%7D

Add database migration for version 2 to 3