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

changeset 138
e2aa673dd473
parent 128
947d0f6a6a83
child 153
e914fbf4decc
--- a/src/main/java/de/uapcore/lightpit/dao/postgres/PGComponentDao.java	Thu Oct 22 12:00:34 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/dao/postgres/PGComponentDao.java	Thu Oct 22 13:03:26 2020 +0200
@@ -39,42 +39,38 @@
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
-import java.util.ArrayList;
 import java.util.List;
-import java.util.Objects;
 
 public final class PGComponentDao implements ComponentDao {
 
-    private final PreparedStatement insert, update, list, find;
+    private final PreparedStatement insert, update, list, find, findByNode;
 
     public PGComponentDao(Connection connection) throws SQLException {
-        list = connection.prepareStatement(
-                "select id, name, color, ordinal, description, " +
-                        "userid, username, givenname, lastname, mail " +
-                        "from lpit_component " +
-                        "left join lpit_user on lead = userid " +
-                        "where project = ? " +
+        final var query = "select id, name, node, color, ordinal, description, " +
+                "userid, username, givenname, lastname, mail " +
+                "from lpit_component " +
+                "left join lpit_user on lead = userid";
+
+        list = connection.prepareStatement(query + " where project = ? " +
                         "order by ordinal desc, lower(name) desc");
 
-        find = connection.prepareStatement(
-                "select id, name, color, ordinal, description, " +
-                        "userid, username, givenname, lastname, mail " +
-                        "from lpit_component " +
-                        "left join lpit_user on lead = userid " +
-                        "where id = ? ");
+        find = connection.prepareStatement(query + " where id = ? ");
+
+        findByNode = connection.prepareStatement(query + " where project = ? and node = ?");
 
         insert = connection.prepareStatement(
-                "insert into lpit_component (project, name, color, ordinal, description, lead) values (?, ?, ?, ?, ?, ?)"
+                "insert into lpit_component (name, node, color, ordinal, description, lead, project) values (?, ?, ?, ?, ?, ?, ?)"
         );
 
         update = connection.prepareStatement(
-                "update lpit_component set name = ?, color = ?, ordinal = ?, description = ?, lead = ? where id = ?"
+                "update lpit_component set name = ?, node = ?, color = ?, ordinal = ?, description = ?, lead = ? where id = ?"
         );
     }
 
     private static Component mapColumns(ResultSet result) throws SQLException {
         final var component = new Component(result.getInt("id"));
         component.setName(result.getString("name"));
+        component.setNode(result.getString("node"));
         try {
             component.setColor(new WebColor(result.getString("color")));
         } catch (IllegalArgumentException ex) {
@@ -87,53 +83,49 @@
         return component;
     }
 
+    private static int setColumns(PreparedStatement stmt, Component instance) throws SQLException {
+        int column = 0;
+        stmt.setString(++column, instance.getName());
+        stmt.setString(++column, instance.getNode());
+        stmt.setString(++column, instance.getColor().getHex());
+        stmt.setInt(++column, instance.getOrdinal());
+        Functions.setStringOrNull(stmt, ++column, instance.getDescription());
+        Functions.setForeignKeyOrNull(stmt, ++column, instance.getLead(), User::getId);
+        return column;
+    }
+
     @Override
     public void save(Component instance, Project project) throws SQLException {
-        Objects.requireNonNull(instance.getName());
-        insert.setInt(1, project.getId());
-        insert.setString(2, instance.getName());
-        insert.setString(3, instance.getColor().getHex());
-        insert.setInt(4, instance.getOrdinal());
-        Functions.setStringOrNull(insert, 5, instance.getDescription());
-        Functions.setForeignKeyOrNull(insert, 6, instance.getLead(), User::getId);
+        int column = setColumns(insert, instance);
+        insert.setInt(++column, project.getId());
         insert.executeUpdate();
     }
 
     @Override
     public boolean update(Component instance) throws SQLException {
         if (instance.getId() < 0) return false;
-        Objects.requireNonNull(instance.getName());
-        Objects.requireNonNull(instance.getColor());
-        update.setString(1, instance.getName());
-        update.setString(2, instance.getColor().getHex());
-        update.setInt(3, instance.getOrdinal());
-        Functions.setStringOrNull(update, 4, instance.getDescription());
-        Functions.setForeignKeyOrNull(update, 5, instance.getLead(), User::getId);
-        update.setInt(6, instance.getId());
+        int column = setColumns(update, instance);
+        update.setInt(++column, instance.getId());
         return update.executeUpdate() > 0;
     }
 
+
     @Override
     public List<Component> list(Project project) throws SQLException {
         list.setInt(1, project.getId());
-        List<Component> components = new ArrayList<>();
-        try (var result = list.executeQuery()) {
-            while (result.next()) {
-                components.add(mapColumns(result));
-            }
-        }
-        return components;
+        return Functions.list(list, PGComponentDao::mapColumns);
     }
 
     @Override
     public Component find(int id) throws SQLException {
         find.setInt(1, id);
-        try (var result = find.executeQuery()) {
-            if (result.next()) {
-                return mapColumns(result);
-            } else {
-                return null;
-            }
-        }
+        return Functions.find(find, PGComponentDao::mapColumns);
+    }
+
+    @Override
+    public Component findByNode(Project project, String node) throws SQLException {
+        findByNode.setInt(1, project.getId());
+        findByNode.setString(2, node);;
+        return Functions.find(findByNode, PGComponentDao::mapColumns);
     }
 }

mercurial