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

Fri, 22 May 2020 21:23:57 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 22 May 2020 21:23:57 +0200
changeset 75
33b6843fdf8a
parent 72
0646c14e36fb
child 83
24a3596b8f98
permissions
-rw-r--r--

adds the ability to create and edit 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@75 46 private final PreparedStatement insert, update, list, find, affectedVersions, scheduledVersions, resolvedVersions;
universe@38 47
universe@62 48 public PGIssueDao(Connection connection) throws SQLException {
universe@47 49 list = connection.prepareStatement(
universe@75 50 "select issueid, project, status, category, subject, description, " +
universe@75 51 "userid, username, givenname, lastname, mail, " +
universe@75 52 "created, updated, eta " +
universe@75 53 "from lpit_issue " +
universe@75 54 "left join lpit_user on userid = assignee " +
universe@75 55 "where project = ? ");
universe@47 56
universe@47 57 find = connection.prepareStatement(
universe@75 58 "select issueid, project, status, category, subject, description, " +
universe@75 59 "userid, username, givenname, lastname, mail, " +
universe@75 60 "created, updated, eta " +
universe@75 61 "from lpit_issue " +
universe@75 62 "left join lpit_user on userid = assignee " +
universe@75 63 "where issueid = ? ");
universe@38 64
universe@38 65 insert = connection.prepareStatement(
universe@75 66 "insert into lpit_issue (project, status, category, subject, description, assignee, eta) " +
universe@75 67 "values (?, ?::issue_status, ?::issue_category, ?, ?, ?, ?) returning issueid"
universe@38 68 );
universe@38 69 update = connection.prepareStatement(
universe@62 70 "update lpit_issue set updated = now(), status = ?::issue_status, category = ?::issue_category, " +
universe@75 71 "subject = ?, description = ?, assignee = ?, eta = ? where issueid = ?"
universe@75 72 );
universe@75 73
universe@75 74 affectedVersions = connection.prepareStatement(
universe@75 75 "select v.versionid, v.name, v.status, v.ordinal " +
universe@75 76 "from lpit_version v join lpit_issue_affected_version using (versionid) " +
universe@75 77 "where issueid = ? " +
universe@75 78 "order by v.ordinal, v.name"
universe@75 79 );
universe@75 80
universe@75 81 scheduledVersions = connection.prepareStatement(
universe@75 82 "select v.versionid, v.name, v.status, v.ordinal " +
universe@75 83 "from lpit_version v join lpit_issue_scheduled_version using (versionid) " +
universe@75 84 "where issueid = ? " +
universe@75 85 "order by v.ordinal, v.name"
universe@75 86 );
universe@75 87
universe@75 88 resolvedVersions = connection.prepareStatement(
universe@75 89 "select v.versionid, v.name, v.status, v.ordinal " +
universe@75 90 "from lpit_version v join lpit_issue_resolved_version using (versionid) " +
universe@75 91 "where issueid = ? " +
universe@75 92 "order by v.ordinal, v.name"
universe@38 93 );
universe@38 94 }
universe@38 95
universe@75 96 private User obtainAssignee(ResultSet result) throws SQLException {
universe@75 97 final int id = result.getInt("userid");
universe@75 98 if (id != 0) {
universe@75 99 final var user = new User(id);
universe@75 100 user.setUsername(result.getString("username"));
universe@75 101 user.setGivenname(result.getString("givenname"));
universe@75 102 user.setLastname(result.getString("lastname"));
universe@75 103 user.setMail(result.getString("mail"));
universe@75 104 return user;
universe@62 105 } else {
universe@62 106 return null;
universe@62 107 }
universe@62 108 }
universe@62 109
universe@75 110 private Issue mapColumns(ResultSet result) throws SQLException {
universe@75 111 final var project = new Project(result.getInt("project"));
universe@75 112 final var issue = new Issue(result.getInt("issueid"), project);
universe@75 113 issue.setStatus(IssueStatus.valueOf(result.getString("status")));
universe@75 114 issue.setCategory(IssueCategory.valueOf(result.getString("category")));
universe@75 115 issue.setSubject(result.getString("subject"));
universe@75 116 issue.setDescription(result.getString("description"));
universe@75 117 issue.setAssignee(obtainAssignee(result));
universe@75 118 issue.setCreated(result.getTimestamp("created"));
universe@75 119 issue.setUpdated(result.getTimestamp("updated"));
universe@75 120 issue.setEta(result.getDate("eta"));
universe@62 121 return issue;
universe@38 122 }
universe@38 123
universe@75 124 private Version mapVersion(ResultSet result, Project project) throws SQLException {
universe@75 125 final var version = new Version(result.getInt("v.versionid"), project);
universe@75 126 version.setName(result.getString("v.name"));
universe@75 127 version.setOrdinal(result.getInt("v.ordinal"));
universe@75 128 version.setStatus(VersionStatus.valueOf(result.getString("v.status")));
universe@75 129 return version;
universe@75 130 }
universe@75 131
universe@38 132 @Override
universe@62 133 public void save(Issue instance) throws SQLException {
universe@62 134 Objects.requireNonNull(instance.getSubject());
universe@59 135 Objects.requireNonNull(instance.getProject());
universe@59 136 insert.setInt(1, instance.getProject().getId());
universe@62 137 insert.setString(2, instance.getStatus().name());
universe@62 138 insert.setString(3, instance.getCategory().name());
universe@62 139 insert.setString(4, instance.getSubject());
universe@62 140 setStringOrNull(insert, 5, instance.getDescription());
universe@75 141 setForeignKeyOrNull(insert, 6, instance.getAssignee(), User::getId);
universe@75 142 setDateOrNull(insert, 7, instance.getEta());
universe@75 143 // insert and retrieve the ID
universe@75 144 final var rs = insert.executeQuery();
universe@75 145 rs.next();
universe@75 146 instance.setId(rs.getInt(1));
universe@38 147 }
universe@38 148
universe@38 149 @Override
universe@62 150 public boolean update(Issue instance) throws SQLException {
universe@75 151 if (instance.getId() < 0) return false;
universe@62 152 Objects.requireNonNull(instance.getSubject());
universe@62 153 update.setString(1, instance.getStatus().name());
universe@62 154 update.setString(2, instance.getCategory().name());
universe@62 155 update.setString(3, instance.getSubject());
universe@62 156 setStringOrNull(update, 4, instance.getDescription());
universe@75 157 setForeignKeyOrNull(update, 5, instance.getAssignee(), User::getId);
universe@75 158 setDateOrNull(update, 6, instance.getEta());
universe@75 159 update.setInt(7, instance.getId());
universe@38 160 return update.executeUpdate() > 0;
universe@38 161 }
universe@47 162
universe@47 163 @Override
universe@62 164 public List<Issue> list(Project project) throws SQLException {
universe@59 165 list.setInt(1, project.getId());
universe@75 166 List<Issue> issues = new ArrayList<>();
universe@47 167 try (var result = list.executeQuery()) {
universe@47 168 while (result.next()) {
universe@75 169 issues.add(mapColumns(result));
universe@47 170 }
universe@47 171 }
universe@75 172 return issues;
universe@47 173 }
universe@47 174
universe@47 175 @Override
universe@62 176 public Issue find(int id) throws SQLException {
universe@47 177 find.setInt(1, id);
universe@47 178 try (var result = find.executeQuery()) {
universe@47 179 if (result.next()) {
universe@47 180 return mapColumns(result);
universe@47 181 } else {
universe@47 182 return null;
universe@47 183 }
universe@47 184 }
universe@47 185 }
universe@75 186
universe@75 187 private List<Version> listVersions(PreparedStatement stmt, Issue issue) throws SQLException {
universe@75 188 stmt.setInt(1, issue.getId());
universe@75 189 List<Version> versions = new ArrayList<>();
universe@75 190 try (var result = stmt.executeQuery()) {
universe@75 191 while (result.next()) {
universe@75 192 versions.add(mapVersion(result, issue.getProject()));
universe@75 193 }
universe@75 194 }
universe@75 195 return versions;
universe@75 196 }
universe@75 197
universe@75 198 @Override
universe@75 199 public void joinVersionInformation(Issue issue) throws SQLException {
universe@75 200 Objects.requireNonNull(issue.getProject());
universe@75 201 issue.setAffectedVersions(listVersions(affectedVersions, issue));
universe@75 202 issue.setScheduledVersions(listVersions(scheduledVersions, issue));
universe@75 203 issue.setResolvedVersions(listVersions(resolvedVersions, issue));
universe@75 204 }
universe@38 205 }

mercurial