src/main/java/de/uapcore/lightpit/dao/postgres/PGIssueDao.java

Sun, 21 Jun 2020 11:38:16 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 21 Jun 2020 11:38:16 +0200
changeset 88
1438e5a22c55
parent 86
0a658e53177c
child 105
250c5cbb8276
permissions
-rw-r--r--

simplifies version overviews by removing "scheduled issues"

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

mercurial