package org.tndata.android.compass.database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.NonNull;
import android.util.Log;


/**
 * A generic class for table handlers containing utilities.
 *
 * @author Ismael Alonso
 * @version 1.1.0
 */
abstract class CompassTableHandler{
    private static final String TAG = "CompassTableHandler";


    private static CompassDbHelper sDbHelper = null;
    private static SQLiteDatabase sDatabase = null;
    private static int sOpenConnections = 0;


    protected void init(Context context){
        Log.i(TAG, "Initializing database connection...");
        if (sDbHelper == null){
            Log.i(TAG, "No preexisting connection found, creating...");
            sDbHelper = new CompassDbHelper(context);
            sDatabase = sDbHelper.getWritableDatabase();
            sOpenConnections = 0;
        }
        sOpenConnections++;
        Log.i(TAG, "Connection initialized, that makes " + sOpenConnections + ".");
    }

    @NonNull
    protected SQLiteDatabase getDatabase(){
        if (sOpenConnections == 0){
            throw new IllegalStateException("The handler needs to be initialized before used");
        }
        return sDatabase;
    }

    protected int getInt(Cursor cursor, String columnName){
        return cursor.getInt(cursor.getColumnIndex(columnName));
    }

    protected long getLong(Cursor cursor, String columnName){
        return cursor.getLong(cursor.getColumnIndex(columnName));
    }

    protected String getString(Cursor cursor, String columnName){
        return cursor.getString(cursor.getColumnIndex(columnName));
    }

    protected boolean getBoolean(Cursor cursor, String columnName){
        return cursor.getInt(cursor.getColumnIndex(columnName)) != 0;
    }

    public void close(){
        Log.i(TAG, "Closing database connection...");
        if (--sOpenConnections == 0){
            Log.i(TAG, "No connections remaining, closing database and helper.");
            sDatabase.close();
            sDatabase = null;
            sDbHelper.close();
            sDbHelper = null;
        }
        else{
            Log.i(TAG, sOpenConnections + " remaining.");
        }
    }
}

@@ -231,63 +231,80 @@
 %0A * 
-A generic class for table handlers containing utilities
+Superclass of all table handlers. Manages the connection to the database
 .%0A *
@@ -598,24 +598,180 @@
 ions = 0;%0A%0A%0A
+    /**%0A     * Initializes a connection to the database. This step is absolutely required.%0A     *%0A     * @param context a reference to the context.%0A     */%0A
     protecte
@@ -1233,24 +1233,157 @@
 .%22);%0A    %7D%0A%0A
+    /**%0A     * Returns an instance of the database connection.%0A     *%0A     * @return an instance of the database connection.%0A     */%0A
     @NonNull
@@ -1610,581 +1610,1983 @@
     
-protected int getInt(Cursor cursor, String columnName)%7B%0A        return cursor.getInt(cursor.getColumnIndex(columnName));%0A    %7D%0A%0A    protected long getLong(Cursor cursor, String columnName)%7B%0A        return cursor.getLong(cursor.getColumnIndex(columnName));%0A    %7D%0A%0A    protected String getString(Cursor cursor, String columnName)%7B%0A        return cursor.getString(cursor.getColumnIndex(columnName));%0A    %7D%0A%0A    protected boolean getBoolean(Cursor cursor, String columnName)%7B%0A        return cursor.getInt(cursor.getColumnIndex(columnName)) != 0;%0A    %7D%0A%0A    public void close()%7B
+/**%0A     * Gets the int in the requested column from the provided cursor.%0A     *%0A     * @param cursor the cursor from where the data should be extracted.%0A     * @param columnName the name of the column storing the int.%0A     * @return an int.%0A     */%0A    protected int getInt(Cursor cursor, String columnName)%7B%0A        return cursor.getInt(cursor.getColumnIndex(columnName));%0A    %7D%0A%0A    /**%0A     * Gets the long in the requested column from the provided cursor.%0A     *%0A     * @param cursor the cursor from where the data should be extracted.%0A     * @param columnName the name of the column storing the long.%0A     * @return a long.%0A     */%0A    protected long getLong(Cursor cursor, String columnName)%7B%0A        return cursor.getLong(cursor.getColumnIndex(columnName));%0A    %7D%0A%0A    /**%0A     * Gets the String in the requested column from the provided cursor.%0A     *%0A     * @param cursor the cursor from where the data should be extracted.%0A     * @param columnName the name of the column storing the String.%0A     * @return a String.%0A     */%0A    protected String getString(Cursor cursor, String columnName)%7B%0A        return cursor.getString(cursor.getColumnIndex(columnName));%0A    %7D%0A%0A    /**%0A     * Gets the boolean in the requested column from the provided cursor.%0A     *%0A     * @param cursor the cursor from where the data should be extracted.%0A     * @param columnName the name of the column storing the boolean.%0A     * @return a boolean.%0A     */%0A    protected boolean getBoolean(Cursor cursor, String columnName)%7B%0A        return cursor.getInt(cursor.getColumnIndex(columnName)) != 0;%0A    %7D%0A%0A    /**%0A     * Closes a connection to the database%0A     */%0A    public void close()%7B%0A        //If the number of open connections is already zero, then something went south%0A        //  wherever this class' children are used, this is an illegal state.%0A        if (sOpenConnections == 0)%7B%0A            throw new IllegalStateException(%22There aren't any connections currently opened%22);%0A        %7D%0A
 %0A   

Throw an exception when trying to close a non-existing connection, add docs