summaryrefslogtreecommitdiff
path: root/src/main/java/design/persistence/CSVMasterDatabase.java
blob: fe89191631ab90916c5cddb9ebb83da933a2dbef (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
package design.persistence;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import design.model.Course;
import design.model.Hole;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CSVMasterDatabase implements MasterDatabase {

    private final List<Course> cache;
    private final CsvMapper mapper;
    private final File file;

    public CSVMasterDatabase(String filename) {
        this.cache = new ArrayList<>();
        this.mapper = new CsvMapper();
        this.file = new File(filename);

        SimpleModule module = new SimpleModule();
        module.addDeserializer(Course.class, new CourseDeserializer());
        mapper.registerModule(module);

        try {
            load();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    public void load() throws IOException {
        MappingIterator<Course> it = mapper
                .readerFor(Course.class)
                .with(CsvSchema.emptySchema().withHeader())
                .readValues(file);

        cache.addAll(it.readAll());
    }

    @Override
    public Course[] getCourses() {
        return cache.toArray(Course[]::new);
    }

    @Override
    public Course getCourse(int id) {
        return cache.get(id);
    }

    private static class CourseDeserializer extends JsonDeserializer<Course> {
        int curID = 0;

        @Override
        public Course deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
            ObjectNode node = p.getCodec().readTree(p);

            int id = curID++;
            String name = node.get("Course Name").asText();
            double difficulty = node.get("Difficulty Rating").asDouble();
            String location = node.get("Location").asText();
            int holes = node.get("Number of Holes").asInt();
            int totalPar = node.get("Total Par").asInt();

            String[] parValues = node.get("Par Per Hole").asText().split(";");
            List<Hole> holeList = new ArrayList<>();
            for (int i = 0; i < parValues.length; i++) {
                holeList.add(new Hole(i + 1, Integer.parseInt(parValues[i].trim())));
            }

            return new Course(id, name, (int) difficulty, location, holes, totalPar, holeList);
        }
    }
}