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

Thu, 15 Oct 2020 13:31:52 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 15 Oct 2020 13:31:52 +0200
changeset 128
947d0f6a6a83
parent 86
0a658e53177c
child 138
e2aa673dd473
permissions
-rw-r--r--

changes the way how to deal with child entities + adds component lead

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@128 88 proj.setOwner(PGUserDao.mapColumns(result));
universe@47 89
universe@38 90 return proj;
universe@38 91 }
universe@38 92
universe@86 93 public IssueSummary getIssueSummary(Project project) throws SQLException {
universe@86 94 issue_summary.setInt(1, project.getId());
universe@81 95 final var result = issue_summary.executeQuery();
universe@86 96 final var summary = new IssueSummary();
universe@81 97 while (result.next()) {
universe@81 98 final var phase = result.getInt("phase");
universe@81 99 final var total = result.getInt("total");
universe@81 100 switch(phase) {
universe@81 101 case 0:
universe@86 102 summary.setOpen(total);
universe@81 103 break;
universe@81 104 case 1:
universe@86 105 summary.setActive(total);
universe@81 106 break;
universe@81 107 case 2:
universe@86 108 summary.setDone(total);
universe@81 109 break;
universe@81 110 }
universe@81 111 }
universe@86 112 return summary;
universe@81 113 }
universe@81 114
universe@38 115 @Override
universe@38 116 public void save(Project instance) throws SQLException {
universe@38 117 Objects.requireNonNull(instance.getName());
universe@38 118 insert.setString(1, instance.getName());
universe@38 119 setStringOrNull(insert, 2, instance.getDescription());
universe@38 120 setStringOrNull(insert, 3, instance.getRepoUrl());
universe@51 121 setForeignKeyOrNull(insert, 4, instance.getOwner(), User::getId);
universe@38 122 insert.executeUpdate();
universe@38 123 }
universe@38 124
universe@38 125 @Override
universe@38 126 public boolean update(Project instance) throws SQLException {
universe@75 127 if (instance.getId() < 0) return false;
universe@38 128 Objects.requireNonNull(instance.getName());
universe@38 129 update.setString(1, instance.getName());
universe@38 130 setStringOrNull(update, 2, instance.getDescription());
universe@38 131 setStringOrNull(update, 3, instance.getRepoUrl());
universe@51 132 setForeignKeyOrNull(update, 4, instance.getOwner(), User::getId);
universe@47 133 update.setInt(5, instance.getId());
universe@38 134 return update.executeUpdate() > 0;
universe@38 135 }
universe@47 136
universe@47 137 @Override
universe@47 138 public List<Project> list() throws SQLException {
universe@47 139 List<Project> projects = new ArrayList<>();
universe@47 140 try (var result = list.executeQuery()) {
universe@47 141 while (result.next()) {
universe@81 142 final var project = mapColumns(result);
universe@81 143 projects.add(project);
universe@47 144 }
universe@47 145 }
universe@47 146 return projects;
universe@47 147 }
universe@47 148
universe@47 149 @Override
universe@47 150 public Project find(int id) throws SQLException {
universe@47 151 find.setInt(1, id);
universe@47 152 try (var result = find.executeQuery()) {
universe@47 153 if (result.next()) {
universe@81 154 final var project = mapColumns(result);
universe@81 155 return project;
universe@47 156 } else {
universe@47 157 return null;
universe@47 158 }
universe@47 159 }
universe@47 160 }
universe@38 161 }

mercurial