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

Thu, 15 Oct 2020 13:31:52 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 15 Oct 2020 13:31:52 +0200
changeset 128
947d0f6a6a83
parent 124
ed2e7aef2a3e
child 134
f47e82cd6077
permissions
-rw-r--r--

changes the way how to deal with child entities + adds component lead

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2018 Mike Becker. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */
package de.uapcore.lightpit.dao.postgres;

import de.uapcore.lightpit.dao.IssueDao;
import de.uapcore.lightpit.entities.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static de.uapcore.lightpit.dao.Functions.*;

public final class PGIssueDao implements IssueDao {

    private final PreparedStatement insert, update, list, listForVersion, find;
    private final PreparedStatement affectedVersions, resolvedVersions;
    private final PreparedStatement clearAffected, clearResolved;
    private final PreparedStatement insertAffected, insertResolved;
    private final PreparedStatement insertComment, updateComment, listComments;

    public PGIssueDao(Connection connection) throws SQLException {
        list = connection.prepareStatement(
                "select issueid, project, p.name as projectname, status, category, subject, i.description, " +
                        "userid, username, givenname, lastname, mail, " +
                        "created, updated, eta " +
                        "from lpit_issue i " +
                        "join lpit_project p on project = projectid " +
                        "left join lpit_user on userid = assignee " +
                        "where project = ? ");

        listForVersion = connection.prepareStatement(
                "with issue_version as ( "+
                        "select issueid, versionid from lpit_issue_affected_version union "+
                        "select issueid, versionid from lpit_issue_resolved_version) "+
                        "select issueid, project, p.name as projectname, status, category, subject, i.description, " +
                        "userid, username, givenname, lastname, mail, " +
                        "created, updated, eta " +
                        "from lpit_issue i " +
                        "join lpit_project p on project = projectid " +
                        "left join issue_version using (issueid) "+
                        "left join lpit_user on userid = assignee " +
                        "where coalesce(versionid,-1) = ? "
        );

        find = connection.prepareStatement(
                "select issueid, project, p.name as projectname, status, category, subject, i.description, " +
                        "userid, username, givenname, lastname, mail, " +
                        "created, updated, eta " +
                        "from lpit_issue i " +
                        "left join lpit_project p on project = projectid " +
                        "left join lpit_user on userid = assignee " +
                        "where issueid = ? ");

        insert = connection.prepareStatement(
                "insert into lpit_issue (project, status, category, subject, description, assignee, eta) " +
                        "values (?, ?::issue_status, ?::issue_category, ?, ?, ?, ?) returning issueid"
        );
        update = connection.prepareStatement(
                "update lpit_issue set updated = now(), status = ?::issue_status, category = ?::issue_category, " +
                        "subject = ?, description = ?, assignee = ?, eta = ? where issueid = ?"
        );

        affectedVersions = connection.prepareStatement(
                "select versionid, name, status, ordinal " +
                        "from lpit_version join lpit_issue_affected_version using (versionid) " +
                        "where issueid = ? " +
                        "order by ordinal, name"
        );
        clearAffected = connection.prepareStatement("delete from lpit_issue_affected_version where issueid = ?");
        insertAffected = connection.prepareStatement("insert into lpit_issue_affected_version (issueid, versionid) values (?,?)");

        resolvedVersions = connection.prepareStatement(
                "select versionid, name, status, ordinal " +
                        "from lpit_version v join lpit_issue_resolved_version using (versionid) " +
                        "where issueid = ? " +
                        "order by ordinal, name"
        );
        clearResolved = connection.prepareStatement("delete from lpit_issue_resolved_version where issueid = ?");
        insertResolved = connection.prepareStatement("insert into lpit_issue_resolved_version (issueid, versionid) values (?,?)");

        insertComment = connection.prepareStatement(
                "insert into lpit_issue_comment (issueid, comment, userid) values (?, ? ,?)"
        );
        updateComment = connection.prepareStatement(
                "update lpit_issue_comment set comment = ?, updated = now(), updatecount = updatecount+1 where commentid = ?"
        );
        listComments = connection.prepareStatement(
                "select * from lpit_issue_comment left join lpit_user using (userid) where issueid = ? order by created"
        );
    }

    private Issue mapColumns(ResultSet result) throws SQLException {
        final var project = new Project(result.getInt("project"));
        project.setName(result.getString("projectname"));
        final var issue = new Issue(result.getInt("issueid"));
        issue.setProject(project);
        issue.setStatus(IssueStatus.valueOf(result.getString("status")));
        issue.setCategory(IssueCategory.valueOf(result.getString("category")));
        issue.setSubject(result.getString("subject"));
        issue.setDescription(result.getString("description"));
        issue.setAssignee(PGUserDao.mapColumns(result));
        issue.setCreated(result.getTimestamp("created"));
        issue.setUpdated(result.getTimestamp("updated"));
        issue.setEta(result.getDate("eta"));
        return issue;
    }

