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

Thu, 19 Nov 2020 13:58:54 +0100

author
mike@uapl01.localdomain
date
Thu, 19 Nov 2020 13:58:54 +0100
changeset 159
86b5d8a1662f
permissions
-rw-r--r--

migrates DAO classes

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

mercurial