1.1 --- a/src/main/java/de/uapcore/lightpit/dao/postgres/PGProjectDao.java Fri Nov 06 10:50:32 2020 +0100 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,151 +0,0 @@ 1.4 -/* 1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 - * 1.7 - * Copyright 2018 Mike Becker. All rights reserved. 1.8 - * 1.9 - * Redistribution and use in source and binary forms, with or without 1.10 - * modification, are permitted provided that the following conditions are met: 1.11 - * 1.12 - * 1. Redistributions of source code must retain the above copyright 1.13 - * notice, this list of conditions and the following disclaimer. 1.14 - * 1.15 - * 2. Redistributions in binary form must reproduce the above copyright 1.16 - * notice, this list of conditions and the following disclaimer in the 1.17 - * documentation and/or other materials provided with the distribution. 1.18 - * 1.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 1.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.29 - * POSSIBILITY OF SUCH DAMAGE. 1.30 - * 1.31 - */ 1.32 -package de.uapcore.lightpit.dao.postgres; 1.33 - 1.34 -import de.uapcore.lightpit.dao.Functions; 1.35 -import de.uapcore.lightpit.dao.ProjectDao; 1.36 -import de.uapcore.lightpit.entities.IssueSummary; 1.37 -import de.uapcore.lightpit.entities.Project; 1.38 -import de.uapcore.lightpit.entities.User; 1.39 - 1.40 -import java.sql.Connection; 1.41 -import java.sql.PreparedStatement; 1.42 -import java.sql.ResultSet; 1.43 -import java.sql.SQLException; 1.44 -import java.util.List; 1.45 - 1.46 -import static de.uapcore.lightpit.dao.Functions.setForeignKeyOrNull; 1.47 -import static de.uapcore.lightpit.dao.Functions.setStringOrNull; 1.48 - 1.49 -public final class PGProjectDao implements ProjectDao { 1.50 - 1.51 - private final PreparedStatement insert, update, list, find, findByNode; 1.52 - private final PreparedStatement issue_summary; 1.53 - 1.54 - public PGProjectDao(Connection connection) throws SQLException { 1.55 - final var query = "select projectid, name, node, description, repourl, " + 1.56 - "userid, username, lastname, givenname, mail " + 1.57 - "from lpit_project " + 1.58 - "left join lpit_user owner on lpit_project.owner = owner.userid "; 1.59 - 1.60 - list = connection.prepareStatement(query + " order by name"); 1.61 - 1.62 - find = connection.prepareStatement(query + " where projectid = ?"); 1.63 - findByNode = connection.prepareStatement(query + " where node = ?"); 1.64 - 1.65 - issue_summary = connection.prepareStatement( 1.66 - "select phase, count(*) as total "+ 1.67 - "from lpit_issue " + 1.68 - "join lpit_issue_phases using(status) " + 1.69 - "where project = ? "+ 1.70 - "group by phase " 1.71 - ); 1.72 - 1.73 - insert = connection.prepareStatement( 1.74 - "insert into lpit_project (name, node, description, repourl, owner) values (?, ?, ?, ?, ?)" 1.75 - ); 1.76 - update = connection.prepareStatement( 1.77 - "update lpit_project set name = ?, node = ?, description = ?, repourl = ?, owner = ? where projectid = ?" 1.78 - ); 1.79 - } 1.80 - 1.81 - private static Project mapColumns(ResultSet result) throws SQLException { 1.82 - final var proj = new Project(result.getInt("projectid")); 1.83 - proj.setName(result.getString("name")); 1.84 - proj.setNode(result.getString("node")); 1.85 - proj.setDescription(result.getString("description")); 1.86 - proj.setRepoUrl(result.getString("repourl")); 1.87 - proj.setOwner(PGUserDao.mapColumns(result)); 1.88 - 1.89 - return proj; 1.90 - } 1.91 - 1.92 - public IssueSummary getIssueSummary(Project project) throws SQLException { 1.93 - issue_summary.setInt(1, project.getId()); 1.94 - final var result = issue_summary.executeQuery(); 1.95 - final var summary = new IssueSummary(); 1.96 - while (result.next()) { 1.97 - final var phase = result.getInt("phase"); 1.98 - final var total = result.getInt("total"); 1.99 - switch(phase) { 1.100 - case 0: 1.101 - summary.setOpen(total); 1.102 - break; 1.103 - case 1: 1.104 - summary.setActive(total); 1.105 - break; 1.106 - case 2: 1.107 - summary.setDone(total); 1.108 - break; 1.109 - } 1.110 - } 1.111 - return summary; 1.112 - } 1.113 - 1.114 - private static int setColumns(PreparedStatement stmt, Project instance) throws SQLException { 1.115 - int column = 0; 1.116 - stmt.setString(++column, instance.getName()); 1.117 - stmt.setString(++column, instance.getNode()); 1.118 - setStringOrNull(stmt, ++column, instance.getDescription()); 1.119 - setStringOrNull(stmt, ++column, instance.getRepoUrl()); 1.120 - setForeignKeyOrNull(stmt, ++column, instance.getOwner(), User::getId); 1.121 - return column; 1.122 - } 1.123 - 1.124 - @Override 1.125 - public void save(Project instance) throws SQLException { 1.126 - setColumns(insert, instance); 1.127 - insert.executeUpdate(); 1.128 - } 1.129 - 1.130 - @Override 1.131 - public boolean update(Project instance) throws SQLException { 1.132 - if (instance.getId() < 0) return false; 1.133 - int column = setColumns(update, instance); 1.134 - update.setInt(++column, instance.getId()); 1.135 - return update.executeUpdate() > 0; 1.136 - } 1.137 - 1.138 - @Override 1.139 - public List<Project> list() throws SQLException { 1.140 - return Functions.list(list, PGProjectDao::mapColumns); 1.141 - } 1.142 - 1.143 - @Override 1.144 - public Project find(int id) throws SQLException { 1.145 - find.setInt(1, id); 1.146 - return Functions.find(find, PGProjectDao::mapColumns); 1.147 - } 1.148 - 1.149 - @Override 1.150 - public Project findByNode(String node) throws SQLException { 1.151 - findByNode.setString(1, node); 1.152 - return Functions.find(findByNode, PGProjectDao::mapColumns); 1.153 - } 1.154 -}