diff --git a/src/de/steamwar/bungeecore/bot/events/SchematicsManager.java b/src/de/steamwar/bungeecore/bot/events/SchematicsManager.java index 82a83705..e407cf98 100644 --- a/src/de/steamwar/bungeecore/bot/events/SchematicsManager.java +++ b/src/de/steamwar/bungeecore/bot/events/SchematicsManager.java @@ -53,13 +53,13 @@ public class SchematicsManager { CheckCommand.getSchemsToCheck().forEach(schematic -> { StringBuilder st = new StringBuilder(); - st.append("Typ: ").append(schematic.getSchemType().getKuerzel()); - st.append("\nVon: ").append(SteamwarUser.get(schematic.getSchemOwner()).getUserName()); + st.append("Typ: ").append(schematic.getSchemtype().getKuerzel()); + st.append("\nVon: ").append(SteamwarUser.get(schematic.getOwner()).getUserName()); String checker = CheckCommand.getChecker(schematic); if (checker != null) { st.append("\nWird Geprüft von: ").append(checker); } - embedBuilder.addField(schematic.getSchemName(), st.toString(), true); + embedBuilder.addField(schematic.getName(), st.toString(), true); }); MessageBuilder messageBuilder = new MessageBuilder(); diff --git a/src/de/steamwar/bungeecore/commands/CheckCommand.java b/src/de/steamwar/bungeecore/commands/CheckCommand.java index bbfd55f2..3ae16263 100644 --- a/src/de/steamwar/bungeecore/commands/CheckCommand.java +++ b/src/de/steamwar/bungeecore/commands/CheckCommand.java @@ -111,7 +111,7 @@ public class CheckCommand extends BasicCommand { } } - private static List getSchemsToCheck(){ + public static List getSchemsToCheck(){ List schematicList = new LinkedList<>(); for (SchematicType type : SchematicType.values()) { @@ -243,8 +243,8 @@ public class CheckCommand extends BasicCommand { SubserverSystem.startTestServer(checker, mode, FightCommand.getMap(checker, mode, "Random"), schematic.getId(), 0); currentCheckers.put(checker.getUniqueId(), this); currentSchems.put(schematic.getId(), this); - for(CheckedSchematic previous : CheckedSchematic.previousChecks(schematic.getId())) - Message.sendPrefixless("CHECK_SCHEMATIC_PREVIOUS", checker, previous.getEndTime(), SteamwarUser.get(previous.getValidator()).getUserName(), previous.getReason()); + for(CheckedSchematic previous : CheckedSchematic.previousChecks(schematic)) + Message.sendPrefixless("CHECK_SCHEMATIC_PREVIOUS", checker, previous.getEndTime(), SteamwarUser.get(previous.getValidator()).getUserName(), previous.getDeclineReason()); next(0); }); } @@ -294,7 +294,7 @@ public class CheckCommand extends BasicCommand { } schematic.setType(schematic.getSchemtype().fightType().toDB()); - CheckedSchematic.create(schematic.getId(), SteamwarUser.get(checker.getUniqueId()).getId(), startTime, Timestamp.from(Instant.now()), "freigegeben"); + CheckedSchematic.create(schematic, SteamwarUser.get(checker.getUniqueId()).getId(), startTime, Timestamp.from(Instant.now()), "freigegeben"); SteamwarUser user = SteamwarUser.get(schematic.getOwner()); ProxiedPlayer player = ProxyServer.getInstance().getPlayer(user.getUuid()); if(player != null) { @@ -308,7 +308,7 @@ public class CheckCommand extends BasicCommand { private void decline(String reason){ schematic.setType(SchematicType.Normal.toDB()); - CheckedSchematic.create(schematic.getId(), SteamwarUser.get(checker.getUniqueId()).getId(), startTime, Timestamp.from(Instant.now()), reason); + CheckedSchematic.create(schematic, SteamwarUser.get(checker.getUniqueId()).getId(), startTime, Timestamp.from(Instant.now()), reason); SteamwarUser user = SteamwarUser.get(schematic.getOwner()); ProxiedPlayer player = ProxyServer.getInstance().getPlayer(user.getUuid()); if(player != null) { @@ -321,13 +321,13 @@ public class CheckCommand extends BasicCommand { } private void abort(){ - CheckedSchematic.create(schematic.getId(), SteamwarUser.get(checker.getUniqueId()).getId(), startTime, Timestamp.from(Instant.now()), "Prüfvorgang abgebrochen"); + CheckedSchematic.create(schematic, SteamwarUser.get(checker.getUniqueId()).getId(), startTime, Timestamp.from(Instant.now()), "Prüfvorgang abgebrochen"); stop(); } private void stop(){ currentCheckers.remove(checker.getUniqueId()); - currentSchems.remove(schematic.getSchemID()); + currentSchems.remove(schematic.getId()); ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { for (Subserver subserver : Subserver.getServerList()) { if (subserver.getType() == Servertype.BAUSERVER && ((Bauserver) subserver).getOwner().equals(checker.getUniqueId())) { @@ -340,7 +340,7 @@ public class CheckCommand extends BasicCommand { private void remove() { currentCheckers.remove(checker.getUniqueId()); - currentSchems.remove(schematic.getSchemID()); + currentSchems.remove(schematic.getId()); } } } diff --git a/src/de/steamwar/bungeecore/sql/CheckedSchematic.java b/src/de/steamwar/bungeecore/sql/CheckedSchematic.java index 499a2f0e..82b71333 100644 --- a/src/de/steamwar/bungeecore/sql/CheckedSchematic.java +++ b/src/de/steamwar/bungeecore/sql/CheckedSchematic.java @@ -24,56 +24,97 @@ import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; +import java.util.UUID; public class CheckedSchematic { - private static final Statement create = new Statement("INSERT INTO CheckedSchematic (SchemName, SchemOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?, ?)"); - private static final Statement previous = new Statement("SELECT * FROM CheckedSchematic WHERE SchemName = ? AND SchemOwner = ? ORDER BY EndTime ASC"); - - private final String schemName; - private final int schemOwner; + private static final Statement checkHistory = new Statement("SELECT * FROM CheckedSchematic WHERE NodeId IN (SELECT NodeId FROM SchematicNode WHERE NodeOwner = ?) AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' AND NodeId is not NULL ORDER BY EndTime DESC"); + private static final Statement nodeHistory = new Statement("SELECT * FROM CheckedSchematic WHERE NodeId = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC"); + private static final Statement insert = new Statement("INSERT INTO CheckedSchematic (NodeId, NodeName, NodeOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?)"); + private final Integer node; private final int validator; private final Timestamp startTime; private final Timestamp endTime; - private final String reason; + private final String declineReason; private CheckedSchematic(ResultSet rs) throws SQLException { - nodeId = rs.getInt("NodeId"); - validator = rs.getInt("Validator"); - startTime = rs.getTimestamp("StartTime"); - endTime = rs.getTimestamp("EndTime"); - reason = rs.getString("DeclineReason"); + this.node = rs.getInt("NodeId"); + this.validator = rs.getInt("Validator"); + this.startTime = rs.getTimestamp("StartTime"); + this.endTime = rs.getTimestamp("EndTime"); + this.declineReason = rs.getString("DeclineReason"); } - public static void create(String schemName, int schemOwner, int validator, Timestamp startTime, Timestamp endTime, String reason){ - SQL.update("INSERT INTO CheckedSchematic (SchemName, SchemOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?, ?)", - schemName, schemOwner, validator, startTime, endTime, reason); + public static void create(int nodeId, String name, int owner, int validator, Timestamp startTime, Timestamp endTime, String reason){ + insert.update(nodeId, name, owner, validator, startTime, endTime, reason); } - public static List previousChecks(String schemName, int schemOwner){ - ResultSet rs = SQL.select("SELECT * FROM CheckedSchematic WHERE SchemName = ? AND SchemOwner = ? ORDER BY EndTime ASC", schemName, schemOwner); - List schematics = new ArrayList<>(); - try { + public static void create(SchematicNode node, int validator, Timestamp startTime, Timestamp endTime, String reason){ + create(node.getId(), node.getName(), node.getOwner(), validator, startTime, endTime, reason); + } + + public static List previousChecks(SchematicNode node){ + return nodeHistory.select(rs -> { + List schematics = new ArrayList<>(); while(rs.next()) schematics.add(new CheckedSchematic(rs)); return schematics; - }, schemName, schemOwner); + }, node.getId()); + } + + public static List getLastDeclinedOfNode(SchematicNode node){ + return getLastDeclinedOfNode(node.getId()); + } + + public static List getLastDeclinedOfNode(int node){ + return nodeHistory.select(rs -> { + List lastDeclined = new ArrayList<>(); + while(rs.next()){ + lastDeclined.add(new CheckedSchematic(rs)); + } + return lastDeclined; + }, node); + } + + public static List getLastDeclined(UUID uuid){ + return getLastDelined(SteamwarUser.get(uuid).getId()); + } + + public static List getLastDelined(int schemOwner){ + return checkHistory.select(rs -> { + List history = new ArrayList<>(); + while(rs.next()) + history.add(new CheckedSchematic(rs)); + return history; + }, schemOwner); } public int getValidator() { return validator; } + public Timestamp getStartTime() { + return startTime; + } + public Timestamp getEndTime() { return endTime; } - public String getReason() { - return reason; + public String getDeclineReason() { + return declineReason; } - public int getNodeId() { - return nodeId; + public int getNode() { + return node; + } + + public String getSchemName() { + return SchematicNode.getSchematicNode(node).getName(); + } + + public int getSchemOwner() { + return SchematicNode.getSchematicNode(node).getId(); } } diff --git a/src/de/steamwar/bungeecore/sql/NodeMember.java b/src/de/steamwar/bungeecore/sql/NodeMember.java index 76c91e4b..063dd487 100644 --- a/src/de/steamwar/bungeecore/sql/NodeMember.java +++ b/src/de/steamwar/bungeecore/sql/NodeMember.java @@ -25,43 +25,40 @@ import java.util.HashSet; import java.util.Set; public class NodeMember { + private static final Statement getNodeMember = new Statement("SELECT * FROM NodeMember WHERE NodeId = ? AND UserId = ?"); + private static final Statement getNodeMembers = new Statement("SELECT * FROM NodeMember WHERE NodeId = ?"); + private static final Statement getSchematics = new Statement("SELECT * FROM NodeMember WHERE UserId = ?"); + private static final Statement createNodeMember = new Statement("INSERT INTO NodeMember (NodeId, UserId) VALUES (?, ?)"); + private static final Statement deleteNodeMember = new Statement("DELETE FROM NodeMember WHERE NodeId = ? AND UserId = ?"); + public static NodeMember getNodeMember(int node, int member) { - ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE NodeId = ? AND UserId = ?", node, member); - try { - if(!set.next()) + return getNodeMember.select(rs -> { + if(!rs.next()) return null; - return new NodeMember(set); - } catch (SQLException e) { - throw new SecurityException("Could not load NodeMember", e); - } + return new NodeMember(rs); + }, node, member); } public static Set getNodeMembers(int node) { - ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE NodeId = ?", node); - try { + return getNodeMembers.select(rs -> { Set members = new HashSet<>(); - while (set.next()) - members.add(new NodeMember(set)); + while (rs.next()) + members.add(new NodeMember(rs)); return members; - } catch (SQLException e) { - throw new SecurityException("Could not load NodeMember", e); - } + }, node); } public static Set getSchematics(int member) { - ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE UserId = ?", member); - try { + return getSchematics.select(rs -> { Set members = new HashSet<>(); - while (set.next()) - members.add(new NodeMember(set)); + while (rs.next()) + members.add(new NodeMember(rs)); return members; - } catch (SQLException e) { - throw new SecurityException("Could not load NodeMember", e); - } + }, member); } public static NodeMember createNodeMember(int node, int member) { - SQL.update("INSERT INTO NodeMember (NodeId, UserId) VALUES (?, ?)", node, member); + createNodeMember.update(node, member); return getNodeMember(node, member); } @@ -82,6 +79,6 @@ public class NodeMember { } public void delete() { - SQL.update("DELETE FROM NodeMember WHERE NodeId = ? AND UserId = ?", node, member); + deleteNodeMember.update(node, member); } } diff --git a/src/de/steamwar/bungeecore/sql/SchematicNode.java b/src/de/steamwar/bungeecore/sql/SchematicNode.java index c419c099..b111a2db 100644 --- a/src/de/steamwar/bungeecore/sql/SchematicNode.java +++ b/src/de/steamwar/bungeecore/sql/SchematicNode.java @@ -29,6 +29,23 @@ import java.util.function.Predicate; public class SchematicNode { + private static final Statement createNode = new Statement("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeType, NodeItem) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUES(NodeName), ParentNode = VALUES(ParentNode), NodeItem = VALUES(NodeItem), NodeType = VALUES(NodeType), NodeItem = VALUES(NodeItem)"); + private static final Statement getSchematicNode_Null = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode is NULL"); + private static final Statement getSchematicNode = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?"); + private static final Statement getSchematicsInNode_Null = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL"); + private static final Statement getSchematicsInNode = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ?"); + private static final Statement getSchematicDirectory_Null = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL"); + private static final Statement getSchematicDirectory = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?"); + private static final Statement getSchematicNodeO_Null = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL"); + private static final Statement getSchematicNodeO = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?"); + private static final Statement getSchematicNodeId = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ?"); + private static final Statement getAllSchemsOfTypeOwner = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?"); + private static final Statement getAllSchemsOfType = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeType = ?"); + private static final Statement getAccessibleByUser = new Statement("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND s.ParentNode is NULL GROUP BY s.NodeId ORDER BY s.NodeName"); + private static final Statement countNodes = new Statement("SELECT COUNT(NodeId) AS 'count' FROM SchematicNode"); + private static final Statement updateDB = new Statement("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?"); + private static final Statement deleteNode = new Statement("DELETE FROM SchematicNode WHERE NodeId = ?"); + public static SchematicNode createSchematic(int owner, String name, Integer parent) { return createSchematicNode(owner, name, parent, SchematicType.Normal.toDB(), ""); } @@ -40,8 +57,7 @@ public class SchematicNode { public static SchematicNode createSchematicNode(int owner, String name, Integer parent, String type, String item) { if (parent == 0) parent = null; - SQL.update("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeType, NodeItem) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUES(NodeName), ParentNode = VALUES(ParentNode), NodeItem = VALUES(NodeItem), NodeType = VALUES(NodeType), NodeItem = VALUES(NodeItem)", - name, owner, parent, type, item); + createNode.update(name, owner, parent, type, item); return getSchematicNode(owner, name, parent); } @@ -77,105 +93,85 @@ public class SchematicNode { } public static SchematicNode getSchematicNode(int owner, String name, Integer parent) { - if (parent != null && parent == 0) + if (parent != null && parent == 0) { parent = null; - ResultSet set; - if (parent == null) { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode is NULL", owner, name); - } else { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?", owner, name, parent); } - try { - while (set.next()) { - SchematicNode node = new SchematicNode(set); + Statement.ResultSetUser user = rs -> { + while (rs.next()) { + SchematicNode node = new SchematicNode(rs); return node; } - List nodes = getSchematicNodeInNode(parent); - for (SchematicNode node:nodes) { - if (node.name.equals(name) && NodeMember.getNodeMember(node.getId(), owner) != null) - return node; - } return null; - }catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); + }; + if(parent == null) { + return getSchematicNode_Null.select(user, owner, name); + } else { + return getSchematicNode.select(user, owner, name, parent); } } public static List getSchematicNodeInNode(Integer parent) { if(parent != null && parent == 0) parent = null; - ResultSet set; - if(parent == null) { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL"); - }else { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ?", parent); - } - try { + Statement.ResultSetUser> user = rs -> { List nodes = new ArrayList<>(); - while (set.next()) - nodes.add(new SchematicNode(set)); + while (rs.next()) + nodes.add(new SchematicNode(rs)); return nodes; - }catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); + }; + if(parent == null) { + return getSchematicsInNode_Null.select(user); + }else { + return getSchematicsInNode.select(user, parent); } } public static SchematicNode getSchematicDirectory(String name, Integer parent) { if(parent != null && parent == 0) parent = null; - ResultSet set; - if(parent == null) { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name, parent); - }else { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); - } - try { - while (set.next()) { - SchematicNode node = new SchematicNode(set); + Statement.ResultSetUser user = rs -> { + while (rs.next()) { + SchematicNode node = new SchematicNode(rs); if(node.isDir()) return node; } return null; - }catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); + }; + + if(parent == null) { + return getSchematicDirectory_Null.select(user, name); + }else { + return getSchematicDirectory.select(user, name, parent); } } - public static SchematicNode getSchematicInParent(String name, Integer parent) { + public static SchematicNode getSchematicNode(String name, Integer parent) { if(parent != null && parent == 0) parent = null; - ResultSet set; - if(parent == null) { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name); - }else { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); - } - try { - while (set.next()) { - SchematicNode node = new SchematicNode(set); - if(!node.isDir()) - return node; + Statement.ResultSetUser user = rs -> { + while (rs.next()) { + return new SchematicNode(rs); } return null; - }catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); + }; + if(parent == null) { + return getSchematicNodeO_Null.select(user, name); + }else { + return getSchematicNodeO.select(user, name, parent); } } public static SchematicNode getSchematicNode(int id) { - ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ?", id); - try { - if (!set.next()) + return getSchematicNodeId.select(rs -> { + if (!rs.next()) return null; - return new SchematicNode(set); - } catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); - } + return new SchematicNode(rs); + }); } public static List getSchematicsOfType(int owner, String schemType, Integer parent) { List schems = getAllSchematicsAccessibleByUser(owner); - schems.removeIf(node -> !node.getType().equals(schemType)); + schems.removeIf(node -> node.isDir() || !node.getType().equals(schemType)); Map nodesInParent = new LinkedHashMap<>(); for (SchematicNode schematicNode : schems) { SchematicNode currentNode = schematicNode; @@ -188,27 +184,22 @@ public class SchematicNode { } public static List getAllSchematicsOfType(int owner, String schemType) { - ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?", owner, schemType); - try { + return getAllSchemsOfTypeOwner.select(rs -> { List nodes = new ArrayList<>(); - while (set.next()) - nodes.add(new SchematicNode(set)); + while (rs.next()) + nodes.add(new SchematicNode(rs)); return nodes; - } catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); - } + }, owner, schemType); } + public static List getAllSchematicsOfType(String schemType) { - ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeType = ?", schemType); - try { + return getAllSchemsOfType.select(rs -> { List nodes = new ArrayList<>(); - while (set.next()) - nodes.add(new SchematicNode(set)); + while (rs.next()) + nodes.add(new SchematicNode(rs)); return nodes; - } catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); - } + }, schemType); } public static List deepGet(Integer parent, Predicate filter) { @@ -241,29 +232,29 @@ public class SchematicNode { if(isAdded) return getSchematicNodeInNode(parent); } else { - ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND s.ParentNode is NULL GROUP BY s.NodeId ORDER BY s.NodeName", user, user); - try{ + return getAccessibleByUser.select(rs -> { List nodes = new ArrayList<>(); - while(set.next()) - nodes.add(new SchematicNode(set)); + while(rs.next()) + nodes.add(new SchematicNode(rs)); return nodes; - }catch(SQLException e){ - throw new SecurityException("Failed listing schematics", e); - } + }, user, user); } return Collections.emptyList(); } public static List getAllSchematicsAccessibleByUser(int user) { - ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember nm ON nm.NodeId = s.NodeId WHERE s.NodeOwner = ? OR nm.UserId = ? GROUP BY s.NodeId ORDER BY s.NodeName", user, user); - try{ + return getAccessibleByUser.select(rs -> { List nodes = new ArrayList<>(); - while(set.next()) - nodes.add(new SchematicNode(set)); + while(rs.next()) { + SchematicNode node = new SchematicNode(rs); + if(node.isDir()) { + nodes.addAll(deepGet(node.getId(), node1 -> true)); + } else { + nodes.add(node); + } + } return nodes; - }catch(SQLException e){ - throw new SecurityException("Failed listing schematics", e); - } + }, user, user); } private final int id; @@ -275,6 +266,7 @@ public class SchematicNode { private boolean schemFormat; private int rank; private final boolean isDir; + private Map brCache = new HashMap<>(); public static List filterSchems(int user, Predicate filter) { List finalList = new ArrayList<>(); @@ -291,14 +283,12 @@ public class SchematicNode { } public static Integer countNodes() { - ResultSet set = SQL.select("SELECT COUNT(NodeId) AS 'count' FROM SchematicNode"); - try { - if (set.next()) - return set.getInt("count"); + return countNodes.select(rs -> { + if (rs.next()) { + return rs.getInt("count"); + } return 0; - } catch (SQLException e) { - throw new SecurityException("Failed listing schematics", e); - } + }); } public int getId() { @@ -322,7 +312,7 @@ public class SchematicNode { return parent; } - public void setParent(int parent) { + public void setParent(Integer parent) { this.parent = parent; updateDB(); } @@ -394,7 +384,7 @@ public class SchematicNode { } public String generateBreadcrumbs(SteamwarUser user) { - return generateBreadcrumbs("/", user); + return brCache.computeIfAbsent(user.getId(), integer -> generateBreadcrumbs("/", user)); } public String generateBreadcrumbs(String split, SteamwarUser user) { @@ -413,20 +403,24 @@ public class SchematicNode { } private void updateDB() { - SQL.update("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?", - name, owner, parent == 0 ? null : parent, item, type, rank, id); + updateDB.update(name, owner, parent == 0 ? null : parent, item, type, rank, id); this.lastUpdate = Timestamp.from(Instant.now()); + this.brCache.clear(); } public void delete() { if (isDir()) { getSchematicNodeInNode(getId()).forEach(SchematicNode::delete); } - SQL.update("DELETE FROM SchematicNode WHERE NodeId = ?", id); + deleteNode.update(id); } - private void updateDatabase(Blob blob, boolean newFormat) { - SQL.update("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?", blob, newFormat, id); - schemFormat = newFormat; + @Override + public boolean equals(Object obj) { + if (!(obj instanceof SchematicNode)) + return false; + + SchematicNode node = (SchematicNode) obj; + return node.getId() == id; } }