package com.humbughq.mobile;

import java.sql.SQLException;

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

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

/**
 * Database helper class used to manage the creation and upgrading of your
 * database. This class also usually provides the DAOs used by the other
 * classes.
 */
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

    // name of the database file for your application -- change to something
    // appropriate for your app
    private static final String DATABASE_NAME = "helloAndroid.db";
    // any time you make changes to your database objects, you may have to
    // increase the database version
    private static final int DATABASE_VERSION = 1;

    // the DAO object we use to access the SimpleData table
    private Dao<SimpleData, Integer> simpleDao = null;
    private RuntimeExceptionDao<SimpleData, Integer> simpleRuntimeDao = null;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION,
                R.raw.ormlite_config);
    }

    /**
     * This is called when the database is first created. Usually you should
     * call createTable statements here to create the tables that will store
     * your data.
     */
    @Override
    public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
        try {
            Log.i(DatabaseHelper.class.getName(), "onCreate");
            TableUtils.createTable(connectionSource, SimpleData.class);
        } catch (SQLException e) {
            Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
            throw new RuntimeException(e);
        }

        // here we try inserting data in the on-create as a test
        RuntimeExceptionDao<SimpleData, Integer> dao = getSimpleDataDao();
        long millis = System.currentTimeMillis();
        // create some entries in the onCreate
        SimpleData simple = new SimpleData(millis);
        dao.create(simple);
        simple = new SimpleData(millis + 1);
        dao.create(simple);
        Log.i(DatabaseHelper.class.getName(),
                "created new entries in onCreate: " + millis);
    }

    /**
     * This is called when your application is upgraded and it has a higher
     * version number. This allows you to adjust the various data to match the
     * new version number.
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
            int oldVersion, int newVersion) {
        try {
            Log.i(DatabaseHelper.class.getName(), "onUpgrade");
            TableUtils.dropTable(connectionSource, SimpleData.class, true);
            // after we drop the old databases, we create the new ones
            onCreate(db, connectionSource);
        } catch (SQLException e) {
            Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * Returns the Database Access Object (DAO) for our SimpleData class. It
     * will create it or just give the cached value.
     */
    public Dao<SimpleData, Integer> getDao() throws SQLException {
        if (simpleDao == null) {
            simpleDao = getDao(SimpleData.class);
        }
        return simpleDao;
    }

    /**
     * Returns the RuntimeExceptionDao (Database Access Object) version of a Dao
     * for our SimpleData class. It will create it or just give the cached
     * value. RuntimeExceptionDao only through RuntimeExceptions.
     */
    public RuntimeExceptionDao<SimpleData, Integer> getSimpleDataDao() {
        if (simpleRuntimeDao == null) {
            simpleRuntimeDao = getRuntimeExceptionDao(SimpleData.class);
        }
        return simpleRuntimeDao;
    }

    /**
     * Close the database connections and clear any cached DAOs.
     */
    @Override
    public void close() {
        super.close();
        simpleDao = null;
        simpleRuntimeDao = null;
    }
}

@@ -229,90 +229,8 @@
 er;%0A
-import com.j256.ormlite.dao.Dao;%0Aimport com.j256.ormlite.dao.RuntimeExceptionDao;%0A
 impo
@@ -275,16 +275,16 @@
 Source;%0A
+
 import c
@@ -711,20 +711,13 @@
  = %22
-helloAndroid
+zulip
 .db%22
@@ -886,202 +886,8 @@
 1;%0A%0A
-    // the DAO object we use to access the SimpleData table%0A    private Dao%3CSimpleData, Integer%3E simpleDao = null;%0A    private RuntimeExceptionDao%3CSimpleData, Integer%3E simpleRuntimeDao = null;%0A%0A
     
@@ -1411,67 +1411,75 @@
     
-TableUtils.createTable(connectionSource, SimpleData.class);
+// Create the databases we're using%0A            // (none currently)
 %0A   
@@ -1645,508 +1645,8 @@
    %7D
-%0A%0A        // here we try inserting data in the on-create as a test%0A        RuntimeExceptionDao%3CSimpleData, Integer%3E dao = getSimpleDataDao();%0A        long millis = System.currentTimeMillis();%0A        // create some entries in the onCreate%0A        SimpleData simple = new SimpleData(millis);%0A        dao.create(simple);%0A        simple = new SimpleData(millis + 1);%0A        dao.create(simple);%0A        Log.i(DatabaseHelper.class.getName(),%0A                %22created new entries in onCreate: %22 + millis);
 %0A   
@@ -2116,26 +2116,22 @@
 Source, 
-SimpleData
+Person
 .class, 
@@ -2429,821 +2429,8 @@
  %7D%0A%0A
-    /**%0A     * Returns the Database Access Object (DAO) for our SimpleData class. It%0A     * will create it or just give the cached value.%0A     */%0A    public Dao%3CSimpleData, Integer%3E getDao() throws SQLException %7B%0A        if (simpleDao == null) %7B%0A            simpleDao = getDao(SimpleData.class);%0A        %7D%0A        return simpleDao;%0A    %7D%0A%0A    /**%0A     * Returns the RuntimeExceptionDao (Database Access Object) version of a Dao%0A     * for our SimpleData class. It will create it or just give the cached%0A     * value. RuntimeExceptionDao only through RuntimeExceptions.%0A     */%0A    public RuntimeExceptionDao%3CSimpleData, Integer%3E getSimpleDataDao() %7B%0A        if (simpleRuntimeDao == null) %7B%0A            simpleRuntimeDao = getRuntimeExceptionDao(SimpleData.class);%0A        %7D%0A        return simpleRuntimeDao;%0A    %7D%0A%0A
     
@@ -2542,24 +2542,24 @@
 d close() %7B%0A
+
         supe
@@ -2573,67 +2573,8 @@
 ();%0A
-        simpleDao = null;%0A        simpleRuntimeDao = null;%0A
     

Configure DatabaseHelper for use with Zulip.