Skip to content

Data Layer

An7ar35 edited this page Mar 10, 2018 · 12 revisions

mtRooms.db

The data layer, mtRooms.db, provides all I/O access to the database file for the rest of the application.

Data Layer

Layer Interfaces

Return Objects

ObjectTable

eadjlib.datastructure.ObjectTable

Datastructure used to store the results of database queries in a more convinient format untied to the databse connection state.

It can be accessed like a spreadsheet table with (x,y) coordinates where x is the column number (1..n) and y is the row number (1..n). Rows can be extracted into a HashMap<K,V> where K = column name and V = value object found at the row/col.

SessionType

mtRooms.db.session.SessionType

Data transfer object that contains the session access level of a user (ADMIN, USER).

IReservationDbAccess

mtRooms.db.IReservationAccess

Used for user access to the data layer. A session token id which is validated when the methods are called must be provided. Queries are only executed if that token id is valid.

boolean pushToDB(String session_id, String query) throws DbQueryException, SessionExpiredException, SessionInvalidException;
ObjectTable pullFromDB(String session_id, String query) throws DbQueryException, SessionExpiredException, SessionInvalidException;

IUserAccDbAccess

mtRooms.db.IUserAccDbAccess

Used for administrative access to the data layer enabling complete access to the database and user session related actions.

void checkValidity(String session_id, Date session_expiry) throws SessionExpiredException, SessionCorruptedException, SessionInvalidException;
SessionType getSessionType(String session_id) throws SessionInvalidException;
Integer getSessionAccountID(String session_id) throws SessionInvalidException;
void openSession(String session_id, Date expiry, SessionType session_type, Integer account_id) throws SessionException;
void closeSession(String session_id) throws SessionInvalidException;
    
boolean pushToDB(String query) throws DbQueryException;
ObjectTable pullFromDB(String query) throws DbQueryException;

IDbSystemBootstrap

mtRooms.db.IDbSystemBootstrap

This component is used to spool up the data layer and a connection to a database file (using init(String) with the name of the sqlite db file.). If the file doesn't exist a new one is created with its tables. Otherwise it just opens up a connection. Running instances for the ReservationDbAccess and UserAccDbAccess can then be accessed with the getters.

void init(String database) throws DbBootstrapException;
IUserAccDbAccess getUserAccDbAccess() throws DbBootstrapException;
IReservationDbAccess getReservationDbAccess() throws DbBootstrapException;

TimestampConverter

mtRooms.db.TimestampConverter

Static component to convert UTC timestamp strings from the database to a Java localised Date object and back. This helps standardise the schedule times regardless of the timezone the application may be running in. It is used throughout the application.

e.g.:

Date date = TimestampConverter.getDateObject("2018-03-07 14:00:00");
String utc = TimestampConverter.getUTCTimestampString(date);

Exceptions thrown by layer

Exception Reason
DbQueryException Problem encountered whilst processing a query, Check log.
SessionException Opening a new session with an already tracked ID
SessionExpiredException Session has expired.
SessionInvalidException Session doesn't exist in the tracker.
SessionCorruptedException Given session expiry does not match expiry in tracker
DbBootstrapException Error has occured during bootstrapping. Check log.

Components

ReservationDbAccess

mtRooms.db.ReservationAccess

Implementation for IReservationDbAccess

UserAccDbAccess

mtRooms.db.UserAccDbAccess

Implementation for IUserAccDbAccess.

DbSystemBootstrap

mtRooms.db.DbSystemBootstrap

Implementation for DbSystemBootstrap.

Database (SQLite wrapper)

mtRooms.db.database.Database

The Database component serves as an access wrapper for the SQLite engine, the table builder and checker components.

Queries that are expected to return data can be passed using Database.pull(String query)

Table data from queries are placed in a ObjectTable datastructure.

Queries that do not fetch data can use Database.push(String query) instead.

Database builder and checker

mtRooms.db.database.DatabaseBuilder contains hard coded SQLite queries to create all the required table for mtRooms.

mtRooms.db.database.DatabaseChecker contains individual checking methods for the required tables and their expected columns.

Any table design changes must be reflected in both.

Session Tracker

The session tracker keeps session IDs that have been cleared for data access. This component is used by the ReservationDbAccess and UserAccDbAccess components during calls to their push(..) and pull(..) methods to make sure data access is valid.