@@ -0,0 +1,3677 @@
+package com.expidev.gcmapp.db;%0A%0Aimport android.content.ContentValues;%0Aimport android.content.Context;%0Aimport android.database.Cursor;%0Aimport android.database.sqlite.SQLiteDatabase;%0Aimport android.database.sqlite.SQLiteOpenHelper;%0Aimport android.util.Log;%0A%0Aimport com.expidev.gcmapp.model.User;%0Aimport com.expidev.gcmapp.sql.TableNames;%0Aimport com.expidev.gcmapp.utils.DatabaseOpenHelper;%0A%0Aimport org.json.JSONObject;%0A%0A/**%0A * Created by matthewfrederick on 1/23/15.%0A */%0Apublic class UserDao%0A%7B%0A    private final String TAG = getClass().getSimpleName();%0A    %0A    private final SQLiteOpenHelper databaseHelper;%0A    %0A    private static final Object instanceLock = new Object();%0A    private static UserDao instance;%0A    %0A    private UserDao(final Context context)%0A    %7B%0A        this.databaseHelper = new DatabaseOpenHelper(context);        %0A    %7D%0A    %0A    public static UserDao getInstance(Context context)%0A    %7B%0A        if (instance == null)%0A        %7B%0A            synchronized (instanceLock)%0A            %7B%0A                instance = new UserDao(context.getApplicationContext());%0A            %7D%0A        %7D%0A        %0A        return instance;%0A    %7D%0A    %0A    public Cursor retrieveUserCursor()%0A    %7B%0A        final SQLiteDatabase database = databaseHelper.getReadableDatabase();%0A        %0A        try%0A        %7B%0A            return database.query(TableNames.USER.getTableName(), null, null, null, null, null, null);%0A        %7D%0A        catch (Exception e)%0A        %7B%0A            Log.e(TAG, %22Failed to retrieve user: %22 + e.getMessage(), e);%0A        %7D%0A        %0A        return null;%0A    %7D%0A    %0A    public User retrieveUser()%0A    %7B%0A        Cursor cursor = null;%0A        User user = new User();%0A        try%0A        %7B%0A            cursor = retrieveUserCursor();%0A            %0A            if (cursor != null && cursor.getCount() == 1)%0A            %7B%0A                cursor.moveToFirst();%0A                user.setFirstName(cursor.getString(cursor.getColumnIndex(%22first_name%22)));%0A                user.setLastName(cursor.getString(cursor.getColumnIndex(%22last_name%22)));%0A                user.setCasUsername(cursor.getString(cursor.getColumnIndex(%22cas_username%22)));%0A                user.setPersonId(cursor.getString(cursor.getColumnIndex(%22person_id%22)));%0A                %0A                return user;%0A            %7D%0A        %7D%0A        finally%0A        %7B%0A            if (cursor != null)%0A            %7B%0A                cursor.close();%0A            %7D%0A        %7D%0A        %0A        return null;%0A    %7D%0A    %0A    public void saveUser(JSONObject jsonObject)%0A    %7B%0A        final SQLiteDatabase database = databaseHelper.getWritableDatabase();%0A        try%0A        %7B%0A            String userTable = TableNames.USER.getTableName();%0A%0A            ContentValues userToInsert = new ContentValues();%0A            userToInsert.put(%22first_name%22, jsonObject.getString(%22first_name%22));%0A            userToInsert.put(%22last_name%22, jsonObject.getString(%22last_name%22));%0A            userToInsert.put(%22cas_username%22, jsonObject.getString(%22cas_username%22));%0A            userToInsert.put(%22person_id%22, jsonObject.getString(%22person_id%22));%0A            %0A            database.beginTransaction();%0A            %0A            User existingUser = retrieveUser();%0A            %0A            if (existingUser == null)%0A            %7B%0A                database.insert(userTable, null, userToInsert);%0A            %7D%0A            else%0A            %7B%0A                database.update(userTable, userToInsert, null, null);%0A            %7D%0A            %0A            database.setTransactionSuccessful();%0A        %7D%0A        catch (Exception e)%0A        %7B%0A            Log.e(TAG, e.getMessage(), e);%0A        %7D%0A        finally%0A        %7B%0A            database.endTransaction();%0A        %7D%0A    %7D%0A%7D%0A

save user to db