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

changeset 62
833e0385572a
parent 59
c759c60507a2
child 70
821c4950b619
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/uapcore/lightpit/dao/postgres/PGIssueDao.java	Mon May 18 21:05:57 2020 +0200
@@ -0,0 +1,155 @@
+/*
+ * 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, find;
+
+    public PGIssueDao(Connection connection) throws SQLException {
+        list = connection.prepareStatement(
+                "select id, project, status, category, subject, description, " +
+                        "vplan.id, vplan.name, vdone.id, vdone.name, " +
+                        "created, updated, eta " +
+                        "from lpit_issue " +
+                        "left join lpit_version vplan on vplan.id = version_plan " +
+                        "left join lpit_version vdone on vdone.id = version_done " +
+                        "where project = ? ");
+
+        find = connection.prepareStatement(
+                "select id, project, status, category, subject, description, " +
+                        "vplan.id, vplan.name, vdone.id, vdone.name, " +
+                        "created, updated, eta " +
+                        "from lpit_issue " +
+                        "left join lpit_version vplan on vplan.id = version_plan " +
+                        "left join lpit_version vdone on vdone.id = version_done " +
+                        "where id = ? ");
+
+        insert = connection.prepareStatement(
+                "insert into lpit_issue (project, status, category, subject, description, version_plan, version_done, eta) " +
+                        "values (?, ?::issue_status, ?::issue_category, ?, ?, ?, ?, ?)"
+        );
+        update = connection.prepareStatement(
+                "update lpit_issue set updated = now(), status = ?::issue_status, category = ?::issue_category, " +
+                        "subject = ?, description = ?, version_plan = ?, version_done = ?, eta = ? where id = ?"
+        );
+    }
+
+    private Version obtainVersion(ResultSet result, Project project, String prefix) throws SQLException {
+        final int vplan = result.getInt(prefix+"id");
+        if (vplan > 0) {
+            final var ver = new Version(vplan, project);
+            ver.setName(result.getString(prefix+"name"));
+            return ver;
+        } else {
+            return null;
+        }
+    }
+
+    public Issue mapColumns(ResultSet result) throws SQLException {
+        final var project = new Project(result.getInt("project"));
+        final var issue = new Issue(result.getInt("id"), 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.setScheduledVersion(obtainVersion(result, project, "vplan."));
+        issue.setResolvedVersion(obtainVersion(result, project, "vdone."));
+        issue.setCreated(result.getTimestamp("created"));
+        issue.setUpdated(result.getTimestamp("updated"));
+        issue.setEta(result.getDate("eta"));
+        return issue;
+    }
+
+    @Override
+    public void save(Issue instance) throws SQLException {
+        Objects.requireNonNull(instance.getSubject());
+        Objects.requireNonNull(instance.getProject());
+        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.getScheduledVersion(), Version::getId);
+        setForeignKeyOrNull(insert, 7, instance.getResolvedVersion(), Version::getId);
+        setDateOrNull(insert, 8, instance.getEta());
+        insert.executeUpdate();
+    }
+
+    @Override
+    public boolean update(Issue instance) throws SQLException {
+        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.getScheduledVersion(), Version::getId);
+        setForeignKeyOrNull(update, 6, instance.getResolvedVersion(), Version::getId);
+        setDateOrNull(update, 7, instance.getEta());
+        update.setInt(8, instance.getId());
+        return update.executeUpdate() > 0;
+    }
+
+    @Override
+    public List<Issue> list(Project project) throws SQLException {
+        list.setInt(1, project.getId());
+        List<Issue> versions = new ArrayList<>();
+        try (var result = list.executeQuery()) {
+            while (result.next()) {
+                versions.add(mapColumns(result));
+            }
+        }
+        return versions;
+    }
+
+    @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;
+            }
+        }
+    }
+}

mercurial