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

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

mercurial