-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlStorage.java
More file actions
213 lines (195 loc) · 7.97 KB
/
SqlStorage.java
File metadata and controls
213 lines (195 loc) · 7.97 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package ru.javaops.webapp.storage;
import ru.javaops.webapp.exception.NotExistStorageException;
import ru.javaops.webapp.model.AbstractSection;
import ru.javaops.webapp.model.ContactType;
import ru.javaops.webapp.model.Resume;
import ru.javaops.webapp.model.SectionType;
import ru.javaops.webapp.sql.SqlHelper;
import ru.javaops.webapp.util.JsonParser;
import java.sql.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class SqlStorage implements Storage {
private final SqlHelper sqlHelper;
public SqlStorage(String dbUrl, String dbUser, String dbPassword) {
sqlHelper = new SqlHelper(() -> DriverManager.getConnection(dbUrl, dbUser, dbPassword));
}
@Override
public void clear() {
sqlHelper.execute("DELETE FROM resume");
}
@Override
public Resume get(String uuid) {
return sqlHelper.transactionExecute(conn -> {
Resume resume;
try (PreparedStatement ps = conn.prepareStatement("SELECT * FROM resume r WHERE r.uuid=?")) {
ps.setString(1, uuid);
ResultSet rs = ps.executeQuery();
if (!rs.next()) {
throw new NotExistStorageException(uuid);
}
resume = (new Resume(rs.getString("uuid"), rs.getString("full_name")));
}
try (PreparedStatement ps = conn.prepareStatement("SELECT * FROM contact c WHERE c.resume_uuid=?")) {
ps.setString(1, uuid);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
addContact(resume, rs);
}
}
try (PreparedStatement ps = conn.prepareStatement("SELECT * FROM section s WHERE s.resume_uuid=?")) {
ps.setString(1, uuid);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
addSection(resume, rs);
}
}
return resume;
});
}
@Override
public void update(Resume r) {
sqlHelper.transactionExecute(conn -> {
try (PreparedStatement ps = conn.prepareStatement("UPDATE resume SET full_name =? WHERE uuid =?")) {
ps.setString(1, r.getFullName());
ps.setString(2, r.getUuid());
if (ps.executeUpdate() == 0) {
throw new NotExistStorageException(r.getUuid());
}
}
deleteFromTable(r.getUuid(), "contact", conn);
deleteFromTable(r.getUuid(), "section", conn);
saveContacts(r, conn);
saveSections(r, conn);
return null;
});
}
private void deleteFromTable(String uuid, String table, Connection conn) throws SQLException {
try (PreparedStatement ps = conn.prepareStatement(String.format("DELETE FROM %s WHERE resume_uuid=?", table))) {
ps.setString(1, uuid);
ps.execute();
}
}
@Override
public void save(Resume r) {
sqlHelper.transactionExecute(conn -> {
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO resume (uuid, full_name) VALUES (?,?)")) {
ps.setString(1, r.getUuid());
ps.setString(2, r.getFullName());
ps.execute();
}
saveContacts(r, conn);
saveSections(r, conn);
return null;
});
}
@Override
public void delete(String uuid) {
sqlHelper.execute("DELETE FROM resume r WHERE r.uuid =?", ps -> {
ps.setString(1, uuid);
if (ps.executeUpdate() == 0) {
throw new NotExistStorageException(uuid);
}
return null;
});
}
@Override
public List<Resume> getAllSorted() {
return sqlHelper.transactionExecute(conn -> {
Map<String, Resume> resumeMap = new LinkedHashMap<>();
try (PreparedStatement ps = conn.prepareStatement("SELECT * FROM resume r ORDER BY r.full_name, r.uuid")) {
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Resume r = (new Resume(rs.getString("uuid"), rs.getString("full_name")));
resumeMap.put(r.getUuid(), r);
}
}
try (PreparedStatement ps = conn.prepareStatement("SELECT * FROM contact")) {
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Resume r = resumeMap.get(rs.getString("resume_uuid"));
addContact(r, rs);
}
}
try (PreparedStatement ps = conn.prepareStatement("SELECT * FROM section")) {
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Resume r = resumeMap.get(rs.getString("resume_uuid"));
addSection(r, rs);
}
}
return new ArrayList<>(resumeMap.values());
});
}
@Override
public int size() {
return sqlHelper.execute("SELECT count(*) FROM resume", ps -> {
ResultSet rs = ps.executeQuery();
return rs.next() ? rs.getInt(1) : 0;
});
}
private void saveContacts(Resume r, Connection conn) throws SQLException {
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO contact (value, resume_uuid, type) VALUES (?,?,?)")) {
ps.setString(2, r.getUuid());
for (Map.Entry<ContactType, String> e : r.getContacts().entrySet()) {
ps.setString(1, e.getValue());
ps.setString(3, e.getKey().name());
ps.addBatch();
}
ps.executeBatch();
}
}
private void saveSections(Resume r, Connection conn) throws SQLException {
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO section (value, resume_uuid, type) VALUES (?,?,?)")) {
ps.setString(2, r.getUuid());
for (Map.Entry<SectionType, AbstractSection> e : r.getSections().entrySet()) {
ps.setString(3, e.getKey().name());
AbstractSection section = e.getValue();
ps.setString(1, JsonParser.write(section, AbstractSection.class));
// switch (e.getKey()) {
// case OBJECTIVE:
// case PERSONAL:
// ps.setString(1, ((TextSection) e.getValue()).getContent());
// break;
// case ACHIEVEMENT:
// case QUALIFICATIONS:
// StringBuilder stringBuilder = new StringBuilder();
// for (String s : ((ListSection) e.getValue()).getStrings()) {
// stringBuilder.append(s).append("\n");
// }
// ps.setString(1, stringBuilder.toString());
// break;
// }
ps.addBatch();
}
ps.executeBatch();
}
}
private void addContact(Resume r, ResultSet rs) throws SQLException {
String value = rs.getString("value");
ContactType type = ContactType.valueOf(rs.getString("type"));
if (value != null) {
r.addContact(type, value);
}
}
private void addSection(Resume r, ResultSet rs) throws SQLException {
String value = rs.getString("value");
if (value != null) {
SectionType section = SectionType.valueOf(rs.getString("type"));
r.addSection(section, JsonParser.read(value, AbstractSection.class));
//
//
// switch (section) {
// case OBJECTIVE:
// case PERSONAL:
// r.addSection(section, new TextSection(value));
// break;
// case ACHIEVEMENT:
// case QUALIFICATIONS:
// r.addSection(section, new ListSection(value.split("\n")));
// break;
}
}
}