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

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

mercurial