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

Fri, 23 Oct 2020 20:34:57 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 23 Oct 2020 20:34:57 +0200
changeset 150
822b7e3d064d
parent 138
e2aa673dd473
child 153
e914fbf4decc
permissions
-rw-r--r--

migrate entities package

     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 = ? " +
    55                         "order by ordinal desc, lower(name) desc");
    57         find = connection.prepareStatement(query + " where id = ? ");
    59         findByNode = connection.prepareStatement(query + " where project = ? and node = ?");
    61         insert = connection.prepareStatement(
    62                 "insert into lpit_component (name, node, color, ordinal, description, lead, project) values (?, ?, ?, ?, ?, ?, ?)"
    63         );
    65         update = connection.prepareStatement(
    66                 "update lpit_component set name = ?, node = ?, color = ?, ordinal = ?, description = ?, lead = ? where id = ?"
    67         );
    68     }
    70     private static Component mapColumns(ResultSet result) throws SQLException {
    71         final var component = new Component(result.getInt("id"));
    72         component.setName(result.getString("name"));
    73         component.setNode(result.getString("node"));
    74         try {
    75             component.setColor(new WebColor(result.getString("color")));
    76         } catch (IllegalArgumentException ex) {
    77             // if someone tempered with the database we default the color to black
    78             component.setColor(new WebColor("000000"));
    79         }
    80         component.setOrdinal(result.getInt("ordinal"));
    81         component.setDescription(result.getString("description"));
    82         component.setLead(PGUserDao.mapColumns(result));
    83         return component;
    84     }
    86     private static int setColumns(PreparedStatement stmt, Component instance) throws SQLException {
    87         int column = 0;
    88         stmt.setString(++column, instance.getName());
    89         stmt.setString(++column, instance.getNode());
    90         stmt.setString(++column, instance.getColor().getHex());
    91         stmt.setInt(++column, instance.getOrdinal());
    92         Functions.setStringOrNull(stmt, ++column, instance.getDescription());
    93         Functions.setForeignKeyOrNull(stmt, ++column, instance.getLead(), User::getId);
    94         return column;
    95     }
    97     @Override
    98     public void save(Component instance, Project project) throws SQLException {
    99         int column = setColumns(insert, instance);
   100         insert.setInt(++column, project.getId());
   101         insert.executeUpdate();
   102     }
   104     @Override
   105     public boolean update(Component instance) throws SQLException {
   106         if (instance.getId() < 0) return false;
   107         int column = setColumns(update, instance);
   108         update.setInt(++column, instance.getId());
   109         return update.executeUpdate() > 0;
   110     }
   113     @Override
   114     public List<Component> list(Project project) throws SQLException {
   115         list.setInt(1, project.getId());
   116         return Functions.list(list, PGComponentDao::mapColumns);
   117     }
   119     @Override
   120     public Component find(int id) throws SQLException {
   121         find.setInt(1, id);
   122         return Functions.find(find, PGComponentDao::mapColumns);
   123     }
   125     @Override
   126     public Component findByNode(Project project, String node) throws SQLException {
   127         findByNode.setInt(1, project.getId());
   128         findByNode.setString(2, node);;
   129         return Functions.find(findByNode, PGComponentDao::mapColumns);
   130     }
   131 }

mercurial