Passed
Branch master (6b380a)
by Vincent
13:11
created

create(Record)   A

Complexity

Conditions 1

Size

Total Lines 13
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 13
dl 0
loc 13
ccs 11
cts 11
cp 1
crap 1
rs 9.75
c 0
b 0
f 0
1
/*
2
 * This file is part of Araknemu.
3
 *
4
 * Araknemu is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Araknemu is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with Araknemu.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2020 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.data.world.repository.implementation.sql;
21
22
import fr.arakne.utils.value.Colors;
23
import fr.arakne.utils.value.constant.Gender;
24
import fr.quatrevieux.araknemu.core.dbal.executor.QueryExecutor;
25
import fr.quatrevieux.araknemu.core.dbal.repository.Record;
26
import fr.quatrevieux.araknemu.core.dbal.repository.RepositoryException;
27
import fr.quatrevieux.araknemu.core.dbal.repository.RepositoryUtils;
28
import fr.quatrevieux.araknemu.data.world.entity.environment.npc.NpcTemplate;
29
import fr.quatrevieux.araknemu.data.world.repository.environment.npc.NpcTemplateRepository;
30
31
import java.sql.ResultSet;
32
import java.sql.SQLException;
33
import java.util.Collection;
34
35
/**
36
 * Map repository implementation for SQL database
37
 */
38
final class SqlNpcTemplateRepository implements NpcTemplateRepository {
39
    private final QueryExecutor executor;
40
    private final RepositoryUtils<NpcTemplate> utils;
41
42 1
    public SqlNpcTemplateRepository(QueryExecutor executor) {
43 1
        this.executor = executor;
44
45 1
        utils = new RepositoryUtils<>(this.executor, new Loader());
46 1
    }
47
48
    @Override
49
    public void initialize() throws RepositoryException {
50
        try {
51 1
            executor.query(
52
                "CREATE TABLE NPC_TEMPLATE(" +
53
                    "NPC_TEMPLATE_ID INTEGER PRIMARY KEY," +
54
                    "GFXID INTEGER," +
55
                    "SCALE_X INTEGER," +
56
                    "SCALE_Y INTEGER," +
57
                    "SEX TINYINT(1)," +
58
                    "COLOR1 INTEGER," +
59
                    "COLOR2 INTEGER," +
60
                    "COLOR3 INTEGER," +
61
                    "ACCESSORIES VARCHAR(30)," +
62
                    "EXTRA_CLIP TINYINT(1)," +
63
                    "CUSTOM_ARTWORK INTEGER," +
64
                    "STORE_ITEMS TEXT" +
65
                ")"
66
            );
67
        } catch (SQLException e) {
68
            throw new RepositoryException(e);
69 1
        }
70 1
    }
71
72
    @Override
73
    public void destroy() throws RepositoryException {
74
        try {
75 1
            executor.query("DROP TABLE NPC_TEMPLATE");
76
        } catch (SQLException e) {
77
            throw new RepositoryException(e);
78 1
        }
79 1
    }
80
81
    @Override
82
    public NpcTemplate get(int id) {
83 1
        return utils.findOne(
84
            "SELECT * FROM NPC_TEMPLATE WHERE NPC_TEMPLATE_ID = ?",
85 1
            stmt -> stmt.setInt(1, id)
86
        );
87
    }
88
89
    @Override
90
    public NpcTemplate get(NpcTemplate entity) throws RepositoryException {
91 1
        return get(entity.id());
92
    }
93
94
    @Override
95
    public boolean has(NpcTemplate entity) throws RepositoryException {
96 1
        return utils.aggregate(
97
            "SELECT COUNT(*) FROM NPC_TEMPLATE WHERE NPC_TEMPLATE_ID = ?",
98 1
            stmt -> stmt.setInt(1, entity.id())
99
        ) > 0;
100
    }
101
102
    @Override
103
    public Collection<NpcTemplate> all() throws RepositoryException {
104 1
        return utils.findAll("SELECT * FROM NPC_TEMPLATE");
105
    }
106
107 1
    private class Loader implements RepositoryUtils.Loader<NpcTemplate> {
0 ignored issues
show
Comprehensibility introduced by
Class or interface names should not shadow other classes or interfaces. In general, shadowing is a bad practice as it makes code harder to understand. Consider renaming this class.
Loading history...
108 1
        private final Gender[] genders = Gender.values();
109
110
        @Override
111
        public NpcTemplate create(Record record) throws SQLException {
112 1
            return new NpcTemplate(
113 1
                record.getInt("NPC_TEMPLATE_ID"),
114 1
                record.getInt("GFXID"),
115 1
                record.getInt("SCALE_X"),
116 1
                record.getInt("SCALE_Y"),
117 1
                record.getArrayValue("SEX", genders),
118 1
                createColors(record),
119 1
                record.getString("ACCESSORIES"),
120 1
                record.getInt("EXTRA_CLIP"),
121 1
                record.getInt("CUSTOM_ARTWORK"),
122 1
                record.getNullableIntArray("STORE_ITEMS", ',')
123
            );
124
        }
125
126
        @Override
127
        public NpcTemplate fillKeys(NpcTemplate entity, ResultSet keys) {
128
            throw new RepositoryException("Read-only entity");
129
        }
130
131
        @SuppressWarnings("argument") // Ignore invalid colors error
132
        private Colors createColors(Record record) throws SQLException {
133 1
            return new Colors(
134 1
                record.getInt("COLOR1"),
135 1
                record.getInt("COLOR2"),
136 1
                record.getInt("COLOR3")
137
            );
138
        }
139
    }
140
}
141