diff -r 4f912cd42876 -r 86b5d8a1662f src/main/kotlin/de/uapcore/lightpit/dao/postgres/PGProjectDao.kt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/kotlin/de/uapcore/lightpit/dao/postgres/PGProjectDao.kt Thu Nov 19 13:58:54 2020 +0100 @@ -0,0 +1,122 @@ +/* + * Copyright 2020 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.AbstractProjectDao +import de.uapcore.lightpit.dao.Functions +import de.uapcore.lightpit.entities.IssueSummary +import de.uapcore.lightpit.entities.Project +import de.uapcore.lightpit.entities.User +import java.sql.Connection +import java.sql.PreparedStatement +import java.sql.ResultSet + +class PGProjectDao(connection: Connection) : AbstractProjectDao() { + + private val query = "select projectid, name, node, description, repourl, " + + "userid, username, lastname, givenname, mail " + + "from lpit_project " + + "left join lpit_user owner on lpit_project.owner = owner.userid " + + private val listStmt = connection.prepareStatement("$query order by name") + private val findStmt = connection.prepareStatement("$query where projectid = ?") + private val findByNodeStmt = connection.prepareStatement("$query where node = ?") + private val issueSummaryStmt = connection.prepareStatement( + "select phase, count(*) as total " + + "from lpit_issue " + + "join lpit_issue_phases using(status) " + + "where project = ? " + + "group by phase " + ) + private val insertStmt = connection.prepareStatement( + "insert into lpit_project (name, node, description, repourl, owner) values (?, ?, ?, ?, ?)" + ) + private val updateStmt = connection.prepareStatement( + "update lpit_project set name = ?, node = ?, description = ?, repourl = ?, owner = ? where projectid = ?" + ) + + override fun mapResult(rs: ResultSet): Project { + val proj = Project(rs.getInt("projectid")) + proj.name = rs.getString("name") + proj.node = rs.getString("node") + proj.description = rs.getString("description") + proj.repoUrl = rs.getString("repourl") + proj.owner = PGUserDao.mapResult(rs).takeUnless { rs.wasNull() } + return proj + } + + override fun getIssueSummary(project: Project): IssueSummary { + issueSummaryStmt.setInt(1, project.id) + val result = issueSummaryStmt.executeQuery() + val summary = IssueSummary() + while (result.next()) { + val phase = result.getInt("phase") + val total = result.getInt("total") + when (phase) { + 0 -> summary.open = total + 1 -> summary.active = total + 2 -> summary.done = total + } + } + return summary + } + + private fun setColumns(stmt: PreparedStatement, instance: Project): Int { + var column = 0 + stmt.setString(++column, instance.name) + stmt.setString(++column, instance.node) + Functions.setStringOrNull(stmt, ++column, instance.description) + Functions.setStringOrNull(stmt, ++column, instance.repoUrl) + setForeignKeyOrNull(stmt, ++column, instance.owner, User::id) + return column + } + + override fun save(instance: Project) { + setColumns(insertStmt, instance) + insertStmt.executeUpdate() + } + + override fun update(instance: Project): Boolean { + var column = setColumns(updateStmt, instance) + updateStmt.setInt(++column, instance.id) + return updateStmt.executeUpdate() > 0 + } + + override fun list(): List { + return super.list(listStmt) + } + + override fun find(id: Int): Project? { + findStmt.setInt(1, id) + return super.find(findStmt) + } + + override fun findByNode(node: String): Project? { + findByNodeStmt.setString(1, node) + return super.find(findByNodeStmt) + } +} \ No newline at end of file