Skip to content
This repository was archived by the owner on Jan 15, 2026. It is now read-only.

Commit b688458

Browse files
committed
identify 500 isseus and resolves it, align with the webapp components
1 parent 5e72119 commit b688458

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1858
-1932
lines changed

src/main/java/config/DIContainer.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@ private static void ensureInitialized() {
4747

4848
private static void initializeDAOs() throws SQLException {
4949
// DAO 인스턴스들을 한 번만 생성하여 재사용 (Singleton 패턴)
50-
register(UserDAO.class, new UserDAOImpl(connection));
51-
register(ProductDAO.class, new ProductDAOImpl(connection));
52-
register(AccessLogDAO.class, new AccessLogDAOImpl(connection));
53-
register(OrderDAO.class, new OrderDAOImpl(connection));
54-
register(CartItemDAO.class, new CartItemDAOImpl(connection));
50+
// DAOs now manage their own connections via DIContainer.getConnection()
51+
register(UserDAO.class, new UserDAOImpl());
52+
register(ProductDAO.class, new ProductDAOImpl());
53+
register(AccessLogDAO.class, new AccessLogDAOImpl());
54+
register(OrderDAO.class, new OrderDAOImpl());
55+
register(CartItemDAO.class, new CartItemDAOImpl());
56+
register(dao.ReviewDAO.class, new dao.ReviewDAO());
57+
register(dao.interfaces.SupplierDAO.class, new dao.SupplierDAOImpl());
58+
register(dao.ShipmentDAO.class, new dao.ShipmentDAO());
5559
}
5660

5761
@SuppressWarnings("unchecked")

src/main/java/controller/AccesslogController.java

Lines changed: 61 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public class AccesslogController extends HttpServlet {
3434

3535
@Override
3636
public void init() throws ServletException {
37-
Connection connection = DIContainer.getConnection();
38-
this.accessLogDAO = new AccessLogDAOImpl(connection);
37+
this.accessLogDAO = DIContainer.get(AccessLogDAO.class);
3938
}
4039

4140
@Override
@@ -57,23 +56,23 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
5756

5857
User user = (User) userObj;
5958
String pathInfo = request.getPathInfo();
60-
59+
6160
// Validate pathInfo
6261
if (pathInfo == null) {
6362
pathInfo = "/";
6463
}
6564
String acceptHeader = request.getHeader("Accept");
6665
boolean isJsonRequest = acceptHeader != null && acceptHeader.contains("application/json");
67-
66+
6867
// Check if this is a specific endpoint
6968
if (pathInfo != null && !pathInfo.equals("/")) {
7069
if (pathInfo.startsWith("/user/")) {
7170
// GET /api/accessLog/user/{userId}
7271
String userIdStr = pathInfo.substring(7);
7372
try {
7473
int userId = Integer.parseInt(userIdStr);
75-
boolean isStaff = "staff".equalsIgnoreCase(user.getRole()) ||
76-
"admin".equalsIgnoreCase(user.getRole());
74+
boolean isStaff = "staff".equalsIgnoreCase(user.getRole()) ||
75+
"admin".equalsIgnoreCase(user.getRole());
7776
if (isStaff || user.getId() == userId) {
7877
if (isJsonRequest) {
7978
getUserAccessLogsJson(response, userId);
@@ -106,8 +105,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
106105
LocalDate startDate = null, endDate = null;
107106
LocalDate today = LocalDate.now();
108107

109-
110-
//date parsing with error handling
108+
// date parsing with error handling
111109
try {
112110
if (startDateStr != null && !startDateStr.isEmpty()) {
113111
startDate = LocalDate.parse(startDateStr);
@@ -116,26 +114,26 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
116114
endDate = LocalDate.parse(endDateStr);
117115
}
118116
} catch (DateTimeParseException e) {
119-
utils.ErrorAction.handleValidationError(request, response,
120-
"Date format is invalid. Please use YYYY-MM-DD.", "AccesslogController.doPost");
117+
utils.ErrorAction.handleValidationError(request, response,
118+
"Date format is invalid. Please use YYYY-MM-DD.", "AccesslogController.doPost");
121119
return;
122120
}
123121

124122
// 3. Date validation
125123
if ((startDate != null && startDate.isAfter(today)) || (endDate != null && endDate.isAfter(today))) {
126-
utils.ErrorAction.handleValidationError(request, response,
127-
"You cannot search for access logs in the future.", "AccesslogController.doPost");
124+
utils.ErrorAction.handleValidationError(request, response,
125+
"You cannot search for access logs in the future.", "AccesslogController.doPost");
128126
return;
129127
}
130128

131-
//the start date is null and end date is not null
129+
// the start date is null and end date is not null
132130
if (startDate == null && endDate != null) {
133-
utils.ErrorAction.handleValidationError(request, response,
134-
"Please select a start date when searching with an end date.", "AccesslogController.doPost");
131+
utils.ErrorAction.handleValidationError(request, response,
132+
"Please select a start date when searching with an end date.", "AccesslogController.doPost");
135133
return;
136134
}
137135

138-
//if the start date is null and end date is null
136+
// if the start date is null and end date is null
139137
if (startDate != null && endDate == null) {
140138
endDate = today;
141139
}
@@ -172,34 +170,34 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
172170
if (isJsonRequest) {
173171
response.setContentType("application/json");
174172
response.setCharacterEncoding("UTF-8");
175-
173+
176174
JsonObject json = new JsonObject();
177175
json.addProperty("success", true);
178176
json.add("accessLogs", gson.toJsonTree(accessLogList != null ? accessLogList : Collections.emptyList()));
179177
json.addProperty("count", accessLogList != null ? accessLogList.size() : 0);
180-
178+
181179
response.getWriter().write(gson.toJson(json));
182180
} else {
183181
request.getRequestDispatcher("/WEB-INF/views/accessLog.jsp").forward(request, response);
184182
}
185183
}
186-
184+
187185
// JSON API methods
188-
private void getUserAccessLogsJson(HttpServletResponse response, int userId)
186+
private void getUserAccessLogsJson(HttpServletResponse response, int userId)
189187
throws ServletException, IOException {
190-
188+
191189
response.setContentType("application/json");
192190
response.setCharacterEncoding("UTF-8");
193-
191+
194192
try {
195193
List<AccessLog> accessLogs = accessLogDAO.getAccessLogsByUserId(userId);
196-
194+
197195
JsonObject json = new JsonObject();
198196
json.addProperty("success", true);
199197
json.add("accessLogs", gson.toJsonTree(accessLogs));
200198
json.addProperty("count", accessLogs.size());
201199
json.addProperty("userId", userId);
202-
200+
203201
response.getWriter().write(gson.toJson(json));
204202
} catch (SQLException e) {
205203
logger.log(Level.SEVERE, "Failed to retrieve access logs for userId " + userId, e);
@@ -210,48 +208,58 @@ private void getUserAccessLogsJson(HttpServletResponse response, int userId)
210208
response.getWriter().write(gson.toJson(json));
211209
}
212210
}
213-
214-
private void searchAccessLogsJson(HttpServletRequest request, HttpServletResponse response, User user)
211+
212+
private void searchAccessLogsJson(HttpServletRequest request, HttpServletResponse response, User user)
215213
throws ServletException, IOException {
216-
214+
217215
response.setContentType("application/json");
218216
response.setCharacterEncoding("UTF-8");
219-
217+
220218
String userIdStr = request.getParameter("userId");
221219
String action = request.getParameter("action");
222220
String dateFrom = request.getParameter("dateFrom");
223221
String dateTo = request.getParameter("dateTo");
224222
String ipAddress = request.getParameter("ipAddress");
225-
226-
boolean isStaff = "staff".equalsIgnoreCase(user.getRole()) ||
227-
"admin".equalsIgnoreCase(user.getRole());
228-
223+
224+
boolean isStaff = "staff".equalsIgnoreCase(user.getRole()) ||
225+
"admin".equalsIgnoreCase(user.getRole());
226+
229227
try {
230228
List<AccessLog> accessLogs;
231-
229+
232230
// If staff/admin, can search all logs; otherwise only own logs
233231
if (isStaff && userIdStr != null && !userIdStr.trim().isEmpty()) {
234-
int searchUserId = Integer.parseInt(userIdStr);
232+
int searchUserId;
233+
try {
234+
searchUserId = Integer.parseInt(userIdStr);
235+
} catch (NumberFormatException e) {
236+
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
237+
JsonObject json = new JsonObject();
238+
json.addProperty("success", false);
239+
json.addProperty("error", "Invalid User ID format");
240+
response.getWriter().write(gson.toJson(json));
241+
return;
242+
}
235243
accessLogs = accessLogDAO.getAccessLogsByUserId(searchUserId);
236244
} else {
237245
accessLogs = accessLogDAO.getAccessLogsByUserId(user.getId());
238246
}
239-
247+
240248
// Apply additional filters if provided
241249
if (action != null && !action.trim().isEmpty()) {
242250
final String actionFilter = action.trim();
243251
accessLogs = accessLogs.stream()
244-
.filter(log -> actionFilter.equalsIgnoreCase(log.getAction()))
245-
.collect(java.util.stream.Collectors.toList());
252+
.filter(log -> actionFilter.equalsIgnoreCase(log.getAction()))
253+
.collect(java.util.stream.Collectors.toList());
246254
}
247-
255+
248256
if (ipAddress != null && !ipAddress.trim().isEmpty()) {
249257
final String ipFilter = ipAddress.trim();
250258
accessLogs = accessLogs.stream()
251-
.filter(log -> log.getIpAddress() != null && log.getIpAddress().contains(ipFilter))
252-
.collect(java.util.stream.Collectors.toList());
259+
.filter(log -> log.getIpAddress() != null && log.getIpAddress().contains(ipFilter))
260+
.collect(java.util.stream.Collectors.toList());
253261
}
254-
262+
255263
// Date range filtering would require additional DAO methods
256264
// For now, filter in memory if dates provided
257265
if (dateFrom != null && dateTo != null) {
@@ -261,23 +269,24 @@ private void searchAccessLogsJson(HttpServletRequest request, HttpServletRespons
261269
final LocalDate finalStartDate = startDate;
262270
final LocalDate finalEndDate = endDate;
263271
accessLogs = accessLogs.stream()
264-
.filter(log -> {
265-
if (log.getTimestamp() == null) return false;
266-
LocalDate logDate = log.getTimestamp().toLocalDate();
267-
return (logDate.isEqual(finalStartDate) || logDate.isAfter(finalStartDate)) &&
268-
(logDate.isEqual(finalEndDate) || logDate.isBefore(finalEndDate));
269-
})
270-
.collect(java.util.stream.Collectors.toList());
272+
.filter(log -> {
273+
if (log.getTimestamp() == null)
274+
return false;
275+
LocalDate logDate = log.getTimestamp().toLocalDate();
276+
return (logDate.isEqual(finalStartDate) || logDate.isAfter(finalStartDate)) &&
277+
(logDate.isEqual(finalEndDate) || logDate.isBefore(finalEndDate));
278+
})
279+
.collect(java.util.stream.Collectors.toList());
271280
} catch (DateTimeParseException e) {
272281
// Invalid date format, ignore filter
273282
}
274283
}
275-
284+
276285
JsonObject json = new JsonObject();
277286
json.addProperty("success", true);
278287
json.add("accessLogs", gson.toJsonTree(accessLogs));
279288
json.addProperty("count", accessLogs.size());
280-
289+
281290
response.getWriter().write(gson.toJson(json));
282291
} catch (SQLException e) {
283292
logger.log(Level.SEVERE, "Failed to search access logs", e);
@@ -289,5 +298,6 @@ private void searchAccessLogsJson(HttpServletRequest request, HttpServletRespons
289298
}
290299
}
291300

292-
// POST, PUT, DELETE are not implemented (users cannot edit/delete their access logs)
301+
// POST, PUT, DELETE are not implemented (users cannot edit/delete their access
302+
// logs)
293303
}

src/main/java/controller/AdminDashboardController.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,64 +33,62 @@ public class AdminDashboardController extends HttpServlet {
3333
private ProductDAO productDAO;
3434
private OrderDAO orderDAO;
3535
private SupplierDAO supplierDAO;
36-
36+
3737
@Override
3838
public void init() throws ServletException {
3939
try {
40-
Connection connection = DIContainer.getConnection();
41-
this.userDAO = new UserDAOImpl(connection);
42-
this.productDAO = new ProductDAOImpl(connection);
43-
this.orderDAO = new OrderDAOImpl(connection);
44-
this.supplierDAO = new SupplierDAOImpl(connection);
40+
this.userDAO = DIContainer.get(UserDAO.class);
41+
this.productDAO = DIContainer.get(ProductDAO.class);
42+
this.orderDAO = DIContainer.get(OrderDAO.class);
43+
this.supplierDAO = DIContainer.get(SupplierDAO.class);
4544
} catch (Exception e) {
4645
throw new ServletException("Failed to initialize database connection", e);
4746
}
4847
}
49-
48+
5049
@Override
5150
protected void doGet(HttpServletRequest request, HttpServletResponse response)
5251
throws ServletException, IOException {
53-
52+
5453
// Check authorization
5554
HttpSession session = request.getSession(false);
5655
if (session == null) {
5756
response.sendRedirect(request.getContextPath() + "/login.jsp");
5857
return;
5958
}
60-
59+
6160
Object userObj = session.getAttribute("user");
6261
if (!(userObj instanceof User)) {
6362
response.sendRedirect(request.getContextPath() + "/login.jsp");
6463
return;
6564
}
66-
65+
6766
User currentUser = (User) userObj;
6867
if (!"staff".equalsIgnoreCase(currentUser.getRole()) && !"admin".equalsIgnoreCase(currentUser.getRole())) {
6968
response.sendRedirect(request.getContextPath() + "/login.jsp");
7069
return;
7170
}
72-
71+
7372
try {
7473
// Get statistics from database
7574
int totalUsers = userDAO.getTotalUserCount();
7675
int totalProducts = productDAO.getTotalProductCount();
7776
int totalOrders = orderDAO.getTotalOrderCount();
7877
int totalSuppliers = supplierDAO.getTotalSupplierCount();
79-
78+
8079
// Set attributes for JSP
8180
request.setAttribute("totalUsers", totalUsers);
8281
request.setAttribute("totalProducts", totalProducts);
8382
request.setAttribute("totalOrders", totalOrders);
8483
request.setAttribute("totalSuppliers", totalSuppliers);
85-
84+
8685
// Forward to admin dashboard JSP
8786
request.getRequestDispatcher("/admin-dashboard.jsp").forward(request, response);
88-
87+
8988
} catch (SQLException e) {
9089
utils.ErrorAction.handleDatabaseError(request, response, e, "AdminDashboardController.doGet");
9190
} catch (Exception e) {
9291
utils.ErrorAction.handleServerError(request, response, e, "AdminDashboardController.doGet");
9392
}
9493
}
9594
}
96-

0 commit comments

Comments
 (0)