Skip to content

Commit

Permalink
Alerts about errors were added
Browse files Browse the repository at this point in the history
  • Loading branch information
ituvtu committed May 30, 2024
1 parent ffab1d9 commit cc5f7dd
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 76 deletions.
57 changes: 1 addition & 56 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,25 +136,12 @@
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>ituvtu.server.EntryPoint</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>ituvtu.server.EntryPoint</mainClass>
<mainClass>ituvtu.server.view.ServerApp</mainClass>
</configuration>
</plugin>
<plugin>
Expand All @@ -167,48 +154,6 @@
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>module-info.class</exclude>
<exclude>META-INF/*.json</exclude>
</excludes>
</filter>
</filters>
<transformers>
<!-- Merge all META-INF/MANIFEST.MF files into one -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>ituvtu.server.EntryPoint</mainClass>
</transformer>
<!-- Merge all service files into one -->
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/services/java.sql.Driver</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/LICENSE</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/LICENSE.md</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/NOTICE.md</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/mailcap.default</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/mimetypes.default</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/LICENSE.txt</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Expand Down
35 changes: 31 additions & 4 deletions src/main/java/ituvtu/server/controller/ConfigController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
21 changes: 14 additions & 7 deletions src/main/java/ituvtu/server/database/DatabaseConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/ituvtu/server/model/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 25 additions & 8 deletions src/main/java/ituvtu/server/view/ServerApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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());
}
});
}
Expand All @@ -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();
});
}
}

0 comments on commit cc5f7dd

Please sign in to comment.