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

Fri, 06 Nov 2020 10:50:32 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 06 Nov 2020 10:50:32 +0100
changeset 158
4f912cd42876
parent 153
e914fbf4decc
permissions
-rw-r--r--

migrates constants and removes global functions

universe@38 1 /*
universe@38 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@38 3 *
universe@38 4 * Copyright 2018 Mike Becker. All rights reserved.
universe@38 5 *
universe@38 6 * Redistribution and use in source and binary forms, with or without
universe@38 7 * modification, are permitted provided that the following conditions are met:
universe@38 8 *
universe@38 9 * 1. Redistributions of source code must retain the above copyright
universe@38 10 * notice, this list of conditions and the following disclaimer.
universe@38 11 *
universe@38 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@38 13 * notice, this list of conditions and the following disclaimer in the
universe@38 14 * documentation and/or other materials provided with the distribution.
universe@38 15 *
universe@38 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@38 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@38 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@38 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@38 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@38 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@38 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@38 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@38 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@38 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@38 26 * POSSIBILITY OF SUCH DAMAGE.
universe@38 27 *
universe@38 28 */
universe@38 29 package de.uapcore.lightpit.dao.postgres;
universe@38 30
universe@128 31 import de.uapcore.lightpit.dao.ComponentDao;
universe@128 32 import de.uapcore.lightpit.dao.Functions;
universe@128 33 import de.uapcore.lightpit.entities.Component;
universe@86 34 import de.uapcore.lightpit.entities.Project;
universe@128 35 import de.uapcore.lightpit.entities.User;
universe@128 36 import de.uapcore.lightpit.types.WebColor;
universe@38 37
universe@38 38 import java.sql.Connection;
universe@38 39 import java.sql.PreparedStatement;
universe@38 40 import java.sql.ResultSet;
universe@38 41 import java.sql.SQLException;
universe@47 42 import java.util.List;
universe@38 43
universe@128 44 public final class PGComponentDao implements ComponentDao {
universe@38 45
universe@138 46 private final PreparedStatement insert, update, list, find, findByNode;
universe@38 47
universe@128 48 public PGComponentDao(Connection connection) throws SQLException {
universe@138 49 final var query = "select id, name, node, color, ordinal, description, " +
universe@138 50 "userid, username, givenname, lastname, mail " +
universe@138 51 "from lpit_component " +
universe@138 52 "left join lpit_user on lead = userid";
universe@138 53
universe@153 54 list = connection.prepareStatement(query + " where project = ? order by ordinal, lower(name)");
universe@47 55
universe@138 56 find = connection.prepareStatement(query + " where id = ? ");
universe@138 57
universe@138 58 findByNode = connection.prepareStatement(query + " where project = ? and node = ?");
universe@38 59
universe@38 60 insert = connection.prepareStatement(
universe@138 61 "insert into lpit_component (name, node, color, ordinal, description, lead, project) values (?, ?, ?, ?, ?, ?, ?)"
universe@38 62 );
universe@128 63
universe@38 64 update = connection.prepareStatement(
universe@138 65 "update lpit_component set name = ?, node = ?, color = ?, ordinal = ?, description = ?, lead = ? where id = ?"
universe@38 66 );
universe@38 67 }
universe@38 68
universe@128 69 private static Component mapColumns(ResultSet result) throws SQLException {
universe@128 70 final var component = new Component(result.getInt("id"));
universe@128 71 component.setName(result.getString("name"));
universe@138 72 component.setNode(result.getString("node"));
universe@128 73 try {
universe@128 74 component.setColor(new WebColor(result.getString("color")));
universe@128 75 } catch (IllegalArgumentException ex) {
universe@128 76 // if someone tempered with the database we default the color to black
universe@128 77 component.setColor(new WebColor("000000"));
universe@128 78 }
universe@128 79 component.setOrdinal(result.getInt("ordinal"));
universe@128 80 component.setDescription(result.getString("description"));
universe@128 81 component.setLead(PGUserDao.mapColumns(result));
universe@128 82 return component;
universe@38 83 }
universe@38 84
universe@138 85 private static int setColumns(PreparedStatement stmt, Component instance) throws SQLException {
universe@138 86 int column = 0;
universe@138 87 stmt.setString(++column, instance.getName());
universe@138 88 stmt.setString(++column, instance.getNode());
universe@138 89 stmt.setString(++column, instance.getColor().getHex());
universe@138 90 stmt.setInt(++column, instance.getOrdinal());
universe@138 91 Functions.setStringOrNull(stmt, ++column, instance.getDescription());
universe@138 92 Functions.setForeignKeyOrNull(stmt, ++column, instance.getLead(), User::getId);
universe@138 93 return column;
universe@138 94 }
universe@138 95
universe@38 96 @Override
universe@128 97 public void save(Component instance, Project project) throws SQLException {
universe@138 98 int column = setColumns(insert, instance);
universe@138 99 insert.setInt(++column, project.getId());
universe@38 100 insert.executeUpdate();
universe@38 101 }
universe@38 102
universe@38 103 @Override
universe@128 104 public boolean update(Component instance) throws SQLException {
universe@75 105 if (instance.getId() < 0) return false;
universe@138 106 int column = setColumns(update, instance);
universe@138 107 update.setInt(++column, instance.getId());
universe@38 108 return update.executeUpdate() > 0;
universe@38 109 }
universe@47 110
universe@138 111
universe@47 112 @Override
universe@128 113 public List<Component> list(Project project) throws SQLException {
universe@59 114 list.setInt(1, project.getId());
universe@138 115 return Functions.list(list, PGComponentDao::mapColumns);
universe@47 116 }
universe@47 117
universe@47 118 @Override
universe@128 119 public Component find(int id) throws SQLException {
universe@47 120 find.setInt(1, id);
universe@138 121 return Functions.find(find, PGComponentDao::mapColumns);
universe@138 122 }
universe@138 123
universe@138 124 @Override
universe@138 125 public Component findByNode(Project project, String node) throws SQLException {
universe@138 126 findByNode.setInt(1, project.getId());
universe@138 127 findByNode.setString(2, node);;
universe@138 128 return Functions.find(findByNode, PGComponentDao::mapColumns);
universe@47 129 }
universe@38 130 }

mercurial