Sun, 21 Jun 2020 11:38:16 +0200
simplifies version overviews by removing "scheduled issues"
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;
51 public PGIssueDao(Connection connection) throws SQLException {
52 list = connection.prepareStatement(
53 "select issueid, project, p.name as projectname, status, category, subject, i.description, " +
54 "userid, username, givenname, lastname, mail, " +
55 "created, updated, eta " +
56 "from lpit_issue i " +
57 "left join lpit_project p on project = projectid " +
58 "left join lpit_user on userid = assignee " +
59 "where project = ? "+
60 "order by eta asc, updated desc");
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 issue_version using (issueid) "+
71 "left join lpit_project p on project = projectid " +
72 "left join lpit_user on userid = assignee " +
73 "where versionid = ? "+
74 "order by eta asc, updated desc"
75 );
77 find = connection.prepareStatement(
78 "select issueid, project, p.name as projectname, status, category, subject, i.description, " +
79 "userid, username, givenname, lastname, mail, " +
80 "created, updated, eta " +
81 "from lpit_issue i " +
82 "left join lpit_project p on project = projectid " +
83 "left join lpit_user on userid = assignee " +
84 "where issueid = ? ");
86 insert = connection.prepareStatement(
87 "insert into lpit_issue (project, status, category, subject, description, assignee, eta) " +
88 "values (?, ?::issue_status, ?::issue_category, ?, ?, ?, ?) returning issueid"
89 );
90 update = connection.prepareStatement(
91 "update lpit_issue set updated = now(), status = ?::issue_status, category = ?::issue_category, " +
92 "subject = ?, description = ?, assignee = ?, eta = ? where issueid = ?"
93 );
95 affectedVersions = connection.prepareStatement(
96 "select versionid, name, status, ordinal " +
97 "from lpit_version join lpit_issue_affected_version using (versionid) " +
98 "where issueid = ? " +
99 "order by ordinal, name"
100 );
101 clearAffected = connection.prepareStatement("delete from lpit_issue_affected_version where issueid = ?");
102 insertAffected = connection.prepareStatement("insert into lpit_issue_affected_version (issueid, versionid) values (?,?)");
104 resolvedVersions = connection.prepareStatement(
105 "select versionid, name, status, ordinal " +
106 "from lpit_version v join lpit_issue_resolved_version using (versionid) " +
107 "where issueid = ? " +
108 "order by ordinal, name"
109 );
110 clearResolved = connection.prepareStatement("delete from lpit_issue_resolved_version where issueid = ?");
111 insertResolved = connection.prepareStatement("insert into lpit_issue_resolved_version (issueid, versionid) values (?,?)");
112 }
114 private User obtainAssignee(ResultSet result) throws SQLException {
115 final int id = result.getInt("userid");
116 if (id != 0) {
117 final var user = new User(id);
118 user.setUsername(result.getString("username"));
119 user.setGivenname(result.getString("givenname"));
120 user.setLastname(result.getString("lastname"));
121 user.setMail(result.getString("mail"));
122 return user;
123 } else {
124 return null;
125 }
126 }
128 private Issue mapColumns(ResultSet result) throws SQLException {
129 final var project = new Project(result.getInt("project"));
130 project.setName(result.getString("projectname"));
131 final var issue = new Issue(result.getInt("issueid"));
132 issue.setProject(project);
133 issue.setStatus(IssueStatus.valueOf(result.getString("status")));
134 issue.setCategory(IssueCategory.valueOf(result.getString("category")));
135 issue.setSubject(result.getString("subject"));
136 issue.setDescription(result.getString("description"));
137 issue.setAssignee(obtainAssignee(result));
138 issue.setCreated(result.getTimestamp("created"));
139 issue.setUpdated(result.getTimestamp("updated"));
140 issue.setEta(result.getDate("eta"));
141 return issue;
142 }
144 private Version mapVersion(ResultSet result) throws SQLException {
145 final var version = new Version(result.getInt("versionid"));
146 version.setName(result.getString("name"));
147 version.setOrdinal(result.getInt("ordinal"));
148 version.setStatus(VersionStatus.valueOf(result.getString("status")));
149 return version;
150 }
152 private void updateVersionLists(Issue instance) throws SQLException {
153 clearAffected.setInt(1, instance.getId());
154 clearResolved.setInt(1, instance.getId());
155 insertAffected.setInt(1, instance.getId());
156 insertResolved.setInt(1, instance.getId());
157 clearAffected.executeUpdate();
158 clearResolved.executeUpdate();
159 for (Version v : instance.getAffectedVersions()) {
160 insertAffected.setInt(2, v.getId());
161 insertAffected.executeUpdate();
162 }
163 for (Version v : instance.getResolvedVersions()) {
164 insertResolved.setInt(2, v.getId());
165 insertResolved.executeUpdate();
166 }
167 }
169 @Override
170 public void save(Issue instance) throws SQLException {
171 Objects.requireNonNull(instance.getSubject());
172 Objects.requireNonNull(instance.getProject());
173 insert.setInt(1, instance.getProject().getId());
174 insert.setString(2, instance.getStatus().name());
175 insert.setString(3, instance.getCategory().name());
176 insert.setString(4, instance.getSubject());
177 setStringOrNull(insert, 5, instance.getDescription());
178 setForeignKeyOrNull(insert, 6, instance.getAssignee(), User::getId);
179 setDateOrNull(insert, 7, instance.getEta());
180 // insert and retrieve the ID
181 final var rs = insert.executeQuery();
182 rs.next();
183 instance.setId(rs.getInt(1));
184 updateVersionLists(instance);
185 }
187 @Override
188 public boolean update(Issue instance) throws SQLException {
189 if (instance.getId() < 0) return false;
190 Objects.requireNonNull(instance.getSubject());
191 update.setString(1, instance.getStatus().name());
192 update.setString(2, instance.getCategory().name());
193 update.setString(3, instance.getSubject());
194 setStringOrNull(update, 4, instance.getDescription());
195 setForeignKeyOrNull(update, 5, instance.getAssignee(), User::getId);
196 setDateOrNull(update, 6, instance.getEta());
197 update.setInt(7, instance.getId());
198 boolean success = update.executeUpdate() > 0;
199 if (success) {
200 updateVersionLists(instance);
201 return true;
202 } else {
203 return false;
204 }
205 }
207 private List<Issue> list(PreparedStatement query, int arg) throws SQLException {
208 query.setInt(1, arg);
209 List<Issue> issues = new ArrayList<>();
210 try (var result = query.executeQuery()) {
211 while (result.next()) {
212 issues.add(mapColumns(result));
213 }
214 }
215 return issues;
216 }
218 @Override
219 public List<Issue> list(Project project) throws SQLException {
220 return list(list, project.getId());
221 }
223 @Override
224 public List<Issue> list(Version version) throws SQLException {
225 return list(listForVersion, version.getId());
226 }
228 @Override
229 public Issue find(int id) throws SQLException {
230 find.setInt(1, id);
231 try (var result = find.executeQuery()) {
232 if (result.next()) {
233 return mapColumns(result);
234 } else {
235 return null;
236 }
237 }
238 }
240 private List<Version> listVersions(PreparedStatement stmt, Issue issue) throws SQLException {
241 stmt.setInt(1, issue.getId());
242 List<Version> versions = new ArrayList<>();
243 try (var result = stmt.executeQuery()) {
244 while (result.next()) {
245 versions.add(mapVersion(result));
246 }
247 }
248 return versions;
249 }
251 @Override
252 public void joinVersionInformation(Issue issue) throws SQLException {
253 Objects.requireNonNull(issue.getProject());
254 issue.setAffectedVersions(listVersions(affectedVersions, issue));
255 issue.setResolvedVersions(listVersions(resolvedVersions, issue));
256 }
257 }