-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleDataPersistence.java
More file actions
178 lines (158 loc) · 6.16 KB
/
SimpleDataPersistence.java
File metadata and controls
178 lines (158 loc) · 6.16 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
package org.app.util;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.app.model.*;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* Simplified Data persistence utility for BUP UCAM Assignment Tracker
*/
public class SimpleDataPersistence {
private static final String DATA_DIR = "data";
private static final String USERS_FILE = DATA_DIR + "/users_simple.json";
private static final String SETTINGS_FILE = DATA_DIR + "/settings.json";
private ObjectMapper objectMapper;
public SimpleDataPersistence() {
this.objectMapper = new ObjectMapper();
this.objectMapper.registerModule(new JavaTimeModule());
this.objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
this.objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
// Create data directory if it doesn't exist
createDataDirectory();
}
private void createDataDirectory() {
File dataDir = new File(DATA_DIR);
if (!dataDir.exists()) {
dataDir.mkdirs();
}
}
/**
* Save simple user data
*/
public void saveUserCredentials(Map<String, SimpleUserData> userData) {
try {
objectMapper.writeValue(new File(USERS_FILE), userData);
System.out.println("✓ Saved " + userData.size() + " user credentials");
} catch (IOException e) {
System.err.println("Error saving user credentials: " + e.getMessage());
}
}
/**
* Load simple user data
*/
public Map<String, SimpleUserData> loadUserCredentials() {
try {
File file = new File(USERS_FILE);
if (file.exists()) {
TypeReference<Map<String, SimpleUserData>> typeRef = new TypeReference<Map<String, SimpleUserData>>() {};
Map<String, SimpleUserData> userData = objectMapper.readValue(file, typeRef);
System.out.println("✓ Loaded " + userData.size() + " user credentials");
return userData;
}
} catch (IOException e) {
System.err.println("Error loading user credentials: " + e.getMessage());
e.printStackTrace();
}
return new HashMap<>();
}
/**
* Save application settings
*/
public void saveSettings(AppSettings settings) {
try {
objectMapper.writeValue(new File(SETTINGS_FILE), settings);
} catch (IOException e) {
System.err.println("Error saving settings: " + e.getMessage());
}
}
/**
* Load application settings
*/
public AppSettings loadSettings() {
try {
File file = new File(SETTINGS_FILE);
if (file.exists()) {
return objectMapper.readValue(file, AppSettings.class);
}
} catch (IOException e) {
System.err.println("Error loading settings: " + e.getMessage());
}
return new AppSettings();
}
public boolean dataExists() {
return new File(USERS_FILE).exists();
}
/**
* Simple user data structure for persistence
*/
public static class SimpleUserData {
public String userId;
public String name;
public String email;
public String password;
public String role; // "TEACHER" or "STUDENT"
public String department; // for teachers
public String employeeId; // for teachers
public String studentId; // for students
public String program; // for students
public int semester; // for students
public boolean isActive = true;
public SimpleUserData() {}
public SimpleUserData(User user) {
this.userId = user.getUserId();
this.name = user.getName();
this.email = user.getEmail();
this.password = user.getPassword();
this.role = user.getRole().toString();
this.isActive = user.isActive();
if (user instanceof Teacher) {
Teacher teacher = (Teacher) user;
this.department = teacher.getDepartment();
this.employeeId = teacher.getEmployeeId();
} else if (user instanceof Student) {
Student student = (Student) user;
this.studentId = student.getStudentId();
this.program = student.getProgram();
this.semester = student.getSemester();
}
}
public User toUser() {
// Handle both uppercase and proper case role names
String roleUpper = role != null ? role.toUpperCase() : "";
if ("TEACHER".equals(roleUpper) || "Teacher".equals(role)) {
if (department != null && employeeId != null) {
Teacher teacher = new Teacher(userId, name, email, password, department, employeeId);
teacher.setActive(isActive);
return teacher;
} else {
System.err.println("Warning: Teacher data incomplete for " + name);
}
} else if ("STUDENT".equals(roleUpper) || "Student".equals(role)) {
if (studentId != null && program != null) {
Student student = new Student(userId, name, email, password, studentId, program, semester);
student.setActive(isActive);
return student;
} else {
System.err.println("Warning: Student data incomplete for " + name);
}
}
System.err.println("Warning: Could not convert user data for " + name + " (role: " + role + ")");
return null;
}
}
/**
* Application settings
*/
public static class AppSettings {
public int userCounter = 1;
public int courseCounter = 1;
public int assignmentCounter = 1;
public int submissionCounter = 1;
public boolean firstRun = true;
public AppSettings() {}
}
}