-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPreformanceFeedbackController.java
More file actions
119 lines (99 loc) · 4.26 KB
/
PreformanceFeedbackController.java
File metadata and controls
119 lines (99 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package jobinterviewpreparationsystem;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import jobinterviewpreparationsystem.DB.DatabaseConnection;
public class PreformanceFeedbackController {
@FXML
private TextField userIdField;
@FXML
private Label feedbackLabel;
private DatabaseConnection dbConnection; // Database connection instance
public PreformanceFeedbackController() throws SQLException {
dbConnection = DatabaseConnection.getInstance(); // Initialize the database connection
}
// Handler for the Enter button
@FXML
private void handleEnterButton(ActionEvent event) {
// Get the user_id from the TextField
String userIdInput = userIdField.getText().trim();
// Validate the input
if (userIdInput.isEmpty()) {
showAlert("Error", "User ID cannot be empty!");
return;
}
try {
int userId = Integer.parseInt(userIdInput);
if (isValidUserId(userId)) {
// Fetch and display feedback for this user
String feedback = getFeedbackForUser(userId);
if (feedback != null) {
feedbackLabel.setText(feedback); // Display feedback
} else {
feedbackLabel.setText("No feedback available for this user.");
}
} else {
showAlert("Error", "Invalid User ID. Please enter a valid User ID.");
}
} catch (NumberFormatException e) {
showAlert("Error", "Invalid User ID format. Please enter a numeric User ID.");
} catch (SQLException e) {
e.printStackTrace();
showAlert("Error", "Database error occurred. Please try again.");
}
}
private boolean isValidUserId(int userId) throws SQLException {
String query = "SELECT COUNT(*) FROM users_table WHERE user_id = ?";
try (Connection connection = dbConnection.getConnection();
PreparedStatement stmt = connection.prepareStatement(query)) {
stmt.setInt(1, userId); // Set the user_id parameter
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
return rs.getInt(1) > 0; // If the count is greater than 0, the user exists
}
}
return false;
}
private String getFeedbackForUser(int userId) throws SQLException {
String query = "SELECT feedback_text, rating FROM feedback_table WHERE job_seeker_id = ?";
try (Connection connection = dbConnection.getConnection();
PreparedStatement stmt = connection.prepareStatement(query)) {
stmt.setInt(1, userId); // Set the user_id parameter
ResultSet rs = stmt.executeQuery();
StringBuilder feedbackBuilder = new StringBuilder();
// Iterate through the results and build the feedback string
while (rs.next()) {
String feedbackText = rs.getString("feedback_text");
int rating = rs.getInt("rating");
feedbackBuilder.append("Rating: ").append(rating).append("\n");
feedbackBuilder.append("Feedback: ").append(feedbackText).append("\n\n");
}
return feedbackBuilder.length() > 0 ? feedbackBuilder.toString() : null;
}
}
private void showAlert(String title, String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
// Go back to the previous screen (implement navigation as per your application's flow)
@FXML
private void GoBackButton(ActionEvent event) {
JobInterviewPreparationSystem.showJobSeekercreen(); // Method to go back to the previous screen
}
}