Thu, 15 Oct 2020 18:36:05 +0200
adds the possibility to specify path parameters to RequestMapping
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2018 Mike Becker. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29 package de.uapcore.lightpit.dao.postgres;
31 import de.uapcore.lightpit.dao.IssueDao;
32 import de.uapcore.lightpit.entities.*;
34 import java.sql.Connection;
35 import java.sql.PreparedStatement;
36 import java.sql.ResultSet;
37 import java.sql.SQLException;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Objects;
42 import static de.uapcore.lightpit.dao.Functions.*;
44 public final class PGIssueDao implements IssueDao {
46 private final PreparedStatement insert, update, list, listForVersion, find;
47 private final PreparedStatement affectedVersions, resolvedVersions;
48 private final PreparedStatement clearAffected, clearResolved;
49 private final PreparedStatement insertAffected, insertResolved;
50 private final PreparedStatement insertComment, updateComment, listComments;
52 public PGIssueDao(Connection connection) throws SQLException {
53 list = connection.prepareStatement(
54 "select issueid, project, p.name as projectname, status, category, subject, i.description, " +
55 "userid, username, givenname, lastname, mail, " +
56 "created, updated, eta " +
57 "from lpit_issue i " +
58 "join lpit_project p on project = projectid " +
59 "left join lpit_user on userid = assignee " +
60 "where project = ? ");
62 listForVersion = connection.prepareStatement(
63 "with issue_version as ( "+
64 "select issueid, versionid from lpit_issue_affected_version union "+
65 "select issueid, versionid from lpit_issue_resolved_version) "+
66 "select issueid, project, p.name as projectname, status, category, subject, i.description, " +
67 "userid, username, givenname, lastname, mail, " +
68 "created, updated, eta " +
69 "from lpit_issue i " +
70 "join lpit_project p on project = projectid " +
71 "left join issue_version using (issueid) "+
72 "left join lpit_user on userid = assignee " +
73 "where coalesce(versionid,-1) = ? "
74 );
76 find = connection.prepareStatement(
77 "select issueid, project, p.name as projectname, status, category, subject, i.description, " +
78 "userid, username, givenname, lastname, mail, " +
79 "created, updated, eta " +
80 "from lpit_issue i " +
81 "left join lpit_project p on project = projectid " +
82 "left join lpit_user on userid = assignee " +
83 "where issueid = ? ");
85 insert = connection.prepareStatement(
86 "insert into lpit_issue (project, status, category, subject, description, assignee, eta) " +
87 "values (?, ?::issue_status, ?::issue_category, ?, ?, ?, ?) returning issueid"
88 );
89 update = connection.prepareStatement(
90 "update lpit_issue set updated = now(), status = ?::issue_status, category = ?::issue_category, " +
91 "subject = ?, description = ?, assignee = ?, eta = ? where issueid = ?"
92 );
94 affectedVersions = connection.prepareStatement(
95 "select versionid, name, status, ordinal " +
96 "from lpit_version join lpit_issue_affected_version using (versionid) " +
97 "where issueid = ? " +
98 "order by ordinal, name"
99 );
100 clearAffected = connection.prepareStatement("delete from lpit_issue_affected_version where issueid = ?");
101 insertAffected = connection.prepareStatement("insert into lpit_issue_affected_version (issueid, versionid) values (?,?)");
103 resolvedVersions = connection.prepareStatement(
104 "select versionid, name, status, ordinal " +
105 "from lpit_version v join lpit_issue_resolved_version using (versionid) " +
106 "where issueid = ? " +
107 "order by ordinal, name"
108 );
109 clearResolved = connection.prepareStatement("delete from lpit_issue_resolved_version where issueid = ?");
110 insertResolved = connection.prepareStatement("insert into lpit_issue_resolved_version (issueid, versionid) values (?,?)");
112 insertComment = connection.prepareStatement(
113 "insert into lpit_issue_comment (issueid, comment, userid) values (?, ? ,?)"
114 );
115 updateComment = connection.prepareStatement(
116 "update lpit_issue_comment set comment = ?, updated = now(), updatecount = updatecount+1 where commentid = ?"
117 );
118 listComments = connection.prepareStatement(
119 "select * from lpit_issue_comment left join lpit_user using (userid) where issueid = ? order by created"
120 );
121 }
123 private Issue mapColumns(ResultSet result) throws SQLException {
124 final var project = new Project(result.getInt("project"));
125 project.setName(result.getString("projectname"));
126 final var issue = new Issue(result.getInt("issueid"));
127 issue.setProject(project);
128 issue.setStatus(IssueStatus.valueOf(result.getString("status")));
129 issue.setCategory(IssueCategory.valueOf(result.getString("category")));
130 issue.setSubject(result.getString("subject"));
131 issue.setDescription(result.getString("description"));
132 issue.setAssignee(PGUserDao.mapColumns(result));
133 issue.setCreated(result.getTimestamp("created"));
134 issue.setUpdated(result.getTimestamp("updated"));
135 issue.setEta(result.getDate("eta"));
136 return issue;
137 }
139 private Version mapVersion(ResultSet result) throws SQLException {
140 final var version = new Version(result.getInt("versionid"));
141 version.setName(result.getString("name"));
142 version.setOrdinal(result.getInt("ordinal"));
143 version.setStatus(VersionStatus.valueOf(result.getString("status")));
144 return version;
145 }
147 private void updateVersionLists(Issue instance) throws SQLException {
148 clearAffected.setInt(1, instance.getId());
149 clearResolved.setInt(1, instance.getId());
150 insertAffected.setInt(1, instance.getId());
151 insertResolved.setInt(1, instance.getId());
152 clearAffected.executeUpdate();
153 clearResolved.executeUpdate();
154 for (Version v : instance.getAffectedVersions()) {
155 insertAffected.setInt(2, v.getId());
156 insertAffected.executeUpdate();
157 }
158 for (Version v : instance.getResolvedVersions()) {
159 insertResolved.setInt(2, v.getId());
160 insertResolved.executeUpdate();
161 }
162 }
164 @Override
165 public void save(Issue instance, Project project) throws SQLException {
166 Objects.requireNonNull(instance.getSubject());
167 instance.setProject(project);
168 insert.setInt(1, instance.getProject().getId());
169 insert.setString(2, instance.getStatus().name());
170 insert.setString(3, instance.getCategory().name());
171 insert.setString(4, instance.getSubject());
172 setStringOrNull(insert, 5, instance.getDescription());
173 setForeignKeyOrNull(insert, 6, instance.getAssignee(), User::getId);
174 setDateOrNull(insert, 7, instance.getEta());
175 // insert and retrieve the ID
176 final var rs = insert.executeQuery();
177 rs.next();
178 instance.setId(rs.getInt(1));
179 updateVersionLists(instance);
180 }
182 @Override
183 public boolean update(Issue instance) throws SQLException {
184 if (instance.getId() < 0) return false;
185 Objects.requireNonNull(instance.getSubject());
186 update.setString(1, instance.getStatus().name());
187 update.setString(2, instance.getCategory().name());
188 update.setString(3, instance.getSubject());
189 setStringOrNull(update, 4, instance.getDescription());
190 setForeignKeyOrNull(update, 5, instance.getAssignee(), User::getId);
191 setDateOrNull(update, 6, instance.getEta());
192 update.setInt(7, instance.getId());
193 boolean success = update.executeUpdate() > 0;
194 if (success) {
195 updateVersionLists(instance);
196 return true;
197 } else {
198 return false;
199 }
200 }
202 private List<Issue> list(PreparedStatement query, int arg) throws SQLException {
203 query.setInt(1, arg);
204 List<Issue> issues = new ArrayList<>();
205 try (var result = query.executeQuery()) {
206 while (result.next()) {
207 issues.add(mapColumns(result));
208 }
209 }
210 return issues;
211 }
213 @Override
214 public List<Issue> list(Project project) throws SQLException {
215 return list(list, project.getId());
216 }
218 @Override
219 public List<Issue> list(Version version) throws SQLException {
220 return list(listForVersion, version == null ? -1 : version.getId());
221 }
223 @Override
224 public Issue find(int id) throws SQLException {
225 find.setInt(1, id);
226 try (var result = find.executeQuery()) {
227 if (result.next()) {
228 return mapColumns(result);
229 } else {
230 return null;
231 }
232 }
233 }
235 private List<Version> listVersions(PreparedStatement stmt, Issue issue) throws SQLException {
236 stmt.setInt(1, issue.getId());
237 List<Version> versions = new ArrayList<>();
238 try (var result = stmt.executeQuery()) {
239 while (result.next()) {
240 versions.add(mapVersion(result));
241 }
242 }
243 return versions;
244 }
246 @Override
247 public void joinVersionInformation(Issue issue) throws SQLException {
248 Objects.requireNonNull(issue.getProject());
249 issue.setAffectedVersions(listVersions(affectedVersions, issue));
250 issue.setResolvedVersions(listVersions(resolvedVersions, issue));
251 }
253 @Override
254 public List<IssueComment> listComments(Issue issue) throws SQLException {
255 listComments.setInt(1, issue.getId());
256 List<IssueComment> comments = new ArrayList<>();
257 try (var result = listComments.executeQuery()) {
258 while (result.next()) {
259 final var comment = new IssueComment(result.getInt("commentid"), issue);
260 comment.setCreated(result.getTimestamp("created"));
261 comment.setUpdated(result.getTimestamp("updated"));
262 comment.setUpdateCount(result.getInt("updatecount"));
263 comment.setComment(result.getString("comment"));
264 comment.setAuthor(PGUserDao.mapColumns(result));
265 comments.add(comment);
266 }
267 }
268 return comments;
269 }
271 @Override
272 public void saveComment(IssueComment comment) throws SQLException {
273 Objects.requireNonNull(comment.getComment());
274 Objects.requireNonNull(comment.getIssue());
275 if (comment.getId() >= 0) {
276 updateComment.setString(1, comment.getComment());
277 updateComment.setInt(2, comment.getId());
278 updateComment.execute();
279 } else {
280 insertComment.setInt(1, comment.getIssue().getId());
281 insertComment.setString(2, comment.getComment());
282 setForeignKeyOrNull(insertComment, 3, comment.getAuthor(), User::getId);
283 insertComment.execute();
284 }
285 }
286 }