aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDAO.java
blob: a51d3077cbf8e848eaea80f585d209db3157a22c (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
117
118
119
120
121
122
123
124
125
126
127
package com.ufund.api.ufundapi.persistence;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ufund.api.ufundapi.model.Need;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

@Component
public class CupboardFileDAO implements CupboardDAO {

    private final Map<Integer, Need> needs; // cache
    private final ObjectMapper objectMapper;
    private static int nextId;
    private final String filename;

    public CupboardFileDAO(@Value("${cupboard.file}") String filename, ObjectMapper objectMapper) throws IOException {
        this.filename = filename;
        this.objectMapper = objectMapper;
        needs = new TreeMap<>();
        load();
    }

    private synchronized static int nextId() {
        int id = nextId;
        nextId++;
        return id;
    }

    /**
     * Load changes from the json file
     *
     * @throws IOException Any IO issue with the file
     */
    private void load() throws IOException {
        needs.clear();
        nextId = 0;

        Need[] needsArray = objectMapper.readValue(new File(filename), Need[].class);

        for (Need need : needsArray) {
            needs.put(need.getId(), need);
            if (need.getId() > nextId()) {
                nextId = need.getId();
            }
        }
        nextId++;
    }

    /**
     * Return an array of the needs
     *
     * @return An array of all the needs
     */
    private Need[] getNeedsArray() {
        return needs.values().toArray(Need[]::new);
    }

    /**
     * Saves the needs to json
     *
     * @return True if the save was successful, false otherwise
     * @throws IOException If there was an IO issue saving the file
     */
    private boolean save() throws IOException {
        Need[] needArray = getNeedsArray();

        objectMapper.writeValue(new File(filename), needArray);
        return true;
    }

    @Override
    public Need[] getNeeds() {
        synchronized (needs) {
            return getNeedsArray();
        }
    }

    @Override
    public Need getNeed(int id) {
        synchronized (needs) {
            return needs.getOrDefault(id, null);
        }
    }

    @Override
    public Need addNeed(Need need) throws IOException {
        synchronized (needs) {
            Need newNeed = new Need(need);
            newNeed.setID(nextId());
            needs.put(newNeed.getId(), newNeed);
            save();
            return newNeed;
        }
    }

    @Override
    public Need updateNeed(Need need) throws IOException {
        System.out.println("UPDATING NEED FOR " + need);
        synchronized (needs) {
            if (needs.containsKey(need.getId())) {
                needs.put(need.getId(), need);
                save();
                return need;
            } else {
                return null;
            }

        }
    }

    @Override
    public boolean deleteNeed(int id) throws IOException {
        synchronized (needs) {
            if (needs.containsKey(id)) {
                needs.remove(id);
                return save();
            } else {
                return false;
            }
        }
    }
}