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

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

mercurial