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

Mon, 11 May 2020 19:09:06 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 11 May 2020 19:09:06 +0200
changeset 38
cf85ef18f231
child 47
57cfb94ab99f
permissions
-rw-r--r--

adds DAO for Project entity and save/update methods

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@38 31 import de.uapcore.lightpit.dao.AbstractDao;
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@38 40 import java.util.Objects;
universe@38 41
universe@38 42 public final class PGProjectDao extends AbstractDao<Project> implements ProjectDao {
universe@38 43
universe@38 44 private final PGUserDao userDao;
universe@38 45
universe@38 46 private final PreparedStatement insert;
universe@38 47 private final PreparedStatement update;
universe@38 48
universe@38 49 public PGProjectDao(Connection connection, PGUserDao userDao) throws SQLException {
universe@38 50 super(connection.prepareStatement(
universe@38 51 "select * from lpit_project join lpit_user owner on lpit_project.owner = owner.userid"));
universe@38 52
universe@38 53 insert = connection.prepareStatement(
universe@38 54 "insert into lpit_project (name, description, repourl, owner) values (?, ?, ?, ?)"
universe@38 55 );
universe@38 56 update = connection.prepareStatement(
universe@38 57 "update lpit_project set name = ?, description = ?, repourl = ?, owner = ? where id = ?"
universe@38 58 );
universe@38 59
universe@38 60 this.userDao = userDao;
universe@38 61 }
universe@38 62
universe@38 63 @Override
universe@38 64 public Project mapColumns(ResultSet result, String q) throws SQLException {
universe@38 65 final var proj = new Project(result.getInt(qual(q, "id")));
universe@38 66 proj.setName(result.getString(qual(q, "name")));
universe@38 67 proj.setDescription(result.getString(qual(q, "description")));
universe@38 68 proj.setRepoUrl(result.getString(qual(q, "repourl")));
universe@38 69 proj.setOwner(userDao.mapColumns(result, "owner"));
universe@38 70 return proj;
universe@38 71 }
universe@38 72
universe@38 73 @Override
universe@38 74 public void save(Project instance) throws SQLException {
universe@38 75 Objects.requireNonNull(instance.getName());
universe@38 76 insert.setString(1, instance.getName());
universe@38 77 setStringOrNull(insert, 2, instance.getDescription());
universe@38 78 setStringOrNull(insert, 3, instance.getRepoUrl());
universe@38 79 setForeignKeyOrNull(insert, 4, instance.getOwner(), User::getUserID);
universe@38 80 insert.executeUpdate();
universe@38 81 }
universe@38 82
universe@38 83 @Override
universe@38 84 public boolean update(Project instance) throws SQLException {
universe@38 85 Objects.requireNonNull(instance.getName());
universe@38 86 update.setString(1, instance.getName());
universe@38 87 setStringOrNull(update, 2, instance.getDescription());
universe@38 88 setStringOrNull(update, 3, instance.getRepoUrl());
universe@38 89 setForeignKeyOrNull(update, 4, instance.getOwner(), User::getUserID);
universe@38 90 return update.executeUpdate() > 0;
universe@38 91 }
universe@38 92 }

mercurial