FileDocCategorySizeDatePackage
BordeauxSessionStorage.javaAPI DocAndroid 5.1 API6015Thu Mar 12 22:22:48 GMT 2015android.bordeaux.services

BordeauxSessionStorage

public class BordeauxSessionStorage extends Object

Fields Summary
private static final String
TAG
public static final String
COLUMN_KEY
public static final String
COLUMN_CLASS
public static final String
COLUMN_MODEL
public static final String
COLUMN_TIME
private static final String
DATABASE_NAME
private static final String
SESSION_TABLE
private static final int
DATABASE_VERSION
private static final String
DATABASE_CREATE
private SessionDBHelper
mDbHelper
private android.database.sqlite.SQLiteDatabase
mDbSessions
Constructors Summary
BordeauxSessionStorage(android.content.Context context)


       
        try {
            mDbHelper = new SessionDBHelper(context);
            mDbSessions = mDbHelper.getWritableDatabase();
        } catch (SQLException e) {
            throw new RuntimeException("Can't open session database");
        }
    
Methods Summary
private android.content.ContentValuescreateSessionEntry(java.lang.String key, java.lang.Class learner, byte[] model)

        ContentValues entry = new ContentValues();
        entry.put(COLUMN_KEY, key);
        entry.put(COLUMN_TIME, System.currentTimeMillis());
        entry.put(COLUMN_MODEL, model);
        entry.put(COLUMN_CLASS, learner.getName());
        return entry;
    
voidgetAllSessions(java.util.concurrent.ConcurrentHashMap sessions)

        Cursor cursor = mDbSessions.rawQuery("select * from ?;", new String[]{SESSION_TABLE});
        if (cursor == null) return;
        cursor.moveToFirst();
        do {
            String key = cursor.getString(cursor.getColumnIndex(COLUMN_KEY));
            BordeauxSessionManager.Session session = getSessionFromCursor(cursor);
            sessions.put(key, session);
        } while (cursor.moveToNext());
    
BordeauxSessionManager.SessiongetSession(java.lang.String key)

        Cursor cursor = mDbSessions.query(true, SESSION_TABLE,
                new String[]{COLUMN_KEY, COLUMN_CLASS, COLUMN_MODEL, COLUMN_TIME},
                COLUMN_KEY + "=\"" + key + "\"", null, null, null, null, null);
        if ((cursor == null) | (cursor.getCount() == 0)) {
            cursor.close();
            return null;
        }
        if (cursor.getCount() > 1) {
            cursor.close();
            throw new RuntimeException("Unexpected duplication in session table for key:" + key);
        }
        cursor.moveToFirst();
        BordeauxSessionManager.Session s = getSessionFromCursor(cursor);
        cursor.close();
        return s;
    
private BordeauxSessionManager.SessiongetSessionFromCursor(android.database.Cursor cursor)

        BordeauxSessionManager.Session session = new BordeauxSessionManager.Session();
        String className = cursor.getString(cursor.getColumnIndex(COLUMN_CLASS));
        try {
            session.learnerClass = Class.forName(className);
            session.learner = (IBordeauxLearner) session.learnerClass.getConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException("Can't instantiate class: " + className);
        }
        byte[] model = cursor.getBlob(cursor.getColumnIndex(COLUMN_MODEL));
        session.learner.setModel(model);
        return session;
    
intremoveSessions(java.lang.String reKey)

        int nDeleteRows = mDbSessions.delete(SESSION_TABLE, "? like \"?\"",
                                             new String[]{COLUMN_KEY, reKey});
        Log.i(TAG, "Number of rows in session table deleted: " + nDeleteRows);
        return nDeleteRows;
    
booleansaveSession(java.lang.String key, java.lang.Class learner, byte[] model)

        ContentValues content = createSessionEntry(key, learner, model);
        long rowID =
                mDbSessions.insertWithOnConflict(SESSION_TABLE, null, content,
                                                 SQLiteDatabase.CONFLICT_REPLACE);
        return rowID >= 0;