From b320448a60490ab7b8b2e91f2e24728a3fb55432 Mon Sep 17 00:00:00 2001 From: minhtrannhat Date: Sat, 4 Nov 2023 21:49:03 -0400 Subject: [PATCH] Feat(DB): DB helpers done --- .../Controllers/AccessDBHelper.java | 104 ++++++++++++++++++ .../Controllers/StudentProfileDBHelper.java | 98 +++++++++++++++++ .../coen390_assignment2/Models/Access.java | 53 +++++++++ .../Models/AccessContract.java | 16 +++ .../Models/AccessType.java | 28 +++++ .../Models/StudentProfile.java | 55 +++++++++ .../Models/StudentProfileContract.java | 22 ++++ 7 files changed, 376 insertions(+) create mode 100644 app/src/main/java/com/example/coen390_assignment2/Controllers/AccessDBHelper.java create mode 100644 app/src/main/java/com/example/coen390_assignment2/Controllers/StudentProfileDBHelper.java create mode 100644 app/src/main/java/com/example/coen390_assignment2/Models/Access.java create mode 100644 app/src/main/java/com/example/coen390_assignment2/Models/AccessContract.java create mode 100644 app/src/main/java/com/example/coen390_assignment2/Models/AccessType.java create mode 100644 app/src/main/java/com/example/coen390_assignment2/Models/StudentProfile.java create mode 100644 app/src/main/java/com/example/coen390_assignment2/Models/StudentProfileContract.java diff --git a/app/src/main/java/com/example/coen390_assignment2/Controllers/AccessDBHelper.java b/app/src/main/java/com/example/coen390_assignment2/Controllers/AccessDBHelper.java new file mode 100644 index 0000000..22893ba --- /dev/null +++ b/app/src/main/java/com/example/coen390_assignment2/Controllers/AccessDBHelper.java @@ -0,0 +1,104 @@ +package com.example.coen390_assignment2.Controllers; + +import android.annotation.SuppressLint; +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.widget.Toast; + +import androidx.annotation.Nullable; + +import com.example.coen390_assignment2.Models.Access; +import com.example.coen390_assignment2.Models.AccessContract; +import com.example.coen390_assignment2.Models.AccessType; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; + +public class AccessDBHelper extends SQLiteOpenHelper { + private static final int DATABASE_VERSION = 1; + private Context context = null; + + public AccessDBHelper(@Nullable Context context) { + super(context, AccessContract.AccessEntry.DATABASE_NAME, null, DATABASE_VERSION); + } + + @Override + public void onCreate(SQLiteDatabase db) { + //Create a table + String CREATE_COURSE_TABLE = "CREATE TABLE " + AccessContract.AccessEntry.TABLE_NAME + " (" + + AccessContract.AccessEntry.COLUMN_NAME_ACCESS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + + AccessContract.AccessEntry.COLUMN_NAME_PROFILE_ID + " INTEGER NOT NULL, " + + AccessContract.AccessEntry.COLUMN_NAME_ACCESS_TYPE + " TEXT NOT NULL, " + + AccessContract.AccessEntry.COLUMN_NAME_TIMESTAMP + " TEXT NOT NULL);"; + + db.execSQL(CREATE_COURSE_TABLE); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + //Drop older table + db.execSQL("DROP TABLE IF EXISTS " + AccessContract.AccessEntry.DATABASE_NAME); + onCreate(db); + } + + public long insertAccess(Access access) { + long id = -1; + SQLiteDatabase db = this.getWritableDatabase(); + ContentValues contentValues = new ContentValues(); + + contentValues.put(AccessContract.AccessEntry.COLUMN_NAME_PROFILE_ID, access.getProfileID()); + contentValues.put(AccessContract.AccessEntry.COLUMN_NAME_ACCESS_TYPE, access.getAccessType().getStringAccessType()); + contentValues.put(AccessContract.AccessEntry.COLUMN_NAME_TIMESTAMP, access.getTimestamp().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); + + try { + id = db.insertOrThrow(AccessContract.AccessEntry.TABLE_NAME, null, contentValues); + } catch (Exception e) { + Toast.makeText(context, "DB insert failed: " + e.getMessage(), Toast.LENGTH_LONG).show(); + } finally { + db.close(); + } + return id; + } + + public List getAccessFromProfileID(long profileID) { + List accessList = new ArrayList<>(); + + try (SQLiteDatabase db = this.getReadableDatabase()) { + Cursor cursor = null; + + String[] columns = {"AccessId", "ProfileID", "AccessType", "Timestamp"}; + String selection = "ProfileID = ?"; + String[] selectionArgs = {String.valueOf(profileID)}; + String orderBy = "Timestamp"; + + cursor = db.query(AccessContract.AccessEntry.TABLE_NAME, columns, selection, selectionArgs, null, null, orderBy); + + if (cursor != null) { + if (cursor.moveToFirst()) { + do { + @SuppressLint("Range") int accessId = cursor.getInt(cursor.getColumnIndex(AccessContract.AccessEntry.COLUMN_NAME_ACCESS_ID)); + @SuppressLint("Range") int _profileID = cursor.getInt(cursor.getColumnIndex(AccessContract.AccessEntry.COLUMN_NAME_PROFILE_ID)); + @SuppressLint("Range") String accessType = cursor.getString(cursor.getColumnIndex("AccessType")); + @SuppressLint("Range") String timestampStr = cursor.getString(cursor.getColumnIndex("Timestamp")); + + // Parse the timestamp string into a LocalDateTime object + LocalDateTime timestamp = LocalDateTime.parse(timestampStr, DateTimeFormatter.ISO_LOCAL_DATE_TIME); + + Access access = new Access(accessId, _profileID, AccessType.matchEnum(accessType), timestamp); + accessList.add(access); + } while (cursor.moveToNext()); + } + cursor.close(); + } + } catch (Exception e) { + Toast.makeText(context, "DB get failed: " + e.getMessage(), Toast.LENGTH_LONG).show(); + } + + return accessList; + } +} diff --git a/app/src/main/java/com/example/coen390_assignment2/Controllers/StudentProfileDBHelper.java b/app/src/main/java/com/example/coen390_assignment2/Controllers/StudentProfileDBHelper.java new file mode 100644 index 0000000..c2a8fa9 --- /dev/null +++ b/app/src/main/java/com/example/coen390_assignment2/Controllers/StudentProfileDBHelper.java @@ -0,0 +1,98 @@ +package com.example.coen390_assignment2.Controllers; + +import android.annotation.SuppressLint; +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.util.Log; +import android.widget.Toast; + +import androidx.annotation.Nullable; + +import com.example.coen390_assignment2.Models.StudentProfile; +import com.example.coen390_assignment2.Models.StudentProfileContract; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; + +public class StudentProfileDBHelper extends SQLiteOpenHelper { + private static final int DATABASE_VERSION = 1; + private Context context = null; + + public StudentProfileDBHelper(@Nullable Context context) { + super(context, StudentProfileContract.StudentProfileEntry.DATABASE_NAME, null, DATABASE_VERSION); + } + + @Override + public void onCreate(SQLiteDatabase db) { + //Create a table + String CREATE_COURSE_TABLE = "CREATE TABLE " + StudentProfileContract.StudentProfileEntry.TABLE_NAME + " (" + StudentProfileContract.StudentProfileEntry.COLUMN_NAME_PROFILE_ID + " INTEGER PRIMARY KEY, " + StudentProfileContract.StudentProfileEntry.COLUMN_NAME_SURNAME + " TEXT NOT NULL, " + StudentProfileContract.StudentProfileEntry.COLUMN_NAME_NAME + " TEXT NOT NULL, " + StudentProfileContract.StudentProfileEntry.COLUMN_NAME_PROFILE_GPA + " REAL NOT NULL, " + StudentProfileContract.StudentProfileEntry.COLUMN_NAME_PROFILE_CREATION_DATE + " TEXT NOT NULL);"; + + db.execSQL(CREATE_COURSE_TABLE); + Log.d("StudentProfileDBHelper", "onCreate: Created course table" + CREATE_COURSE_TABLE); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + //Drop older table + db.execSQL("DROP TABLE IF EXISTS " + StudentProfileContract.StudentProfileEntry.TABLE_NAME); + onCreate(db); + } + + public long insertStudentProfile(StudentProfile studentProfile) { + long id = -1; + SQLiteDatabase db = this.getWritableDatabase(); + ContentValues contentValues = new ContentValues(); + + contentValues.put(StudentProfileContract.StudentProfileEntry.COLUMN_NAME_PROFILE_ID, studentProfile.getProfileID()); + contentValues.put(StudentProfileContract.StudentProfileEntry.COLUMN_NAME_SURNAME, studentProfile.getSurname()); + contentValues.put(StudentProfileContract.StudentProfileEntry.COLUMN_NAME_NAME, studentProfile.getName()); + contentValues.put(StudentProfileContract.StudentProfileEntry.COLUMN_NAME_PROFILE_GPA, studentProfile.getGPA()); + contentValues.put(StudentProfileContract.StudentProfileEntry.COLUMN_NAME_PROFILE_CREATION_DATE, studentProfile.getProfileCreationDate().format(DateTimeFormatter.ISO_LOCAL_DATE)); + + try { + // row id + id = db.insertOrThrow(StudentProfileContract.StudentProfileEntry.TABLE_NAME, null, contentValues); + } catch (Exception e) { + Toast.makeText(context, "DB insert failed: " + e.getMessage(), Toast.LENGTH_LONG).show(); + } finally { + db.close(); + } + return id; + } + + public List getAllStudentProfile() { + SQLiteDatabase db = this.getReadableDatabase(); + Cursor cursor = null; + List studentProfiles = new ArrayList<>(); + + try { + cursor = db.query(StudentProfileContract.StudentProfileEntry.TABLE_NAME, null, null, null, null, null, null); + if (cursor != null) { + if (cursor.moveToFirst()) { + do { + @SuppressLint("Range") int profileID = cursor.getInt(cursor.getColumnIndex(StudentProfileContract.StudentProfileEntry.COLUMN_NAME_PROFILE_ID)); + @SuppressLint("Range") String profileSurname = cursor.getString(cursor.getColumnIndex(StudentProfileContract.StudentProfileEntry.COLUMN_NAME_SURNAME)); + @SuppressLint("Range") String profileName = cursor.getString(cursor.getColumnIndex(StudentProfileContract.StudentProfileEntry.COLUMN_NAME_NAME)); + @SuppressLint("Range") float profileGPA = cursor.getFloat(cursor.getColumnIndex(StudentProfileContract.StudentProfileEntry.COLUMN_NAME_PROFILE_GPA)); + @SuppressLint("Range") String profileCreationDate = cursor.getString(cursor.getColumnIndex(StudentProfileContract.StudentProfileEntry.COLUMN_NAME_PROFILE_CREATION_DATE)); + + StudentProfile studentProfile = new StudentProfile(profileSurname, profileName, profileID, profileGPA, LocalDate.parse(profileCreationDate, DateTimeFormatter.ISO_LOCAL_DATE)); + + studentProfiles.add(studentProfile); + } while (cursor.moveToNext()); + } + cursor.close(); + } + } catch (Exception e) { + Toast.makeText(context, "DB get failed: " + e.getMessage(), Toast.LENGTH_LONG).show(); + } finally { + db.close(); + } + return studentProfiles; + } +} diff --git a/app/src/main/java/com/example/coen390_assignment2/Models/Access.java b/app/src/main/java/com/example/coen390_assignment2/Models/Access.java new file mode 100644 index 0000000..0a02e8e --- /dev/null +++ b/app/src/main/java/com/example/coen390_assignment2/Models/Access.java @@ -0,0 +1,53 @@ +package com.example.coen390_assignment2.Models; + +import java.time.LocalDateTime; + +public class Access { + private int accessID; + + private long profileID; + + private AccessType accessType; + + private LocalDateTime timestamp; + + public Access(int accessID, long profileID, AccessType accessType, LocalDateTime timestamp) { + this.accessID = accessID; + this.profileID = profileID; + this.accessType = accessType; + this.timestamp = timestamp; + } + + + public int getAccessID() { + return accessID; + } + + public void setAccessID(int accessID) { + this.accessID = accessID; + } + + public long getProfileID() { + return profileID; + } + + public void setProfileID(long profileID) { + this.profileID = profileID; + } + + public AccessType getAccessType() { + return accessType; + } + + public void setAccessType(AccessType accessType) { + this.accessType = accessType; + } + + public LocalDateTime getTimestamp() { + return timestamp; + } + + public void setTimestamp(LocalDateTime timestamp) { + this.timestamp = timestamp; + } +} diff --git a/app/src/main/java/com/example/coen390_assignment2/Models/AccessContract.java b/app/src/main/java/com/example/coen390_assignment2/Models/AccessContract.java new file mode 100644 index 0000000..ec2f594 --- /dev/null +++ b/app/src/main/java/com/example/coen390_assignment2/Models/AccessContract.java @@ -0,0 +1,16 @@ +package com.example.coen390_assignment2.Models; + +import android.provider.BaseColumns; + +public final class AccessContract { + private AccessContract(){} + + public static class AccessEntry implements BaseColumns{ + public static final String DATABASE_NAME = "access-entry-db"; + public static final String TABLE_NAME = "access"; + public static final String COLUMN_NAME_ACCESS_ID = "access-id"; + public static final String COLUMN_NAME_PROFILE_ID = "profile-id"; + public static final String COLUMN_NAME_ACCESS_TYPE = "access-type"; + public static final String COLUMN_NAME_TIMESTAMP = "timestamp"; + } +} diff --git a/app/src/main/java/com/example/coen390_assignment2/Models/AccessType.java b/app/src/main/java/com/example/coen390_assignment2/Models/AccessType.java new file mode 100644 index 0000000..eaf42b5 --- /dev/null +++ b/app/src/main/java/com/example/coen390_assignment2/Models/AccessType.java @@ -0,0 +1,28 @@ +package com.example.coen390_assignment2.Models; + +public enum AccessType { + CREATED("Created"), + OPENED("Opened"), + CLOSED("Closed"), + DELETED("Deleted"); + + private String accessType; + + AccessType(String accessType) { + this.accessType = accessType; + } + + public static AccessType matchEnum(String input) { + for (AccessType enumValue : AccessType.values()) { + if (enumValue.getStringAccessType().equals(input)) { + return enumValue; + } + } + + return null; + } + + public String getStringAccessType() { + return this.accessType; + } +} diff --git a/app/src/main/java/com/example/coen390_assignment2/Models/StudentProfile.java b/app/src/main/java/com/example/coen390_assignment2/Models/StudentProfile.java new file mode 100644 index 0000000..dea8390 --- /dev/null +++ b/app/src/main/java/com/example/coen390_assignment2/Models/StudentProfile.java @@ -0,0 +1,55 @@ +package com.example.coen390_assignment2.Models; + +import java.time.LocalDate; + +public class StudentProfile { + private long profileID; + private String name; + private String surname; + private float gpa; + private final LocalDate profileCreationDate; + + public StudentProfile(String surname, String name, long profileID, float gpa, LocalDate profileCreationDate) { + this.surname = surname; + this.name = name; + this.profileID = profileID; + this.gpa = gpa; + this.profileCreationDate = profileCreationDate; + } + + public String getSurname() { + return this.surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public long getProfileID() { + return this.profileID; + } + + public void setProfileID(long profileID) { + this.profileID = profileID; + } + + public float getGPA(){ + return this.gpa; + } + + public void setGPA(float gpa){ + this.gpa = gpa; + } + + public LocalDate getProfileCreationDate() { + return profileCreationDate; + } +} diff --git a/app/src/main/java/com/example/coen390_assignment2/Models/StudentProfileContract.java b/app/src/main/java/com/example/coen390_assignment2/Models/StudentProfileContract.java new file mode 100644 index 0000000..cc3ff97 --- /dev/null +++ b/app/src/main/java/com/example/coen390_assignment2/Models/StudentProfileContract.java @@ -0,0 +1,22 @@ +package com.example.coen390_assignment2.Models; + +import android.provider.BaseColumns; + +public final class StudentProfileContract { + + // To prevent someone from accidentally instantiating the contract class, + // make the constructor private. + private StudentProfileContract() { + } + + /* Inner class that defines the table contents */ + public static class StudentProfileEntry implements BaseColumns { + public static final String DATABASE_NAME = "student-profile-db"; + public static final String TABLE_NAME = "profile"; + public static final String COLUMN_NAME_PROFILE_ID = "profile-id"; + public static final String COLUMN_NAME_SURNAME = "surname"; + public static final String COLUMN_NAME_NAME = "name"; + public static final String COLUMN_NAME_PROFILE_GPA = "profile-gpa"; + public static final String COLUMN_NAME_PROFILE_CREATION_DATE = "profile-creation-date"; + } +}