From cc5f7dd6ef8129950ec0b8d62fbd6c5c6f861210 Mon Sep 17 00:00:00 2001 From: ituvtu Date: Thu, 30 May 2024 10:56:52 +0200 Subject: [PATCH] Alerts about errors were added --- pom.xml | 57 +------------------ .../server/controller/ConfigController.java | 35 ++++++++++-- .../server/database/DatabaseConnection.java | 21 ++++--- src/main/java/ituvtu/server/model/Server.java | 4 +- .../java/ituvtu/server/view/ServerApp.java | 33 ++++++++--- 5 files changed, 74 insertions(+), 76 deletions(-) diff --git a/pom.xml b/pom.xml index 3ee2ec8..03c26c3 100644 --- a/pom.xml +++ b/pom.xml @@ -136,25 +136,12 @@ ${maven.compiler.target} - - org.apache.maven.plugins - maven-jar-plugin - 3.2.2 - - - - true - ituvtu.server.EntryPoint - - - - org.openjfx javafx-maven-plugin 0.0.8 - ituvtu.server.EntryPoint + ituvtu.server.view.ServerApp @@ -167,48 +154,6 @@ shade - - - - *:* - - META-INF/*.SF - META-INF/*.DSA - META-INF/*.RSA - module-info.class - META-INF/*.json - - - - - - - ituvtu.server.EntryPoint - - - - META-INF/services/java.sql.Driver - - - META-INF/LICENSE - - - META-INF/LICENSE.md - - - META-INF/NOTICE.md - - - META-INF/mailcap.default - - - META-INF/mimetypes.default - - - META-INF/LICENSE.txt - - - diff --git a/src/main/java/ituvtu/server/controller/ConfigController.java b/src/main/java/ituvtu/server/controller/ConfigController.java index e9ea1b3..5972e19 100644 --- a/src/main/java/ituvtu/server/controller/ConfigController.java +++ b/src/main/java/ituvtu/server/controller/ConfigController.java @@ -3,6 +3,7 @@ import ituvtu.server.database.DatabaseConnection; import ituvtu.server.view.ServerApp; import javafx.fxml.FXML; +import javafx.scene.control.Alert; import javafx.scene.control.PasswordField; import javafx.scene.control.TextField; import javafx.stage.Stage; @@ -32,18 +33,44 @@ public void handleSave() { username = usernameField.getText(); password = passwordField.getText(); - // Close the configuration window - Stage stage = (Stage) portField.getScene().getWindow(); - stage.close(); + // Validate inputs + if (serverPort.isEmpty() || databaseUrl.isEmpty() || username.isEmpty() || password.isEmpty()) { + showAlert("Validation Error", "All fields must be filled out."); + return; + } + int port; + try { + port = Integer.parseInt(serverPort); + } catch (NumberFormatException e) { + showAlert("Validation Error", "Port must be a valid number."); + return; + } // Initialize the database connection - DatabaseConnection.initialize(databaseUrl, username, password); + if (!DatabaseConnection.initialize(databaseUrl, username, password)) { + showAlert("Database Connection Error", "Could not connect to the database. Please check your credentials and try again."); + return; + } // Proceed to start the server ServerApp.initializeServer(serverPort); ServerApp.showMainScreen(); + + // Close the configuration window + Stage stage = (Stage) portField.getScene().getWindow(); + stage.close(); + } catch (Exception e) { + showAlert("Error", "An unexpected error occurred: " + e.getMessage()); e.printStackTrace(); } } + + private void showAlert(String title, String message) { + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle(title); + alert.setHeaderText(null); + alert.setContentText(message); + alert.showAndWait(); + } } diff --git a/src/main/java/ituvtu/server/database/DatabaseConnection.java b/src/main/java/ituvtu/server/database/DatabaseConnection.java index 6e71087..47f37bf 100644 --- a/src/main/java/ituvtu/server/database/DatabaseConnection.java +++ b/src/main/java/ituvtu/server/database/DatabaseConnection.java @@ -3,6 +3,7 @@ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; +import java.util.Properties; public class DatabaseConnection { private static Connection connection = null; @@ -13,12 +14,6 @@ public class DatabaseConnection { private DatabaseConnection() { } - public static void initialize(String url, String user, String password) { - dbUrl = url + "?autoReconnect=true"; - dbUser = user; - dbPassword = password; - } - public static synchronized DatabaseConnection getInstance() { if (instance == null) { instance = new DatabaseConnection(); @@ -33,9 +28,21 @@ public synchronized Connection getConnection() { return connection; } + public static boolean initialize(String url, String user, String password) { + dbUrl = url; + dbUser = user; + dbPassword = password; + connection = createNewConnection(); + return connection != null; + } + private static Connection createNewConnection() { try { - return DriverManager.getConnection(dbUrl, dbUser, dbPassword); + Properties props = new Properties(); + props.setProperty("user", dbUser); + props.setProperty("password", dbPassword); + + return DriverManager.getConnection(dbUrl, props); } catch (SQLException e) { System.err.println("Database connection failed: " + e.getMessage()); return null; diff --git a/src/main/java/ituvtu/server/model/Server.java b/src/main/java/ituvtu/server/model/Server.java index 780a14c..e3460df 100644 --- a/src/main/java/ituvtu/server/model/Server.java +++ b/src/main/java/ituvtu/server/model/Server.java @@ -43,8 +43,10 @@ void notifyObserversWithLog(String message, String styleClass) { public void onStart() { String logMessage = "Server started successfully on port: " + getPort(); System.out.println(logMessage); - notifyObserversWithLog(logMessage, "log-message-color-success"); + + updateChatList(); + notifyObserversWithLog(logMessage, "log-message-color-success"); } @Override diff --git a/src/main/java/ituvtu/server/view/ServerApp.java b/src/main/java/ituvtu/server/view/ServerApp.java index 238f5d7..a9c5856 100644 --- a/src/main/java/ituvtu/server/view/ServerApp.java +++ b/src/main/java/ituvtu/server/view/ServerApp.java @@ -11,6 +11,7 @@ import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; +import javafx.scene.control.Alert; import javafx.stage.Stage; import java.util.Objects; @@ -22,15 +23,20 @@ public class ServerApp extends Application { private static Stage primaryStage; public static void initializeServer(String portStr) { - // Convert port string to integer - int port = Integer.parseInt(portStr); - server = Server.getInstance(port); - if (serverController == null) { - serverController = new ServerController(); + try { + int port = Integer.parseInt(portStr); + server = Server.getInstance(port); + if (serverController == null) { + serverController = new ServerController(); + } + serverController.setServer(server); + server.addObserver((IServerObserver) serverController); + server.startserver(); + } catch (NumberFormatException e) { + showAlert("Initialization Error", "Port must be a valid number."); + } catch (Exception e) { + showAlert("Initialization Error", "An error occurred while starting the server: " + e.getMessage()); } - serverController.setServer(server); - server.addObserver((IServerObserver) serverController); - server.startserver(); } public void showConfigScreen() throws Exception { @@ -62,6 +68,7 @@ public static void showMainScreen() { primaryStage.show(); } catch (Exception e) { e.printStackTrace(); + showAlert("Error", "An error occurred while showing the main screen: " + e.getMessage()); } }); } @@ -87,4 +94,14 @@ public void stop() { public static void main(String[] args) { launch(args); } + + private static void showAlert(String title, String message) { + Platform.runLater(() -> { + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle(title); + alert.setHeaderText(null); + alert.setContentText(message); + alert.showAndWait(); + }); + } }