Skip to content

Commit cc5f7dd

Browse files
committed
Alerts about errors were added
1 parent ffab1d9 commit cc5f7dd

File tree

5 files changed

+74
-76
lines changed

5 files changed

+74
-76
lines changed

pom.xml

+1-56
Original file line numberDiff line numberDiff line change
@@ -136,25 +136,12 @@
136136
<target>${maven.compiler.target}</target>
137137
</configuration>
138138
</plugin>
139-
<plugin>
140-
<groupId>org.apache.maven.plugins</groupId>
141-
<artifactId>maven-jar-plugin</artifactId>
142-
<version>3.2.2</version>
143-
<configuration>
144-
<archive>
145-
<manifest>
146-
<addClasspath>true</addClasspath>
147-
<mainClass>ituvtu.server.EntryPoint</mainClass>
148-
</manifest>
149-
</archive>
150-
</configuration>
151-
</plugin>
152139
<plugin>
153140
<groupId>org.openjfx</groupId>
154141
<artifactId>javafx-maven-plugin</artifactId>
155142
<version>0.0.8</version>
156143
<configuration>
157-
<mainClass>ituvtu.server.EntryPoint</mainClass>
144+
<mainClass>ituvtu.server.view.ServerApp</mainClass>
158145
</configuration>
159146
</plugin>
160147
<plugin>
@@ -167,48 +154,6 @@
167154
<goals>
168155
<goal>shade</goal>
169156
</goals>
170-
<configuration>
171-
<filters>
172-
<filter>
173-
<artifact>*:*</artifact>
174-
<excludes>
175-
<exclude>META-INF/*.SF</exclude>
176-
<exclude>META-INF/*.DSA</exclude>
177-
<exclude>META-INF/*.RSA</exclude>
178-
<exclude>module-info.class</exclude>
179-
<exclude>META-INF/*.json</exclude>
180-
</excludes>
181-
</filter>
182-
</filters>
183-
<transformers>
184-
<!-- Merge all META-INF/MANIFEST.MF files into one -->
185-
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
186-
<mainClass>ituvtu.server.EntryPoint</mainClass>
187-
</transformer>
188-
<!-- Merge all service files into one -->
189-
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
190-
<resource>META-INF/services/java.sql.Driver</resource>
191-
</transformer>
192-
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
193-
<resource>META-INF/LICENSE</resource>
194-
</transformer>
195-
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
196-
<resource>META-INF/LICENSE.md</resource>
197-
</transformer>
198-
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
199-
<resource>META-INF/NOTICE.md</resource>
200-
</transformer>
201-
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
202-
<resource>META-INF/mailcap.default</resource>
203-
</transformer>
204-
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
205-
<resource>META-INF/mimetypes.default</resource>
206-
</transformer>
207-
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
208-
<resource>META-INF/LICENSE.txt</resource>
209-
</transformer>
210-
</transformers>
211-
</configuration>
212157
</execution>
213158
</executions>
214159
</plugin>

src/main/java/ituvtu/server/controller/ConfigController.java

+31-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import ituvtu.server.database.DatabaseConnection;
44
import ituvtu.server.view.ServerApp;
55
import javafx.fxml.FXML;
6+
import javafx.scene.control.Alert;
67
import javafx.scene.control.PasswordField;
78
import javafx.scene.control.TextField;
89
import javafx.stage.Stage;
@@ -32,18 +33,44 @@ public void handleSave() {
3233
username = usernameField.getText();
3334
password = passwordField.getText();
3435

35-
// Close the configuration window
36-
Stage stage = (Stage) portField.getScene().getWindow();
37-
stage.close();
36+
// Validate inputs
37+
if (serverPort.isEmpty() || databaseUrl.isEmpty() || username.isEmpty() || password.isEmpty()) {
38+
showAlert("Validation Error", "All fields must be filled out.");
39+
return;
40+
}
41+
int port;
42+
try {
43+
port = Integer.parseInt(serverPort);
44+
} catch (NumberFormatException e) {
45+
showAlert("Validation Error", "Port must be a valid number.");
46+
return;
47+
}
3848

3949
// Initialize the database connection
40-
DatabaseConnection.initialize(databaseUrl, username, password);
50+
if (!DatabaseConnection.initialize(databaseUrl, username, password)) {
51+
showAlert("Database Connection Error", "Could not connect to the database. Please check your credentials and try again.");
52+
return;
53+
}
4154

4255
// Proceed to start the server
4356
ServerApp.initializeServer(serverPort);
4457
ServerApp.showMainScreen();
58+
59+
// Close the configuration window
60+
Stage stage = (Stage) portField.getScene().getWindow();
61+
stage.close();
62+
4563
} catch (Exception e) {
64+
showAlert("Error", "An unexpected error occurred: " + e.getMessage());
4665
e.printStackTrace();
4766
}
4867
}
68+
69+
private void showAlert(String title, String message) {
70+
Alert alert = new Alert(Alert.AlertType.ERROR);
71+
alert.setTitle(title);
72+
alert.setHeaderText(null);
73+
alert.setContentText(message);
74+
alert.showAndWait();
75+
}
4976
}

src/main/java/ituvtu/server/database/DatabaseConnection.java

+14-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.sql.Connection;
44
import java.sql.DriverManager;
55
import java.sql.SQLException;
6+
import java.util.Properties;
67

78
public class DatabaseConnection {
89
private static Connection connection = null;
@@ -13,12 +14,6 @@ public class DatabaseConnection {
1314

1415
private DatabaseConnection() { }
1516

16-
public static void initialize(String url, String user, String password) {
17-
dbUrl = url + "?autoReconnect=true";
18-
dbUser = user;
19-
dbPassword = password;
20-
}
21-
2217
public static synchronized DatabaseConnection getInstance() {
2318
if (instance == null) {
2419
instance = new DatabaseConnection();
@@ -33,9 +28,21 @@ public synchronized Connection getConnection() {
3328
return connection;
3429
}
3530

31+
public static boolean initialize(String url, String user, String password) {
32+
dbUrl = url;
33+
dbUser = user;
34+
dbPassword = password;
35+
connection = createNewConnection();
36+
return connection != null;
37+
}
38+
3639
private static Connection createNewConnection() {
3740
try {
38-
return DriverManager.getConnection(dbUrl, dbUser, dbPassword);
41+
Properties props = new Properties();
42+
props.setProperty("user", dbUser);
43+
props.setProperty("password", dbPassword);
44+
45+
return DriverManager.getConnection(dbUrl, props);
3946
} catch (SQLException e) {
4047
System.err.println("Database connection failed: " + e.getMessage());
4148
return null;

src/main/java/ituvtu/server/model/Server.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ void notifyObserversWithLog(String message, String styleClass) {
4343
public void onStart() {
4444
String logMessage = "Server started successfully on port: " + getPort();
4545
System.out.println(logMessage);
46-
notifyObserversWithLog(logMessage, "log-message-color-success");
46+
47+
4748
updateChatList();
49+
notifyObserversWithLog(logMessage, "log-message-color-success");
4850
}
4951

5052
@Override

src/main/java/ituvtu/server/view/ServerApp.java

+25-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import javafx.fxml.FXMLLoader;
1212
import javafx.scene.Parent;
1313
import javafx.scene.Scene;
14+
import javafx.scene.control.Alert;
1415
import javafx.stage.Stage;
1516

1617
import java.util.Objects;
@@ -22,15 +23,20 @@ public class ServerApp extends Application {
2223
private static Stage primaryStage;
2324

2425
public static void initializeServer(String portStr) {
25-
// Convert port string to integer
26-
int port = Integer.parseInt(portStr);
27-
server = Server.getInstance(port);
28-
if (serverController == null) {
29-
serverController = new ServerController();
26+
try {
27+
int port = Integer.parseInt(portStr);
28+
server = Server.getInstance(port);
29+
if (serverController == null) {
30+
serverController = new ServerController();
31+
}
32+
serverController.setServer(server);
33+
server.addObserver((IServerObserver) serverController);
34+
server.startserver();
35+
} catch (NumberFormatException e) {
36+
showAlert("Initialization Error", "Port must be a valid number.");
37+
} catch (Exception e) {
38+
showAlert("Initialization Error", "An error occurred while starting the server: " + e.getMessage());
3039
}
31-
serverController.setServer(server);
32-
server.addObserver((IServerObserver) serverController);
33-
server.startserver();
3440
}
3541

3642
public void showConfigScreen() throws Exception {
@@ -62,6 +68,7 @@ public static void showMainScreen() {
6268
primaryStage.show();
6369
} catch (Exception e) {
6470
e.printStackTrace();
71+
showAlert("Error", "An error occurred while showing the main screen: " + e.getMessage());
6572
}
6673
});
6774
}
@@ -87,4 +94,14 @@ public void stop() {
8794
public static void main(String[] args) {
8895
launch(args);
8996
}
97+
98+
private static void showAlert(String title, String message) {
99+
Platform.runLater(() -> {
100+
Alert alert = new Alert(Alert.AlertType.ERROR);
101+
alert.setTitle(title);
102+
alert.setHeaderText(null);
103+
alert.setContentText(message);
104+
alert.showAndWait();
105+
});
106+
}
90107
}

0 commit comments

Comments
 (0)