    private Version mapVersion(ResultSet result) throws SQLException {
        final var version = new Version(result.getInt("versionid"));
        version.setName(result.getString("name"));
        version.setOrdinal(result.getInt("ordinal"));
        version.setStatus(VersionStatus.valueOf(result.getString("status")));
        return version;
    }

    private void updateVersionLists(Issue instance) throws SQLException {
        clearAffected.setInt(1, instance.getId());
        clearResolved.setInt(1, instance.getId());
        insertAffected.setInt(1, instance.getId());
        insertResolved.setInt(1, instance.getId());
        clearAffected.executeUpdate();
        clearResolved.executeUpdate();
        for (Version v : instance.getAffectedVersions()) {
            insertAffected.setInt(2, v.getId());
            insertAffected.executeUpdate();
        }
        for (Version v : instance.getResolvedVersions()) {
            insertResolved.setInt(2, v.getId());
            insertResolved.executeUpdate();
        }
    }

    @Override
    public void save(Issue instance, Project project) throws SQLException {
        Objects.requireNonNull(instance.getSubject());
        instance.setProject(project);
        insert.setInt(1, instance.getProject().getId());
        insert.setString(2, instance.getStatus().name());
        insert.setString(3, instance.getCategory().name());
        insert.setString(4, instance.getSubject());
        setStringOrNull(insert, 5, instance.getDescription());
        setForeignKeyOrNull(insert, 6, instance.getAssignee(), User::getId);
        setDateOrNull(insert, 7, instance.getEta());
        // insert and retrieve the ID
        final var rs = insert.executeQuery();
        rs.next();
        instance.setId(rs.getInt(1));
        updateVersionLists(instance);
    }

    @Override
    public boolean update(Issue instance) throws SQLException {
        if (instance.getId() < 0) return false;
        Objects.requireNonNull(instance.getSubject());
        update.setString(1, instance.getStatus().name());
        update.setString(2, instance.getCategory().name());
        update.setString(3, instance.getSubject());
        setStringOrNull(update, 4, instance.getDescription());
        setForeignKeyOrNull(update, 5, instance.getAssignee(), User::getId);
        setDateOrNull(update, 6, instance.getEta());
        update.setInt(7, instance.getId());
        boolean success = update.executeUpdate() > 0;
        if (success) {
            updateVersionLists(instance);
            return true;
        } else {
            return false;
        }
    }

    private List<Issue> list(PreparedStatement query, int arg) throws SQLException {
        query.setInt(1, arg);
        List<Issue> issues = new ArrayList<>();
        try (var result = query.executeQuery()) {
            while (result.next()) {
                issues.add(mapColumns(result));
            }
        }
        return issues;
    }

    @Override
    public List<Issue> list(Project project) throws SQLException {
        return list(list, project.getId());
    }

    @Override
    public List<Issue> list(Version version) throws SQLException {
        return list(listForVersion, version == null ? -1 : version.getId());
    }

    @Override
    public Issue find(int id) throws SQLException {
        find.setInt(1, id);
        try (var result = find.executeQuery()) {
            if (result.next()) {
                return mapColumns(result);
            } else {
                return null;
            }
        }
    }

    private List<Version> listVersions(PreparedStatement stmt, Issue issue) throws SQLException {
        stmt.setInt(1, issue.getId());
        List<Version> versions = new ArrayList<>();
        try (var result = stmt.executeQuery()) {
            while (result.next()) {
                versions.add(mapVersion(result));
            }
        }
        return versions;
    }

    @Override
    public void joinVersionInformation(Issue issue) throws SQLException {
        Objects.requireNonNull(issue.getProject());
        issue.setAffectedVersions(listVersions(affectedVersions, issue));
        issue.setResolvedVersions(listVersions(resolvedVersions, issue));
    }

    @Override
    public List<IssueComment> listComments(Issue issue) throws SQLException {
        listComments.setInt(1, issue.getId());
        List<IssueComment> comments = new ArrayList<>();
        try (var result = listComments.executeQuery()) {
            while (result.next()) {
                final var comment = new IssueComment(result.getInt("commentid"), issue);
                comment.setCreated(result.getTimestamp("created"));
                comment.setUpdated(result.getTimestamp("updated"));
                comment.setUpdateCount(result.getInt("updatecount"));
                comment.setComment(result.getString("comment"));
                comment.setAuthor(PGUserDao.mapColumns(result));
                comments.add(comment);
            }
        }
        return comments;
    }

    @Override
    public void saveComment(IssueComment comment) throws SQLException {
        Objects.requireNonNull(comment.getComment());
        Objects.requireNonNull(comment.getIssue());
        if (comment.getId() >= 0) {
            updateComment.setString(1, comment.getComment());
            updateComment.setInt(2, comment.getId());
            updateComment.execute();
        } else {
            insertComment.setInt(1, comment.getIssue().getId());
            insertComment.setString(2, comment.getComment());
            setForeignKeyOrNull(insertComment, 3, comment.getAuthor(), User::getId);
            insertComment.execute();
        }
    }
}

mercurial