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/bot/util/DiscordSchemAlert.java b/src/de/steamwar/bungeecore/bot/util/DiscordSchemAlert.java index b4b4261e..f0d9d7b6 100644 --- a/src/de/steamwar/bungeecore/bot/util/DiscordSchemAlert.java +++ b/src/de/steamwar/bungeecore/bot/util/DiscordSchemAlert.java @@ -20,7 +20,7 @@ package de.steamwar.bungeecore.bot.util; import de.steamwar.bungeecore.bot.SteamwarDiscordBot; -import de.steamwar.bungeecore.sql.Schematic; +import de.steamwar.bungeecore.sql.SchematicNode; import de.steamwar.bungeecore.sql.SteamwarUser; import lombok.experimental.UtilityClass; import net.dv8tion.jda.api.EmbedBuilder; @@ -36,14 +36,14 @@ import java.time.Instant; @UtilityClass public class DiscordSchemAlert { - public void sendDecline(Schematic schematic, SteamwarUser user, String reason) { + public void sendDecline(SchematicNode schematic, SteamwarUser user, String reason) { if(user.getDiscordId() != null) { User dcUser = SteamwarDiscordBot.instance().getJda().retrieveUserById(user.getDiscordId()).complete(); EmbedBuilder builder = new EmbedBuilder(); builder.setAuthor("SteamWar", "https://steamwar.de", "https://cdn.discordapp.com/app-icons/869606970099904562/60c884000407c02671d91d8e7182b8a1.png"); builder.setColor(Color.RED); builder.setTitle("SteamWar-Schematic Info"); - builder.setDescription("Deine Schematic **" + schematic.getSchemName() + "** wurde abgelehnt. **Grund:** \n" + reason); + builder.setDescription("Deine Schematic **" + schematic.getName() + "** wurde abgelehnt. **Grund:** \n" + reason); builder.setTimestamp(Instant.now()); MessageBuilder messageBuilder = new MessageBuilder(); @@ -56,14 +56,14 @@ public class DiscordSchemAlert { } } - public void sendAccept(Schematic schematic, SteamwarUser user) { + public void sendAccept(SchematicNode schematic, SteamwarUser user) { if(user.getDiscordId() != null) { User dcUser = SteamwarDiscordBot.instance().getJda().retrieveUserById(user.getDiscordId()).complete(); EmbedBuilder builder = new EmbedBuilder(); builder.setAuthor("SteamWar", "https://steamwar.de", "https://cdn.discordapp.com/app-icons/869606970099904562/60c884000407c02671d91d8e7182b8a1.png"); builder.setColor(Color.GREEN); builder.setTitle("SteamWar-Schematic Info"); - builder.setDescription("Deine Schematic **" + schematic.getSchemName() + "** wurde angenommen."); + builder.setDescription("Deine Schematic **" + schematic.getName() + "** wurde angenommen."); builder.setTimestamp(Instant.now()); MessageBuilder messageBuilder = new MessageBuilder(); diff --git a/src/de/steamwar/bungeecore/commands/CheckCommand.java b/src/de/steamwar/bungeecore/commands/CheckCommand.java index e61dd710..3ae16263 100644 --- a/src/de/steamwar/bungeecore/commands/CheckCommand.java +++ b/src/de/steamwar/bungeecore/commands/CheckCommand.java @@ -23,7 +23,7 @@ import de.steamwar.bungeecore.*; import de.steamwar.bungeecore.bot.util.DiscordSchemAlert; import de.steamwar.bungeecore.listeners.ConnectionListener; import de.steamwar.bungeecore.sql.CheckedSchematic; -import de.steamwar.bungeecore.sql.Schematic; +import de.steamwar.bungeecore.sql.SchematicNode; import de.steamwar.bungeecore.sql.SchematicType; import de.steamwar.bungeecore.sql.SteamwarUser; import net.md_5.bungee.api.ChatColor; @@ -67,13 +67,13 @@ public class CheckCommand extends BasicCommand { super("check", ConnectionListener.CHECK_PERMISSION); ProxyServer.getInstance().getScheduler().schedule(BungeeCore.get(), () -> { - List schematics = getSchemsToCheck(); + List schematics = getSchemsToCheck(); if(schematics.size() != currentCheckers.size()) Message.team("CHECK_REMINDER", "CHECK_REMINDER_HOVER", new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/check list"), schematics.size() - currentCheckers.size()); }, 10, 10, TimeUnit.MINUTES); } public static void sendReminder(ProxiedPlayer player) { - List schematics = getSchemsToCheck(); + List schematics = getSchemsToCheck(); if(schematics.size() != currentCheckers.size()) Message.send("CHECK_REMINDER", player, "CHECK_REMINDER_HOVER", new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/check list"), schematics.size() - currentCheckers.size()); } @@ -111,38 +111,38 @@ public class CheckCommand extends BasicCommand { } } - public static List getSchemsToCheck(){ - List schematicList = new LinkedList<>(); + public static List getSchemsToCheck(){ + List schematicList = new LinkedList<>(); for (SchematicType type : SchematicType.values()) { if (type.check()) - schematicList.addAll(Schematic.getAllSchemsOfType(type)); + schematicList.addAll(SchematicNode.getAllSchematicsOfType(type.toDB())); } return schematicList; } - public static String getChecker(Schematic schematic) { - if (currentSchems.get(schematic.getSchemID()) == null) return null; - return currentSchems.get(schematic.getSchemID()).checker.getName(); + public static String getChecker(SchematicNode schematic) { + if (currentSchems.get(schematic.getId()) == null) return null; + return currentSchems.get(schematic.getId()).checker.getName(); } private void list(ProxiedPlayer player) { - List schematicList = getSchemsToCheck(); + List schematicList = getSchemsToCheck(); Message.sendPrefixless("CHECK_LIST_HEADER", player, schematicList.size()); - for (Schematic schematic : schematicList) { - CheckSession current = currentSchems.get(schematic.getSchemID()); + for (SchematicNode schematic : schematicList) { + CheckSession current = currentSchems.get(schematic.getId()); if (current == null) { Message.sendPrefixless("CHECK_LIST_TO_CHECK", player, Message.parse("CHECK_LIST_TO_CHECK_HOVER", player), - new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/check schematic " + schematic.getSchemID()), - schematic.getSchemType().getKuerzel(), SteamwarUser.get(schematic.getSchemOwner()).getUserName(), schematic.getSchemName()); + new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/check schematic " + schematic.getId()), + schematic.getSchemtype().getKuerzel(), SteamwarUser.get(schematic.getOwner()).getUserName(), schematic.getName()); } else { Message.sendPrefixless("CHECK_LIST_CHECKING", player, Message.parse("CHECK_LIST_CHECKING_HOVER", player), new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/join " + current.checker.getName()), - schematic.getSchemType().getKuerzel(), SteamwarUser.get(schematic.getSchemOwner()).getUserName(), schematic.getSchemName(), current.checker.getName()); + schematic.getSchemtype().getKuerzel(), SteamwarUser.get(schematic.getOwner()).getUserName(), schematic.getName(), current.checker.getName()); } } } @@ -153,11 +153,11 @@ public class CheckCommand extends BasicCommand { return; } - Schematic schem = Schematic.getSchemFromDB(Integer.parseInt(schemID)); - if(!schem.getSchemType().check()){ + SchematicNode schem = SchematicNode.getSchematicNode(Integer.parseInt(schemID)); + if(!schem.getSchemtype().check()){ ProxyServer.getInstance().getLogger().log(Level.SEVERE, player.getName() + " tried to check an uncheckable schematic!"); return; - }else if(schem.getSchemOwner() == SteamwarUser.get(player.getUniqueId()).getId()) { + }else if(schem.getOwner() == SteamwarUser.get(player.getUniqueId()).getId()) { Message.send("CHECK_SCHEMATIC_OWN", player); return; } @@ -223,15 +223,15 @@ public class CheckCommand extends BasicCommand { private static class CheckSession{ private final ProxiedPlayer checker; - private final Schematic schematic; + private final SchematicNode schematic; private final Timestamp startTime; private final ListIterator checkList; - private CheckSession(ProxiedPlayer checker, Schematic schematic){ + private CheckSession(ProxiedPlayer checker, SchematicNode schematic){ this.checker = checker; this.schematic = schematic; this.startTime = Timestamp.from(Instant.now()); - this.checkList = checkQuestions.get(schematic.getSchemType()).listIterator(); + this.checkList = checkQuestions.get(schematic.getSchemtype()).listIterator(); ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { if(!BauCommand.stopBauserver(checker)){ @@ -239,12 +239,12 @@ public class CheckCommand extends BasicCommand { return; } - ArenaMode mode = ArenaMode.getBySchemType(schematic.getSchemType().fightType()); - SubserverSystem.startTestServer(checker, mode, FightCommand.getMap(checker, mode, "Random"), schematic.getSchemID(), 0); + ArenaMode mode = ArenaMode.getBySchemType(schematic.getSchemtype()); + SubserverSystem.startTestServer(checker, mode, FightCommand.getMap(checker, mode, "Random"), schematic.getId(), 0); currentCheckers.put(checker.getUniqueId(), this); - currentSchems.put(schematic.getSchemID(), this); - for(CheckedSchematic previous : CheckedSchematic.previousChecks(schematic.getSchemName(), schematic.getSchemOwner())) - Message.sendPrefixless("CHECK_SCHEMATIC_PREVIOUS", checker, previous.getEndTime(), SteamwarUser.get(previous.getValidator()).getUserName(), previous.getReason()); + currentSchems.put(schematic.getId(), this); + for(CheckedSchematic previous : CheckedSchematic.previousChecks(schematic)) + Message.sendPrefixless("CHECK_SCHEMATIC_PREVIOUS", checker, previous.getEndTime(), SteamwarUser.get(previous.getValidator()).getUserName(), previous.getDeclineReason()); next(0); }); } @@ -262,8 +262,8 @@ public class CheckCommand extends BasicCommand { if(checkList.hasNext()){ next.setText(Message.parse("CHECK_NEXT", checker)); next.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/check next")); - }else if(ranks.containsKey(schematic.getSchemType())){ - List r = ranks.get(schematic.getSchemType()); + }else if(ranks.containsKey(schematic.getSchemtype())){ + List r = ranks.get(schematic.getSchemtype()); for(int i = 0; i < r.size(); i++){ Message.sendPrefixless("CHECK_RANK", checker, Message.parse("CHECK_RANK_HOVER", checker), @@ -285,48 +285,49 @@ public class CheckCommand extends BasicCommand { } private void accept(int rank){ - if(ranks.containsKey(schematic.getSchemType())){ - if(rank <= 0 || ranks.get(schematic.getSchemType()).size() < rank){ + if(ranks.containsKey(schematic.getSchemtype())){ + if(rank <= 0 || ranks.get(schematic.getSchemtype()).size() < rank){ Message.send("CHECK_INVALID_RANK", checker); return; } schematic.setRank(rank); } - schematic.setSchemType(schematic.getSchemType().fightType()); - CheckedSchematic.create(schematic.getSchemName(), schematic.getSchemOwner(), SteamwarUser.get(checker.getUniqueId()).getId(), startTime, Timestamp.from(Instant.now()), "freigegeben"); - SteamwarUser user = SteamwarUser.get(schematic.getSchemOwner()); + schematic.setType(schematic.getSchemtype().fightType().toDB()); + 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) { - Message.send("CHECK_ACCEPTED", player, schematic.getSchemType().name(), schematic.getSchemName()); + Message.send("CHECK_ACCEPTED", player, schematic.getSchemtype().name(), schematic.getName()); } else { DiscordSchemAlert.sendAccept(schematic, user); } - Message.team("CHECK_ACCEPTED_TEAM", schematic.getSchemName(), user.getUserName()); + Message.team("CHECK_ACCEPTED_TEAM", schematic.getName(), user.getUserName()); stop(); } private void decline(String reason){ - schematic.setSchemType(SchematicType.Normal); - CheckedSchematic.create(schematic.getSchemName(), schematic.getSchemOwner(), SteamwarUser.get(checker.getUniqueId()).getId(), startTime, Timestamp.from(Instant.now()), reason); - SteamwarUser user = SteamwarUser.get(schematic.getSchemOwner()); + schematic.setType(SchematicType.Normal.toDB()); + 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) { - Message.send("CHECK_DECLINED", player, schematic.getSchemType().name(), schematic.getSchemName(), reason); + Message.send("CHECK_DECLINED", player, schematic.getSchemtype().name(), schematic.getName(), reason); } else { DiscordSchemAlert.sendDecline(schematic, user, reason); } - Message.team("CHECK_DECLINED_TEAM", schematic.getSchemName(), user.getUserName(), reason); + Message.team("CHECK_DECLINED_TEAM", schematic.getName(), user.getUserName(), reason); stop(); } private void abort(){ - CheckedSchematic.create(schematic.getSchemName(), schematic.getSchemOwner(), 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(){ - remove(); + currentCheckers.remove(checker.getUniqueId()); + 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())) { @@ -339,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/inventory/SWListInv.java b/src/de/steamwar/bungeecore/inventory/SWListInv.java index 1161260c..8636054d 100644 --- a/src/de/steamwar/bungeecore/inventory/SWListInv.java +++ b/src/de/steamwar/bungeecore/inventory/SWListInv.java @@ -19,8 +19,6 @@ package de.steamwar.bungeecore.inventory; -import de.steamwar.bungeecore.sql.Schematic; -import de.steamwar.bungeecore.sql.SchematicType; import net.md_5.bungee.BungeeCord; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -103,27 +101,6 @@ public class SWListInv extends SWInventory { return onlinePlayers; } - public static List> getSchemList(SchematicType type, int steamwarUserId){ - List> schemList = new ArrayList<>(); - - List schems; - if(type == null) - schems = Schematic.getSchemsAccessibleByUser(steamwarUserId); - else - schems = Schematic.getSchemsOfType(steamwarUserId, type); - - for(Schematic s : schems){ - String m; - if(s.getSchemItem().isEmpty()) - m = "CAULDRON_ITEM"; - else - m = s.getSchemItem(); - SWItem item = new SWItem(m, "§e" + s.getSchemName()); - schemList.add(new SWListEntry<>(item, s)); - } - return schemList; - } - public static class SWListEntry{ final SWItem item; final T object; diff --git a/src/de/steamwar/bungeecore/listeners/CheckListener.java b/src/de/steamwar/bungeecore/listeners/CheckListener.java index f4aec741..1370a0db 100644 --- a/src/de/steamwar/bungeecore/listeners/CheckListener.java +++ b/src/de/steamwar/bungeecore/listeners/CheckListener.java @@ -21,7 +21,7 @@ package de.steamwar.bungeecore.listeners; import de.steamwar.bungeecore.*; import de.steamwar.bungeecore.commands.CheckCommand; -import de.steamwar.bungeecore.sql.Schematic; +import de.steamwar.bungeecore.sql.SchematicNode; import de.steamwar.bungeecore.sql.SchematicType; import de.steamwar.bungeecore.sql.SteamwarUser; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -40,10 +40,10 @@ public class CheckListener extends BasicListener { ProxiedPlayer player = e.getPlayer(); SteamwarUser user = SteamwarUser.get(player.getUniqueId()); - List uncheckedSchematics = new LinkedList<>(); + List uncheckedSchematics = new LinkedList<>(); for(SchematicType type : SchematicType.values()){ if(type.check()) - uncheckedSchematics.addAll(Schematic.getSchemsOfType(user.getId(), type)); + uncheckedSchematics.addAll(SchematicNode.getAllSchematicsOfType(user.getId(), type.toDB())); } if(!uncheckedSchematics.isEmpty()) diff --git a/src/de/steamwar/bungeecore/sql/CheckedSchematic.java b/src/de/steamwar/bungeecore/sql/CheckedSchematic.java index 436d5ab3..baae8933 100644 --- a/src/de/steamwar/bungeecore/sql/CheckedSchematic.java +++ b/src/de/steamwar/bungeecore/sql/CheckedSchematic.java @@ -24,51 +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 { - schemName = rs.getString("SchemName"); - schemOwner = rs.getInt("SchemOwner"); - 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){ - create.update(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){ - return previous.select(rs -> { + 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 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 new file mode 100644 index 00000000..063dd487 --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/NodeMember.java @@ -0,0 +1,84 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + */ + +package de.steamwar.bungeecore.sql; + +import java.sql.ResultSet; +import java.sql.SQLException; +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) { + return getNodeMember.select(rs -> { + if(!rs.next()) + return null; + return new NodeMember(rs); + }, node, member); + } + + public static Set getNodeMembers(int node) { + return getNodeMembers.select(rs -> { + Set members = new HashSet<>(); + while (rs.next()) + members.add(new NodeMember(rs)); + return members; + }, node); + } + + public static Set getSchematics(int member) { + return getSchematics.select(rs -> { + Set members = new HashSet<>(); + while (rs.next()) + members.add(new NodeMember(rs)); + return members; + }, member); + } + + public static NodeMember createNodeMember(int node, int member) { + createNodeMember.update(node, member); + return getNodeMember(node, member); + } + + final int node; + final int member; + + private NodeMember(ResultSet set) throws SQLException { + node = set.getInt("NodeId"); + member = set.getInt("UserId"); + } + + public int getNode() { + return node; + } + + public int getMember() { + return member; + } + + public void delete() { + deleteNodeMember.update(node, member); + } +} diff --git a/src/de/steamwar/bungeecore/sql/Schematic.java b/src/de/steamwar/bungeecore/sql/Schematic.java deleted file mode 100644 index b7155100..00000000 --- a/src/de/steamwar/bungeecore/sql/Schematic.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2020 SteamWar.de-Serverteam - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -package de.steamwar.bungeecore.sql; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; - -public class Schematic { - - private static final Statement schemByName = new Statement("SELECT SchemID, SchemName, SchemOwner, SchemType, Item FROM Schematic WHERE SchemName = ? AND SchemOwner = ?"); - private static final Statement schemById = new Statement("SELECT SchemID, SchemName, SchemOwner, SchemType, Item FROM Schematic WHERE SchemID = ?"); - private static final Statement schemsByType = new Statement("SELECT SchemID, SchemName, SchemOwner, SchemType, Item FROM Schematic WHERE SchemType = ?"); - private static final Statement schemsByUserType = new Statement("SELECT SchemID, SchemName, SchemOwner, SchemType, Item FROM Schematic WHERE SchemType = ? AND SchemOwner = ?"); - private static final Statement schemsOfUser = new Statement("SELECT SchemID, SchemName, SchemOwner, Item, SchemType, Rank, SchemFormat, Item FROM Schematic WHERE SchemOwner = ? ORDER BY SchemName"); - private static final Statement updateType = new Statement("UPDATE Schematic SET SchemType = ? WHERE SchemID = ?"); - private static final Statement updateRank = new Statement("UPDATE Schematic SET Rank = ? WHERE SchemID = ?"); - - private final int schemID; - private final String schemName; - private final int schemOwner; - private SchematicType schemType; - private final String schemItem; - - private Schematic(ResultSet rs) throws SQLException { - this.schemID = rs.getInt("SchemID"); - this.schemName = rs.getString("SchemName"); - this.schemOwner = rs.getInt("SchemOwner"); - this.schemType = SchematicType.fromDB(rs.getString("SchemType")); - this.schemItem = rs.getString("Item"); - } - - public static Schematic getSchemFromDB(String schemName, int schemOwner){ - return schemByName.select(rs -> { - if(!rs.next()) - return new Schematic(rs); - return null; - }, schemName, schemOwner); - } - - public static Schematic getSchemFromDB(int schemID){ - return schemById.select(rs -> { - if(!rs.next()) - throw new SecurityException("Failed loading schematic " + schemID); - return new Schematic(rs); - }, schemID); - } - - public static List getAllSchemsOfType(SchematicType schemType){ - return schemsByType.select(rs -> { - List schematics = new ArrayList<>(); - while(rs.next()) - schematics.add(new Schematic(rs)); - return schematics; - }, schemType.toDB()); - } - - public static List getSchemsOfType(int userId, SchematicType schemType){ - return schemsByUserType.select(rs -> { - List schematics = new ArrayList<>(); - while(rs.next()) - schematics.add(new Schematic(rs)); - return schematics; - }, schemType.toDB(), userId); - } - - public static List getSchemsAccessibleByUser(int schemOwner){ - return schemsOfUser.select(rs -> { - List schematics = new ArrayList<>(); - while(rs.next()){ - schematics.add(new Schematic(rs)); - } - List addedSchems = SchematicMember.getAccessibleSchems(schemOwner); - for(SchematicMember schem : addedSchems){ - schematics.add(getSchemFromDB(schem.getSchemName(), schem.getSchemOwner())); - } - return schematics; - }, schemOwner); - } - - public int getSchemID() { - return schemID; - } - - public String getSchemName() { - return schemName; - } - - public int getSchemOwner() { - return schemOwner; - } - - public String getSchemItem() { - return schemItem; - } - - public SchematicType getSchemType() { - return schemType; - } - - public void setSchemType(SchematicType schemType) { - this.schemType = schemType; - updateType.update(schemType.toDB(), schemID); - } - - public void setRank(int rank) { - updateRank.update(rank, schemID); - } -} diff --git a/src/de/steamwar/bungeecore/sql/SchematicMember.java b/src/de/steamwar/bungeecore/sql/SchematicMember.java deleted file mode 100644 index c4ac21ac..00000000 --- a/src/de/steamwar/bungeecore/sql/SchematicMember.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2020 SteamWar.de-Serverteam - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -package de.steamwar.bungeecore.sql; - -import java.util.ArrayList; -import java.util.List; - -public class SchematicMember { - - private static final Statement insert = new Statement("INSERT INTO SchemMember (SchemName, SchemOwner, Member) VALUES (?, ?, ?)"); - private static final Statement selectSchems = new Statement("SELECT * FROM SchemMember WHERE Member = ?"); - private static final Statement delete = new Statement("DELETE FROM SchemMember WHERE SchemOwner = ? AND SchemName = ? AND Member = ?"); - - private final int schemOwner; - private final String schemName; - private final int member; - - private SchematicMember(String schemName, int schemOwner, int schemMember, boolean updateDB){ - this.schemOwner = schemOwner; - member = schemMember; - this.schemName = schemName; - if(updateDB) - updateDB(); - } - - private void updateDB(){ - insert.update(schemName, schemOwner, member); - } - - public static List getAccessibleSchems(int schemMember){ - return selectSchems.select(rs -> { - List schematicMembers = new ArrayList<>(); - while(rs.next()) - schematicMembers.add(new SchematicMember(rs.getString("SchemName"), rs.getInt("SchemOwner"), schemMember, false)); - return schematicMembers; - }, schemMember); - } - - public int getSchemOwner() { - return schemOwner; - } - - public String getSchemName() { - return schemName; - } - - public int getMember() { - return member; - } - - public void remove(){ - delete.update(schemOwner, schemName, member); - } -} diff --git a/src/de/steamwar/bungeecore/sql/SchematicNode.java b/src/de/steamwar/bungeecore/sql/SchematicNode.java new file mode 100644 index 00000000..83e9391f --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/SchematicNode.java @@ -0,0 +1,460 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bungeecore.sql; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.sql.Blob; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.time.Instant; +import java.util.*; +import java.util.function.Predicate; +import java.util.zip.GZIPInputStream; + +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 getDirsInNode_Null = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL AND NodeType is NULL"); + private static final Statement getDirsInNode = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ? AND NodeType is NULL"); + 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.NodeOwner = ? AND s.ParentNode IS NULL) OR NOT s.NodeOwner = ?) GROUP BY s.NodeId ORDER BY s.NodeName"); + private static final Statement getAccessibleByUserByTypeInNode = new Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (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 = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode = ?"); + private static final Statement getAccessibleByUserByTypeInNode_Null = new Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (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 = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode is null"); + private static final Statement getAccessibleByUserByType = new Statement("WITH RECURSIVE RSN as (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 = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ?"); + private static final Statement getAllSchematicsAccessibleByUser = new Statement("WITH RECURSIVE RSN as (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 = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN"); + private static final Statement isSchematicAccessibleForUser = new Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT COUNT(RSN.NodeId) AS `Accessible` FROM RSN LEFT Join NodeMember NM On NM.NodeId = RSN.NodeId WHERE NodeOwner = ? OR UserId = ? LIMIT 1"); + private static final Statement getAllParentsOfNode = new Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? UNION SELECT SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT * FROM RSN"); + 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 updateDatabase = new Statement("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?"); + private static final Statement selSchemData = new Statement("SELECT NodeData FROM SchematicNode 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(), ""); + } + + public static SchematicNode createSchematicDirectory(int owner, String name, Integer parent) { + return createSchematicNode(owner, name, parent, null, ""); + } + + public static SchematicNode createSchematicNode(int owner, String name, Integer parent, String type, String item) { + if (parent == 0) + parent = null; + createNode.update(name, owner, parent, type, item); + return getSchematicNode(owner, name, parent); + } + + private Timestamp lastUpdate; + + public static SchematicNode getSchematicNode(int owner, String name, SchematicNode parent) { + return getSchematicNode(owner, name, parent.getId()); + } + + private SchematicNode(ResultSet set) throws SQLException { + id = set.getInt("NodeId"); + owner = set.getInt("NodeOwner"); + name = set.getString("NodeName"); + parent = set.getInt("ParentNode"); + item = set.getString("NodeItem"); + type = set.getString("NodeType"); + lastUpdate = set.getTimestamp("LastUpdate"); + if (type != null) { + isDir = false; + rank = set.getInt("NodeRank"); + schemFormat = set.getBoolean("NodeFormat"); + } else { + isDir = true; + } + } + + public static List getSchematicNodeInNode(SchematicNode parent) { + return getSchematicNodeInNode(parent.getId()); + } + + public static SchematicNode getSchematicDirectory(String name, SchematicNode parent) { + return getSchematicDirectory(name, parent.getId()); + } + + public static SchematicNode getSchematicNode(int owner, String name, Integer parent) { + if (parent != null && parent == 0) { + parent = null; + } + Statement.ResultSetUser user = rs -> { + while (rs.next()) { + SchematicNode node = new SchematicNode(rs); + return node; + } + return null; + }; + 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; + Statement.ResultSetUser> user = rs -> { + List nodes = new ArrayList<>(); + while (rs.next()) + nodes.add(new SchematicNode(rs)); + return nodes; + }; + if(parent == null) { + return getSchematicsInNode_Null.select(user); + }else { + return getSchematicsInNode.select(user, parent); + } + } + + public static List getSchematicDirectoryInNode(Integer parent) { + if(parent != null && parent == 0) + parent = null; + Statement.ResultSetUser> user = rs -> { + List nodes = new ArrayList<>(); + while (rs.next()) + nodes.add(new SchematicNode(rs)); + return nodes; + }; + if(parent == null) { + return getDirsInNode_Null.select(user); + }else { + return getDirsInNode.select(user, parent); + } + } + + public static SchematicNode getSchematicDirectory(String name, Integer parent) { + if(parent != null && parent == 0) + parent = null; + Statement.ResultSetUser user = rs -> { + while (rs.next()) { + SchematicNode node = new SchematicNode(rs); + if(node.isDir()) + return node; + } + return null; + }; + + if(parent == null) { + return getSchematicDirectory_Null.select(user, name); + }else { + return getSchematicDirectory.select(user, name, parent); + } + } + + public static SchematicNode getSchematicNode(String name, Integer parent) { + if(parent != null && parent == 0) + parent = null; + Statement.ResultSetUser user = rs -> { + while (rs.next()) { + return new SchematicNode(rs); + } + return null; + }; + if(parent == null) { + return getSchematicNodeO_Null.select(user, name); + }else { + return getSchematicNodeO.select(user, name, parent); + } + } + + public static SchematicNode getSchematicNode(int id) { + return getSchematicNodeId.select(rs -> { + if (!rs.next()) + return null; + return new SchematicNode(rs); + }, id); + } + + public static List getAccessibleSchematicsOfTypeInParent(int owner, String schemType, Integer parent) { + Statement.ResultSetUser> user = rs -> { + List nodes = new ArrayList<>(); + while (rs.next()) { + nodes.add(new SchematicNode(rs)); + } + return nodes; + }; + if(parent == null || parent == 0) { + return getAccessibleByUserByTypeInNode_Null.select(user, owner, owner, schemType); + } else { + return getAccessibleByUserByTypeInNode.select(user, owner, owner, schemType, parent); + } + } + + public static List getAllAccessibleSchematicsOfType(int user, String schemType) { + return getAccessibleByUserByType.select(rs -> { + List nodes = new ArrayList<>(); + while (rs.next()) { + nodes.add(new SchematicNode(rs)); + } + return nodes; + }, user, user, schemType); + } + + public static List getAllSchematicsOfType(int owner, String schemType) { + return getAllSchemsOfTypeOwner.select(rs -> { + List nodes = new ArrayList<>(); + while (rs.next()) + nodes.add(new SchematicNode(rs)); + return nodes; + }, owner, schemType); + } + + public static List getAllSchematicsOfType(String schemType) { + return getAllSchemsOfType.select(rs -> { + List nodes = new ArrayList<>(); + while (rs.next()) + nodes.add(new SchematicNode(rs)); + return nodes; + }, schemType); + } + + public static List deepGet(Integer parent, Predicate filter) { + List finalList = new ArrayList<>(); + List nodes = SchematicNode.getSchematicNodeInNode(parent); + nodes.forEach(node -> { + if (node.isDir()) { + finalList.addAll(deepGet(node.getId(), filter)); + } else { + if (filter.test(node)) + finalList.add(node); + } + }); + return finalList; + } + + public static List getSchematicsAccessibleByUser(int user, Integer parent) { + if (parent != null && parent != 0) { + if(isSchematicAccessibleForUser.select(rs -> { + rs.next(); + return rs.getInt("Accessible") > 0; + }, parent, user, user)) + return getSchematicNodeInNode(parent); + } else { + return getAccessibleByUser.select(rs -> { + List nodes = new ArrayList<>(); + while(rs.next()) + nodes.add(new SchematicNode(rs)); + return nodes; + }, user, user, user, user); + } + return Collections.emptyList(); + } + + public static List getAllSchematicsAccessibleByUser(int user) { + return getAllSchematicsAccessibleByUser.select(rs -> { + List nodes = new ArrayList<>(); + while(rs.next()) { + nodes.add(new SchematicNode(rs)); + } + return nodes; + }, user, user); + } + + public static List getAllParentsOfNode(SchematicNode node) { + return getAllParentsOfNode(node.getId()); + } + + public static List getAllParentsOfNode(int node) { + return getAllParentsOfNode.select(rs -> { + List nodes = new ArrayList<>(); + while(rs.next()) { + nodes.add(new SchematicNode(rs)); + } + return nodes; + }, node); + } + + private final int id; + private final int owner; + private String name; + private Integer parent; + private String item; + private String type; + 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<>(); + List nodes = SchematicNode.getSchematicsAccessibleByUser(user, null); + nodes.forEach(node -> { + if (node.isDir()) { + finalList.addAll(deepGet(node.getId(), filter)); + } else { + if (filter.test(node)) + finalList.add(node); + } + }); + return finalList; + } + + public static Integer countNodes() { + return countNodes.select(rs -> { + if (rs.next()) { + return rs.getInt("count"); + } + return 0; + }); + } + + public int getId() { + return id; + } + + public int getOwner() { + return owner; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + updateDB(); + } + + public Integer getParent() { + return parent; + } + + public void setParent(Integer parent) { + this.parent = parent; + updateDB(); + } + + public String getItem() { + return item; + } + + public void setItem(String item) { + this.item = item; + updateDB(); + } + + public String getType() { + if(isDir) + throw new SecurityException("Node is Directory"); + return type; + } + + public void setType(String type) { + if(isDir) + throw new SecurityException("Node is Directory"); + this.type = type; + updateDB(); + } + + public boolean isDir() { + return isDir; + } + + public boolean getSchemFormat() { + if(isDir) + throw new SecurityException("Node is Directory"); + return schemFormat; + } + + public int getRank() { + if(isDir) + throw new SecurityException("Node is Directory"); + return rank; + } + + public void setRank(int rank) { + if(isDir) + throw new SecurityException("Node is Directory"); + this.rank = rank; + } + + public SchematicType getSchemtype() { + if(isDir()) + throw new RuntimeException("Is Directory"); + return SchematicType.fromDB(type); + } + + public SchematicNode getParentNode() { + return SchematicNode.getSchematicNode(parent); + } + + public boolean accessibleByUser(int user) { + return NodeMember.getNodeMember(id, user) != null; + } + + public Set getMembers() { + return NodeMember.getNodeMembers(id); + } + + public Timestamp getLastUpdate() { + return lastUpdate; + } + + public String generateBreadcrumbs(SteamwarUser user) { + return brCache.computeIfAbsent(user.getId(), integer -> generateBreadcrumbs("/", user)); + } + + public String generateBreadcrumbs(String split, SteamwarUser user) { + StringBuilder builder = new StringBuilder(getName()); + SchematicNode currentNode = this; + if (currentNode.isDir()) builder.append("/"); + while (currentNode.getParentNode() != null) { + currentNode = currentNode.getParentNode(); + builder.insert(0, split) + .insert(0, currentNode.getName()); + if (currentNode.getMembers().stream().anyMatch(member -> member.getMember() == user.getId())) { + break; + } + } + return builder.toString(); + } + + private void updateDB() { + updateDB.update(name, owner, parent, item, type, rank, id); + this.lastUpdate = Timestamp.from(Instant.now()); + this.brCache.clear(); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof SchematicNode)) + return false; + + SchematicNode node = (SchematicNode) obj; + return node.getId() == id; + } +}