From 0edcf7fb84c0dbb2d541858dee644b64d0218c1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Sun, 19 May 2024 23:15:43 +0200 Subject: [PATCH] #8: Setup Model and DAO MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dominik Säume --- src/main/java/de/hitec/nhplus/login/User.java | 34 +++++ .../hitec/nhplus/login/database/UserDao.java | 118 ++++++++++++++++++ .../de/hitec/nhplus/login/database/User.sql | 2 +- .../nhplus/login/database/UserPermission.sql | 4 +- .../nhplus/login/database/UserToNurse.sql | 4 +- 5 files changed, 157 insertions(+), 5 deletions(-) create mode 100644 src/main/java/de/hitec/nhplus/login/User.java create mode 100644 src/main/java/de/hitec/nhplus/login/database/UserDao.java diff --git a/src/main/java/de/hitec/nhplus/login/User.java b/src/main/java/de/hitec/nhplus/login/User.java new file mode 100644 index 0000000..52ab712 --- /dev/null +++ b/src/main/java/de/hitec/nhplus/login/User.java @@ -0,0 +1,34 @@ +package de.hitec.nhplus.login; + +import de.hitec.nhplus.nurse.Nurse; + +public class User { + + private int id; + private String username; + private int permissions = 0; + private Nurse nurse; + + public User( + int id, + String username, + int permissions, + Nurse nurse + ) { + this.id = id; + this.username = username; + this.permissions = permissions; + this.nurse = nurse; + } + + public User( + String username, + int permissions, + Nurse nurse, + boolean admin + ) { + this.username = username; + this.permissions = permissions; + this.nurse = nurse; + } +} diff --git a/src/main/java/de/hitec/nhplus/login/database/UserDao.java b/src/main/java/de/hitec/nhplus/login/database/UserDao.java new file mode 100644 index 0000000..72a0d8c --- /dev/null +++ b/src/main/java/de/hitec/nhplus/login/database/UserDao.java @@ -0,0 +1,118 @@ +package de.hitec.nhplus.login.database; + +import de.hitec.nhplus.datastorage.Dao; +import de.hitec.nhplus.datastorage.DaoFactory; +import de.hitec.nhplus.login.User; +import de.hitec.nhplus.nurse.Nurse; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class UserDao implements Dao { + + + protected final Connection connection; + + public UserDao(Connection connection) { + this.connection = connection; + } + + public int readUserId(String username) throws SQLException { + final String SQL = "SELECT id FROM user WHERE username = ?"; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setString(1, username); + return statement.executeQuery().getInt(1); + } + + public byte[] readPasswordSalt(int id) throws SQLException { + final String SQL = "SELECT passwordSalt FROM user WHERE id = ?"; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setInt(1, id); + return statement.executeQuery().getBytes(1); + } + + public byte[] readPasswordHash(int id) throws SQLException { + final String SQL = "SELECT passwordHash FROM user WHERE id = ?"; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setInt(1, id); + return statement.executeQuery().getBytes(1); + } + + // User + @Override + public User read(int id) throws SQLException { + final String SQL = """ + SELECT user.username, user__permissions.permissions, user__nurse.nurseId + FROM user + LEFT JOIN user__permissions ON user.id = user__permissions.userId + LEFT JOIN user__nurse ON user.id = user__nurse.userId + WHERE user.id = ?; + """; + PreparedStatement statement = this.connection.prepareStatement(SQL); + statement.setInt(1, id); + ResultSet result = statement.executeQuery(); + int nurseId = result.getInt(3); + Nurse nurse = null; + if (!result.wasNull()) { + nurse = DaoFactory.getInstance().createNurseDAO().read(nurseId); + } + return new User( + id, + result.getString(1), + result.getInt(2), + nurse + ); + } + + // Administration + @Override + public void create(User user) throws SQLException { + + } + + @Override + public void update(User user) throws SQLException { + + } + + @Override + public void delete(int id) throws SQLException { + final String SQL = """ + DELETE FROM user WHERE user.id = ?; + """; + PreparedStatement preparedStatement = this.connection.prepareStatement(SQL); + preparedStatement.setInt(1, id); + preparedStatement.executeUpdate(); + } + + @Override + public List readAll() throws SQLException { + final String SQL = """ + SELECT user.id, user.username, user__permissions.permissions, user__nurse.nurseId + FROM user + LEFT JOIN user__permissions ON user.id = user__permissions.userId + LEFT JOIN user__nurse ON user.id = user__nurse.userId + """; + ResultSet result = connection.prepareStatement(SQL).executeQuery(); + + List users = new ArrayList<>(); + while (result.next()) { + int nurseId = result.getInt(4); + Nurse nurse = null; + if (!result.wasNull()) { + nurse = DaoFactory.getInstance().createNurseDAO().read(nurseId); + } + users.add(new User( + result.getInt(1), + result.getString(2), + result.getInt(3), + nurse + )); + } + return users; + } +} diff --git a/src/main/resources/de/hitec/nhplus/login/database/User.sql b/src/main/resources/de/hitec/nhplus/login/database/User.sql index b92a04a..8773d00 100644 --- a/src/main/resources/de/hitec/nhplus/login/database/User.sql +++ b/src/main/resources/de/hitec/nhplus/login/database/User.sql @@ -1,7 +1,7 @@ CREATE TABLE user ( id INTEGER PRIMARY KEY AUTOINCREMENT, - username TEXT NOT NULL, + username TEXT NOT NULL UNIQUE, passwordSalt BLOB NOT NULL, passwordHash BLOB NOT NULL ) \ No newline at end of file diff --git a/src/main/resources/de/hitec/nhplus/login/database/UserPermission.sql b/src/main/resources/de/hitec/nhplus/login/database/UserPermission.sql index 536dce0..4e968f2 100644 --- a/src/main/resources/de/hitec/nhplus/login/database/UserPermission.sql +++ b/src/main/resources/de/hitec/nhplus/login/database/UserPermission.sql @@ -1,6 +1,6 @@ CREATE TABLE user__permissions ( - userId INTEGER NOT NULL, - permissions INTEGER, -- Binary Bitmask for Permissions + userId INTEGER NOT NULL UNIQUE, + permissions INTEGER NOT NULL, -- Binary Bitmask for Permissions FOREIGN KEY (userId) REFERENCES user (id) ON DELETE CASCADE ) \ No newline at end of file diff --git a/src/main/resources/de/hitec/nhplus/login/database/UserToNurse.sql b/src/main/resources/de/hitec/nhplus/login/database/UserToNurse.sql index 4507f47..b46be11 100644 --- a/src/main/resources/de/hitec/nhplus/login/database/UserToNurse.sql +++ b/src/main/resources/de/hitec/nhplus/login/database/UserToNurse.sql @@ -1,7 +1,7 @@ CREATE TABLE user__nurse ( - userId INTEGER NOT NULL, - nurseId INTEGER NOT NULL, + userId INTEGER NOT NULL UNIQUE, + nurseId INTEGER NOT NULL UNIQUE, FOREIGN KEY (userId) REFERENCES user (id) ON DELETE CASCADE, FOREIGN KEY (nurseId) REFERENCES nurse (id) ON DELETE CASCADE ) \ No newline at end of file