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

Sun, 17 May 2020 16:23:39 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 17 May 2020 16:23:39 +0200
changeset 59
c759c60507a2
parent 51
dd0a45ae25d7
child 75
33b6843fdf8a
permissions
-rw-r--r--

adds version management

     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 id, 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 id, 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 id = ?");
    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 id = ?"
    70         );
    71     }
    73     public Project mapColumns(ResultSet result) throws SQLException {
    74         final var proj = new Project(result.getInt("id"));
    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         Objects.requireNonNull(instance.getName());
   105         update.setString(1, instance.getName());
   106         setStringOrNull(update, 2, instance.getDescription());
   107         setStringOrNull(update, 3, instance.getRepoUrl());
   108         setForeignKeyOrNull(update, 4, instance.getOwner(), User::getId);
   109         update.setInt(5, instance.getId());
   110         return update.executeUpdate() > 0;
   111     }
   113     @Override
   114     public List<Project> list() throws SQLException {
   115         List<Project> projects = new ArrayList<>();
   116         try (var result = list.executeQuery()) {
   117             while (result.next()) {
   118                 projects.add(mapColumns(result));
   119             }
   120         }
   121         return projects;
   122     }
   124     @Override
   125     public Project find(int id) throws SQLException {
   126         find.setInt(1, id);
   127         try (var result = find.executeQuery()) {
   128             if (result.next()) {
   129                 return mapColumns(result);
   130             } else {
   131                 return null;
   132             }
   133         }
   134     }
   135 }

mercurial