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

Fri, 22 May 2020 16:21:59 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 22 May 2020 16:21:59 +0200
changeset 72
0646c14e36fb
parent 70
821c4950b619
child 75
33b6843fdf8a
permissions
-rw-r--r--

some reformatting

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@47 46 private final PreparedStatement insert, update, list, find;
universe@38 47
universe@62 48 public PGIssueDao(Connection connection) throws SQLException {
universe@47 49 list = connection.prepareStatement(
universe@70 50 "select issue.id, issue.project, issue.status, issue.category, issue.subject, issue.description, " +
universe@62 51 "vplan.id, vplan.name, vdone.id, vdone.name, " +
universe@70 52 "issue.created, issue.updated, issue.eta " +
universe@70 53 "from lpit_issue issue " +
universe@62 54 "left join lpit_version vplan on vplan.id = version_plan " +
universe@62 55 "left join lpit_version vdone on vdone.id = version_done " +
universe@70 56 "where issue.project = ? ");
universe@47 57
universe@47 58 find = connection.prepareStatement(
universe@70 59 "select issue.id, issue.project, issue.status, issue.category, issue.subject, issue.description, " +
universe@62 60 "vplan.id, vplan.name, vdone.id, vdone.name, " +
universe@70 61 "issue.created, issue.updated, issue.eta " +
universe@70 62 "from lpit_issue issue " +
universe@62 63 "left join lpit_version vplan on vplan.id = version_plan " +
universe@62 64 "left join lpit_version vdone on vdone.id = version_done " +
universe@70 65 "where issue.id = ? ");
universe@38 66
universe@38 67 insert = connection.prepareStatement(
universe@62 68 "insert into lpit_issue (project, status, category, subject, description, version_plan, version_done, eta) " +
universe@62 69 "values (?, ?::issue_status, ?::issue_category, ?, ?, ?, ?, ?)"
universe@38 70 );
universe@38 71 update = connection.prepareStatement(
universe@62 72 "update lpit_issue set updated = now(), status = ?::issue_status, category = ?::issue_category, " +
universe@62 73 "subject = ?, description = ?, version_plan = ?, version_done = ?, eta = ? where id = ?"
universe@38 74 );
universe@38 75 }
universe@38 76
universe@62 77 private Version obtainVersion(ResultSet result, Project project, String prefix) throws SQLException {
universe@72 78 final int vplan = result.getInt(prefix + "id");
universe@62 79 if (vplan > 0) {
universe@62 80 final var ver = new Version(vplan, project);
universe@72 81 ver.setName(result.getString(prefix + "name"));
universe@62 82 return ver;
universe@62 83 } else {
universe@62 84 return null;
universe@62 85 }
universe@62 86 }
universe@62 87
universe@62 88 public Issue mapColumns(ResultSet result) throws SQLException {
universe@70 89 final var project = new Project(result.getInt("issue.project"));
universe@70 90 final var issue = new Issue(result.getInt("issue.id"), project);
universe@70 91 issue.setStatus(IssueStatus.valueOf(result.getString("issue.status")));
universe@70 92 issue.setCategory(IssueCategory.valueOf(result.getString("issue.category")));
universe@70 93 issue.setSubject(result.getString("issue.subject"));
universe@70 94 issue.setDescription(result.getString("issue.description"));
universe@62 95 issue.setScheduledVersion(obtainVersion(result, project, "vplan."));
universe@62 96 issue.setResolvedVersion(obtainVersion(result, project, "vdone."));
universe@70 97 issue.setCreated(result.getTimestamp("issue.created"));
universe@70 98 issue.setUpdated(result.getTimestamp("issue.updated"));
universe@70 99 issue.setEta(result.getDate("issue.eta"));
universe@62 100 return issue;
universe@38 101 }
universe@38 102
universe@38 103 @Override
universe@62 104 public void save(Issue instance) throws SQLException {
universe@62 105 Objects.requireNonNull(instance.getSubject());
universe@59 106 Objects.requireNonNull(instance.getProject());
universe@59 107 insert.setInt(1, instance.getProject().getId());
universe@62 108 insert.setString(2, instance.getStatus().name());
universe@62 109 insert.setString(3, instance.getCategory().name());
universe@62 110 insert.setString(4, instance.getSubject());
universe@62 111 setStringOrNull(insert, 5, instance.getDescription());
universe@62 112 setForeignKeyOrNull(insert, 6, instance.getScheduledVersion(), Version::getId);
universe@62 113 setForeignKeyOrNull(insert, 7, instance.getResolvedVersion(), Version::getId);
universe@62 114 setDateOrNull(insert, 8, instance.getEta());
universe@38 115 insert.executeUpdate();
universe@38 116 }
universe@38 117
universe@38 118 @Override
universe@62 119 public boolean update(Issue instance) throws SQLException {
universe@62 120 Objects.requireNonNull(instance.getSubject());
universe@62 121 update.setString(1, instance.getStatus().name());
universe@62 122 update.setString(2, instance.getCategory().name());
universe@62 123 update.setString(3, instance.getSubject());
universe@62 124 setStringOrNull(update, 4, instance.getDescription());
universe@62 125 setForeignKeyOrNull(update, 5, instance.getScheduledVersion(), Version::getId);
universe@62 126 setForeignKeyOrNull(update, 6, instance.getResolvedVersion(), Version::getId);
universe@62 127 setDateOrNull(update, 7, instance.getEta());
universe@62 128 update.setInt(8, instance.getId());
universe@38 129 return update.executeUpdate() > 0;
universe@38 130 }
universe@47 131
universe@47 132 @Override
universe@62 133 public List<Issue> list(Project project) throws SQLException {
universe@59 134 list.setInt(1, project.getId());
universe@62 135 List<Issue> versions = new ArrayList<>();
universe@47 136 try (var result = list.executeQuery()) {
universe@47 137 while (result.next()) {
universe@59 138 versions.add(mapColumns(result));
universe@47 139 }
universe@47 140 }
universe@59 141 return versions;
universe@47 142 }
universe@47 143
universe@47 144 @Override
universe@62 145 public Issue find(int id) throws SQLException {
universe@47 146 find.setInt(1, id);
universe@47 147 try (var result = find.executeQuery()) {
universe@47 148 if (result.next()) {
universe@47 149 return mapColumns(result);
universe@47 150 } else {
universe@47 151 return null;
universe@47 152 }
universe@47 153 }
universe@47 154 }
universe@38 155 }

mercurial