|
1 /* |
|
2 * Copyright 2020 Mike Becker. All rights reserved. |
|
3 * |
|
4 * Redistribution and use in source and binary forms, with or without |
|
5 * modification, are permitted provided that the following conditions are met: |
|
6 * |
|
7 * 1. Redistributions of source code must retain the above copyright |
|
8 * notice, this list of conditions and the following disclaimer. |
|
9 * |
|
10 * 2. Redistributions in binary form must reproduce the above copyright |
|
11 * notice, this list of conditions and the following disclaimer in the |
|
12 * documentation and/or other materials provided with the distribution. |
|
13 * |
|
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
|
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
24 * |
|
25 */ |
|
26 |
|
27 package de.uapcore.lightpit.dao.postgres |
|
28 |
|
29 import de.uapcore.lightpit.dao.AbstractProjectDao |
|
30 import de.uapcore.lightpit.dao.Functions |
|
31 import de.uapcore.lightpit.entities.IssueSummary |
|
32 import de.uapcore.lightpit.entities.Project |
|
33 import de.uapcore.lightpit.entities.User |
|
34 import java.sql.Connection |
|
35 import java.sql.PreparedStatement |
|
36 import java.sql.ResultSet |
|
37 |
|
38 class PGProjectDao(connection: Connection) : AbstractProjectDao() { |
|
39 |
|
40 private val query = "select projectid, name, node, description, repourl, " + |
|
41 "userid, username, lastname, givenname, mail " + |
|
42 "from lpit_project " + |
|
43 "left join lpit_user owner on lpit_project.owner = owner.userid " |
|
44 |
|
45 private val listStmt = connection.prepareStatement("$query order by name") |
|
46 private val findStmt = connection.prepareStatement("$query where projectid = ?") |
|
47 private val findByNodeStmt = connection.prepareStatement("$query where node = ?") |
|
48 private val issueSummaryStmt = connection.prepareStatement( |
|
49 "select phase, count(*) as total " + |
|
50 "from lpit_issue " + |
|
51 "join lpit_issue_phases using(status) " + |
|
52 "where project = ? " + |
|
53 "group by phase " |
|
54 ) |
|
55 private val insertStmt = connection.prepareStatement( |
|
56 "insert into lpit_project (name, node, description, repourl, owner) values (?, ?, ?, ?, ?)" |
|
57 ) |
|
58 private val updateStmt = connection.prepareStatement( |
|
59 "update lpit_project set name = ?, node = ?, description = ?, repourl = ?, owner = ? where projectid = ?" |
|
60 ) |
|
61 |
|
62 override fun mapResult(rs: ResultSet): Project { |
|
63 val proj = Project(rs.getInt("projectid")) |
|
64 proj.name = rs.getString("name") |
|
65 proj.node = rs.getString("node") |
|
66 proj.description = rs.getString("description") |
|
67 proj.repoUrl = rs.getString("repourl") |
|
68 proj.owner = PGUserDao.mapResult(rs).takeUnless { rs.wasNull() } |
|
69 return proj |
|
70 } |
|
71 |
|
72 override fun getIssueSummary(project: Project): IssueSummary { |
|
73 issueSummaryStmt.setInt(1, project.id) |
|
74 val result = issueSummaryStmt.executeQuery() |
|
75 val summary = IssueSummary() |
|
76 while (result.next()) { |
|
77 val phase = result.getInt("phase") |
|
78 val total = result.getInt("total") |
|
79 when (phase) { |
|
80 0 -> summary.open = total |
|
81 1 -> summary.active = total |
|
82 2 -> summary.done = total |
|
83 } |
|
84 } |
|
85 return summary |
|
86 } |
|
87 |
|
88 private fun setColumns(stmt: PreparedStatement, instance: Project): Int { |
|
89 var column = 0 |
|
90 stmt.setString(++column, instance.name) |
|
91 stmt.setString(++column, instance.node) |
|
92 Functions.setStringOrNull(stmt, ++column, instance.description) |
|
93 Functions.setStringOrNull(stmt, ++column, instance.repoUrl) |
|
94 setForeignKeyOrNull(stmt, ++column, instance.owner, User::id) |
|
95 return column |
|
96 } |
|
97 |
|
98 override fun save(instance: Project) { |
|
99 setColumns(insertStmt, instance) |
|
100 insertStmt.executeUpdate() |
|
101 } |
|
102 |
|
103 override fun update(instance: Project): Boolean { |
|
104 var column = setColumns(updateStmt, instance) |
|
105 updateStmt.setInt(++column, instance.id) |
|
106 return updateStmt.executeUpdate() > 0 |
|
107 } |
|
108 |
|
109 override fun list(): List<Project> { |
|
110 return super.list(listStmt) |
|
111 } |
|
112 |
|
113 override fun find(id: Int): Project? { |
|
114 findStmt.setInt(1, id) |
|
115 return super.find(findStmt) |
|
116 } |
|
117 |
|
118 override fun findByNode(node: String): Project? { |
|
119 findByNodeStmt.setString(1, node) |
|
120 return super.find(findByNodeStmt) |
|
121 } |
|
122 } |