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

Fri, 23 Oct 2020 20:34:57 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 23 Oct 2020 20:34:57 +0200
changeset 150
822b7e3d064d
parent 138
e2aa673dd473
child 154
3d10f2a390a1
permissions
-rw-r--r--

migrate entities package

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@134 34 import java.sql.*;
universe@47 35 import java.util.ArrayList;
universe@47 36 import java.util.List;
universe@38 37 import java.util.Objects;
universe@134 38 import java.util.Optional;
universe@38 39
universe@62 40 import static de.uapcore.lightpit.dao.Functions.*;
universe@62 41
universe@62 42 public final class PGIssueDao implements IssueDao {
universe@38 43
universe@86 44 private final PreparedStatement insert, update, list, listForVersion, find;
universe@88 45 private final PreparedStatement affectedVersions, resolvedVersions;
universe@88 46 private final PreparedStatement clearAffected, clearResolved;
universe@88 47 private final PreparedStatement insertAffected, insertResolved;
universe@124 48 private final PreparedStatement insertComment, updateComment, listComments;
universe@38 49
universe@62 50 public PGIssueDao(Connection connection) throws SQLException {
universe@138 51 final var query = "select issueid, i.project, p.name as projectname, p.node as projectnode, "+
universe@138 52 "component, c.name as componentname, c.node as componentnode, " +
universe@134 53 "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@134 57 "join lpit_project p on i.project = projectid " +
universe@134 58 "left join lpit_component c on component = c.id " +
universe@138 59 "left join lpit_user on userid = assignee ";
universe@138 60
universe@138 61 list = connection.prepareStatement(query +
universe@134 62 "where i.project = ? and coalesce(component, -1) = coalesce(?, component, -1)");
universe@86 63
universe@86 64 listForVersion = connection.prepareStatement(
universe@86 65 "with issue_version as ( "+
universe@86 66 "select issueid, versionid from lpit_issue_affected_version union "+
universe@86 67 "select issueid, versionid from lpit_issue_resolved_version) "+
universe@138 68 query +
universe@105 69 "left join issue_version using (issueid) "+
universe@134 70 "where coalesce(versionid,-1) = ? and coalesce(component, -1) = coalesce(?, component, -1)"
universe@86 71 );
universe@47 72
universe@138 73 find = connection.prepareStatement(query + "where issueid = ? ");
universe@38 74
universe@38 75 insert = connection.prepareStatement(
universe@134 76 "insert into lpit_issue (project, component, status, category, subject, description, assignee, eta) " +
universe@135 77 "values (?, ?, ?::issue_status, ?::issue_category, ?, ?, ?, ?) returning issueid"
universe@38 78 );
universe@38 79 update = connection.prepareStatement(
universe@134 80 "update lpit_issue set " +
universe@134 81 "updated = now(), component = ?, status = ?::issue_status, category = ?::issue_category, " +
universe@75 82 "subject = ?, description = ?, assignee = ?, eta = ? where issueid = ?"
universe@75 83 );
universe@75 84
universe@75 85 affectedVersions = connection.prepareStatement(
universe@83 86 "select versionid, name, status, ordinal " +
universe@83 87 "from lpit_version join lpit_issue_affected_version using (versionid) " +
universe@75 88 "where issueid = ? " +
universe@83 89 "order by ordinal, name"
universe@75 90 );
universe@83 91 clearAffected = connection.prepareStatement("delete from lpit_issue_affected_version where issueid = ?");
universe@83 92 insertAffected = connection.prepareStatement("insert into lpit_issue_affected_version (issueid, versionid) values (?,?)");
universe@75 93
universe@75 94 resolvedVersions = connection.prepareStatement(
universe@83 95 "select versionid, name, status, ordinal " +
universe@75 96 "from lpit_version v join lpit_issue_resolved_version using (versionid) " +
universe@75 97 "where issueid = ? " +
universe@83 98 "order by ordinal, name"
universe@38 99 );
universe@83 100 clearResolved = connection.prepareStatement("delete from lpit_issue_resolved_version where issueid = ?");
universe@83 101 insertResolved = connection.prepareStatement("insert into lpit_issue_resolved_version (issueid, versionid) values (?,?)");
universe@124 102
universe@124 103 insertComment = connection.prepareStatement(
universe@124 104 "insert into lpit_issue_comment (issueid, comment, userid) values (?, ? ,?)"
universe@124 105 );
universe@124 106 updateComment = connection.prepareStatement(
universe@124 107 "update lpit_issue_comment set comment = ?, updated = now(), updatecount = updatecount+1 where commentid = ?"
universe@124 108 );
universe@124 109 listComments = connection.prepareStatement(
universe@124 110 "select * from lpit_issue_comment left join lpit_user using (userid) where issueid = ? order by created"
universe@124 111 );
universe@38 112 }
universe@38 113
universe@75 114 private Issue mapColumns(ResultSet result) throws SQLException {
universe@75 115 final var project = new Project(result.getInt("project"));
universe@83 116 project.setName(result.getString("projectname"));
universe@138 117 project.setNode(result.getString("projectnode"));
universe@134 118 var component = new Component(result.getInt("component"));
universe@134 119 if (result.wasNull()) {
universe@134 120 component = null;
universe@134 121 } else {
universe@134 122 component.setName(result.getString("componentname"));
universe@138 123 component.setNode(result.getString("componentnode"));
universe@134 124 }
universe@86 125 final var issue = new Issue(result.getInt("issueid"));
universe@86 126 issue.setProject(project);
universe@134 127 issue.setComponent(component);
universe@75 128 issue.setStatus(IssueStatus.valueOf(result.getString("status")));
universe@75 129 issue.setCategory(IssueCategory.valueOf(result.getString("category")));
universe@75 130 issue.setSubject(result.getString("subject"));
universe@75 131 issue.setDescription(result.getString("description"));
universe@128 132 issue.setAssignee(PGUserDao.mapColumns(result));
universe@75 133 issue.setCreated(result.getTimestamp("created"));
universe@75 134 issue.setUpdated(result.getTimestamp("updated"));
universe@75 135 issue.setEta(result.getDate("eta"));
universe@62 136 return issue;
universe@38 137 }
universe@38 138
universe@86 139 private Version mapVersion(ResultSet result) throws SQLException {
universe@86 140 final var version = new Version(result.getInt("versionid"));
universe@83 141 version.setName(result.getString("name"));
universe@83 142 version.setOrdinal(result.getInt("ordinal"));
universe@83 143 version.setStatus(VersionStatus.valueOf(result.getString("status")));
universe@75 144 return version;
universe@75 145 }
universe@75 146
universe@83 147 private void updateVersionLists(Issue instance) throws SQLException {
universe@83 148 clearAffected.setInt(1, instance.getId());
universe@83 149 clearResolved.setInt(1, instance.getId());
universe@83 150 insertAffected.setInt(1, instance.getId());
universe@83 151 insertResolved.setInt(1, instance.getId());
universe@83 152 clearAffected.executeUpdate();
universe@83 153 clearResolved.executeUpdate();
universe@83 154 for (Version v : instance.getAffectedVersions()) {
universe@83 155 insertAffected.setInt(2, v.getId());
universe@83 156 insertAffected.executeUpdate();
universe@83 157 }
universe@83 158 for (Version v : instance.getResolvedVersions()) {
universe@83 159 insertResolved.setInt(2, v.getId());
universe@83 160 insertResolved.executeUpdate();
universe@83 161 }
universe@83 162 }
universe@83 163
universe@134 164 private int setData(PreparedStatement stmt, int column, Issue instance) throws SQLException {
universe@134 165 setForeignKeyOrNull(stmt, ++column, instance.getComponent(), Component::getId);
universe@134 166 stmt.setString(++column, instance.getStatus().name());
universe@134 167 stmt.setString(++column, instance.getCategory().name());
universe@134 168 stmt.setString(++column, instance.getSubject());
universe@134 169 setStringOrNull(stmt, ++column, instance.getDescription());
universe@134 170 setForeignKeyOrNull(stmt, ++column, instance.getAssignee(), User::getId);
universe@134 171 setDateOrNull(stmt, ++column, instance.getEta());
universe@134 172 return column;
universe@134 173 }
universe@134 174
universe@38 175 @Override
universe@128 176 public void save(Issue instance, Project project) throws SQLException {
universe@62 177 Objects.requireNonNull(instance.getSubject());
universe@128 178 instance.setProject(project);
universe@134 179 int column = 0;
universe@134 180 insert.setInt(++column, instance.getProject().getId());
universe@134 181 setData(insert, column, instance);
universe@75 182 // insert and retrieve the ID
universe@75 183 final var rs = insert.executeQuery();
universe@75 184 rs.next();
universe@75 185 instance.setId(rs.getInt(1));
universe@83 186 updateVersionLists(instance);
universe@38 187 }
universe@38 188
universe@38 189 @Override
universe@62 190 public boolean update(Issue instance) throws SQLException {
universe@75 191 if (instance.getId() < 0) return false;
universe@62 192 Objects.requireNonNull(instance.getSubject());
universe@134 193 int column = setData(update, 0, instance);
universe@134 194 update.setInt(++column, instance.getId());
universe@83 195 boolean success = update.executeUpdate() > 0;
universe@83 196 if (success) {
universe@83 197 updateVersionLists(instance);
universe@83 198 return true;
universe@83 199 } else {
universe@83 200 return false;
universe@83 201 }
universe@38 202 }
universe@47 203
universe@134 204 private List<Issue> executeQuery(PreparedStatement query) throws SQLException {
universe@75 205 List<Issue> issues = new ArrayList<>();
universe@86 206 try (var result = query.executeQuery()) {
universe@47 207 while (result.next()) {
universe@75 208 issues.add(mapColumns(result));
universe@47 209 }
universe@47 210 }
universe@75 211 return issues;
universe@47 212 }
universe@47 213
universe@47 214 @Override
universe@86 215 public List<Issue> list(Project project) throws SQLException {
universe@134 216 list.setInt(1, project.getId());
universe@134 217 list.setNull(2, Types.INTEGER);
universe@134 218 return executeQuery(list);
universe@86 219 }
universe@86 220
universe@86 221 @Override
universe@134 222 public List<Issue> list(Project project, Component component, Version version) throws SQLException {
universe@134 223 listForVersion.setInt(1, Optional.ofNullable(version).map(Version::getId).orElse(-1));
universe@134 224 listForVersion.setInt(2, Optional.ofNullable(component).map(Component::getId).orElse(-1));
universe@134 225 return executeQuery(listForVersion);
universe@134 226 }
universe@134 227
universe@134 228 @Override
universe@134 229 public List<Issue> list(Project project, Version version) throws SQLException {
universe@134 230 listForVersion.setInt(1, Optional.ofNullable(version).map(Version::getId).orElse(-1));
universe@134 231 listForVersion.setNull(2, Types.INTEGER);
universe@134 232 return executeQuery(listForVersion);
universe@134 233 }
universe@134 234
universe@134 235 @Override
universe@134 236 public List<Issue> list(Project project, Component component) throws SQLException {
universe@134 237 list.setInt(1, project.getId());
universe@134 238 list.setInt(2, Optional.ofNullable(component).map(Component::getId).orElse(-1));
universe@134 239 return executeQuery(list);
universe@86 240 }
universe@86 241
universe@86 242 @Override
universe@62 243 public Issue find(int id) throws SQLException {
universe@47 244 find.setInt(1, id);
universe@47 245 try (var result = find.executeQuery()) {
universe@47 246 if (result.next()) {
universe@47 247 return mapColumns(result);
universe@47 248 } else {
universe@47 249 return null;
universe@47 250 }
universe@47 251 }
universe@47 252 }
universe@75 253
universe@75 254 private List<Version> listVersions(PreparedStatement stmt, Issue issue) throws SQLException {
universe@75 255 stmt.setInt(1, issue.getId());
universe@75 256 List<Version> versions = new ArrayList<>();
universe@75 257 try (var result = stmt.executeQuery()) {
universe@75 258 while (result.next()) {
universe@86 259 versions.add(mapVersion(result));
universe@75 260 }
universe@75 261 }
universe@75 262 return versions;
universe@75 263 }
universe@75 264
universe@75 265 @Override
universe@75 266 public void joinVersionInformation(Issue issue) throws SQLException {
universe@75 267 Objects.requireNonNull(issue.getProject());
universe@75 268 issue.setAffectedVersions(listVersions(affectedVersions, issue));
universe@75 269 issue.setResolvedVersions(listVersions(resolvedVersions, issue));
universe@75 270 }
universe@124 271
universe@124 272 @Override
universe@124 273 public List<IssueComment> listComments(Issue issue) throws SQLException {
universe@124 274 listComments.setInt(1, issue.getId());
universe@124 275 List<IssueComment> comments = new ArrayList<>();
universe@124 276 try (var result = listComments.executeQuery()) {
universe@124 277 while (result.next()) {
universe@150 278 final var comment = new IssueComment(result.getInt("commentid"));
universe@124 279 comment.setCreated(result.getTimestamp("created"));
universe@124 280 comment.setUpdated(result.getTimestamp("updated"));
universe@124 281 comment.setUpdateCount(result.getInt("updatecount"));
universe@124 282 comment.setComment(result.getString("comment"));
universe@128 283 comment.setAuthor(PGUserDao.mapColumns(result));
universe@124 284 comments.add(comment);
universe@124 285 }
universe@124 286 }
universe@124 287 return comments;
universe@124 288 }
universe@124 289
universe@124 290 @Override
universe@150 291 public void saveComment(Issue issue, IssueComment comment) throws SQLException {
universe@124 292 if (comment.getId() >= 0) {
universe@124 293 updateComment.setString(1, comment.getComment());
universe@124 294 updateComment.setInt(2, comment.getId());
universe@124 295 updateComment.execute();
universe@124 296 } else {
universe@150 297 insertComment.setInt(1, issue.getId());
universe@124 298 insertComment.setString(2, comment.getComment());
universe@124 299 setForeignKeyOrNull(insertComment, 3, comment.getAuthor(), User::getId);
universe@124 300 insertComment.execute();
universe@124 301 }
universe@124 302 }
universe@38 303 }

mercurial