aboutsummaryrefslogtreecommitdiff
path: root/src/Store/Book.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/Store/Book.java')
-rw-r--r--src/Store/Book.java214
1 files changed, 214 insertions, 0 deletions
diff --git a/src/Store/Book.java b/src/Store/Book.java
new file mode 100644
index 0000000..4232d77
--- /dev/null
+++ b/src/Store/Book.java
@@ -0,0 +1,214 @@
+package Store;
+
+import Server.JSONMessage;
+import Server.SocketServer;
+import UI.MainWindow;
+import com.corundumstudio.socketio.listener.DataListener;
+
+import java.sql.*;
+import java.util.ArrayList;
+import java.util.Map;
+
+public class Book {
+ private int id;
+ private String title;
+ private String author;
+ private int issuedBy;
+ private String issuedByName = "";
+
+ public Book(int id) {
+ this.id = id;
+ }
+
+ public Book(String title, String author) {
+ this.title = title;
+ this.author = author;
+ this.issuedBy = 0; // 0 means available since IDs start with 1
+ }
+
+ public Book(int id, String title, String author, int issuedBy, String issuedByName) {
+ this.id = id;
+ this.title = title;
+ this.author = author;
+ this.issuedBy = issuedBy;
+ this.issuedByName = issuedByName;
+ }
+
+ public String getTitle() {
+ return this.title;
+ }
+
+ public String getAuthor() {
+ return this.author;
+ }
+
+ public int getIssuedBy() {
+ return this.issuedBy;
+ }
+
+ public int getId() {
+ return this.id;
+ }
+
+ public String getIssuedByName() {
+ return issuedByName;
+ }
+
+ public int bookIssuedBy() throws SQLException {
+ String sql = "SELECT issuedBy from Book where ID = " + this.id;
+ Statement statement = ConnectionHandler.connection.createStatement();
+
+ ResultSet response = statement.executeQuery(sql);
+ while (response.next()) {
+ return response.getInt("issuedBy");
+ }
+
+ return 0;
+ }
+
+ public boolean issue(int userId) throws SQLException {
+ if (bookIssuedBy() == 0) {
+ String sql = "UPDATE Book SET issuedBy = ? where ID = ?";
+ PreparedStatement statement = ConnectionHandler.connection.prepareStatement(sql);
+
+ statement.setInt(1, userId);
+ statement.setInt(2, this.id);
+
+ statement.executeUpdate();
+
+ ArrayList<Book> allBooks = Book.getAll(false);
+ UI.Components.BooksList.refreshBooks(allBooks);
+ if (MainWindow.serverStarted) {
+ SocketServer.server.getBroadcastOperations().sendEvent("booksUpdated", allBooks);
+ }
+
+ return true;
+ }
+
+ return false;
+ }
+
+ public boolean returnBook(int userId) throws SQLException {
+ if (bookIssuedBy() == userId) {
+ String sql = "UPDATE Book SET issuedBy = 0 where ID = ?";
+ PreparedStatement statement = ConnectionHandler.connection.prepareStatement(sql);
+
+ statement.setInt(1, this.id);
+
+ statement.executeUpdate();
+
+ ArrayList<Book> allBooks = Book.getAll(false);
+ UI.Components.BooksList.refreshBooks(allBooks);
+ if (MainWindow.serverStarted) {
+ SocketServer.server.getBroadcastOperations().sendEvent("booksUpdated", allBooks);
+ }
+
+ return true;
+ }
+
+ return false;
+ }
+
+ public void save() throws SQLException {
+ String sql = "INSERT INTO Book (title, author, issuedBy) VALUES (?, ?, ?)";
+
+ PreparedStatement statement = ConnectionHandler.connection.prepareStatement(sql);
+
+ statement.setString(1, title);
+ statement.setString(2, author);
+ statement.setInt(3, issuedBy);
+
+ statement.executeUpdate();
+
+ ArrayList<Book> allBooks = Book.getAll(false);
+ UI.Components.BooksList.refreshBooks(allBooks);
+ if (MainWindow.serverStarted) {
+ SocketServer.server.getBroadcastOperations().sendEvent("booksUpdated", allBooks);
+ }
+ }
+
+ public void delete() throws SQLException {
+ String sql = "DELETE FROM Book WHERE ID = ?";
+ PreparedStatement statement = ConnectionHandler.connection.prepareStatement(sql);
+
+ statement.setInt(1, this.id);
+
+ statement.executeUpdate();
+
+ ArrayList<Book> allBooks = Book.getAll(false);
+ UI.Components.BooksList.refreshBooks(allBooks);
+ if (MainWindow.serverStarted) {
+ SocketServer.server.getBroadcastOperations().sendEvent("booksUpdated", allBooks);
+ }
+ }
+
+ public static ArrayList<Book> getAll(boolean availableOnly) throws SQLException {
+ ArrayList<Book> books = new ArrayList<>();
+
+ String sql;
+ if (availableOnly) {
+ sql = "SELECT * FROM Book WHERE issuedBy = 0";
+ } else {
+ sql = "SELECT Book.ID, Book.title, Book.issuedBy, Book.author, " +
+ "User.userName FROM Book LEFT JOIN User ON User.ID = Book.issuedBy";
+ }
+ Statement statement = ConnectionHandler.connection.createStatement();
+
+ ResultSet response = statement.executeQuery(sql);
+ while (response.next()) {
+ books.add(new Book(response.getInt("ID"), response.getString("title"), response.getString("author"), response.getInt("issuedBy"), response.getString("userName")));
+ }
+
+ return books;
+ }
+
+ public static DataListener<JSONMessage> getListHandler(boolean availableOnly) {
+ return ((client, data, ackSender) -> {
+ ArrayList<Book> books = getAll(availableOnly);
+ client.sendEvent(availableOnly ? "availableBooksList" : "allBooksList", books);
+ });
+ }
+
+ public static DataListener<JSONMessage> issueHandler() {
+ return ((client, data, ackSender) -> {
+ Map d = data.getData();
+
+ int userId = (int) d.get("id");
+ Book b = new Book((int) d.get("bookId"));
+
+ if (b.issue(userId)) {
+ client.sendEvent("issued");
+ } else {
+ client.sendEvent("alreadyIssued");
+ }
+ });
+ }
+
+ public static DataListener<JSONMessage> returnHandler() {
+ return ((client, data, ackSender) -> {
+ Map d = data.getData();
+
+ int userId = (int) d.get("id");
+ Book b = new Book((int) d.get("bookId"));
+
+ if (b.returnBook(userId)) {
+ client.sendEvent("returned");
+ } else {
+ client.sendEvent("returnFailed");
+ }
+ });
+ }
+
+ public static void createTable() throws SQLException {
+ String sql = "CREATE TABLE IF NOT EXISTS Book (" +
+ "ID INTEGER NOT NULL PRIMARY KEY, " +
+ "title TEXT NOT NULL, " +
+ "author text NOT NULL, " +
+ "issuedBy INTEGER NOT NULL," +
+ "FOREIGN KEY(issuedBy) REFERENCES User(ID)" +
+ ");";
+
+ Statement statement = ConnectionHandler.connection.createStatement();
+ statement.execute(sql);
+ }
+}