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

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

mercurial