-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuditDAO.java
More file actions
640 lines (506 loc) · 25.1 KB
/
Copy pathAuditDAO.java
File metadata and controls
640 lines (506 loc) · 25.1 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
package com.securefileshare.dao;
import com.securefileshare.models.Activity;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class AuditDAO {
private static final String DB_URL = "jdbc:mysql://localhost:3306/securefileshare";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "durga2005";
private Connection getConnection() throws SQLException {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
} catch (ClassNotFoundException e) {
throw new SQLException("MySQL JDBC Driver not found", e);
}
}
public void logActivity(int userId, String actionType, String description,
String ipAddress, String status) throws SQLException {
String sql = "INSERT INTO access_logs (user_id, action_type, description, ip_address, " +
"access_time, status) VALUES (?, ?, ?, ?, NOW(), ?)";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, userId);
pstmt.setString(2, actionType);
pstmt.setString(3, description);
pstmt.setString(4, ipAddress);
pstmt.setString(5, status);
pstmt.executeUpdate();
System.out.println("Activity logged: " + actionType + " - " + description);
}
}
public void logFileActivity(int userId, int fileId, String actionType,
String description, String ipAddress, String status) throws SQLException {
String sql = "INSERT INTO access_logs (user_id, file_id, action_type, description, " +
"ip_address, access_time, status) VALUES (?, ?, ?, ?, ?, NOW(), ?)";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, userId);
pstmt.setInt(2, fileId);
pstmt.setString(3, actionType);
pstmt.setString(4, description);
pstmt.setString(5, ipAddress);
pstmt.setString(6, status);
pstmt.executeUpdate();
}
}
public List<ActivityLog> getRecentActivities(int userId, int limit) {
List<ActivityLog> activities = new ArrayList<>();
String sql = "SELECT * FROM access_logs WHERE user_id = ? ORDER BY access_time DESC LIMIT ?";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, userId);
stmt.setInt(2, limit);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
ActivityLog log = new ActivityLog();
log.setId(rs.getInt("log_id"));
log.setUserId(rs.getInt("user_id"));
try {
log.setFileId(rs.getInt("file_id"));
} catch (SQLException e) {
log.setFileId(0);
}
log.setAction(rs.getString("action_type"));
log.setDescription(rs.getString("description"));
log.setIpAddress(rs.getString("ip_address"));
log.setStatus(rs.getString("status"));
log.setTimestamp(rs.getTimestamp("access_time"));
activities.add(log);
}
System.out.println("Retrieved " + activities.size() + " recent activities for user: " + userId);
} catch (SQLException e) {
System.err.println("Error getting recent activities for user " + userId + ": " + e.getMessage());
e.printStackTrace();
}
return activities;
}
public List<ActivityLog> getRecentActivities(int limit) {
List<ActivityLog> activities = new ArrayList<>();
String sql = "SELECT * FROM access_logs ORDER BY access_time DESC LIMIT ?";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, limit);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
ActivityLog log = new ActivityLog();
log.setId(rs.getInt("log_id"));
log.setUserId(rs.getInt("user_id"));
try {
log.setFileId(rs.getInt("file_id"));
} catch (SQLException e) {
log.setFileId(0);
}
log.setAction(rs.getString("action_type"));
log.setDescription(rs.getString("description"));
log.setIpAddress(rs.getString("ip_address"));
log.setStatus(rs.getString("status"));
log.setTimestamp(rs.getTimestamp("access_time"));
activities.add(log);
}
} catch (SQLException e) {
System.err.println("Error getting recent activities: " + e.getMessage());
e.printStackTrace();
}
return activities;
}
public List<ActivityLog> getRecentActivitiesByAction(String actionType, int limit) {
List<ActivityLog> activities = new ArrayList<>();
String sql = "SELECT * FROM access_logs WHERE action_type = ? ORDER BY access_time DESC LIMIT ?";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, actionType);
stmt.setInt(2, limit);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
ActivityLog log = new ActivityLog();
log.setId(rs.getInt("log_id"));
log.setUserId(rs.getInt("user_id"));
try {
log.setFileId(rs.getInt("file_id"));
} catch (SQLException e) {
log.setFileId(0);
}
log.setAction(rs.getString("action_type"));
log.setDescription(rs.getString("description"));
log.setIpAddress(rs.getString("ip_address"));
log.setStatus(rs.getString("status"));
log.setTimestamp(rs.getTimestamp("access_time"));
activities.add(log);
}
} catch (SQLException e) {
System.err.println("Error getting activities by action: " + e.getMessage());
e.printStackTrace();
}
return activities;
}
public List<ActivityLog> getRecentActivitiesByDateRange(int userId, Timestamp startDate, Timestamp endDate) {
List<ActivityLog> activities = new ArrayList<>();
String sql = "SELECT * FROM access_logs WHERE user_id = ? AND access_time BETWEEN ? AND ? ORDER BY access_time DESC";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, userId);
stmt.setTimestamp(2, startDate);
stmt.setTimestamp(3, endDate);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
ActivityLog log = new ActivityLog();
log.setId(rs.getInt("log_id"));
log.setUserId(rs.getInt("user_id"));
try {
log.setFileId(rs.getInt("file_id"));
} catch (SQLException e) {
log.setFileId(0);
}
log.setAction(rs.getString("action_type"));
log.setDescription(rs.getString("description"));
log.setIpAddress(rs.getString("ip_address"));
log.setStatus(rs.getString("status"));
log.setTimestamp(rs.getTimestamp("access_time"));
activities.add(log);
}
} catch (SQLException e) {
System.err.println("Error getting activities by date range: " + e.getMessage());
e.printStackTrace();
}
return activities;
}
public List<ActivityLog> getRecentActivitiesByAction(int userId, String[] actions, int limit) {
List<ActivityLog> activities = new ArrayList<>();
StringBuilder placeholders = new StringBuilder();
for (int i = 0; i < actions.length; i++) {
if (i > 0) placeholders.append(",");
placeholders.append("?");
}
String sql = "SELECT * FROM access_logs WHERE user_id = ? AND action_type IN (" +
placeholders.toString() + ") ORDER BY access_time DESC LIMIT ?";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
int paramIndex = 1;
stmt.setInt(paramIndex++, userId);
for (String action : actions) {
stmt.setString(paramIndex++, action);
}
stmt.setInt(paramIndex, limit);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
ActivityLog log = new ActivityLog();
log.setId(rs.getInt("log_id"));
log.setUserId(rs.getInt("user_id"));
try {
log.setFileId(rs.getInt("file_id"));
} catch (SQLException e) {
log.setFileId(0);
}
log.setAction(rs.getString("action_type"));
log.setDescription(rs.getString("description"));
log.setIpAddress(rs.getString("ip_address"));
log.setStatus(rs.getString("status"));
log.setTimestamp(rs.getTimestamp("access_time"));
activities.add(log);
}
} catch (SQLException e) {
System.err.println("Error getting recent activities by action: " + e.getMessage());
e.printStackTrace();
}
return activities;
}
public int getDownloadCount(int userId) throws SQLException {
String sql = "SELECT COUNT(*) FROM access_logs WHERE user_id = ? AND action_type = 'DOWNLOAD' AND status = 'SUCCESS'";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, userId);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return rs.getInt(1);
}
}
return 0;
}
public int getUploadCount(int userId) throws SQLException {
String sql = "SELECT COUNT(*) FROM access_logs WHERE user_id = ? AND action_type = 'UPLOAD' AND status = 'SUCCESS'";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, userId);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return rs.getInt(1);
}
}
return 0;
}
public List<Activity> getRecentUserActivity(int userId, int limit) throws SQLException {
List<Activity> activities = new ArrayList<>();
String sql = "SELECT log_id, user_id, action_type, description, access_time " +
"FROM access_logs WHERE user_id = ? ORDER BY access_time DESC LIMIT ?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, userId);
pstmt.setInt(2, limit);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Activity activity = new Activity();
activity.setId(rs.getInt("log_id"));
activity.setUserId(rs.getInt("user_id"));
activity.setType(rs.getString("action_type"));
activity.setDescription(rs.getString("description"));
activity.setTimestamp(rs.getTimestamp("access_time"));
activities.add(activity);
}
} catch (SQLException e) {
System.err.println("Error loading activities: " + e.getMessage());
throw e;
}
return activities;
}
public List<Activity> getAllLogs(int limit) {
List<Activity> activities = new ArrayList<>();
String sql = "SELECT log_id, user_id, action_type, description, access_time, ip_address, status " +
"FROM access_logs ORDER BY access_time DESC LIMIT ?";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, limit);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Activity activity = new Activity();
activity.setId(rs.getInt("log_id"));
activity.setUserId(rs.getInt("user_id"));
activity.setAction(rs.getString("action_type"));
activity.setDescription(rs.getString("description"));
activity.setTimestamp(rs.getTimestamp("access_time"));
activity.setIpAddress(rs.getString("ip_address"));
activity.setStatus(rs.getString("status"));
activities.add(activity);
}
} catch (SQLException e) {
System.err.println("Error getting logs: " + e.getMessage());
e.printStackTrace();
}
return activities;
}
public List<Activity> getUserLogs(int userId, int limit) throws SQLException {
List<Activity> activities = new ArrayList<>();
String sql = "SELECT log_id, user_id, action_type, description, access_time " +
"FROM access_logs WHERE user_id = ? ORDER BY access_time DESC LIMIT ?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, userId);
pstmt.setInt(2, limit);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Activity activity = new Activity();
activity.setId(rs.getInt("log_id"));
activity.setUserId(rs.getInt("user_id"));
activity.setType(rs.getString("action_type"));
activity.setDescription(rs.getString("description"));
activity.setTimestamp(rs.getTimestamp("access_time"));
activities.add(activity);
}
} catch (SQLException e) {
System.err.println("Error loading user logs: " + e.getMessage());
throw e;
}
return activities;
}
/**
* FIXED: Get activities by action type (returns Activity objects)
* This method was missing and causing AdminServlet error
*/
public List<Activity> getActivitiesByType(String actionType, int limit) {
List<Activity> activities = new ArrayList<>();
String sql = "SELECT log_id, user_id, action_type, description, access_time, ip_address, status " +
"FROM access_logs WHERE action_type = ? ORDER BY access_time DESC LIMIT ?";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, actionType);
stmt.setInt(2, limit);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Activity activity = new Activity();
activity.setId(rs.getInt("log_id"));
activity.setUserId(rs.getInt("user_id"));
activity.setAction(rs.getString("action_type"));
activity.setDescription(rs.getString("description"));
activity.setTimestamp(rs.getTimestamp("access_time"));
activity.setIpAddress(rs.getString("ip_address"));
activity.setStatus(rs.getString("status"));
activities.add(activity);
}
System.out.println("Retrieved " + activities.size() + " activities of type: " + actionType);
} catch (SQLException e) {
System.err.println("Error getting activities by type '" + actionType + "': " + e.getMessage());
e.printStackTrace();
}
return activities;
}
public int getTotalActivities() throws SQLException {
String sql = "SELECT COUNT(*) FROM access_logs";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return rs.getInt(1);
}
}
return 0;
}
public int clearOldLogs() throws SQLException {
String sql = "DELETE FROM access_logs WHERE access_time < DATE_SUB(NOW(), INTERVAL 30 DAY)";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
return pstmt.executeUpdate();
}
}
public int getUnreadNotifications(int userId) throws SQLException {
String sql = "SELECT COUNT(*) FROM access_logs WHERE user_id = ? AND status = 'SUCCESS' " +
"AND access_time > DATE_SUB(NOW(), INTERVAL 24 HOUR)";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, userId);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return Math.min(rs.getInt(1), 9);
}
}
return 0;
}
public String getLastLogin(int userId) throws SQLException {
String sql = "SELECT MAX(access_time) FROM access_logs " +
"WHERE user_id = ? AND action_type = 'LOGIN' AND status = 'SUCCESS'";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, userId);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
Timestamp lastLogin = rs.getTimestamp(1);
if (lastLogin != null) {
return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(lastLogin);
}
}
}
return null;
}
/**
* Get total download count for a specific user
*/
public int getUserDownloadCount(int userId) throws SQLException {
String sql = "SELECT COUNT(*) FROM access_logs WHERE user_id = ? AND action_type = 'DOWNLOAD' AND status = 'SUCCESS'";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, userId);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return rs.getInt(1);
}
} catch (SQLException e) {
System.err.println("Error getting download count for user " + userId + ": " + e.getMessage());
e.printStackTrace();
throw e;
}
return 0;
}
/**
* Get today's download count for a specific user
*/
public int getUserTodayDownloadCount(int userId) throws SQLException {
String sql = "SELECT COUNT(*) FROM access_logs WHERE user_id = ? AND action_type = 'DOWNLOAD' " +
"AND status = 'SUCCESS' AND DATE(access_time) = CURDATE()";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, userId);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return rs.getInt(1);
}
} catch (SQLException e) {
System.err.println("Error getting today's download count for user " + userId + ": " + e.getMessage());
e.printStackTrace();
throw e;
}
return 0;
}
/**
* Get total upload count for a specific user
*/
public int getUserUploadCount(int userId) throws SQLException {
String sql = "SELECT COUNT(*) FROM access_logs WHERE user_id = ? AND action_type = 'UPLOAD' AND status = 'SUCCESS'";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, userId);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return rs.getInt(1);
}
} catch (SQLException e) {
System.err.println("Error getting upload count for user " + userId + ": " + e.getMessage());
e.printStackTrace();
throw e;
}
return 0;
}
public String getLastLoginIP(int userId) throws SQLException {
String sql = "SELECT ip_address FROM access_logs " +
"WHERE user_id = ? AND action_type = 'LOGIN' AND status = 'SUCCESS' " +
"ORDER BY access_time DESC LIMIT 1";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, userId);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return rs.getString("ip_address");
}
}
return null;
}
public void updateLastLogin(int userId, String ipAddress) throws SQLException {
String sql = "UPDATE users SET last_login = NOW() WHERE user_id = ?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, userId);
pstmt.executeUpdate();
}
logActivity(userId, "LOGIN", "User logged in", ipAddress, "SUCCESS");
}
public static class ActivityLog {
private int id;
private int userId;
private int fileId;
private String action;
private String description;
private String ipAddress;
private String status;
private Timestamp timestamp;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public int getUserId() { return userId; }
public void setUserId(int userId) { this.userId = userId; }
public int getFileId() { return fileId; }
public void setFileId(int fileId) { this.fileId = fileId; }
public String getAction() { return action; }
public void setAction(String action) { this.action = action; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public String getIpAddress() { return ipAddress; }
public void setIpAddress(String ipAddress) { this.ipAddress = ipAddress; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
public Timestamp getTimestamp() { return timestamp; }
public void setTimestamp(Timestamp timestamp) { this.timestamp = timestamp; }
@Override
public String toString() {
return "ActivityLog{" +
"id=" + id +
", userId=" + userId +
", fileId=" + fileId +
", action='" + action + '\'' +
", description='" + description + '\'' +
", ipAddress='" + ipAddress + '\'' +
", status='" + status + '\'' +
", timestamp=" + timestamp +
'}';
}
}
// REMOVED: The placeholder method that returned null
// Now replaced with proper implementation above
}