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

Sat, 30 May 2020 15:26:15 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 30 May 2020 15:26:15 +0200
changeset 81
1a2e7b5d48f7
parent 75
33b6843fdf8a
child 86
0a658e53177c
permissions
-rw-r--r--

adds issue summaries

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2018 Mike Becker. All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  *
    28  */
    29 package de.uapcore.lightpit.dao.postgres;
    31 import de.uapcore.lightpit.dao.ProjectDao;
    32 import de.uapcore.lightpit.entities.Project;
    33 import de.uapcore.lightpit.entities.User;
    35 import java.sql.Connection;
    36 import java.sql.PreparedStatement;
    37 import java.sql.ResultSet;
    38 import java.sql.SQLException;
    39 import java.util.ArrayList;
    40 import java.util.List;
    41 import java.util.Objects;
    43 import static de.uapcore.lightpit.dao.Functions.setForeignKeyOrNull;
    44 import static de.uapcore.lightpit.dao.Functions.setStringOrNull;
    46 public final class PGProjectDao implements ProjectDao {
    48     private final PreparedStatement insert, update, list, find;
    49     private final PreparedStatement issue_summary;
    51     public PGProjectDao(Connection connection) throws SQLException {
    52         list = connection.prepareStatement(
    53                 "select projectid, name, description, repourl, " +
    54                         "userid, username, lastname, givenname, mail " +
    55                         "from lpit_project " +
    56                         "left join lpit_user owner on lpit_project.owner = owner.userid " +
    57                         "order by name");
    59         find = connection.prepareStatement(
    60                 "select projectid, name, description, repourl, " +
    61                         "userid, username, lastname, givenname, mail " +
    62                         "from lpit_project " +
    63                         "left join lpit_user owner on lpit_project.owner = owner.userid " +
    64                         "where projectid = ?");
    66         issue_summary = connection.prepareStatement(
    67                 "select phase, count(*) as total "+
    68                         "from lpit_issue " +
    69                         "join lpit_issue_phases using(status) " +
    70                         "where project = ? "+
    71                         "group by phase "
    72         );
    74         insert = connection.prepareStatement(
    75                 "insert into lpit_project (name, description, repourl, owner) values (?, ?, ?, ?)"
    76         );
    77         update = connection.prepareStatement(
    78                 "update lpit_project set name = ?, description = ?, repourl = ?, owner = ? where projectid = ?"
    79         );
    80     }
    82     public Project mapColumns(ResultSet result) throws SQLException {
    83         final var proj = new Project(result.getInt("projectid"));
    84         proj.setName(result.getString("name"));
    85         proj.setDescription(result.getString("description"));
    86         proj.setRepoUrl(result.getString("repourl"));
    88         final int id = result.getInt("userid");
    89         if (id != 0) {
    90             final var user = new User(id);
    91             user.setUsername(result.getString("username"));
    92             user.setGivenname(result.getString("givenname"));
    93             user.setLastname(result.getString("lastname"));
    94             user.setMail(result.getString("mail"));
    95             proj.setOwner(user);
    96         }
    98         return proj;
    99     }
   101     private void mapIssueSummary(Project proj) throws SQLException {
   102         issue_summary.setInt(1, proj.getId());
   103         final var result = issue_summary.executeQuery();
   104         while (result.next()) {
   105             final var phase = result.getInt("phase");
   106             final var total = result.getInt("total");
   107             switch(phase) {
   108                 case 0:
   109                     proj.setOpenIssues(total);
   110                     break;
   111                 case 1:
   112                     proj.setActiveIssues(total);
   113                     break;
   114                 case 2:
   115                     proj.setDoneIssues(total);
   116                     break;
   117             }
   118         }
   119     }
   121     @Override
   122     public void save(Project instance) throws SQLException {
   123         Objects.requireNonNull(instance.getName());
   124         insert.setString(1, instance.getName());
   125         setStringOrNull(insert, 2, instance.getDescription());
   126         setStringOrNull(insert, 3, instance.getRepoUrl());
   127         setForeignKeyOrNull(insert, 4, instance.getOwner(), User::getId);
   128         insert.executeUpdate();
   129     }
   131     @Override
   132     public boolean update(Project instance) throws SQLException {
   133         if (instance.getId() < 0) return false;
   134         Objects.requireNonNull(instance.getName());
   135         update.setString(1, instance.getName());
   136         setStringOrNull(update, 2, instance.getDescription());
   137         setStringOrNull(update, 3, instance.getRepoUrl());
   138         setForeignKeyOrNull(update, 4, instance.getOwner(), User::getId);
   139         update.setInt(5, instance.getId());
   140         return update.executeUpdate() > 0;
   141     }
   143     @Override
   144     public List<Project> list() throws SQLException {
   145         List<Project> projects = new ArrayList<>();
   146         try (var result = list.executeQuery()) {
   147             while (result.next()) {
   148                 final var project = mapColumns(result);
   149                 mapIssueSummary(project);
   150                 projects.add(project);
   151             }
   152         }
   153         return projects;
   154     }
   156     @Override
   157     public Project find(int id) throws SQLException {
   158         find.setInt(1, id);
   159         try (var result = find.executeQuery()) {
   160             if (result.next()) {
   161                 final var project = mapColumns(result);
   162                 mapIssueSummary(project);
   163                 return project;
   164             } else {
   165                 return null;
   166             }
   167         }
   168     }
   169 }

mercurial