diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java index 741f637..e7a5ccc 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java @@ -83,6 +83,24 @@ public class CheckedSchematic { return lastDeclined; } + public static List getLastDeclined(UUID uuid){ + List lastDeclined = new LinkedList<>(); + try{ + ResultSet lastRS = SQL.select("SELECT * FROM CheckedSchematic WHERE NodeId IN (SELECT NodeId FROM SchematicNode WHERE NodeOwner = ?) AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC", SteamwarUser.get(uuid).getId()); + while(lastRS.next()){ + int node = lastRS.getInt("NodeId"); + int validator = lastRS.getInt("Validator"); + Timestamp startTime = lastRS.getTimestamp("StartTime"); + Timestamp endTime = lastRS.getTimestamp("EndTime"); + String declineReason = lastRS.getString("DeclineReason"); + lastDeclined.add(new CheckedSchematic(node, validator, startTime, endTime, declineReason, false)); + } + }catch(SQLException e){ + Bukkit.getLogger().log(Level.SEVERE, "getLastDeclined failed", e); + } + return lastDeclined; + } + public void remove() { SQL.update("DELETE FROM CheckedSchematic WHERE NodeId", node); } diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java index 038e574..1439379 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -48,7 +48,10 @@ public class Schematic { } public static Schematic getSchemFromDB(String schemName, int schemOwner){ - return new Schematic(SchematicNode.getSchematicNode(schemOwner, schemName, 0)); + SchematicNode node = SchematicNode.getSchematicNode(schemOwner, schemName, (Integer) null); + if(node == null) + return null; + return new Schematic(node); } public static Schematic getSchemFromDB(int schemID){ @@ -62,7 +65,9 @@ public class Schematic { public static List getSchemsAccessibleByUser(int schemOwner){ List nodes = SchematicNode.getSchematicsAccessibleByUser(schemOwner, 0); List schematics = new ArrayList<>(); - nodes.forEach(node1 -> schematics.add(new Schematic(node1))); + nodes.forEach(node1 -> { + if (!node1.isDir()) schematics.add(new Schematic(node1)); + }); return schematics; } diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index a9f63fd..a168cdc 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -33,39 +33,42 @@ import java.util.*; public class SchematicNode { - public static SchematicNode createSchematic(int owner, String name, int parent) { + 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, int parent) { + public static SchematicNode createSchematicDirectory(int owner, String name, Integer parent) { return createSchematicNode(owner, name, parent, null, ""); } - public static SchematicNode createSchematicNode(int owner, String name, int parent, String type, String item) { + 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); - return getSchematicNode(owner, name, type, parent); + return getSchematicNode(owner, name, parent); } - public static SchematicNode getSchematicNode(int owner, String name, String type, int parent) { - ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ? AND NodeType = ?", owner, name, parent, type); - try { - if(!set.next()) - return null; - return new SchematicNode(set); - }catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); + public static SchematicNode getSchematicNode(int owner, String name, Integer parent) { + if(parent != null && parent == 0) + parent = null; + ResultSet set; + if(parent == null) { + set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode is NULL", owner, name); + }else { + set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?", owner, name, parent); } - } - - public static SchematicNode getSchematicNode(int owner, String name, int parent) { - ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?", owner, name, parent); try { while (set.next()) { SchematicNode node = new SchematicNode(set); if(!node.isDir()) return node; } + List nodes = getSchematicNodeInNode(parent); + for (SchematicNode node:nodes) { + if(!node.isDir() && 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); @@ -76,8 +79,15 @@ public class SchematicNode { return getSchematicNode(owner, name, parent.getId()); } - public static List getSchematicNodeInNode(int parent) { - ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE ParentNode = ?", parent); + public static List getSchematicNodeInNode(Integer parent) { + if(parent != null && parent == 0) + parent = null; + ResultSet set; + if(parent == null) { + set = SQL.select("SELECT * FROM SchematicNode WHERE ParentNode is NULL"); + }else { + set = SQL.select("SELECT * FROM SchematicNode WHERE ParentNode = ?", parent); + } try { List nodes = new ArrayList<>(); while (set.next()) @@ -92,12 +102,19 @@ public class SchematicNode { return getSchematicNodeInNode(parent.getId()); } - public static SchematicNode getSchematicDirectory(int owner, String name, SchematicNode parent) { - return getSchematicDirectory(owner, name, parent.getId()); + public static SchematicNode getSchematicDirectory(String name, SchematicNode parent) { + return getSchematicDirectory(name, parent.getId()); } - public static SchematicNode getSchematicDirectory(int owner, String name, int parent) { - ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?", owner, name, 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 * FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name, parent); + }else { + set = SQL.select("SELECT * FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); + } try { while (set.next()) { SchematicNode node = new SchematicNode(set); @@ -110,8 +127,15 @@ public class SchematicNode { } } - public static SchematicNode getSchematicInParent(String name, int parent) { - ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); + public static SchematicNode getSchematicInParent(String name, Integer parent) { + if(parent != null && parent == 0) + parent = null; + ResultSet set; + if(parent == null) { + set = SQL.select("SELECT * FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name, parent); + }else { + set = SQL.select("SELECT * FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); + } try { while (set.next()) { SchematicNode node = new SchematicNode(set); @@ -159,7 +183,9 @@ public class SchematicNode { } } - public static List getSchematicsOfType(int owner, String schemType, int parent) { + public static List getSchematicsOfType(int owner, String schemType, Integer parent) { + if(parent != null && parent == 0) + parent = null; List schems = getAllSchematicsOfType(owner, schemType); Map nodesInParent = new LinkedHashMap<>(); for (SchematicNode schematicNode : schems) { @@ -172,8 +198,8 @@ public class SchematicNode { return new ArrayList<>(nodesInParent.values()); } - public static List getSchematicsAccessibleByUser(int user, int parent) { - if(parent != 0) { + public static List getSchematicsAccessibleByUser(int user, Integer parent) { + if(parent != null && parent != 0) { SchematicNode node = SchematicNode.getSchematicNode(parent); boolean isAdded = false; while (node.getId() != 0) { @@ -187,16 +213,18 @@ public class SchematicNode { } if(isAdded) return getSchematicNodeInNode(parent); + } else { + ResultSet set = SQL.select("SELECT * 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{ + List nodes = new ArrayList<>(); + while(set.next()) + nodes.add(new SchematicNode(set)); + return nodes; + }catch(SQLException e){ + throw new SecurityException("Failed listing schematics", e); + } } - ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode FROM SchematicNode s LEFT JOIN NodeMember nm ON nm.NodeId = s.NodeId WHERE ( s.NodeOwner = ? OR nm.UserId = ? ) AND s.ParentNode = ? GROUP BY s.NodeId ORDER BY s.NodeName", user, user, parent); - try{ - List nodes = new ArrayList<>(); - while(set.next()) - nodes.add(new SchematicNode(set)); - return nodes; - }catch(SQLException e){ - throw new SecurityException("Failed listing schematics", e); - } + return null; } public static List getAllSchematicsAccessibleByUser(int user) { @@ -231,7 +259,7 @@ public class SchematicNode { if(type != null) { isDir = false; rank = set.getInt("NodeRank"); - schemFormat = set.getBoolean("SchemFormat"); + schemFormat = set.getBoolean("NodeFormat"); }else { isDir = true; } @@ -417,7 +445,7 @@ public class SchematicNode { } private void updateDatabase(Blob blob, boolean newFormat) { - SQL.update("UPDATE SchematicNode SET NodeData = ?, SchemFormat = ? WHERE NodeId = ?", blob, newFormat, id); + SQL.update("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?", blob, newFormat, id); schemFormat = newFormat; } }