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

changeset 47
57cfb94ab99f
parent 38
cf85ef18f231
child 51
dd0a45ae25d7
     1.1 --- a/src/main/java/de/uapcore/lightpit/dao/postgres/PGProjectDao.java	Wed May 13 21:46:26 2020 +0200
     1.2 +++ b/src/main/java/de/uapcore/lightpit/dao/postgres/PGProjectDao.java	Thu May 14 22:48:01 2020 +0200
     1.3 @@ -28,7 +28,7 @@
     1.4   */
     1.5  package de.uapcore.lightpit.dao.postgres;
     1.6  
     1.7 -import de.uapcore.lightpit.dao.AbstractDao;
     1.8 +import de.uapcore.lightpit.dao.GenericDao;
     1.9  import de.uapcore.lightpit.dao.ProjectDao;
    1.10  import de.uapcore.lightpit.entities.Project;
    1.11  import de.uapcore.lightpit.entities.User;
    1.12 @@ -37,18 +37,31 @@
    1.13  import java.sql.PreparedStatement;
    1.14  import java.sql.ResultSet;
    1.15  import java.sql.SQLException;
    1.16 +import java.util.ArrayList;
    1.17 +import java.util.List;
    1.18  import java.util.Objects;
    1.19  
    1.20 -public final class PGProjectDao extends AbstractDao<Project> implements ProjectDao {
    1.21 +import static de.uapcore.lightpit.dao.Functions.setForeignKeyOrNull;
    1.22 +import static de.uapcore.lightpit.dao.Functions.setStringOrNull;
    1.23  
    1.24 -    private final PGUserDao userDao;
    1.25 +public final class PGProjectDao implements ProjectDao, GenericDao<Project> {
    1.26  
    1.27 -    private final PreparedStatement insert;
    1.28 -    private final PreparedStatement update;
    1.29 +    private final PreparedStatement insert, update, list, find;
    1.30  
    1.31 -    public PGProjectDao(Connection connection, PGUserDao userDao) throws SQLException {
    1.32 -        super(connection.prepareStatement(
    1.33 -                "select * from lpit_project join lpit_user owner on lpit_project.owner = owner.userid"));
    1.34 +    public PGProjectDao(Connection connection) throws SQLException {
    1.35 +        list = connection.prepareStatement(
    1.36 +                "select id, name, description, repourl, " +
    1.37 +                        "userid, username, lastname, givenname, mail " +
    1.38 +                        "from lpit_project " +
    1.39 +                        "left join lpit_user owner on lpit_project.owner = owner.userid " +
    1.40 +                        "order by name");
    1.41 +
    1.42 +        find = connection.prepareStatement(
    1.43 +                "select id, name, description, repourl, " +
    1.44 +                        "userid, username, lastname, givenname, mail " +
    1.45 +                        "from lpit_project " +
    1.46 +                        "left join lpit_user owner on lpit_project.owner = owner.userid " +
    1.47 +                        "where id = ?");
    1.48  
    1.49          insert = connection.prepareStatement(
    1.50                  "insert into lpit_project (name, description, repourl, owner) values (?, ?, ?, ?)"
    1.51 @@ -56,17 +69,24 @@
    1.52          update = connection.prepareStatement(
    1.53                  "update lpit_project set name = ?, description = ?, repourl = ?, owner = ? where id = ?"
    1.54          );
    1.55 -
    1.56 -        this.userDao = userDao;
    1.57      }
    1.58  
    1.59 -    @Override
    1.60 -    public Project mapColumns(ResultSet result, String q) throws SQLException {
    1.61 -        final var proj = new Project(result.getInt(qual(q, "id")));
    1.62 -        proj.setName(result.getString(qual(q, "name")));
    1.63 -        proj.setDescription(result.getString(qual(q, "description")));
    1.64 -        proj.setRepoUrl(result.getString(qual(q, "repourl")));
    1.65 -        proj.setOwner(userDao.mapColumns(result, "owner"));
    1.66 +    public Project mapColumns(ResultSet result) throws SQLException {
    1.67 +        final var proj = new Project(result.getInt("id"));
    1.68 +        proj.setName(result.getString("name"));
    1.69 +        proj.setDescription(result.getString("description"));
    1.70 +        proj.setRepoUrl(result.getString("repourl"));
    1.71 +
    1.72 +        final int id = result.getInt("userid");
    1.73 +        if (id != 0) {
    1.74 +            final var user = new User(id);
    1.75 +            user.setUsername(result.getString("username"));
    1.76 +            user.setGivenname(result.getString("givenname"));
    1.77 +            user.setLastname(result.getString("lastname"));
    1.78 +            user.setMail(result.getString("mail"));
    1.79 +            proj.setOwner(user);
    1.80 +        }
    1.81 +
    1.82          return proj;
    1.83      }
    1.84  
    1.85 @@ -87,6 +107,30 @@
    1.86          setStringOrNull(update, 2, instance.getDescription());
    1.87          setStringOrNull(update, 3, instance.getRepoUrl());
    1.88          setForeignKeyOrNull(update, 4, instance.getOwner(), User::getUserID);
    1.89 +        update.setInt(5, instance.getId());
    1.90          return update.executeUpdate() > 0;
    1.91      }
    1.92 +
    1.93 +    @Override
    1.94 +    public List<Project> list() throws SQLException {
    1.95 +        List<Project> projects = new ArrayList<>();
    1.96 +        try (var result = list.executeQuery()) {
    1.97 +            while (result.next()) {
    1.98 +                projects.add(mapColumns(result));
    1.99 +            }
   1.100 +        }
   1.101 +        return projects;
   1.102 +    }
   1.103 +
   1.104 +    @Override
   1.105 +    public Project find(int id) throws SQLException {
   1.106 +        find.setInt(1, id);
   1.107 +        try (var result = find.executeQuery()) {
   1.108 +            if (result.next()) {
   1.109 +                return mapColumns(result);
   1.110 +            } else {
   1.111 +                return null;
   1.112 +            }
   1.113 +        }
   1.114 +    }
   1.115  }

mercurial