Advertisement
Guest User

Untitled

a guest
Dec 6th, 2017
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.51 KB | None | 0 0
  1. package com.example.gamingbox.a2101011029.config;
  2. /**
  3.  * Alvin Santiago
  4.  * ID: 101011029
  5.  */
  6.  
  7. import android.content.ContentValues;
  8. import android.content.Context;
  9. import android.database.Cursor;
  10. import android.database.CursorIndexOutOfBoundsException;
  11. import android.database.sqlite.SQLiteDatabase;
  12. import android.database.sqlite.SQLiteOpenHelper;
  13.  
  14. import java.util.ArrayList;
  15. import java.util.List;
  16.  
  17. import com.example.gamingbox.a2101011029.Model.Patient;
  18. import com.example.gamingbox.a2101011029.Model.Test;
  19. import com.example.gamingbox.a2101011029.Model.User;
  20.  
  21.  
  22.  
  23. public class Databasehelper extends SQLiteOpenHelper {
  24.  
  25.  
  26.     // Logcat Tag
  27.     private static final int DATABASE_VERSION = 1;
  28.  
  29.     // Database Name
  30.     private static final String DATABASE_NAME = "AndroidAssignmentTwoDatabase";
  31.  
  32.     // Table Names
  33.     // Authentication Tables
  34.     private static final String TABLE_USER = "users";
  35.     private static final String TABLE_USER_ROLE = "user_role";
  36.     private static final String TABLE_ROLE = "roles";
  37.     private static final String TABLE_ROLE_PERMISSION = "role_permission";
  38.     private static final String TABLE_PERMISSION = "permissions";
  39.     // Object Tables
  40.     private static final String TABLE_PATIENT = "patients";
  41.     private static final String TABLE_TEST = "tests";
  42.  
  43.     // Common Column Names
  44.     private static final String COLUMN_ID = "id";
  45.     private static final String COLUMN_CREATED_AT = "created_at";
  46.     private static final String COLUMN_FIRST_NAME = "first_name";
  47.     private static final String COLUMN_LAST_NAME = "last_name";
  48.     private static final String COLUMN_DEPARTMENT = "department";
  49.  
  50.     // Authentication Column Names
  51.     // User Table - Column Names
  52.     private static final String COLUMN_EMAIL = "email";
  53.     private static final String COLUMN_PASSWORD = "password";
  54.     // User_Role Table - Column Names
  55.     private static final String COLUMN_USER_ID = "user_id";
  56.     private static final String COLUMN_ROLE_ID = "role_id";
  57.     // Role Table - Column Names
  58.     private static final String COLUMN_NAME = "name";
  59.     // Role_Permission Table - Column Names
  60.     private static final String COLUMN_PERMISSION_ID = "permission_id";
  61.     // Permission Table - Column Names
  62.     private static final String COLUMN_ACTION_CODE = "action_code";
  63.  
  64.  
  65.     // PATIENTS Table - Column Names
  66.     private static final String COLUMN_DOCTOR_ID = "doctor_id";
  67.     private static final String COLUMN_ROOM = "room";
  68.  
  69.     // TESTS Table - Column Names
  70.     private static final String COLUMN_PATIENT_ID = "patient_id";
  71.     private static final String COLUMN_BPL = "bpl";
  72.     private static final String COLUMN_BPH = "bph";
  73.     private static final String COLUMN_TEMPERATURE = "temperature";
  74.  
  75.     // Table Create Statements
  76.     // User Table SQL Create Statement
  77.     private static final String CREATE_TABLE_USERS =
  78.             "CREATE TABLE " + TABLE_USER + "("
  79.                     + COLUMN_ID + " INTEGER PRIMARY KEY,"
  80.                     + COLUMN_EMAIL + " TEXT,"
  81.                     + COLUMN_PASSWORD + " TEXT,"
  82.                     + COLUMN_FIRST_NAME + " TEXT,"
  83.                     + COLUMN_LAST_NAME + " TEXT,"
  84.                     + COLUMN_DEPARTMENT + " TEXT);";
  85.  
  86.     // User_Role Table SQL Create Statement
  87.     private static final String CREATE_TABLE_USER_ROLE =
  88.             "CREATE TABLE " + TABLE_USER_ROLE + "("
  89.                     + COLUMN_USER_ID + " INTEGER,"
  90.                     + COLUMN_ROLE_ID + " INTEGER);";
  91.  
  92.     // Role Table SQL Create Statement
  93.     private static final String CREATE_TABLE_ROLES =
  94.             "CREATE TABLE " + TABLE_ROLE + "("
  95.                     + COLUMN_ID + " INTEGER PRIMARY KEY,"
  96.                     + COLUMN_NAME + " TEXT);";
  97.  
  98.     // Role Table SQL Create Statement
  99.     private static final String CREATE_TABLE_ROLE_PERMISSION =
  100.             "CREATE TABLE " + TABLE_ROLE_PERMISSION + "("
  101.                     + COLUMN_ROLE_ID + " INTEGER,"
  102.                     + COLUMN_PERMISSION_ID + " INTEGER);";
  103.  
  104.     // Permission Table SQL Create Statement
  105.     private static final String CREATE_TABLE_PERMISSION =
  106.             "CREATE TABLE " + TABLE_PERMISSION + "("
  107.                     + COLUMN_ID + " INTEGER PRIMARY KEY,"
  108.                     + COLUMN_ACTION_CODE + " TEXT);";
  109.  
  110.     // Patient Table SQL Create Statement
  111.     private static final String CREATE_TABLE_PATIENTS =
  112.             "CREATE TABLE " + TABLE_PATIENT + "("
  113.                     + COLUMN_ID + " INTEGER PRIMARY KEY,"
  114.                     + COLUMN_DOCTOR_ID + " TEXT,"
  115.                     + COLUMN_FIRST_NAME + " TEXT,"
  116.                     + COLUMN_LAST_NAME + " TEXT,"
  117.                     + COLUMN_DEPARTMENT + " TEXT,"
  118.                     + COLUMN_ROOM + " TEXT,"
  119.                     + COLUMN_CREATED_AT + " DATETIME);";
  120.  
  121.     // Test Table SQL Create Statement
  122.     private static final String CREATE_TABLE_TESTS =
  123.             "CREATE TABLE " + TABLE_TEST + "("
  124.                     + COLUMN_ID + " INTEGER PRIMARY KEY,"
  125.                     + COLUMN_PATIENT_ID + " INTEGER,"
  126.                     + COLUMN_BPL + " TEXT,"
  127.                     + COLUMN_BPH + " TEXT,"
  128.                     + COLUMN_TEMPERATURE + " TEXT,"
  129.                     + COLUMN_CREATED_AT + " DATETIME);";
  130.  
  131.  
  132.     // Account Insert Functions
  133.  
  134.     /**
  135.      * Inserts an account into the User table in the local database
  136.      * @param db
  137.      * @param email
  138.      * @param password
  139.      * @return The ID of the created account record
  140.      */
  141.     private long insertUser(SQLiteDatabase db, String email, String password, String firstName, String lastName, String department) {
  142.  
  143.         ContentValues values = new ContentValues();
  144.         values.put(COLUMN_EMAIL, email);
  145.         values.put(COLUMN_PASSWORD, password);
  146.         values.put(COLUMN_FIRST_NAME, firstName);
  147.         values.put(COLUMN_LAST_NAME, lastName);
  148.         values.put(COLUMN_DEPARTMENT, department);
  149.  
  150.         return db.insert(TABLE_USER, null, values);
  151.     }
  152.  
  153.     /**
  154.      * Inserts a Admin Role into the Role table of the device database
  155.      * @param db
  156.      * @param role
  157.      * @return The ID of the created admin role record
  158.      */
  159.     private long insertRole(SQLiteDatabase db, String role) {
  160.         ContentValues values = new ContentValues();
  161.         values.put(COLUMN_NAME, role);
  162.  
  163.         return db.insert(TABLE_ROLE, null, values);
  164.     }
  165.  
  166.     /**
  167.      * Inserts a UserRole association record to associate an existing user with an existing role.
  168.      * @param db
  169.      * @param userID
  170.      * @param roleID
  171.      */
  172.     private void insertUserRole(SQLiteDatabase db, long userID, long roleID) {
  173.  
  174.         ContentValues values = new ContentValues();
  175.         values.put(COLUMN_USER_ID, userID);
  176.         values.put(COLUMN_ROLE_ID, roleID);
  177.  
  178.         db.insert(TABLE_USER_ROLE, null, values);
  179.     }
  180.  
  181.     /**
  182.      * Inserts a Permission record in the Permission table
  183.      * @param db
  184.      * @param actionCode
  185.      * @return The ID of the created Permission record
  186.      */
  187.     private long insertPermission(SQLiteDatabase db, String actionCode) {
  188.         ContentValues values = new ContentValues();
  189.         values.put(COLUMN_ACTION_CODE, actionCode);
  190.  
  191.         return db.insert(TABLE_PERMISSION, null, values);
  192.     }
  193.  
  194.     /**
  195.      * Inserts a RolePermission Record into Role_Permission table to associate an existing role with
  196.      * an existing permission.
  197.      * @param db
  198.      * @param roleID
  199.      * @param permissionID
  200.      */
  201.     private void insertRolePermission(SQLiteDatabase db, long roleID, long permissionID) {
  202.         ContentValues values = new ContentValues();
  203.         values.put(COLUMN_ROLE_ID, roleID);
  204.         values.put(COLUMN_PERMISSION_ID, permissionID);
  205.  
  206.         db.insert(TABLE_ROLE_PERMISSION, null, values);
  207.     }
  208.  
  209.     /**
  210.      * Retrieve User record from the database where the record's
  211.      *   password = @param password
  212.      *   AND
  213.      *   email = @param email
  214.      * @param email
  215.      * @param password
  216.      * @return
  217.      */
  218.     public User login(String email, String password) {
  219.         //Initialize userID
  220.         String role;
  221.         Cursor c;
  222.         User user = null;
  223.  
  224.         SQLiteDatabase db = this.getReadableDatabase();
  225.  
  226.         //Store the sql query for selecting the User Record
  227.         String selectQuery =
  228.                 "SELECT * FROM " + TABLE_USER
  229.                         + " WHERE " + COLUMN_EMAIL + " = \"" + email
  230.                         + "\" AND " + COLUMN_PASSWORD + " = \"" + password + "\";";
  231.  
  232.         //Create a Cursor to store the Record and execute the Query stored in selectQuery
  233.         c = db.rawQuery(selectQuery, null);
  234.  
  235.  
  236.         if(c == null) {
  237.             return null;
  238.         }
  239.  
  240.         try {
  241.             c.moveToFirst();
  242.  
  243.             role = getUserRole(c.getString(c.getColumnIndex(COLUMN_ID)));
  244.  
  245.             //Set userID to the id of the User Record
  246.             user = new User(
  247.                     c.getString(c.getColumnIndex(COLUMN_ID)),
  248.                     c.getString(c.getColumnIndex(COLUMN_FIRST_NAME)),
  249.                     c.getString(c.getColumnIndex(COLUMN_LAST_NAME)),
  250.                     c.getString(c.getColumnIndex(COLUMN_DEPARTMENT)),
  251.                     role
  252.             );
  253.  
  254.         } catch(CursorIndexOutOfBoundsException e) {
  255.             return null;
  256.         }
  257.  
  258.         return user;
  259.     }
  260.  
  261.     public String getUserRole(String userId) {
  262.         String selectQuery;
  263.         SQLiteDatabase db;
  264.         Cursor c;
  265.         String roleId, role;
  266.  
  267.         db = this.getReadableDatabase();
  268.  
  269.         selectQuery =
  270.                 "SELECT * FROM " + TABLE_USER_ROLE
  271.                         + " WHERE " + COLUMN_USER_ID + " = " + userId
  272.                         + ";";
  273.  
  274.         c = db.rawQuery(selectQuery, null);
  275.  
  276.         if(c == null) {
  277.             return null;
  278.         }
  279.  
  280.         try {
  281.             c.moveToFirst();
  282.             roleId = c.getString(c.getColumnIndex(COLUMN_ROLE_ID));
  283.             role = getRole(roleId);
  284.  
  285.         } catch(CursorIndexOutOfBoundsException e) {
  286.             return null;
  287.         }
  288.  
  289.         return role;
  290.  
  291.  
  292.     }
  293.  
  294.     public String getRole(String roleId) {
  295.         String selectQuery;
  296.         SQLiteDatabase db;
  297.         Cursor c;
  298.         String role;
  299.  
  300.         db = this.getReadableDatabase();
  301.  
  302.         selectQuery =
  303.                 "SELECT * FROM " + TABLE_ROLE
  304.                         + " WHERE " + COLUMN_ID + " = " + roleId
  305.                         + ";";
  306.  
  307.         c = db.rawQuery(selectQuery, null);
  308.  
  309.         if(c == null) {
  310.             return null;
  311.         }
  312.  
  313.         try {
  314.             c.moveToFirst();
  315.             role = c.getString(c.getColumnIndex(COLUMN_NAME));
  316.  
  317.         } catch(CursorIndexOutOfBoundsException e) {
  318.             return null;
  319.         }
  320.  
  321.         return role;
  322.  
  323.     }
  324.  
  325.     /**
  326.      * Retrieves all patients in the Patient Table. Creates a Patient object with each retrieved record. Returns all created Patients in a List<Patient>.
  327.      * @return List<Patient> containing all patients retrieved from the Patient table.
  328.      */
  329.     public List<Patient> getAllPatients() {
  330.  
  331.         List<Patient> patients = new ArrayList<Patient>();
  332.  
  333.         //Store the sql query for retrieving the Patient records
  334.         String selectQuery =
  335.                 "SELECT * FROM " + TABLE_PATIENT;
  336.  
  337.         //Get the readable database
  338.         SQLiteDatabase db = this.getReadableDatabase();
  339.         //Execute the query and store the retrieved in a Cursor
  340.         Cursor c = db.rawQuery(selectQuery, null);
  341.  
  342.         //if the first record exists
  343.         if(c.moveToFirst()) {
  344.             do {
  345.                 //Create a Patient object for each record in Cursor
  346.                 Patient patient = new Patient();
  347.                 //patient.setPatientID(c.getString(c.getColumnIndex(COLUMN_ID)));
  348.                 patient.setPatientFname(c.getString(c.getColumnIndex(COLUMN_FIRST_NAME)));
  349.                 patient.setPatientLname(c.getString(c.getColumnIndex(COLUMN_LAST_NAME)));
  350.                 patient.setPatientRoom(c.getString(c.getColumnIndex(COLUMN_ROOM)));
  351.                 patient.setPatientDepartment(c.getString(c.getColumnIndex(COLUMN_DEPARTMENT)));
  352.  
  353.                 patients.add(patient);
  354.             } while (c.moveToNext()); //Advance the Cursor
  355.         }
  356.  
  357.         return patients;
  358.     }
  359.  
  360.     public Databasehelper(Context context) {
  361.         super(context, DATABASE_NAME, null, DATABASE_VERSION);
  362.     }
  363.  
  364.     @Override
  365.     public void onCreate(SQLiteDatabase db) {
  366.  
  367.         // Create Tables
  368.         db.execSQL(CREATE_TABLE_USERS);
  369.         db.execSQL(CREATE_TABLE_USER_ROLE);
  370.         db.execSQL(CREATE_TABLE_ROLES);
  371.         db.execSQL(CREATE_TABLE_ROLE_PERMISSION);
  372.         db.execSQL(CREATE_TABLE_PERMISSION);
  373.         db.execSQL(CREATE_TABLE_PATIENTS);
  374.         db.execSQL(CREATE_TABLE_TESTS);
  375.  
  376.         //Insert Users
  377.         // Insert Doctor Demo User Record
  378.         long doctorID = insertUser(db, "doctor@doctor.com", "doctor", "Doc", "Doe", "Emergency");
  379.         // Insert Nurse Demo User Record
  380.         long nurseID = insertUser(db, "nurse@nurse.com", "nurse", "Nurse", "Doe", "Emergency");
  381.  
  382.         //Insert Roles
  383.         // Insert the Doctor Role Record
  384.         long doctorRoleID = insertRole(db, "doctor");
  385.         // Insert the Nurse Role Record
  386.         long nurseRoleID = insertRole(db, "nurse");
  387.  
  388.         //Insert the UserRole Records
  389.         // Insert the Doctor Demo User Record to the Doctor Role Record association
  390.         insertUserRole(db, doctorID, doctorRoleID);
  391.         // Insert the Nurse Demo User Record to the Nurse Role Record association
  392.         insertUserRole(db, nurseID, nurseRoleID);
  393.  
  394.  
  395.         //Create Permissions
  396.         // Insert Create Patient Permission
  397.         long createPatientID = insertPermission(db, "C_PATIENT");
  398.         // Insert Retrieve Patient Permission
  399.         long viewPatientID = insertPermission(db, "R_PATIENT");
  400.  
  401.         // Insert Create Test Permission
  402.         long createTestID = insertPermission(db, "C_TEST");
  403.         // Insert Retrieve Test Permission
  404.         long viewTestID = insertPermission(db, "R_TEST");
  405.  
  406.         // Associate Nurse Role to the following permissions
  407.         insertRolePermission(db, nurseRoleID, createPatientID);
  408.         insertRolePermission(db, nurseRoleID, viewPatientID);
  409.  
  410.         insertRolePermission(db, nurseRoleID, createTestID);
  411.         insertRolePermission(db, nurseRoleID, viewTestID);
  412.  
  413.         // Associate Doctor Role to the following permissions
  414.         insertRolePermission(db, doctorRoleID, viewPatientID);
  415.  
  416.         insertRolePermission(db, doctorRoleID, createTestID);
  417.         insertRolePermission(db, doctorRoleID, viewTestID);
  418.     }
  419.  
  420.     @Override
  421.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  422.         // Drop Tables
  423.         db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_USERS);
  424.         db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_USER_ROLE);
  425.         db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_ROLES);
  426.         db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_ROLE_PERMISSION);
  427.         db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_PERMISSION);
  428.         db.execSQL("DROP TABLE IF EXISTS " + TABLE_PATIENT);
  429.         db.execSQL("DROP TABLE IF EXISTS " + TABLE_TEST);
  430.     }
  431.     /*
  432.  
  433.     public void insertPatient(Patient patient){
  434.         SQLiteDatabase db=this.getWritableDatabase();
  435.         ContentValues values=new ContentValues();
  436.         values.put(COLUMN_FIRST_NAME , patient.getPatientFname());
  437.         values.put(COLUMN_LAST_NAME, patient.getPatientLname());
  438.         values.put(COLUMN_DEPARTMENT, patient.getPatientDepartment());
  439.         values.put(COLUMN_ROOM, patient.getPatientRoom());
  440.         db.insert(TABLE_PATIENT, null, values);
  441.         db.close();
  442.  
  443.  
  444.  
  445.     }
  446.     public void insertTest(Test test ){
  447.         SQLiteDatabase db=this.getWritableDatabase();
  448.         ContentValues values=new ContentValues();
  449.         values.put(COLUMN_PATIENT_ID, test.getPatientId());
  450.         values.put(COLUMN_BPL,test.getBPL());
  451.         values.put(COLUMN_BPH,test.getBPH());
  452.         values.put(COLUMN_TEMPERATURE,test.getTemp());
  453.         db.insert(TABLE_TEST,null,values);
  454.         db.close();
  455.     }
  456.  
  457. */
  458.     public long insertPatient(SQLiteDatabase db, String first_name, String last_name, String department, String room){
  459.  
  460.         ContentValues values = new ContentValues();
  461.         values.put (COLUMN_FIRST_NAME, first_name);
  462.         values.put (COLUMN_LAST_NAME, last_name);
  463.         values.put (COLUMN_DEPARTMENT, department);
  464.         values.put (COLUMN_ROOM, room);
  465.         return db.insert(CREATE_TABLE_PATIENTS, null, values);
  466.     }
  467.  
  468. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement