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

Fri, 22 May 2020 21:23:57 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 22 May 2020 21:23:57 +0200
changeset 75
33b6843fdf8a
parent 59
c759c60507a2
child 81
1a2e7b5d48f7
permissions
-rw-r--r--

adds the ability to create and edit issues

     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;
    50     public PGProjectDao(Connection connection) throws SQLException {
    51         list = connection.prepareStatement(
    52                 "select projectid, name, description, repourl, " +
    53                         "userid, username, lastname, givenname, mail " +
    54                         "from lpit_project " +
    55                         "left join lpit_user owner on lpit_project.owner = owner.userid " +
    56                         "order by name");
    58         find = connection.prepareStatement(
    59                 "select projectid, name, description, repourl, " +
    60                         "userid, username, lastname, givenname, mail " +
    61                         "from lpit_project " +
    62                         "left join lpit_user owner on lpit_project.owner = owner.userid " +
    63                         "where projectid = ?");
    65         insert = connection.prepareStatement(
    66                 "insert into lpit_project (name, description, repourl, owner) values (?, ?, ?, ?)"
    67         );
    68         update = connection.prepareStatement(
    69                 "update lpit_project set name = ?, description = ?, repourl = ?, owner = ? where projectid = ?"
    70         );
    71     }
    73     public Project mapColumns(ResultSet result) throws SQLException {
    74         final var proj = new Project(result.getInt("projectid"));
    75         proj.setName(result.getString("name"));
    76         proj.setDescription(result.getString("description"));
    77         proj.setRepoUrl(result.getString("repourl"));
    79         final int id = result.getInt("userid");
    80         if (id != 0) {
    81             final var user = new User(id);
    82             user.setUsername(result.getString("username"));
    83             user.setGivenname(result.getString("givenname"));
    84             user.setLastname(result.getString("lastname"));
    85             user.setMail(result.getString("mail"));
    86             proj.setOwner(user);
    87         }
    89         return proj;
    90     }
    92     @Override
    93     public void save(Project instance) throws SQLException {
    94         Objects.requireNonNull(instance.getName());
    95         insert.setString(1, instance.getName());
    96         setStringOrNull(insert, 2, instance.getDescription());
    97         setStringOrNull(insert, 3, instance.getRepoUrl());
    98         setForeignKeyOrNull(insert, 4, instance.getOwner(), User::getId);
    99         insert.executeUpdate();
   100     }
   102     @Override
   103     public boolean update(Project instance) throws SQLException {
   104         if (instance.getId() < 0) return false;
   105         Objects.requireNonNull(instance.getName());
   106         update.setString(1, instance.getName());
   107         setStringOrNull(update, 2, instance.getDescription());
   108         setStringOrNull(update, 3, instance.getRepoUrl());
   109         setForeignKeyOrNull(update, 4, instance.getOwner(), User::getId);
   110         update.setInt(5, instance.getId());
   111         return update.executeUpdate() > 0;
   112     }
   114     @Override
   115     public List<Project> list() throws SQLException {
   116         List<Project> projects = new ArrayList<>();
   117         try (var result = list.executeQuery()) {
   118             while (result.next()) {
   119                 projects.add(mapColumns(result));
   120             }
   121         }
   122         return projects;
   123     }
   125     @Override
   126     public Project find(int id) throws SQLException {
   127         find.setInt(1, id);
   128         try (var result = find.executeQuery()) {
   129             if (result.next()) {
   130                 return mapColumns(result);
   131             } else {
   132                 return null;
   133             }
   134         }
   135     }
   136 }

mercurial