Package com.ehealthsystem.database
Class Database
java.lang.Object
com.ehealthsystem.database.Database
Class for for database communication (reading and writing data)
-
Field Summary
Modifier and TypeFieldDescriptionstatic Connection
static final DateTimeFormatter
static final String
The file name of the database filestatic final DateTimeFormatter
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
addHealthInformation
(String selected, String username) Add a disease to a user's health informationstatic boolean
checkPassword
(String usernameOrEmail, String password) Check if user entered their correct password Retrieves stored password and verifies that the entered one matches the stored hash This method is not part of the User class because there isn't a user object during login yetstatic void
close()
Properly closes the connection to the database Probably only needed if a big query would still be runningstatic void
Create database with provided admin passwordstatic void
deleteHealthInformation
(HealthInformationTableView selected, String username) Delete a user's health statusstatic void
deleteUserInformation
(UserTableView selectedRow, String username) Delete a disease from a user's health informationGet all rows of users table from the database For admin GUIGet all possible diseases.Get all doctors from the database as objects of class Doctorstatic ArrayList<Appointment>
getDoctorsAppointments
(int doctor, LocalDate date) Get appointments that a doctor already has within a range of days To be used to display a doctor's timetable to the patient, to find a free time for their appointmentgetDoctorsBySpecialization
(String specialization) Get all doctors that have a specified specialization from the database as objects of class Doctorstatic ArrayList<DoctorTimeSlot>
getDoctorsFreeTimes
(Doctor doctor, LocalDate selectedDate) Deprecated.: FoundDoctorFullController.getFreeTimeSlots() is used insteadstatic ArrayList<HealthInformation>
getHealthInformation
(String email) Get health statuses of a userstatic javafx.collections.ObservableList<HealthInformationTableView>
getHealthInformationForTableView
(String username) Get a users current health statuses so that they can manage theirsstatic String
getPassword
(String email) Get a users password to display in the admin GUIstatic ArrayList<Appointment>
Get upcoming appointments that have a reminder.static User
getUserByUsername
(String username) Get a user object with all user's details loaded from the databasestatic User
getUserByUsernameOrEmail
(String usernameOrEmail) Get a user object with all user's details loaded from the databasestatic javafx.collections.ObservableList<UserTableView>
Load users into a table view for the Admin GUIstatic ArrayList<Appointment>
getUsersAppointments
(String username) Get a user's past and future appointments, ordered by appointment time (newest first) To be used for the patient to see their appointments.static String
hashPassword
(String password) Helper method to generate a password's hashstatic void
init()
To be called on application start.static int
General method for inserting a single row into a database tableprivate static void
insertValueIntoStatement
(int i, Object value, PreparedStatement statement) General method for setting a value for a placeholder in a prepared statementstatic boolean
isEmailTaken
(String email) Check, if an email is already taken, used for registrationstatic boolean
isUsernameTaken
(String username) Check, if a username is already taken, used for registrationstatic ArrayList<Appointment>
Helper method to turn result set into array of appointment objectsstatic Doctor
loadDoctorFromId
(int doctorId) Get a doctor by their IDHelper method to turn result set into array of Doctor objectsloadDoctorSpecializations
(int id) Get a list of a doctor's specializationsGet list of all potential health problems based on which you can select a doctorGet a list of all specializations that doctors can haveHelper method to turn result set into array of User objectsstatic String
problemToSuitableSpecialization
(String problem) Get the suitable specialization for a provided health problemstatic void
General method for updating rows in a database table
-
Field Details
-
connection
-
fileName
The file name of the database file- See Also:
-
dateFormatter
-
timeFormatterAppointment
-
-
Constructor Details
-
Database
public Database()
-
-
Method Details
-
init
public static void init()To be called on application start. Establishes connection to the database. Creates a database if it doesn't exist yet. Before creating a database, it asks for an admin password. -
createDB
Create database with provided admin password- Parameters:
initialAdminPassword
- provided admin password desired by the user- Throws:
SQLException
- if a creation step failed
-
hashPassword
Helper method to generate a password's hash- Parameters:
password
- the password to hash- Returns:
- hash
-
close
public static void close()Properly closes the connection to the database Probably only needed if a big query would still be running -
checkPassword
Check if user entered their correct password Retrieves stored password and verifies that the entered one matches the stored hash This method is not part of the User class because there isn't a user object during login yet- Parameters:
usernameOrEmail
- since a user can enter both username or email in the text fieldpassword
- in plain text- Returns:
- whether password is correct
- Throws:
SQLException
-
insert
public static int insert(String tableName, Object[][] parameters) throws SQLException, javax.activation.UnsupportedDataTypeException General method for inserting a single row into a database table- Parameters:
tableName
- the name of the table to which the row shall be addedparameters
- array consisting of pairs of column name and value- Returns:
- id of inserted row
- Throws:
SQLException
- if insert failed or succeeded but no generated ID was receivedjavax.activation.UnsupportedDataTypeException
-
update
public static void update(String tableName, Object[][] newValues, Object[][] conditions) throws SQLException, javax.activation.UnsupportedDataTypeException General method for updating rows in a database table- Parameters:
tableName
- the name of the table where the row is to be foundnewValues
- array consisting of pairs of column name and value, these values will be set on the matching rowsconditions
- array consisting of pairs of column name and value, rows have to match these criteria- Throws:
SQLException
javax.activation.UnsupportedDataTypeException
-
insertValueIntoStatement
private static void insertValueIntoStatement(int i, Object value, PreparedStatement statement) throws SQLException, javax.activation.UnsupportedDataTypeException General method for setting a value for a placeholder in a prepared statement- Parameters:
i
- index of the placeholdervalue
- the value to set for the placeholderstatement
- the query to apply the operation to- Throws:
SQLException
javax.activation.UnsupportedDataTypeException
-
getAllUsers
public static ArrayList<User> getAllUsers() throws SQLException, javax.activation.UnsupportedDataTypeExceptionGet all rows of users table from the database For admin GUI- Returns:
- all users
- Throws:
SQLException
javax.activation.UnsupportedDataTypeException
-
getUserByUsername
public static User getUserByUsername(String username) throws SQLException, javax.activation.UnsupportedDataTypeException Get a user object with all user's details loaded from the database- Parameters:
username
-- Returns:
- Throws:
SQLException
javax.activation.UnsupportedDataTypeException
-
isUsernameTaken
Check, if a username is already taken, used for registration- Parameters:
username
- the username to check- Returns:
- whether the username is already taken
- Throws:
SQLException
-
isEmailTaken
Check, if an email is already taken, used for registration- Parameters:
email
- the email address to check- Returns:
- whether the email is already in use
- Throws:
SQLException
-
getUserByUsernameOrEmail
public static User getUserByUsernameOrEmail(String usernameOrEmail) throws SQLException, javax.activation.UnsupportedDataTypeException Get a user object with all user's details loaded from the database- Parameters:
usernameOrEmail
- since a user can enter both username or email in the text field- Returns:
- Throws:
SQLException
javax.activation.UnsupportedDataTypeException
-
getDoctors
Get all doctors from the database as objects of class Doctor- Returns:
- all doctors
- Throws:
SQLException
- if query fails
-
getDoctorsBySpecialization
public static ArrayList<Doctor> getDoctorsBySpecialization(String specialization) throws SQLException Get all doctors that have a specified specialization from the database as objects of class Doctor- Returns:
- all doctors with the specified specialization
- Throws:
SQLException
- if query fails
-
loadDoctorsFromResultSet
Helper method to turn result set into array of Doctor objects- Parameters:
rs
- resultSet after the query was executed- Returns:
- Doctor objects
- Throws:
SQLException
- if reading an attribute fails
-
getHealthInformation
Get health statuses of a user- Parameters:
email
- User's email address- Throws:
SQLException
-
loadUsersFromResultSet
private static ArrayList<User> loadUsersFromResultSet(ResultSet rs) throws SQLException, javax.activation.UnsupportedDataTypeException Helper method to turn result set into array of User objects- Parameters:
rs
- resultSet after the query was executed- Returns:
- User objects
- Throws:
SQLException
- if reading an attribute failsjavax.activation.UnsupportedDataTypeException
-
loadDoctorFromId
Get a doctor by their ID- Parameters:
doctorId
- the numeric identifier for the doctor- Returns:
- the requested doctor
- Throws:
SQLException
-
loadDoctorSpecializations
Get a list of a doctor's specializations- Parameters:
id
- the numeric identifier for the doctor- Returns:
- list of names of their specializations
- Throws:
SQLException
-
loadSpecializations
Get a list of all specializations that doctors can have- Returns:
- specialization names
- Throws:
SQLException
-
loadProblems
Get list of all potential health problems based on which you can select a doctor- Returns:
- problem names
- Throws:
SQLException
-
problemToSuitableSpecialization
Get the suitable specialization for a provided health problem- Parameters:
problem
- the health problem to get a suitable specialization for- Returns:
- the suitable specialization for that problem
- Throws:
SQLException
-
getDoctorsFreeTimes
public static ArrayList<DoctorTimeSlot> getDoctorsFreeTimes(Doctor doctor, LocalDate selectedDate) throws SQLException Deprecated.: FoundDoctorFullController.getFreeTimeSlots() is used instead- Throws:
SQLException
-
loadAppointmentsFromResultSet
public static ArrayList<Appointment> loadAppointmentsFromResultSet(ResultSet rs) throws SQLException, javax.activation.UnsupportedDataTypeException Helper method to turn result set into array of appointment objects- Parameters:
rs
- resultSet after the query was executed- Returns:
- list of appointments, may be empty
- Throws:
SQLException
- if reading an attribute failsjavax.activation.UnsupportedDataTypeException
-
getDoctorsAppointments
public static ArrayList<Appointment> getDoctorsAppointments(int doctor, LocalDate date) throws SQLException, javax.activation.UnsupportedDataTypeException Get appointments that a doctor already has within a range of days To be used to display a doctor's timetable to the patient, to find a free time for their appointment- Parameters:
doctor
-date
-- Returns:
- doctorsAppointments
- Throws:
SQLException
- if reading an attribute failsjavax.activation.UnsupportedDataTypeException
-
getUsersAppointments
public static ArrayList<Appointment> getUsersAppointments(String username) throws SQLException, javax.activation.UnsupportedDataTypeException Get a user's past and future appointments, ordered by appointment time (newest first) To be used for the patient to see their appointments. This method is not part of the User class because the content is so similar to DB.getDoctorsAppointments() and hence shall be next to it- Returns:
- usersAppointments
- Throws:
SQLException
javax.activation.UnsupportedDataTypeException
-
getUpcomingAppointmentsWithReminder
public static ArrayList<Appointment> getUpcomingAppointmentsWithReminder() throws SQLException, javax.activation.UnsupportedDataTypeExceptionGet upcoming appointments that have a reminder. To be used to set reminders initially (application start).- Throws:
SQLException
javax.activation.UnsupportedDataTypeException
-
getUserForTableView
public static javafx.collections.ObservableList<UserTableView> getUserForTableView() throws SQLExceptionLoad users into a table view for the Admin GUI- Throws:
SQLException
-
getPassword
Get a users password to display in the admin GUI- Parameters:
email
-- Returns:
- Throws:
SQLException
-
getHealthInformationForTableView
public static javafx.collections.ObservableList<HealthInformationTableView> getHealthInformationForTableView(String username) throws SQLException Get a users current health statuses so that they can manage theirs- Parameters:
username
- the user to get health statuses for- Throws:
SQLException
-
deleteHealthInformation
public static void deleteHealthInformation(HealthInformationTableView selected, String username) throws SQLException Delete a user's health status- Parameters:
selected
- the selected table view from which the health status to delete is read out fromusername
- the concerned user's name- Throws:
SQLException
-
getDisease
Get all possible diseases. Used by the UI for the patient to add a disease to their health information.- Returns:
- list of all possible disease
- Throws:
SQLException
-
addHealthInformation
Add a disease to a user's health information- Parameters:
selected
-username
-- Throws:
SQLException
-
deleteUserInformation
public static void deleteUserInformation(UserTableView selectedRow, String username) throws SQLException Delete a disease from a user's health information- Parameters:
selectedRow
- the selected row from which the health information to delete is derived fromusername
- the name of the user from which to delete this health information from- Throws:
SQLException
-