//This Software is distributed under The Apache License, Version 2.0
//The License is available at http://www.apache.org/licenses/LICENSE-2.0
package com.gmail.emerssso.srbase.database;

import android.app.ProgressDialog;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.AsyncTask;

// TODO: Auto-generated Javadoc
/**
 * The Class SRTableHelper.
 * @author Conner Kasten
 */
public class SRDatabaseHelper extends SQLiteOpenHelper {
	
	/** The Constant DATABASE_NAME. */
	private static final String DATABASE_NAME = "SRtable.db";
	
	/** The Constant DATABASE_VERSION. */
	private static final int DATABASE_VERSION = 2;
	
	private Context ctx;
	private SQLiteDatabase database;
	private int oldVersion;
	private int newVersion;
	
	
	/**
	 * Instantiates a new sR table helper.
	 *
	 * @param context the context
	 */
	public SRDatabaseHelper(Context context){
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		ctx = context;
	}

	/* (non-Javadoc)
	 * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
	 */
	@Override
	public void onCreate(SQLiteDatabase database) {
		SRTable.onCreate(database);
		DailyTable.onCreate(database);
		PartTable.onCreate(database);
	}

	/* (non-Javadoc)
	 * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
	 */
	@Override
	public void onUpgrade(SQLiteDatabase database, 
			int oldVersion, int newVersion) {
		this.database = database;
		this.oldVersion = oldVersion;
		this.newVersion = newVersion;
		new UpgradeAsyncTask().execute();
	}
	
	private void callUpgrades() {
		SRTable.onUpgrade(database, oldVersion, newVersion);
		DailyTable.onUpgrade(database, oldVersion, newVersion);
		PartTable.onUpgrade(database, oldVersion, newVersion);
	}

	class UpgradeAsyncTask extends AsyncTask<Void, Void, Void> {

	    private ProgressDialog progressDialog;

	    @Override
	    protected void onPreExecute() {
	        progressDialog = ProgressDialog.show(ctx, "Upgrading Your Database", 
	        		"Please wait a moment while your data is migrated", 
	        		true, false);
	    }

	    @Override
	    protected Void doInBackground(Void... params) {        
	        callUpgrades();
	        return null;
	    }

	    @Override
	    protected void onPostExecute(Void result) {
	        progressDialog.dismiss();
	    }
	}
	
}

@@ -184,43 +184,8 @@
 e;%0A%0A
-import android.app.ProgressDialog;%0A
 impo
@@ -212,16 +212,16 @@
 ontext;%0A
+
 import a
@@ -311,37 +311,8 @@
 per;
-%0Aimport android.os.AsyncTask;
 %0A%0A//
@@ -651,118 +651,8 @@
 ;%0A%09%0A
-%09private Context ctx;%0A%09private SQLiteDatabase database;%0A%09private int oldVersion;%0A%09private int newVersion;%0A%09%0A%09%0A
 %09/**
@@ -836,25 +836,8 @@
 N);%0A
-%09%09ctx = context;%0A
 %09%7D%0A%0A
@@ -1351,172 +1351,8 @@
 ) %7B%0A
-%09%09this.database = database;%0A%09%09this.oldVersion = oldVersion;%0A%09%09this.newVersion = newVersion;%0A%09%09new UpgradeAsyncTask().execute();%0A%09%7D%0A%09%0A%09private void callUpgrades() %7B%0A
 %09%09SR
@@ -1525,584 +1525,6 @@
 %09%7D%0A%0A
-%09class UpgradeAsyncTask extends AsyncTask%3CVoid, Void, Void%3E %7B%0A%0A%09    private ProgressDialog progressDialog;%0A%0A%09    @Override%0A%09    protected void onPreExecute() %7B%0A%09        progressDialog = ProgressDialog.show(ctx, %22Upgrading Your Database%22, %0A%09        %09%09%22Please wait a moment while your data is migrated%22, %0A%09        %09%09true, false);%0A%09    %7D%0A%0A%09    @Override%0A%09    protected Void doInBackground(Void... params) %7B        %0A%09        callUpgrades();%0A%09        return null;%0A%09    %7D%0A%0A%09    @Override%0A%09    protected void onPostExecute(Void result) %7B%0A%09        progressDialog.dismiss();%0A%09    %7D%0A%09%7D%0A%09%0A
 %7D%0A

Revert "Show dialog while upgrading Database"