summaryrefslogtreecommitdiff
path: root/src/main/java/design/persistence/JSONLeagueDatabase.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/design/persistence/JSONLeagueDatabase.java')
-rw-r--r--src/main/java/design/persistence/JSONLeagueDatabase.java72
1 files changed, 65 insertions, 7 deletions
diff --git a/src/main/java/design/persistence/JSONLeagueDatabase.java b/src/main/java/design/persistence/JSONLeagueDatabase.java
index 01f4fc3..78e1903 100644
--- a/src/main/java/design/persistence/JSONLeagueDatabase.java
+++ b/src/main/java/design/persistence/JSONLeagueDatabase.java
@@ -1,11 +1,26 @@
package design.persistence;
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
+import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
+import design.model.Course;
+import design.model.Golfer;
import design.model.League;
import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
+import static design.persistence.JSONPersonalDatabase.*;
+
public class JSONLeagueDatabase implements LeagueDatabase {
private static JSONLeagueDatabase INSTANCE;
@@ -18,19 +33,44 @@ public class JSONLeagueDatabase implements LeagueDatabase {
}
private final Map<Integer, League> cache;
+ private final ObjectMapper mapper;
private final File file;
+ private int nextLeagueID;
public JSONLeagueDatabase(String filename) {
this.file = new File(filename);
this.cache = new HashMap<>();
+ this.mapper = new ObjectMapper();
+
+ SimpleModule module = new SimpleModule();
+ module.addDeserializer(Course.class, new CourseIdDeserializer());
+ module.addSerializer(Course.class, new CourseIdSerializer());
+ module.addDeserializer(Golfer.class, new GolferUsernameDeserializer());
+ module.addSerializer(Golfer.class, new GolferUsernameSerializer());
+ mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+ mapper.registerModule(module);
+ mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
+ mapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));
+ mapper.registerModule(new JavaTimeModule());
+
+ try {
+ load();
+ } catch (IOException ex) {
+ throw new RuntimeException(ex);
+ }
+
+ this.nextLeagueID = cache.values().stream().mapToInt(League::getId).max().orElse(0) + 1;
}
- private void load() {
- //TODO impl
+ private void load() throws IOException {
+ League[] data = mapper.readValue(file, League[].class);
+ cache.clear();
+ Arrays.stream(data).forEach(i -> cache.put(i.getId(), i));
}
- private void save() {
- //TODO impl
+ private void save() throws IOException {
+ League[] data = cache.values().toArray(League[]::new);
+ mapper.writer(new CustomPrettyPrinter()).writeValue(file, data);
}
@Override
@@ -44,20 +84,38 @@ public class JSONLeagueDatabase implements LeagueDatabase {
}
@Override
- public void addLeague(League league) {
+ public void addLeague(League league) throws IOException {
+ league.setId(nextLeagueID++);
cache.putIfAbsent(league.getId(), league);
save();
}
@Override
- public void removeLeague(League league) {
+ public void removeLeague(League league) throws IOException {
cache.remove(league.getId());
save();
}
@Override
- public void updateLeague(League league) {
+ public void updateLeague(League league) throws IOException {
cache.put(league.getId(), league);
save();
}
+
+ protected static class GolferUsernameSerializer extends JsonSerializer<Golfer> {
+ @Override
+ public void serialize(Golfer course, JsonGenerator gen, SerializerProvider serializers) throws IOException {
+ gen.writeString(course.getUsername());
+ }
+ }
+
+ protected static class GolferUsernameDeserializer extends JsonDeserializer<Golfer> {
+ PersonalDatabase personalDB = PersonalDatabase.instance();
+
+ @Override
+ public Golfer deserialize(JsonParser p, DeserializationContext context) throws IOException {
+ String username = p.getValueAsString();
+ return personalDB.getGolfer(username);
+ }
+ }
}