/**
 * 
 */
package org.commcare.android.database.global;

import net.sqlcipher.database.SQLiteDatabase;

import org.commcare.android.database.ConcreteDbHelper;
import org.commcare.android.database.DbUtil;
import org.commcare.android.database.SqlStorage;
import org.commcare.android.database.global.models.ApplicationRecord;
import org.commcare.android.database.global.models.ApplicationRecordV1;
import org.javarosa.core.services.storage.Persistable;

import android.content.Context;

/**
 * @author ctsims
 *
 */
public class GlobalDatabaseUpgrader {
    private Context c;
    
    public GlobalDatabaseUpgrader(Context c) {
        this.c = c;
    }

    public void upgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if(oldVersion == 1) {
            if(upgradeOneTwo(db, oldVersion, newVersion)) {
                oldVersion = 2;
            }
        }
        if (oldVersion == 2) {
            if(upgradeTwoThree(db, oldVersion, newVersion)) {
                oldVersion = 3;
            }
        }
    }

    private boolean upgradeOneTwo(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.beginTransaction();
        try {
            DbUtil.createNumbersTable(db);
            db.setTransactionSuccessful();
            return true;
        } finally {
            db.endTransaction();
        }
    }
    
    private boolean upgradeTwoThree(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.beginTransaction();
        try {
            SqlStorage<Persistable> storage = new SqlStorage<Persistable>("app_record", ApplicationRecordV1.class, new ConcreteDbHelper(c, db));
            for (Persistable r : storage) {
                ApplicationRecordV1 oldRecord = (ApplicationRecordV1) r;
                ApplicationRecord newRecord = new ApplicationRecord(oldRecord.getApplicationId(), oldRecord.getStatus());
                //set this new record to have same ID as the old one
                newRecord.setID(oldRecord.getID());
                //set default values for the new fields
                newRecord.setResourcesStatus(true);
                newRecord.setArchiveStatus(false);
                newRecord.setUniqueId("");
                newRecord.setDisplayName("");
                newRecord.setConvertedByDbUpgrader(true);
                newRecord.setPreMultipleAppsProfile(true);
                storage.write(newRecord);
            }
            db.setTransactionSuccessful();
            return true;
        } finally {
            db.endTransaction();
        }
    }
    
    
}

@@ -1,15 +1,14 @@
 /**%0A *
- 
 %0A */%0Apac
@@ -568,20 +568,16 @@
 text c;%0A
-    
 %0A    pub
@@ -1330,28 +1330,162 @@
     %7D%0A    %7D%0A
+%0A
     
+/**%0A     * Migrate from database V2 to V3, where the ApplicationRecord has%0A     * new fields for multiple app seating support%0A     */
 %0A    private
@@ -1652,16 +1652,36 @@
 torage =
+%0A                   
  new Sql
@@ -1934,16 +1934,40 @@
 Record =
+%0A                       
  new App
@@ -2718,16 +2718,6 @@
   %7D%0A
-    %0A    %0A
 %7D%0A

Add comment to DB migration method