forked from CodingMirage/JavaSwing-SessionTracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppBackend.java
More file actions
102 lines (80 loc) · 3.7 KB
/
Copy pathAppBackend.java
File metadata and controls
102 lines (80 loc) · 3.7 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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class AppBackend {
static Map<String, List<String>> configMap = new LinkedHashMap<>();
protected void setupDatabase() {
String localDbUrl = ConfigLoader.getLocalDBUrl();
// Creates Configuration Table if doesn't exit
OptionsManager.createConfigurationTableLocal(localDbUrl);
try {
// Connect to the database
try (Connection conn = DriverManager.getConnection(localDbUrl)) {
if (conn != null) {
try (Statement stmt = conn.createStatement()) {
// Creates the sessions table if it doesn't already exist
OptionsManager.createRecordsTableLocal(conn);
}
}
}
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());
}
configMap = CloudDatabaseUpload.loadConfigMap(localDbUrl);
}
protected Map<String,String> getData(String name, String usn, String sub) {
int year = LocalDate.now().getYear() % 2000 - Integer.parseInt(usn.substring(3, 5)) + 1;
int sem = LocalDate.now().getMonthValue() < 6 ? year * 2 - 2/* even sem */ : year * 2 - 1/* odd */ ;
int usnNumber = Integer.parseInt(usn.substring(7));
String batch = (usnNumber <= 30 || (usnNumber >= 60 && usnNumber <= 90)) ? "I" : "II";
String deptCode = usn.substring(5, 7);
List<String> depts = configMap.get("Department");
String dept = new String();
for(String dep : depts) {
if (dep.toLowerCase().contains(deptCode.toLowerCase())) {
dept = dep;
break;
}
}
Map<String, String> studentMap = new HashMap<>();
studentMap.put("name", name);
studentMap.put("usn", usn);
// PascalCase for category names
studentMap.put("Batch", batch);
studentMap.put("Department", dept);
studentMap.put("Sem", String.valueOf(sem));
studentMap.put("Subject", sub);
//Order by category for details
return studentMap;
}
protected void insertData(String[] StudentData) {
String localDbUrl = ConfigLoader.getLocalDBUrl();
String insertSQL = """
INSERT INTO {TABLE} (name, usn, login_time, details,logout_time, session_id)
VALUES (?, ?, ?, ?, ?, ?);
""".replace("{TABLE}", ConfigLoader.config.getProperty("LOCAL_TABLE"));
try (Connection conn = DriverManager.getConnection(localDbUrl);
PreparedStatement pstmt = conn.prepareStatement(insertSQL)) {
pstmt.setString(1, StudentData[0]);
pstmt.setString(2, StudentData[1]);
pstmt.setString(3, StudentData[2]);
pstmt.setString(4, StudentData[3]);
pstmt.setString(5, StudentData[4]);
pstmt.setString(6, StudentData[5]);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());
}
}
}