-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathCourseDb.java
More file actions
256 lines (225 loc) · 7.73 KB
/
CourseDb.java
File metadata and controls
256 lines (225 loc) · 7.73 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
package fi.helsinki.cs.tmc.model;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import fi.helsinki.cs.tmc.core.domain.Course;
import fi.helsinki.cs.tmc.data.CourseListUtils;
import fi.helsinki.cs.tmc.core.domain.Exercise;
import fi.helsinki.cs.tmc.events.TmcEvent;
import fi.helsinki.cs.tmc.events.TmcEventBus;
import fi.helsinki.cs.tmc.core.domain.ExerciseKey;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Stores the list of available courses, the current course and its exercise
* list.
*/
public class CourseDb {
public static class ChangedEvent implements TmcEvent {}
public static final Logger logger = Logger.getLogger(CourseDb.class.getName());
private static CourseDb defaultInstance;
public static CourseDb getInstance() {
if (defaultInstance == null) {
defaultInstance = new CourseDb();
}
return defaultInstance;
}
private TmcEventBus eventBus;
private ConfigFile configFile;
private List<Course> availableCourses;
private String currentCourseName;
private Map<ExerciseKey, String> downloadedExerciseChecksums;
private CourseDb() {
this(TmcEventBus.getDefault(), new ConfigFile("CourseDb.json"));
}
public CourseDb(TmcEventBus eventBus, ConfigFile configFile) {
this.eventBus = eventBus;
this.configFile = configFile;
this.availableCourses = new ArrayList<Course>();
this.currentCourseName = null;
this.downloadedExerciseChecksums = new HashMap<ExerciseKey, String>();
try {
loadFromFile();
} catch (Exception e) {
logger.log(Level.WARNING, "Failed to load course database", e);
}
}
public List<Course> getAvailableCourses() {
return Collections.unmodifiableList(availableCourses);
}
public void setAvailableCourses(List<Course> availableCourses) {
this.availableCourses = availableCourses;
save();
}
public Course getCurrentCourse() {
return CourseListUtils.getCourseByName(availableCourses, currentCourseName);
}
public String getCurrentCourseName() {
return currentCourseName;
}
public void setCurrentCourseName(String currentCourseName) {
this.currentCourseName = currentCourseName;
save();
}
public void putDetailedCourse(Course course) {
for (int i = 0; i < availableCourses.size(); ++i) {
if (availableCourses.get(i).getName().equals(course.getName())) {
availableCourses.set(i, course);
save();
return;
}
}
}
public Exercise getExerciseByKey(ExerciseKey key) {
for (Exercise ex : getCurrentCourseExercises()) {
if (key.equals(ex.getKey())) {
return ex;
}
}
return null;
}
/**
* Returns the exercises from currently selected course.
*
* <p>
* If no course is currently selected then returns the empty collection.
*/
public List<Exercise> getCurrentCourseExercises() {
Course course = getCurrentCourse();
if (course != null) {
return course.getExercises();
} else {
return Collections.emptyList();
}
}
public Course getCourseByName(String name) {
for (Course course : availableCourses) {
if (course.getName().equals(name)) {
return course;
}
}
return null;
}
public boolean isUnlockable(Exercise ex) {
Course course = getCourseByName(ex.getCourseName());
if (course != null) {
return course.getUnlockables().contains(ex.getName());
} else {
return false;
}
}
/**
* Returns all exercises from the current course that can be unlocked (and
* must be unlocked together).
*/
public List<Exercise> getCurrentCourseUnlockableExercises() {
List<Exercise> result = new ArrayList<Exercise>();
Course course = getCurrentCourse();
if (course != null) {
List<URI> unlockables = course.getUnlockables();
if (unlockables == null) {
unlockables = Collections.emptyList();
}
for (URI exerciseName : unlockables) {
for (Exercise ex : course.getExercises()) {
if (ex.getName().equals(exerciseName.toString())) {
result.add(ex);
}
}
}
}
return result;
}
public String getDownloadedExerciseChecksum(ExerciseKey ex) {
return downloadedExerciseChecksums.get(ex);
}
/**
* Informs the course database that the exercise is considered downloaded.
*
* <p>
* Sets the downloaded checksum of the exercise to be the one reported by
* the server.
*/
public void exerciseDownloaded(Exercise ex) {
List<Exercise> exercises = new ArrayList<Exercise>();
exercises.add(ex);
this.multipleExerciseDownloaded(exercises);
}
/**
* Informs the course database that the exercises are considered downloaded.
*
* <p>
* Sets the downloaded checksums of the exercises to be the ones reported by
* the server.
*/
public void multipleExerciseDownloaded(List<Exercise> exercises) {
for (Exercise ex : exercises) {
downloadedExerciseChecksums.put(ex.getKey(), ex.getChecksum());
}
save();
}
//TODO: arrange for downloadedExerciseChecksums.put(..., null) when a project is deleted!
public void save() {
try {
saveToFile();
} catch (Exception e) {
logger.log(Level.WARNING, "Failed to save course database", e);
}
eventBus.post(new ChangedEvent());
}
private static class StoredStuff {
public List<Course> availableCourses;
public String currentCourseName;
public Map<ExerciseKey, String> downloadedExerciseChecksums;
}
private void saveToFile() throws IOException {
StoredStuff stuff = new StoredStuff();
stuff.availableCourses = this.availableCourses;
stuff.currentCourseName = this.currentCourseName;
stuff.downloadedExerciseChecksums = this.downloadedExerciseChecksums;
Writer w = configFile.getWriter();
try {
getGson().toJson(stuff, w);
} finally {
w.close();
}
}
private void loadFromFile() throws IOException {
if (!configFile.exists()) {
return;
}
Reader reader = configFile.getReader();
StoredStuff stuff;
try {
stuff = getGson().fromJson(reader, StoredStuff.class);
} finally {
reader.close();
}
if (stuff != null) {
if (stuff.availableCourses != null) {
this.availableCourses.clear();
this.availableCourses.addAll(stuff.availableCourses);
}
this.currentCourseName = stuff.currentCourseName;
if (stuff.downloadedExerciseChecksums != null) {
this.downloadedExerciseChecksums.clear();
this.downloadedExerciseChecksums.putAll(stuff.downloadedExerciseChecksums);
}
}
}
private Gson getGson() {
return new GsonBuilder()
.serializeNulls()
.setPrettyPrinting()
.registerTypeAdapter(ExerciseKey.class, new ExerciseKey.GsonAdapter())
.create();
}
}