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

Mon, 01 Jun 2020 14:46:58 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 01 Jun 2020 14:46:58 +0200
changeset 86
0a658e53177c
parent 81
1a2e7b5d48f7
child 128
947d0f6a6a83
permissions
-rw-r--r--

improves issue overview and adds progress information

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@38 31 import de.uapcore.lightpit.dao.ProjectDao;
universe@86 32 import de.uapcore.lightpit.entities.IssueSummary;
universe@38 33 import de.uapcore.lightpit.entities.Project;
universe@38 34 import de.uapcore.lightpit.entities.User;
universe@38 35
universe@38 36 import java.sql.Connection;
universe@38 37 import java.sql.PreparedStatement;
universe@38 38 import java.sql.ResultSet;
universe@38 39 import java.sql.SQLException;
universe@47 40 import java.util.ArrayList;
universe@47 41 import java.util.List;
universe@38 42 import java.util.Objects;
universe@38 43
universe@47 44 import static de.uapcore.lightpit.dao.Functions.setForeignKeyOrNull;
universe@47 45 import static de.uapcore.lightpit.dao.Functions.setStringOrNull;
universe@38 46
universe@59 47 public final class PGProjectDao implements ProjectDao {
universe@38 48
universe@47 49 private final PreparedStatement insert, update, list, find;
universe@81 50 private final PreparedStatement issue_summary;
universe@38 51
universe@47 52 public PGProjectDao(Connection connection) throws SQLException {
universe@47 53 list = connection.prepareStatement(
universe@75 54 "select projectid, name, description, repourl, " +
universe@47 55 "userid, username, lastname, givenname, mail " +
universe@47 56 "from lpit_project " +
universe@47 57 "left join lpit_user owner on lpit_project.owner = owner.userid " +
universe@47 58 "order by name");
universe@47 59
universe@47 60 find = connection.prepareStatement(
universe@75 61 "select projectid, name, description, repourl, " +
universe@47 62 "userid, username, lastname, givenname, mail " +
universe@47 63 "from lpit_project " +
universe@47 64 "left join lpit_user owner on lpit_project.owner = owner.userid " +
universe@75 65 "where projectid = ?");
universe@38 66
universe@81 67 issue_summary = connection.prepareStatement(
universe@81 68 "select phase, count(*) as total "+
universe@81 69 "from lpit_issue " +
universe@81 70 "join lpit_issue_phases using(status) " +
universe@81 71 "where project = ? "+
universe@81 72 "group by phase "
universe@81 73 );
universe@81 74
universe@38 75 insert = connection.prepareStatement(
universe@38 76 "insert into lpit_project (name, description, repourl, owner) values (?, ?, ?, ?)"
universe@38 77 );
universe@38 78 update = connection.prepareStatement(
universe@75 79 "update lpit_project set name = ?, description = ?, repourl = ?, owner = ? where projectid = ?"
universe@38 80 );
universe@38 81 }
universe@38 82
universe@47 83 public Project mapColumns(ResultSet result) throws SQLException {
universe@75 84 final var proj = new Project(result.getInt("projectid"));
universe@47 85 proj.setName(result.getString("name"));
universe@47 86 proj.setDescription(result.getString("description"));
universe@47 87 proj.setRepoUrl(result.getString("repourl"));
universe@47 88
universe@47 89 final int id = result.getInt("userid");
universe@47 90 if (id != 0) {
universe@47 91 final var user = new User(id);
universe@47 92 user.setUsername(result.getString("username"));
universe@47 93 user.setGivenname(result.getString("givenname"));
universe@47 94 user.setLastname(result.getString("lastname"));
universe@47 95 user.setMail(result.getString("mail"));
universe@47 96 proj.setOwner(user);
universe@47 97 }
universe@47 98
universe@38 99 return proj;
universe@38 100 }
universe@38 101
universe@86 102 public IssueSummary getIssueSummary(Project project) throws SQLException {
universe@86 103 issue_summary.setInt(1, project.getId());
universe@81 104 final var result = issue_summary.executeQuery();
universe@86 105 final var summary = new IssueSummary();
universe@81 106 while (result.next()) {
universe@81 107 final var phase = result.getInt("phase");
universe@81 108 final var total = result.getInt("total");
universe@81 109 switch(phase) {
universe@81 110 case 0:
universe@86 111 summary.setOpen(total);
universe@81 112 break;
universe@81 113 case 1:
universe@86 114 summary.setActive(total);
universe@81 115 break;
universe@81 116 case 2:
universe@86 117 summary.setDone(total);
universe@81 118 break;
universe@81 119 }
universe@81 120 }
universe@86 121 return summary;
universe@81 122 }
universe@81 123
universe@38 124 @Override
universe@38 125 public void save(Project instance) throws SQLException {
universe@38 126 Objects.requireNonNull(instance.getName());
universe@38 127 insert.setString(1, instance.getName());
universe@38 128 setStringOrNull(insert, 2, instance.getDescription());
universe@38 129 setStringOrNull(insert, 3, instance.getRepoUrl());
universe@51 130 setForeignKeyOrNull(insert, 4, instance.getOwner(), User::getId);
universe@38 131 insert.executeUpdate();
universe@38 132 }
universe@38 133
universe@38 134 @Override
universe@38 135 public boolean update(Project instance) throws SQLException {
universe@75 136 if (instance.getId() < 0) return false;
universe@38 137 Objects.requireNonNull(instance.getName());
universe@38 138 update.setString(1, instance.getName());
universe@38 139 setStringOrNull(update, 2, instance.getDescription());
universe@38 140 setStringOrNull(update, 3, instance.getRepoUrl());
universe@51 141 setForeignKeyOrNull(update, 4, instance.getOwner(), User::getId);
universe@47 142 update.setInt(5, instance.getId());
universe@38 143 return update.executeUpdate() > 0;
universe@38 144 }
universe@47 145
universe@47 146 @Override
universe@47 147 public List<Project> list() throws SQLException {
universe@47 148 List<Project> projects = new ArrayList<>();
universe@47 149 try (var result = list.executeQuery()) {
universe@47 150 while (result.next()) {
universe@81 151 final var project = mapColumns(result);
universe@81 152 projects.add(project);
universe@47 153 }
universe@47 154 }
universe@47 155 return projects;
universe@47 156 }
universe@47 157
universe@47 158 @Override
universe@47 159 public Project find(int id) throws SQLException {
universe@47 160 find.setInt(1, id);
universe@47 161 try (var result = find.executeQuery()) {
universe@47 162 if (result.next()) {
universe@81 163 final var project = mapColumns(result);
universe@81 164 return project;
universe@47 165 } else {
universe@47 166 return null;
universe@47 167 }
universe@47 168 }
universe@47 169 }
universe@38 170 }

mercurial