Feat(DB): DB helpers done

This commit is contained in:
minhtrannhat 2023-11-04 21:49:03 -04:00
parent 06445e2a9f
commit b320448a60
Signed by: minhtrannhat
GPG Key ID: E13CFA85C53F8062
7 changed files with 376 additions and 0 deletions

View File

@ -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<Access> getAccessFromProfileID(long profileID) {
List<Access> 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;
}
}

View File

@ -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<StudentProfile> getAllStudentProfile() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
List<StudentProfile> 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;
}
}

View File

@ -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;
}
}

View File

@ -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";
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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";
}
}