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

Thu, 15 Oct 2020 18:36:05 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 15 Oct 2020 18:36:05 +0200
changeset 130
7ef369744fd1
parent 128
947d0f6a6a83
child 134
f47e82cd6077
permissions
-rw-r--r--

adds the possibility to specify path parameters to RequestMapping

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@124 50 private final PreparedStatement insertComment, updateComment, listComments;
universe@38 51
universe@62 52 public PGIssueDao(Connection connection) throws SQLException {
universe@47 53 list = connection.prepareStatement(
universe@83 54 "select issueid, project, p.name as projectname, status, category, subject, i.description, " +
universe@75 55 "userid, username, givenname, lastname, mail, " +
universe@75 56 "created, updated, eta " +
universe@83 57 "from lpit_issue i " +
universe@105 58 "join lpit_project p on project = projectid " +
universe@75 59 "left join lpit_user on userid = assignee " +
universe@121 60 "where project = ? ");
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@105 70 "join lpit_project p on project = projectid " +
universe@105 71 "left join issue_version using (issueid) "+
universe@86 72 "left join lpit_user on userid = assignee " +
universe@121 73 "where coalesce(versionid,-1) = ? "
universe@86 74 );
universe@47 75
universe@47 76 find = connection.prepareStatement(
universe@83 77 "select issueid, project, p.name as projectname, status, category, subject, i.description, " +
universe@75 78 "userid, username, givenname, lastname, mail, " +
universe@75 79 "created, updated, eta " +
universe@83 80 "from lpit_issue i " +
universe@83 81 "left join lpit_project p on project = projectid " +
universe@75 82 "left join lpit_user on userid = assignee " +
universe@75 83 "where issueid = ? ");
universe@38 84
universe@38 85 insert = connection.prepareStatement(
universe@75 86 "insert into lpit_issue (project, status, category, subject, description, assignee, eta) " +
universe@75 87 "values (?, ?::issue_status, ?::issue_category, ?, ?, ?, ?) returning issueid"
universe@38 88 );
universe@38 89 update = connection.prepareStatement(
universe@62 90 "update lpit_issue set updated = now(), status = ?::issue_status, category = ?::issue_category, " +
universe@75 91 "subject = ?, description = ?, assignee = ?, eta = ? where issueid = ?"
universe@75 92 );
universe@75 93
universe@75 94 affectedVersions = connection.prepareStatement(
universe@83 95 "select versionid, name, status, ordinal " +
universe@83 96 "from lpit_version join lpit_issue_affected_version using (versionid) " +
universe@75 97 "where issueid = ? " +
universe@83 98 "order by ordinal, name"
universe@75 99 );
universe@83 100 clearAffected = connection.prepareStatement("delete from lpit_issue_affected_version where issueid = ?");
universe@83 101 insertAffected = connection.prepareStatement("insert into lpit_issue_affected_version (issueid, versionid) values (?,?)");
universe@75 102
universe@75 103 resolvedVersions = connection.prepareStatement(
universe@83 104 "select versionid, name, status, ordinal " +
universe@75 105 "from lpit_version v join lpit_issue_resolved_version using (versionid) " +
universe@75 106 "where issueid = ? " +
universe@83 107 "order by ordinal, name"
universe@38 108 );
universe@83 109 clearResolved = connection.prepareStatement("delete from lpit_issue_resolved_version where issueid = ?");
universe@83 110 insertResolved = connection.prepareStatement("insert into lpit_issue_resolved_version (issueid, versionid) values (?,?)");
universe@124 111
universe@124 112 insertComment = connection.prepareStatement(
universe@124 113 "insert into lpit_issue_comment (issueid, comment, userid) values (?, ? ,?)"
universe@124 114 );
universe@124 115 updateComment = connection.prepareStatement(
universe@124 116 "update lpit_issue_comment set comment = ?, updated = now(), updatecount = updatecount+1 where commentid = ?"
universe@124 117 );
universe@124 118 listComments = connection.prepareStatement(
universe@124 119 "select * from lpit_issue_comment left join lpit_user using (userid) where issueid = ? order by created"
universe@124 120 );
universe@38 121 }
universe@38 122
universe@75 123 private Issue mapColumns(ResultSet result) throws SQLException {
universe@75 124 final var project = new Project(result.getInt("project"));
universe@83 125 project.setName(result.getString("projectname"));
universe@86 126 final var issue = new Issue(result.getInt("issueid"));
universe@86 127 issue.setProject(project);
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@38 164 @Override
universe@128 165 public void save(Issue instance, Project project) throws SQLException {
universe@62 166 Objects.requireNonNull(instance.getSubject());
universe@128 167 instance.setProject(project);
universe@59 168 insert.setInt(1, instance.getProject().getId());
universe@62 169 insert.setString(2, instance.getStatus().name());
universe@62 170 insert.setString(3, instance.getCategory().name());
universe@62 171 insert.setString(4, instance.getSubject());
universe@62 172 setStringOrNull(insert, 5, instance.getDescription());
universe@75 173 setForeignKeyOrNull(insert, 6, instance.getAssignee(), User::getId);
universe@75 174 setDateOrNull(insert, 7, instance.getEta());
universe@75 175 // insert and retrieve the ID
universe@75 176 final var rs = insert.executeQuery();
universe@75 177 rs.next();
universe@75 178 instance.setId(rs.getInt(1));
universe@83 179 updateVersionLists(instance);
universe@38 180 }
universe@38 181
universe@38 182 @Override
universe@62 183 public boolean update(Issue instance) throws SQLException {
universe@75 184 if (instance.getId() < 0) return false;
universe@62 185 Objects.requireNonNull(instance.getSubject());
universe@62 186 update.setString(1, instance.getStatus().name());
universe@62 187 update.setString(2, instance.getCategory().name());
universe@62 188 update.setString(3, instance.getSubject());
universe@62 189 setStringOrNull(update, 4, instance.getDescription());
universe@75 190 setForeignKeyOrNull(update, 5, instance.getAssignee(), User::getId);
universe@75 191 setDateOrNull(update, 6, instance.getEta());
universe@75 192 update.setInt(7, instance.getId());
universe@83 193 boolean success = update.executeUpdate() > 0;
universe@83 194 if (success) {
universe@83 195 updateVersionLists(instance);
universe@83 196 return true;
universe@83 197 } else {
universe@83 198 return false;
universe@83 199 }
universe@38 200 }
universe@47 201
universe@86 202 private List<Issue> list(PreparedStatement query, int arg) throws SQLException {
universe@86 203 query.setInt(1, arg);
universe@75 204 List<Issue> issues = new ArrayList<>();
universe@86 205 try (var result = query.executeQuery()) {
universe@47 206 while (result.next()) {
universe@75 207 issues.add(mapColumns(result));
universe@47 208 }
universe@47 209 }
universe@75 210 return issues;
universe@47 211 }
universe@47 212
universe@47 213 @Override
universe@86 214 public List<Issue> list(Project project) throws SQLException {
universe@86 215 return list(list, project.getId());
universe@86 216 }
universe@86 217
universe@86 218 @Override
universe@86 219 public List<Issue> list(Version version) throws SQLException {
universe@105 220 return list(listForVersion, version == null ? -1 : version.getId());
universe@86 221 }
universe@86 222
universe@86 223 @Override
universe@62 224 public Issue find(int id) throws SQLException {
universe@47 225 find.setInt(1, id);
universe@47 226 try (var result = find.executeQuery()) {
universe@47 227 if (result.next()) {
universe@47 228 return mapColumns(result);
universe@47 229 } else {
universe@47 230 return null;
universe@47 231 }
universe@47 232 }
universe@47 233 }
universe@75 234
universe@75 235 private List<Version> listVersions(PreparedStatement stmt, Issue issue) throws SQLException {
universe@75 236 stmt.setInt(1, issue.getId());
universe@75 237 List<Version> versions = new ArrayList<>();
universe@75 238 try (var result = stmt.executeQuery()) {
universe@75 239 while (result.next()) {
universe@86 240 versions.add(mapVersion(result));
universe@75 241 }
universe@75 242 }
universe@75 243 return versions;
universe@75 244 }
universe@75 245
universe@75 246 @Override
universe@75 247 public void joinVersionInformation(Issue issue) throws SQLException {
universe@75 248 Objects.requireNonNull(issue.getProject());
universe@75 249 issue.setAffectedVersions(listVersions(affectedVersions, issue));
universe@75 250 issue.setResolvedVersions(listVersions(resolvedVersions, issue));
universe@75 251 }
universe@124 252
universe@124 253 @Override
universe@124 254 public List<IssueComment> listComments(Issue issue) throws SQLException {
universe@124 255 listComments.setInt(1, issue.getId());
universe@124 256 List<IssueComment> comments = new ArrayList<>();
universe@124 257 try (var result = listComments.executeQuery()) {
universe@124 258 while (result.next()) {
universe@124 259 final var comment = new IssueComment(result.getInt("commentid"), issue);
universe@124 260 comment.setCreated(result.getTimestamp("created"));
universe@124 261 comment.setUpdated(result.getTimestamp("updated"));
universe@124 262 comment.setUpdateCount(result.getInt("updatecount"));
universe@124 263 comment.setComment(result.getString("comment"));
universe@128 264 comment.setAuthor(PGUserDao.mapColumns(result));
universe@124 265 comments.add(comment);
universe@124 266 }
universe@124 267 }
universe@124 268 return comments;
universe@124 269 }
universe@124 270
universe@124 271 @Override
universe@124 272 public void saveComment(IssueComment comment) throws SQLException {
universe@124 273 Objects.requireNonNull(comment.getComment());
universe@124 274 Objects.requireNonNull(comment.getIssue());
universe@124 275 if (comment.getId() >= 0) {
universe@124 276 updateComment.setString(1, comment.getComment());
universe@124 277 updateComment.setInt(2, comment.getId());
universe@124 278 updateComment.execute();
universe@124 279 } else {
universe@124 280 insertComment.setInt(1, comment.getIssue().getId());
universe@124 281 insertComment.setString(2, comment.getComment());
universe@124 282 setForeignKeyOrNull(insertComment, 3, comment.getAuthor(), User::getId);
universe@124 283 insertComment.execute();
universe@124 284 }
universe@124 285 }
universe@38 286 }

mercurial