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

Thu, 15 Oct 2020 18:36:05 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 15 Oct 2020 18:36:05 +0200
changeset 130
7ef369744fd1
parent 128
947d0f6a6a83
child 138
e2aa673dd473
permissions
-rw-r--r--

adds the possibility to specify path parameters to RequestMapping

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.ArrayList;
universe@47 43 import java.util.List;
universe@38 44 import java.util.Objects;
universe@38 45
universe@128 46 public final class PGComponentDao implements ComponentDao {
universe@38 47
universe@47 48 private final PreparedStatement insert, update, list, find;
universe@38 49
universe@128 50 public PGComponentDao(Connection connection) throws SQLException {
universe@47 51 list = connection.prepareStatement(
universe@128 52 "select id, name, color, ordinal, description, " +
universe@128 53 "userid, username, givenname, lastname, mail " +
universe@128 54 "from lpit_component " +
universe@128 55 "left join lpit_user on lead = userid " +
universe@59 56 "where project = ? " +
universe@128 57 "order by ordinal desc, lower(name) desc");
universe@47 58
universe@47 59 find = connection.prepareStatement(
universe@128 60 "select id, name, color, ordinal, description, " +
universe@128 61 "userid, username, givenname, lastname, mail " +
universe@128 62 "from lpit_component " +
universe@128 63 "left join lpit_user on lead = userid " +
universe@128 64 "where id = ? ");
universe@38 65
universe@38 66 insert = connection.prepareStatement(
universe@128 67 "insert into lpit_component (project, name, color, ordinal, description, lead) values (?, ?, ?, ?, ?, ?)"
universe@38 68 );
universe@128 69
universe@38 70 update = connection.prepareStatement(
universe@128 71 "update lpit_component set name = ?, color = ?, ordinal = ?, description = ?, lead = ? where id = ?"
universe@38 72 );
universe@38 73 }
universe@38 74
universe@128 75 private static Component mapColumns(ResultSet result) throws SQLException {
universe@128 76 final var component = new Component(result.getInt("id"));
universe@128 77 component.setName(result.getString("name"));
universe@128 78 try {
universe@128 79 component.setColor(new WebColor(result.getString("color")));
universe@128 80 } catch (IllegalArgumentException ex) {
universe@128 81 // if someone tempered with the database we default the color to black
universe@128 82 component.setColor(new WebColor("000000"));
universe@128 83 }
universe@128 84 component.setOrdinal(result.getInt("ordinal"));
universe@128 85 component.setDescription(result.getString("description"));
universe@128 86 component.setLead(PGUserDao.mapColumns(result));
universe@128 87 return component;
universe@38 88 }
universe@38 89
universe@38 90 @Override
universe@128 91 public void save(Component instance, Project project) throws SQLException {
universe@38 92 Objects.requireNonNull(instance.getName());
universe@128 93 insert.setInt(1, project.getId());
universe@59 94 insert.setString(2, instance.getName());
universe@128 95 insert.setString(3, instance.getColor().getHex());
universe@128 96 insert.setInt(4, instance.getOrdinal());
universe@128 97 Functions.setStringOrNull(insert, 5, instance.getDescription());
universe@128 98 Functions.setForeignKeyOrNull(insert, 6, instance.getLead(), User::getId);
universe@38 99 insert.executeUpdate();
universe@38 100 }
universe@38 101
universe@38 102 @Override
universe@128 103 public boolean update(Component instance) throws SQLException {
universe@75 104 if (instance.getId() < 0) return false;
universe@38 105 Objects.requireNonNull(instance.getName());
universe@128 106 Objects.requireNonNull(instance.getColor());
universe@38 107 update.setString(1, instance.getName());
universe@128 108 update.setString(2, instance.getColor().getHex());
universe@128 109 update.setInt(3, instance.getOrdinal());
universe@128 110 Functions.setStringOrNull(update, 4, instance.getDescription());
universe@128 111 Functions.setForeignKeyOrNull(update, 5, instance.getLead(), User::getId);
universe@128 112 update.setInt(6, instance.getId());
universe@38 113 return update.executeUpdate() > 0;
universe@38 114 }
universe@47 115
universe@47 116 @Override
universe@128 117 public List<Component> list(Project project) throws SQLException {
universe@59 118 list.setInt(1, project.getId());
universe@128 119 List<Component> components = new ArrayList<>();
universe@47 120 try (var result = list.executeQuery()) {
universe@47 121 while (result.next()) {
universe@128 122 components.add(mapColumns(result));
universe@47 123 }
universe@47 124 }
universe@128 125 return components;
universe@47 126 }
universe@47 127
universe@47 128 @Override
universe@128 129 public Component find(int id) throws SQLException {
universe@47 130 find.setInt(1, id);
universe@47 131 try (var result = find.executeQuery()) {
universe@47 132 if (result.next()) {
universe@47 133 return mapColumns(result);
universe@47 134 } else {
universe@47 135 return null;
universe@47 136 }
universe@47 137 }
universe@47 138 }
universe@38 139 }

mercurial