src/main/kotlin/de/uapcore/lightpit/dao/postgres/PGComponentDao.kt

changeset 159
86b5d8a1662f
equal deleted inserted replaced
158:4f912cd42876 159:86b5d8a1662f
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.AbstractComponentDao
30 import de.uapcore.lightpit.dao.Functions
31 import de.uapcore.lightpit.entities.Component
32 import de.uapcore.lightpit.entities.Project
33 import de.uapcore.lightpit.entities.User
34 import de.uapcore.lightpit.types.WebColor
35 import java.sql.Connection
36 import java.sql.PreparedStatement
37 import java.sql.ResultSet
38
39 class PGComponentDao(connection: Connection) : AbstractComponentDao() {
40
41 private val query = "select id, name, node, color, ordinal, description, " +
42 "userid, username, givenname, lastname, mail " +
43 "from lpit_component " +
44 "left join lpit_user on lead = userid"
45
46 private val listStmt = connection.prepareStatement("$query where project = ? order by ordinal, lower(name)")
47 private val findStmt = connection.prepareStatement("$query where id = ? ")
48 private val findByNodeStmt = connection.prepareStatement("$query where project = ? and node = ?")
49 private val insertStmt = connection.prepareStatement(
50 "insert into lpit_component (name, node, color, ordinal, description, lead, project) values (?, ?, ?, ?, ?, ?, ?)"
51 )
52 private val updateStmt = connection.prepareStatement(
53 "update lpit_component set name = ?, node = ?, color = ?, ordinal = ?, description = ?, lead = ? where id = ?"
54 )
55
56 override fun mapResult(rs: ResultSet): Component {
57 val component = Component(rs.getInt("id"))
58 component.name = rs.getString("name")
59 component.node = rs.getString("node")
60 component.color = try {
61 WebColor(rs.getString("color"))
62 } catch (ex: IllegalArgumentException) {
63 WebColor("000000")
64 }
65 component.ordinal = rs.getInt("ordinal")
66 component.description = rs.getString("description")
67 component.lead = PGUserDao.mapResult(rs).takeUnless { rs.wasNull() }
68 return component
69 }
70
71 private fun setColumns(stmt: PreparedStatement, instance: Component): Int {
72 var column = 0
73 stmt.setString(++column, instance.name)
74 stmt.setString(++column, instance.node)
75 stmt.setString(++column, instance.color.hex)
76 stmt.setInt(++column, instance.ordinal)
77 Functions.setStringOrNull(stmt, ++column, instance.description)
78 setForeignKeyOrNull(stmt, ++column, instance.lead, User::id)
79 return column
80 }
81
82 override fun save(instance: Component, parent: Project) {
83 var column = setColumns(insertStmt, instance)
84 insertStmt.setInt(++column, parent.id)
85 insertStmt.executeUpdate()
86 }
87
88 override fun update(instance: Component): Boolean {
89 var column = setColumns(updateStmt, instance)
90 updateStmt.setInt(++column, instance.id)
91 return updateStmt.executeUpdate() > 0
92 }
93
94
95 override fun list(parent: Project): List<Component> {
96 listStmt.setInt(1, parent.id)
97 return super.list(listStmt)
98 }
99
100 override fun find(id: Int): Component? {
101 findStmt.setInt(1, id)
102 return super.find(findStmt)
103 }
104
105 override fun findByNode(parent: Project, node: String): Component? {
106 findByNodeStmt.setInt(1, parent.id)
107 findByNodeStmt.setString(2, node)
108 return super.find(findByNodeStmt)
109 }
110 }

mercurial