package com.tierep.notificationanalyser.Models;

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

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

import java.sql.SQLException;

/**
 * Database helper class used to manage the creation and upgrading of your database. This class also provides
 * the DAOs used by the other classes.
 *
 * Created by pieter on 21/09/14.
 */
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
    private static final String DATABASE_NAME = "notifications.db";

    private static final int DATABASE_VERSION = 5;

    private ApplicationDao applicationDao = null;
    private NotificationItemDao notificationDao = null;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    /**
     * This is called when the database is first created. This call createTable statements here to
     * create the tables that will store your data.
     */
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            TableUtils.createTable(connectionSource, NotificationItem.class);
            TableUtils.createTable(connectionSource, Application.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i2) {
        // TODO update mechanisme van de database verbeteren
        try {
            TableUtils.dropTable(connectionSource, NotificationItem.class, true);
            TableUtils.dropTable(connectionSource, Application.class, true);
            onCreate(sqLiteDatabase, connectionSource);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * Returns the Database Access Object (DAO) for Application class. It will create it or
     * just give the cached value.
     */
    public ApplicationDao getApplicationDao() throws SQLException {
        if (applicationDao == null) {
            applicationDao = getDao(Application.class);
        }
        return applicationDao;
    }

    /**
     * Returns the Database Access Object (DAO) for NotificationItem class. It will create it or
     * just give the cached value.
     */
    public NotificationItemDao getNotificationDao() throws SQLException {
        if (notificationDao == null) {
            notificationDao = getDao(NotificationItem.class);
        }
        return notificationDao;
    }

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

@@ -467,16 +467,21 @@
 sses.%0A *
+ %3Cp/%3E
 %0A * Crea
@@ -690,17 +690,17 @@
 RSION = 
-5
+1
 ;%0A%0A    p
@@ -1554,342 +1554,151 @@
 int 
-i, int i2) %7B%0A        // TODO update mechanisme van de database verbeteren%0A        try %7B%0A            TableUtils.dropTable(connectionSource, NotificationItem.class, true);%0A            TableUtils.dropTable(connectionSource, Application.class, true);%0A            onCreate(sqLiteDatabase, connectionSource);%0A        %7D catch (SQLExcept
+oldVersion, int newVersion) %7B%0A        if (oldVersion %3C 2) %7B%0A            // Do changes from version 1 to 2%0A        %7D%0A%0A        if (oldVers
 ion 
-e
+%3C 3
 ) %7B%0A
@@ -1709,36 +1709,49 @@
         
-e.printStackTrace();
+// Do changes from version 2 to 3
 %0A       

Upgrade mechanisme voor db is nu beter voorbereid.