-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEProfileServlet.java
More file actions
116 lines (103 loc) · 5.03 KB
/
Copy pathEProfileServlet.java
File metadata and controls
116 lines (103 loc) · 5.03 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
package Servlet;
import model.ProfileManager;
import model.ServiceProvider;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
@WebServlet("/event/profiles")
public class EProfileServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
// Get the real file path with enhanced logging
String filePath = getServletContext().getRealPath("/WEB-INF/Data/profiles.txt");
System.out.println("[DEBUG] Attempting to load profiles from: " + filePath);
// Verify file existence
File dataFile = new File(filePath);
if (!dataFile.exists()) {
System.err.println("[ERROR] Data file not found at: " + filePath);
req.setAttribute("error", "Profile data not available. Please try again later.");
req.getRequestDispatcher("/event/EProfile/EProfile.jsp").forward(req, resp);
return;
}
// Check if file is empty
if (dataFile.length() == 0) {
System.out.println("[WARNING] Data file exists but is empty");
}
ProfileManager profileManager = new ProfileManager(filePath);
String action = req.getParameter("action");
if ("view".equals(action)) {
handleSingleProfile(req, resp, profileManager);
} else {
handleAllProfiles(req, resp, profileManager);
}
} catch (Exception e) {
System.err.println("[ERROR] Exception in EProfileServlet: " + e.getMessage());
e.printStackTrace();
req.setAttribute("error", "An error occurred while loading profiles.");
req.getRequestDispatcher("/event/EProfile/EProfile.jsp").forward(req, resp);
}
}
private void handleSingleProfile(HttpServletRequest req, HttpServletResponse resp,
ProfileManager profileManager) throws ServletException, IOException {
String id = req.getParameter("id");
if (id != null) {
ServiceProvider provider = profileManager.getProfileById(id);
if (provider != null) {
System.out.println("[DEBUG] Found profile with ID: " + id);
req.setAttribute("provider", provider);
req.getRequestDispatcher("/event/EProfile/profiles.jsp").forward(req, resp);
} else {
System.out.println("[WARNING] Profile not found for ID: " + id);
resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Profile not found");
}
} else {
System.out.println("[WARNING] Missing ID parameter for view action");
resp.sendRedirect("/event/profiles");
}
}
private void handleAllProfiles(HttpServletRequest req, HttpServletResponse resp,
ProfileManager profileManager) throws ServletException, IOException {
List<ServiceProvider> providers = profileManager.getAllProfiles();
System.out.println("[DEBUG] Loaded " + providers.size() + " profiles");
// Sort profiles by rating in descending order using bubble sort
providers = bubbleSortByRating(providers);
System.out.println("[DEBUG] Sorted " + providers.size() + " profiles by rating");
if (providers.isEmpty()) {
System.out.println("[INFO] No profiles found in data file");
}
req.setAttribute("providers", providers);
req.getRequestDispatcher("/event/EProfile/EProfile.jsp").forward(req, resp);
}
private List<ServiceProvider> bubbleSortByRating(List<ServiceProvider> providers) {
int n = providers.size();
for (int i = 0; i < n - 1; i++) {
boolean swapped = false;
for (int j = 0; j < n - i - 1; j++) {
// Compare adjacent providers' ratings for descending order
if (providers.get(j).getRating() < providers.get(j + 1).getRating()) {
// Swap providers
ServiceProvider temp = providers.get(j);
providers.set(j, providers.get(j + 1));
providers.set(j + 1, temp);
swapped = true;
}
}
// If no swapping occurred, array is sorted
if (!swapped) {
break;
}
}
// Log sorted order for debugging
System.out.println("[DEBUG] Sorted profiles ratings: ");
for (ServiceProvider p : providers) {
System.out.println("[DEBUG] Name: " + p.getName() + ", Rating: " + p.getRating());
}
return providers;
}
}