summaryrefslogtreecommitdiff
path: root/src/main/java/design/persistence/JSONLeagueDatabase.java
blob: 795c58284a87e0123e3223c772ca29d1a2fda6bf (plain) (blame)
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
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.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;

public class JSONLeagueDatabase implements LeagueDatabase {

    private static JSONLeagueDatabase INSTANCE;

    public static JSONLeagueDatabase instance() {
        if (INSTANCE == null) {
            INSTANCE = new JSONLeagueDatabase("data/leaguedb.json");
        }
        return INSTANCE;
    }

    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 Serializers.CourseIdDeserializer());
        module.addSerializer(Course.class, new Serializers.CourseIdSerializer());
        module.addDeserializer(Golfer.class, new Serializers.GolferUsernameDeserializer());
        module.addSerializer(Golfer.class, new Serializers.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() throws IOException {
        League[] data = mapper.readValue(file, League[].class);
        cache.clear();
        Arrays.stream(data).forEach(i -> cache.put(i.getId(), i));
    }

    private void save() throws IOException {
        League[] data = cache.values().toArray(League[]::new);
        mapper.writer(new Serializers.CustomPrettyPrinter()).writeValue(file, data);
    }

    @Override
    public League getLeague(int id) {
        return cache.get(id);
    }

    @Override
    public League[] getLeagues() {
        return cache.values().toArray(League[]::new);
    }

    @Override
    public void addLeague(League league) throws IOException {
        league.setId(nextLeagueID++);
        cache.putIfAbsent(league.getId(), league);
        save();
    }

    @Override
    public void removeLeague(League league) throws IOException {
        cache.remove(league.getId());
        save();
    }

    @Override
    public void updateLeague(League league) throws IOException {
        cache.put(league.getId(), league);
        save();
    }

    @Override
    public void importData(JsonNode tree) throws IOException {
        League[] data = mapper.treeToValue(tree, League[].class);
        cache.clear();
        for (League league : data) {
            cache.put(league.getId(), league);
        }
        save();
    }

    @Override
    public JsonNode exportData() {
        Object[] data = cache.values().toArray();
        return mapper.valueToTree(data);
    }
}