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

Sat, 30 May 2020 18:05:06 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 30 May 2020 18:05:06 +0200
changeset 83
24a3596b8f98
parent 80
27a25f32048e
child 86
0a658e53177c
permissions
-rw-r--r--

adds version selection in issue editor

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@59 31 import de.uapcore.lightpit.dao.VersionDao;
universe@80 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@59 42 public final class PGVersionDao implements VersionDao {
universe@38 43
universe@47 44 private final PreparedStatement insert, update, list, find;
universe@80 45 private final PreparedStatement issuesAffected, issuesScheduled, issuesResolved;
universe@38 46
universe@59 47 public PGVersionDao(Connection connection) throws SQLException {
universe@47 48 list = connection.prepareStatement(
universe@83 49 "select versionid, project, p.name as projectname, v.name, ordinal, status " +
universe@83 50 "from lpit_version v " +
universe@83 51 "join lpit_project p on v.project = p.projectid " +
universe@59 52 "where project = ? " +
universe@83 53 "order by ordinal desc, lower(v.name) desc");
universe@47 54
universe@47 55 find = connection.prepareStatement(
universe@83 56 "select versionid, project, p.name as projectname, v.name, ordinal, status " +
universe@83 57 "from lpit_version v " +
universe@83 58 "join lpit_project p on v.project = p.projectid " +
universe@75 59 "where versionid = ?");
universe@38 60
universe@38 61 insert = connection.prepareStatement(
universe@59 62 "insert into lpit_version (project, name, ordinal, status) values (?, ?, ?, ?::version_status)"
universe@38 63 );
universe@38 64 update = connection.prepareStatement(
universe@75 65 "update lpit_version set name = ?, ordinal = ?, status = ?::version_status where versionid = ?"
universe@38 66 );
universe@80 67
universe@80 68 issuesAffected = connection.prepareStatement(
universe@80 69 "select category, status, count(*) as issuecount " +
universe@80 70 "from lpit_issue_affected_version " +
universe@80 71 "join lpit_issue using (issueid) " +
universe@80 72 "where versionid = ? " +
universe@80 73 "group by category, status"
universe@80 74 );
universe@80 75 issuesScheduled = connection.prepareStatement(
universe@80 76 "select category, status, count(*) as issuecount " +
universe@80 77 "from lpit_issue_scheduled_version " +
universe@80 78 "join lpit_issue using (issueid) " +
universe@80 79 "where versionid = ? " +
universe@80 80 "group by category, status"
universe@80 81 );
universe@80 82 issuesResolved = connection.prepareStatement(
universe@80 83 "select category, status, count(*) as issuecount " +
universe@80 84 "from lpit_issue_resolved_version " +
universe@80 85 "join lpit_issue using (issueid) " +
universe@80 86 "where versionid = ? " +
universe@80 87 "group by category, status"
universe@80 88 );
universe@38 89 }
universe@38 90
universe@75 91 private Version mapColumns(ResultSet result) throws SQLException {
universe@75 92 final var project = new Project(result.getInt("project"));
universe@83 93 project.setName(result.getString("projectname"));
universe@75 94 final var version = new Version(result.getInt("versionid"), project);
universe@59 95 version.setName(result.getString("name"));
universe@59 96 version.setOrdinal(result.getInt("ordinal"));
universe@59 97 version.setStatus(VersionStatus.valueOf(result.getString("status")));
universe@59 98 return version;
universe@38 99 }
universe@38 100
universe@80 101 private VersionStatistics versionStatistics(Version version, PreparedStatement stmt) throws SQLException {
universe@80 102 stmt.setInt(1, version.getId());
universe@80 103 final var result = stmt.executeQuery();
universe@80 104 final var stats = new VersionStatistics(version);
universe@80 105 while (result.next()) {
universe@80 106 stats.setIssueCount(
universe@80 107 IssueCategory.valueOf(result.getString("category")),
universe@80 108 IssueStatus.valueOf(result.getString("status")),
universe@80 109 result.getInt("issuecount")
universe@80 110 );
universe@80 111 }
universe@80 112 return stats;
universe@80 113 }
universe@80 114
universe@38 115 @Override
universe@59 116 public void save(Version instance) throws SQLException {
universe@38 117 Objects.requireNonNull(instance.getName());
universe@59 118 Objects.requireNonNull(instance.getProject());
universe@59 119 insert.setInt(1, instance.getProject().getId());
universe@59 120 insert.setString(2, instance.getName());
universe@59 121 insert.setInt(3, instance.getOrdinal());
universe@59 122 insert.setString(4, instance.getStatus().name());
universe@38 123 insert.executeUpdate();
universe@38 124 }
universe@38 125
universe@38 126 @Override
universe@59 127 public boolean update(Version instance) throws SQLException {
universe@75 128 if (instance.getId() < 0) return false;
universe@38 129 Objects.requireNonNull(instance.getName());
universe@38 130 update.setString(1, instance.getName());
universe@59 131 update.setInt(2, instance.getOrdinal());
universe@59 132 update.setString(3, instance.getStatus().name());
universe@59 133 update.setInt(4, instance.getId());
universe@38 134 return update.executeUpdate() > 0;
universe@38 135 }
universe@47 136
universe@47 137 @Override
universe@59 138 public List<Version> list(Project project) throws SQLException {
universe@59 139 list.setInt(1, project.getId());
universe@59 140 List<Version> versions = new ArrayList<>();
universe@47 141 try (var result = list.executeQuery()) {
universe@47 142 while (result.next()) {
universe@75 143 final var v = mapColumns(result);
universe@75 144 v.setProject(project);
universe@75 145 versions.add(v);
universe@47 146 }
universe@47 147 }
universe@59 148 return versions;
universe@47 149 }
universe@47 150
universe@47 151 @Override
universe@59 152 public Version find(int id) throws SQLException {
universe@47 153 find.setInt(1, id);
universe@47 154 try (var result = find.executeQuery()) {
universe@47 155 if (result.next()) {
universe@47 156 return mapColumns(result);
universe@47 157 } else {
universe@47 158 return null;
universe@47 159 }
universe@47 160 }
universe@47 161 }
universe@80 162
universe@80 163 @Override
universe@80 164 public VersionStatistics statsOpenedIssues(Version version) throws SQLException {
universe@80 165 return versionStatistics(version, issuesAffected);
universe@80 166 }
universe@80 167
universe@80 168 @Override
universe@80 169 public VersionStatistics statsScheduledIssues(Version version) throws SQLException {
universe@80 170 return versionStatistics(version, issuesScheduled);
universe@80 171 }
universe@80 172
universe@80 173 @Override
universe@80 174 public VersionStatistics statsResolvedIssues(Version version) throws SQLException {
universe@80 175 return versionStatistics(version, issuesResolved);
universe@80 176 }
universe@38 177 }

mercurial