-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataPersistence.java
More file actions
174 lines (154 loc) · 5.97 KB
/
DataPersistence.java
File metadata and controls
174 lines (154 loc) · 5.97 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
package org.app.util;
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;
/**
* Data persistence utility for BUP UCAM Assignment Tracker
* Handles saving and loading application data to/from JSON files
*/
public class DataPersistence {
private static final String DATA_DIR = "data";
private static final String USERS_FILE = DATA_DIR + "/users.json";
private static final String COURSES_FILE = DATA_DIR + "/courses.json";
private static final String ASSIGNMENTS_FILE = DATA_DIR + "/assignments.json";
private static final String SUBMISSIONS_FILE = DATA_DIR + "/submissions.json";
private static final String COUNTERS_FILE = DATA_DIR + "/counters.json";
private ObjectMapper objectMapper;
public DataPersistence() {
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 users data to JSON file
*/
public void saveUsers(Map<String, User> users, Map<String, Teacher> teachers,
Map<String, Student> students, int userCounter) {
try {
UserData userData = new UserData();
userData.users = users;
userData.teachers = teachers;
userData.students = students;
userData.userCounter = userCounter;
objectMapper.writeValue(new File(USERS_FILE), userData);
} catch (IOException e) {
System.err.println("Error saving users data: " + e.getMessage());
}
}
/**
* Load users data from JSON file
*/
public UserData loadUsers() {
try {
File file = new File(USERS_FILE);
if (file.exists()) {
System.out.println("Loading users from: " + file.getAbsolutePath());
UserData userData = objectMapper.readValue(file, UserData.class);
System.out.println("✓ Loaded " + userData.users.size() + " users successfully");
return userData;
} else {
System.out.println("No existing users file found - starting fresh");
}
} catch (IOException e) {
System.err.println("Error loading users data: " + e.getMessage());
e.printStackTrace();
}
return new UserData(); // Return empty data if file doesn't exist or error occurs
}
/**
* Save courses data to JSON file
*/
public void saveCourses(Map<String, Course> courses, int courseCounter) {
try {
CourseData courseData = new CourseData();
courseData.courses = courses;
courseData.courseCounter = courseCounter;
objectMapper.writeValue(new File(COURSES_FILE), courseData);
} catch (IOException e) {
System.err.println("Error saving courses data: " + e.getMessage());
}
}
/**
* Load courses data from JSON file
*/
public CourseData loadCourses() {
try {
File file = new File(COURSES_FILE);
if (file.exists()) {
return objectMapper.readValue(file, CourseData.class);
}
} catch (IOException e) {
System.err.println("Error loading courses data: " + e.getMessage());
}
return new CourseData();
}
/**
* Save assignments data to JSON file
*/
public void saveAssignments(Map<String, Assignment> assignments, Map<String, Submission> submissions,
int assignmentCounter, int submissionCounter) {
try {
AssignmentData assignmentData = new AssignmentData();
assignmentData.assignments = assignments;
assignmentData.submissions = submissions;
assignmentData.assignmentCounter = assignmentCounter;
assignmentData.submissionCounter = submissionCounter;
objectMapper.writeValue(new File(ASSIGNMENTS_FILE), assignmentData);
} catch (IOException e) {
System.err.println("Error saving assignments data: " + e.getMessage());
}
}
/**
* Load assignments data from JSON file
*/
public AssignmentData loadAssignments() {
try {
File file = new File(ASSIGNMENTS_FILE);
if (file.exists()) {
return objectMapper.readValue(file, AssignmentData.class);
}
} catch (IOException e) {
System.err.println("Error loading assignments data: " + e.getMessage());
}
return new AssignmentData();
}
/**
* Check if data files exist (indicates if this is first run)
*/
public boolean dataExists() {
return new File(USERS_FILE).exists();
}
/**
* Data holder classes for JSON serialization
*/
public static class UserData {
public Map<String, User> users = new HashMap<>();
public Map<String, Teacher> teachers = new HashMap<>();
public Map<String, Student> students = new HashMap<>();
public int userCounter = 1;
}
public static class CourseData {
public Map<String, Course> courses = new HashMap<>();
public int courseCounter = 1;
}
public static class AssignmentData {
public Map<String, Assignment> assignments = new HashMap<>();
public Map<String, Submission> submissions = new HashMap<>();
public int assignmentCounter = 1;
public int submissionCounter = 1;
}
}