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

universe@38 1 /*
universe@38 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@38 3 *
universe@38 4 * Copyright 2018 Mike Becker. All rights reserved.
universe@38 5 *
universe@38 6 * Redistribution and use in source and binary forms, with or without
universe@38 7 * modification, are permitted provided that the following conditions are met:
universe@38 8 *
universe@38 9 * 1. Redistributions of source code must retain the above copyright
universe@38 10 * notice, this list of conditions and the following disclaimer.
universe@38 11 *
universe@38 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@38 13 * notice, this list of conditions and the following disclaimer in the
universe@38 14 * documentation and/or other materials provided with the distribution.
universe@38 15 *
universe@38 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@38 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@38 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@38 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@38 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@38 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@38 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@38 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@38 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@38 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@38 26 * POSSIBILITY OF SUCH DAMAGE.
universe@38 27 *
universe@38 28 */
universe@38 29 package de.uapcore.lightpit.dao.postgres;
universe@38 30
universe@47 31 import de.uapcore.lightpit.dao.GenericDao;
universe@38 32 import de.uapcore.lightpit.dao.ProjectDao;
universe@38 33 import de.uapcore.lightpit.entities.Project;
universe@38 34 import de.uapcore.lightpit.entities.User;
universe@38 35
universe@38 36 import java.sql.Connection;
universe@38 37 import java.sql.PreparedStatement;
universe@38 38 import java.sql.ResultSet;
universe@38 39 import java.sql.SQLException;
universe@47 40 import java.util.ArrayList;
universe@47 41 import java.util.List;
universe@38 42 import java.util.Objects;
universe@38 43
universe@47 44 import static de.uapcore.lightpit.dao.Functions.setForeignKeyOrNull;
universe@47 45 import static de.uapcore.lightpit.dao.Functions.setStringOrNull;
universe@38 46
universe@47 47 public final class PGProjectDao implements ProjectDao, GenericDao<Project> {
universe@38 48
universe@47 49 private final PreparedStatement insert, update, list, find;
universe@38 50
universe@47 51 public PGProjectDao(Connection connection) throws SQLException {
universe@47 52 list = connection.prepareStatement(
universe@47 53 "select id, name, description, repourl, " +
universe@47 54 "userid, username, lastname, givenname, mail " +
universe@47 55 "from lpit_project " +
universe@47 56 "left join lpit_user owner on lpit_project.owner = owner.userid " +
universe@47 57 "order by name");
universe@47 58
universe@47 59 find = connection.prepareStatement(
universe@47 60 "select id, name, description, repourl, " +
universe@47 61 "userid, username, lastname, givenname, mail " +
universe@47 62 "from lpit_project " +
universe@47 63 "left join lpit_user owner on lpit_project.owner = owner.userid " +
universe@47 64 "where id = ?");
universe@38 65
universe@38 66 insert = connection.prepareStatement(
universe@38 67 "insert into lpit_project (name, description, repourl, owner) values (?, ?, ?, ?)"
universe@38 68 );
universe@38 69 update = connection.prepareStatement(
universe@38 70 "update lpit_project set name = ?, description = ?, repourl = ?, owner = ? where id = ?"
universe@38 71 );
universe@38 72 }
universe@38 73
universe@47 74 public Project mapColumns(ResultSet result) throws SQLException {
universe@47 75 final var proj = new Project(result.getInt("id"));
universe@47 76 proj.setName(result.getString("name"));
universe@47 77 proj.setDescription(result.getString("description"));
universe@47 78 proj.setRepoUrl(result.getString("repourl"));
universe@47 79
universe@47 80 final int id = result.getInt("userid");
universe@47 81 if (id != 0) {
universe@47 82 final var user = new User(id);
universe@47 83 user.setUsername(result.getString("username"));
universe@47 84 user.setGivenname(result.getString("givenname"));
universe@47 85 user.setLastname(result.getString("lastname"));
universe@47 86 user.setMail(result.getString("mail"));
universe@47 87 proj.setOwner(user);
universe@47 88 }
universe@47 89
universe@38 90 return proj;
universe@38 91 }
universe@38 92
universe@38 93 @Override
universe@38 94 public void save(Project instance) throws SQLException {
universe@38 95 Objects.requireNonNull(instance.getName());
universe@38 96 insert.setString(1, instance.getName());
universe@38 97 setStringOrNull(insert, 2, instance.getDescription());
universe@38 98 setStringOrNull(insert, 3, instance.getRepoUrl());
universe@38 99 setForeignKeyOrNull(insert, 4, instance.getOwner(), User::getUserID);
universe@38 100 insert.executeUpdate();
universe@38 101 }
universe@38 102
universe@38 103 @Override
universe@38 104 public boolean update(Project instance) throws SQLException {
universe@38 105 Objects.requireNonNull(instance.getName());
universe@38 106 update.setString(1, instance.getName());
universe@38 107 setStringOrNull(update, 2, instance.getDescription());
universe@38 108 setStringOrNull(update, 3, instance.getRepoUrl());
universe@38 109 setForeignKeyOrNull(update, 4, instance.getOwner(), User::getUserID);
universe@47 110 update.setInt(5, instance.getId());
universe@38 111 return update.executeUpdate() > 0;
universe@38 112 }
universe@47 113
universe@47 114 @Override
universe@47 115 public List<Project> list() throws SQLException {
universe@47 116 List<Project> projects = new ArrayList<>();
universe@47 117 try (var result = list.executeQuery()) {
universe@47 118 while (result.next()) {
universe@47 119 projects.add(mapColumns(result));
universe@47 120 }
universe@47 121 }
universe@47 122 return projects;
universe@47 123 }
universe@47 124
universe@47 125 @Override
universe@47 126 public Project find(int id) throws SQLException {
universe@47 127 find.setInt(1, id);
universe@47 128 try (var result = find.executeQuery()) {
universe@47 129 if (result.next()) {
universe@47 130 return mapColumns(result);
universe@47 131 } else {
universe@47 132 return null;
universe@47 133 }
universe@47 134 }
universe@47 135 }
universe@38 136 }

mercurial