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

Thu, 14 May 2020 22:48:01 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 14 May 2020 22:48:01 +0200
changeset 47
57cfb94ab99f
parent 38
cf85ef18f231
child 51
dd0a45ae25d7
permissions
-rw-r--r--

projects can now be added and updated

     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.GenericDao;
    32 import de.uapcore.lightpit.dao.ProjectDao;
    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, GenericDao<Project> {
    49     private final PreparedStatement insert, update, list, find;
    51     public PGProjectDao(Connection connection) throws SQLException {
    52         list = connection.prepareStatement(
    53                 "select id, 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 id, 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 id = ?");
    66         insert = connection.prepareStatement(
    67                 "insert into lpit_project (name, description, repourl, owner) values (?, ?, ?, ?)"
    68         );
    69         update = connection.prepareStatement(
    70                 "update lpit_project set name = ?, description = ?, repourl = ?, owner = ? where id = ?"
    71         );
    72     }
    74     public Project mapColumns(ResultSet result) throws SQLException {
    75         final var proj = new Project(result.getInt("id"));
    76         proj.setName(result.getString("name"));
    77         proj.setDescription(result.getString("description"));
    78         proj.setRepoUrl(result.getString("repourl"));
    80         final int id = result.getInt("userid");
    81         if (id != 0) {
    82             final var user = new User(id);
    83             user.setUsername(result.getString("username"));
    84             user.setGivenname(result.getString("givenname"));
    85             user.setLastname(result.getString("lastname"));
    86             user.setMail(result.getString("mail"));
    87             proj.setOwner(user);
    88         }
    90         return proj;
    91     }
    93     @Override
    94     public void save(Project instance) throws SQLException {
    95         Objects.requireNonNull(instance.getName());
    96         insert.setString(1, instance.getName());
    97         setStringOrNull(insert, 2, instance.getDescription());
    98         setStringOrNull(insert, 3, instance.getRepoUrl());
    99         setForeignKeyOrNull(insert, 4, instance.getOwner(), User::getUserID);
   100         insert.executeUpdate();
   101     }
   103     @Override
   104     public boolean update(Project instance) throws SQLException {
   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::getUserID);
   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