SteamWar/SpigotCore
Archiviert
13
0

Add Basic SchematicNode System

Dieser Commit ist enthalten in:
Chaoscaot 2021-01-25 23:58:11 +01:00
Ursprung d67a8657b3
Commit 5d96c5a084
7 geänderte Dateien mit 482 neuen und 408 gelöschten Zeilen

Datei anzeigen

@ -19,7 +19,7 @@
package de.steamwar.inventory;
import de.steamwar.sql.Schematic;
import de.steamwar.sql.SchematicNode;
import de.steamwar.sql.SchematicType;
import org.bukkit.Bukkit;
import org.bukkit.Material;
@ -115,22 +115,23 @@ public class SWListInv<T> extends SWInventory {
return onlinePlayers;
}
public static List<SWListEntry<Schematic>> getSchemList(SchematicType type, int steamwarUserId){
List<SWListEntry<Schematic>> schemList = new ArrayList<>();
public static List<SWListEntry<SchematicNode>> getSchemList(SchematicType type, int steamwarUserId){
List<SWListEntry<SchematicNode>> schemList = new ArrayList<>();
List<Schematic> schems;
List<SchematicNode> schems;
if(type == null)
schems = Schematic.getSchemsAccessibleByUser(steamwarUserId);
schems = SchematicNode.getSchematicsAccessibleByUser(steamwarUserId, 0);
else
schems = Schematic.getSchemsOfType(steamwarUserId, type);
schems = SchematicNode.getSchematicsOfType(steamwarUserId, type.toDB(), 0);
for(Schematic s : schems){
for(SchematicNode s : schems){
Material m;
if(s.getItem().isEmpty())
m = SWItem.getMaterial("CAULDRON_ITEM");
else
m = SWItem.getMaterial(s.getItem());
SWItem item = new SWItem(m,"§e" + s.getSchemName());
SWItem item = new SWItem(m,"§e" + s.getName());
item.setEnchanted(s.isDir());
schemList.add(new SWListEntry<>(item, s));
}
return schemList;

Datei anzeigen

@ -30,7 +30,9 @@ public class DownloadSchematic {
private static final String BASE = "https://steamwar.de/download.php?schem=";
public static String getLink(Schematic schem){
public static String getLink(SchematicNode schem){
if(schem.isDir())
throw new SecurityException("Can not Download Directorys");
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-1");
@ -38,9 +40,9 @@ public class DownloadSchematic {
throw new SecurityException(e);
}
digest.reset();
digest.update((Instant.now().toString() + schem.getSchemOwner() + schem.getSchemID()).getBytes());
digest.update((Instant.now().toString() + schem.getOwner() + schem.getId()).getBytes());
String hash = BaseEncoding.base16().encode(digest.digest());
SQL.update("INSERT INTO SchemDownload (SchemID, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)", schem.getSchemID(), hash);
SQL.update("INSERT INTO SchemDownload (SchemID, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)", schem.getId(), hash);
return BASE + hash;
}
}

Datei anzeigen

@ -0,0 +1,157 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.sql;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import de.steamwar.core.VersionedCallable;
import de.steamwar.core.VersionedRunnable;
import org.bukkit.entity.Player;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;
public class NodeData {
public static NodeData getSchemdata(int node) {
ResultSet set = SQL.select("SELECT NodeId, SchemFormat, NodeRank FROM NodeData WHERE NodeId = ?", node);
try {
if(!set.next())
return null;
return new NodeData(set);
} catch (SQLException e) {
throw new SecurityException("Could not load NodeData", e);
}
}
public static NodeData createNodeDate(SchematicNode node) {
SQL.update("INSERT INTO NodeData (NodeId) VALUES(?)", node.getId());
return getSchemdata(node.getId());
}
private final int node;
private boolean schemFormat;
private int rank;
private NodeData(ResultSet set) throws SQLException {
node = set.getInt("NodeId");
schemFormat = set.getBoolean("SchemFormat");
rank = set.getInt("NodeRank");
}
public Clipboard load() throws IOException, NoClipboardException {
ResultSet rs = SQL.select("SELECT SchemData FROM NodeData WHERE NodeId = ?", node);
try {
rs.next();
Blob schemData = rs.getBlob("SchemData");
if(schemData == null)
throw new IOException("SchemData is null");
InputStream is = schemData.getBinaryStream();
return VersionedCallable.call(new VersionedCallable<>(() -> Schematic_8.getClipboard(is, schemFormat), 8),
new VersionedCallable<>(() -> Schematic_14.getClipboard(is, schemFormat), 14));
} catch (SQLException e) {
throw new IOException(e);
}
}
public void loadToPlayer(Player player) throws IOException, NoClipboardException {
ResultSet rs = SQL.select("SELECT SchemData FROM NodeData WHERE NodeId = ?", node);
try {
rs.next();
Blob blob = rs.getBlob("SchemData");
if(blob == null)
throw new NoClipboardException();
InputStream is = blob.getBinaryStream();
VersionedRunnable.call(new VersionedRunnable(() -> Schematic_8.setPlayerClipboard(player, is, schemFormat), 8),
new VersionedRunnable(() -> Schematic_14.setPlayerClipboard(player, is, schemFormat), 14));
} catch (SQLException e) {
throw new IOException(e);
}
}
public void saveOldFormatFromPlayer(Player player) throws IOException, NoClipboardException {
saveFromPlayer(player, false);
}
public void saveFromPlayer(Player player) throws IOException, NoClipboardException {
saveFromPlayer(player, true);
}
public void saveFromBytes(byte[] bytes, boolean newFormat) {
Blob blob = SQL.blob();
try {
blob.setBytes(1, bytes);
updateDatabase(blob, newFormat);
} catch (SQLException e) {
throw new SecurityException(e);
}
}
private void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException {
Blob blob = SQL.blob();
VersionedRunnable.call(new VersionedRunnable(() -> {
try {
blob.setBytes(1, Schematic_8.getPlayerClipboard(player));
} catch (SQLException e) {
throw new RuntimeException(e.getMessage(), e);
}
updateDatabase(blob, false);
}, 8), new VersionedRunnable(() -> {
try {
blob.setBytes(1, Schematic_14.getPlayerClipboard(player, newFormat));
} catch (SQLException exception) {
throw new RuntimeException(exception.getMessage(), exception);
}
updateDatabase(blob, newFormat);
}, 14));
}
private void updateDatabase(Blob blob, boolean newFormat) {
SQL.update("UPDATE NodeData SET SchemData = ?, SchemFormat = ? WHERE NodeId = ?", blob, newFormat, node);
schemFormat = newFormat;
}
public int getNode() {
return node;
}
public boolean isSchemFormat() {
return schemFormat;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
updateDB();
}
private void updateDB() {
SQL.update("UPDATE NodeData SET Rank = ? WHERE NodeId = ?", rank, node);
}
public void delete() {
SQL.update("DELETE FROM NodeData WHERE NodeId = ?", node);
}
}

Datei anzeigen

@ -0,0 +1,83 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.sql;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
public class NodeMember {
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 null;
return new NodeMember(set);
} catch (SQLException e) {
throw new SecurityException("Could not load NodeMember", e);
}
}
public static Set<NodeMember> getNodeMembers(int node) {
ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE NodeId = ?", node);
try {
Set<NodeMember> members = new HashSet<>();
while (set.next())
members.add(new NodeMember(set));
return members;
} catch (SQLException e) {
throw new SecurityException("Could not load NodeMember", e);
}
}
public static Set<NodeMember> getSchematics(int member) {
ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE UserId = ?", member);
try {
Set<NodeMember> members = new HashSet<>();
while (set.next())
members.add(new NodeMember(set));
return members;
} catch (SQLException e) {
throw new SecurityException("Could not load NodeMember", e);
}
}
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() {
SQL.update("DELETE FROM NodeMember WHERE NodeId = ? AND UserId = ?", node, member);
}
}

Datei anzeigen

@ -1,263 +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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.sql;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import de.steamwar.core.VersionedCallable;
import de.steamwar.core.VersionedRunnable;
import org.bukkit.entity.Player;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class Schematic {
private final int schemID;
private final String schemName;
private final int schemOwner;
private boolean schemFormat;
private String item;
private int rank;
private SchematicType schemType;
private Schematic(ResultSet rs) throws SQLException {
this.schemID = rs.getInt("SchemID");
this.schemName = rs.getString("SchemName");
this.schemOwner = rs.getInt("SchemOwner");
this.item = rs.getString("Item");
this.rank = rs.getInt("Rank");
this.schemType = SchematicType.fromDB(rs.getString("SchemType"));
this.schemFormat = rs.getBoolean("SchemFormat");
}
private void updateDB(){
createSchem(schemName, schemOwner, item, schemType);
}
public static void createSchem(String schemName, UUID schemOwner, String item, SchematicType schemType){
createSchem(schemName, SteamwarUser.get(schemOwner).getId(), item, schemType);
}
public static void createSchem(String schemName, int schemOwner, String item, SchematicType schemType){
SQL.update("INSERT INTO Schematic (SchemName, SchemOwner, Item, SchemType) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE Item = VALUES(Item), SchemType = VALUES(SchemType)",
schemName, schemOwner, item, schemType.toDB());
}
public static Schematic getSchemFromDB(String schemName, UUID schemOwner){
return getSchemFromDB(schemName, SteamwarUser.get(schemOwner).getId());
}
public static Schematic getSchemFromDB(String schemName, int schemOwner){
ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, Item, SchemType, Rank, SchemFormat FROM Schematic WHERE SchemName = ? AND SchemOwner = ?", schemName, schemOwner);
try {
if(schematic == null || !schematic.next()){
SchematicMember member = SchematicMember.getMemberBySchematic(schemName, schemOwner);
if(member == null){
return null;
}
return getSchemFromDB(schemName, member.getSchemOwner());
}
return new Schematic(schematic);
} catch (SQLException e) {
throw new SecurityException("Failed loading schematic", e);
}
}
public static Schematic getSchemFromDB(int schemID){
ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, Item, SchemType, Rank, SchemFormat FROM Schematic WHERE SchemID = ?", schemID);
try {
if(!schematic.next())
throw new SecurityException("Failed loading schematic " + schemID);
return new Schematic(schematic);
} catch (SQLException e) {
throw new SecurityException("Failed loading schematic", e);
}
}
public static List<Schematic> getSchemsAccessibleByUser(UUID schemOwner){
return getSchemsAccessibleByUser(SteamwarUser.get(schemOwner).getId());
}
public static List<Schematic> getSchemsAccessibleByUser(int schemOwner){
try{
ResultSet schematic = SQL.select("SELECT s.SchemID, s.SchemName, s.SchemOwner, s.Item, s.SchemType, s.Rank, s.SchemFormat FROM Schematic s LEFT JOIN SchemMember sm ON sm.SchemName = s.SchemName AND sm.SchemOwner = s.SchemOwner WHERE s.SchemOwner = ? OR sm.Member = ? GROUP BY s.SchemID ORDER BY s.SchemName", schemOwner, schemOwner);
List<Schematic> schematics = new ArrayList<>();
while(schematic.next())
schematics.add(new Schematic(schematic));
return schematics;
}catch(SQLException e){
throw new SecurityException("Failed listing schematics", e);
}
}
public static List<Schematic> getSchemsOfType(UUID schemOwner, SchematicType schemType){
return getSchemsOfType(SteamwarUser.get(schemOwner).getId(), schemType);
}
public static List<Schematic> getSchemsOfType(int schemOwner, SchematicType schemType){
//Unsauber, dafür auch geaddede Schematics dabei
List<Schematic> schems = getSchemsAccessibleByUser(schemOwner);
for(int i = schems.size()-1; i >= 0; i--)
if(!schems.get(i).getSchemType().equals(schemType))
schems.remove(i);
return schems;
}
public static List<Schematic> getAllSchemsOfType(SchematicType schemType){
try{
ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, Item, SchemType, Rank, SchemFormat FROM Schematic WHERE SchemType = ?", schemType.toDB());
List<Schematic> schematics = new ArrayList<>();
while(schematic.next()){
schematics.add(new Schematic(schematic));
}
return schematics;
}catch(SQLException e){
throw new SecurityException("Failed loading all schems of type", e);
}
}
public int getSchemID() {
return schemID;
}
public String getSchemName() {
return schemName;
}
public int getSchemOwner() {
return schemOwner;
}
public int getRank(){
return rank;
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
updateDB();
}
public void setRank(int rank){
this.rank = rank;
SQL.update("UPDATE Schematic SET Rank = ? WHERE SchemID = ?", rank, schemID);
}
public SchematicType getSchemType() {
return schemType;
}
public void setSchemType(SchematicType schemType) {
this.schemType = schemType;
updateDB();
}
public boolean availible(){
return true;
}
public Clipboard load() throws IOException, NoClipboardException {
ResultSet rs = SQL.select("SELECT SchemData FROM Schematic WHERE SchemID = ?", schemID);
try {
rs.next();
Blob schemData = rs.getBlob("SchemData");
if(schemData == null)
throw new IOException("SchemData is null");
InputStream is = schemData.getBinaryStream();
return VersionedCallable.call(new VersionedCallable<>(() -> Schematic_8.getClipboard(is, schemFormat), 8),
new VersionedCallable<>(() -> Schematic_14.getClipboard(is, schemFormat), 14));
} catch (SQLException e) {
throw new IOException(e);
}
}
public void loadToPlayer(Player player) throws IOException, NoClipboardException {
ResultSet rs = SQL.select("SELECT SchemData FROM Schematic WHERE SchemID = ?", schemID);
try {
rs.next();
Blob blob = rs.getBlob("SchemData");
if(blob == null)
throw new NoClipboardException();
InputStream is = blob.getBinaryStream();
VersionedRunnable.call(new VersionedRunnable(() -> Schematic_8.setPlayerClipboard(player, is, schemFormat), 8),
new VersionedRunnable(() -> Schematic_14.setPlayerClipboard(player, is, schemFormat), 14));
} catch (SQLException e) {
throw new IOException(e);
}
}
public void saveOldFormatFromPlayer(Player player) throws IOException, NoClipboardException {
saveFromPlayer(player, false);
}
public void saveFromPlayer(Player player) throws IOException, NoClipboardException {
saveFromPlayer(player, true);
}
public void saveFromBytes(byte[] bytes, boolean newFormat) {
Blob blob = SQL.blob();
try {
blob.setBytes(1, bytes);
updateDatabase(blob, newFormat);
} catch (SQLException e) {
throw new SecurityException(e);
}
}
private void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException {
Blob blob = SQL.blob();
VersionedRunnable.call(new VersionedRunnable(() -> {
try {
blob.setBytes(1, Schematic_8.getPlayerClipboard(player));
} catch (SQLException e) {
throw new RuntimeException(e.getMessage(), e);
}
updateDatabase(blob, false);
}, 8), new VersionedRunnable(() -> {
try {
blob.setBytes(1, Schematic_14.getPlayerClipboard(player, newFormat));
} catch (SQLException exception) {
throw new RuntimeException(exception.getMessage(), exception);
}
updateDatabase(blob, newFormat);
}, 14));
}
private void updateDatabase(Blob blob, boolean newFormat) {
SQL.update("UPDATE Schematic SET SchemData = ?, SchemFormat = ? WHERE SchemID = ?", blob, newFormat, schemID);
schemFormat = newFormat;
}
public void remove(){
SQL.update("DELETE FROM SchemMember WHERE SchemOwner = ? AND SchemName = ?", schemOwner, schemName);
SQL.update("DELETE FROM Schematic WHERE SchemOwner = ? AND SchemName = ?", schemOwner, schemName);
}
public static class WrongVersionException extends Exception{}
}

Datei anzeigen

@ -1,134 +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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.sql;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class SchematicMember {
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();
}
public SchematicMember(String schemName, int schemOwner, int schemMember){
this(schemName, schemOwner, schemMember, true);
}
public SchematicMember(String schemName, UUID schemOwner, UUID schemMember){
this(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(schemMember).getId(), true);
}
private void updateDB(){
SQL.update("INSERT INTO SchemMember (SchemName, SchemOwner, Member) VALUES (?, ?, ?)", schemName, schemOwner, member);
}
public static SchematicMember getSchemMemberFromDB(String schemName, UUID schemOwner, UUID schemMember){
return getSchemMemberFromDB(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(schemMember).getId());
}
public static SchematicMember getSchemMemberFromDB(String schemName, int schemOwner, int schemMember){
ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND SchemOwner = ? AND Member = ?", schemName, schemOwner, schemMember);
try {
if(schematicMember == null || !schematicMember.next()){
return null;
}
return new SchematicMember(schemName, schemOwner, schemMember, false);
} catch (SQLException e) {
throw new SecurityException("Could not get schemmember", e);
}
}
public static SchematicMember getMemberBySchematic(String schemName, int schemMember){
ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND Member = ?", schemName, schemMember);
try {
if(schematicMember == null || !schematicMember.next()){
return null;
}
int schemOwner = schematicMember.getInt("SchemOwner");
return new SchematicMember(schemName, schemOwner, schemMember, false);
} catch (SQLException e) {
throw new SecurityException("Could not get member", e);
}
}
public static List<SchematicMember> getSchemMembers(String schemName, UUID schemOwner){
return getSchemMembers(schemName, SteamwarUser.get(schemOwner).getId());
}
public static List<SchematicMember> getSchemMembers(String schemName, int schemOwner){
ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND SchemOwner = ?", schemName, schemOwner);
try {
List<SchematicMember> schematicMembers = new ArrayList<>();
while(schematicMember.next()){
int schemMember = schematicMember.getInt("Member");
schematicMembers.add(new SchematicMember(schemName, schemOwner, schemMember, false));
}
return schematicMembers;
} catch (SQLException e) {
throw new SecurityException("Could not get schemmembers", e);
}
}
public static List<SchematicMember> getAccessibleSchems(UUID schemMember){
return getAccessibleSchems(SteamwarUser.get(schemMember).getId());
}
public static List<SchematicMember> getAccessibleSchems(int schemMember){
ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE Member = ?", schemMember);
try {
List<SchematicMember> schematicMembers = new ArrayList<>();
while(schematicMember.next()){
String schemName = schematicMember.getString("SchemName");
int schemOwner = schematicMember.getInt("SchemOwner");
schematicMembers.add(new SchematicMember(schemName, schemOwner, schemMember, false));
}
return schematicMembers;
} catch (SQLException e) {
throw new SecurityException("Could not get accessible schems", e);
}
}
public int getSchemOwner() {
return schemOwner;
}
public String getSchemName() {
return schemName;
}
public int getMember() {
return member;
}
public void remove(){
SQL.update("DELETE FROM SchemMember WHERE SchemOwner = ? AND SchemName = ? AND Member = ?", schemOwner, schemName, member);
}
}

Datei anzeigen

@ -0,0 +1,228 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.sql;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
public class SchematicNode {
private static final String DIR_TYPE = "directory";
public static final int ROOT_DIR = 0;
public static SchematicNode createSchematic(int owner, String name, int parent) {
return createSchematicNode(owner, name, parent, SchematicType.Normal.toDB());
}
public static SchematicNode createSchematicDirectory(int owner, String name, int parent) {
return createSchematicNode(owner, name, parent, DIR_TYPE);
}
private static SchematicNode createSchematicNode(int owner, String name, int parent, String type) {
SQL.update("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeType) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUE(NodeName), ParentNode = VALUE(ParentNode), NodeItem = VALUE(NodeItem), NodeType = VALUE(NodeType)",
name, owner, parent, type);
return getSchematicNode(owner, name, type, 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 NodeType = ? AND ParentNode = ?", owner, name, type, parent);
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, String type, SchematicNode parent) {
return getSchematicNode(owner, name, type, parent.getId());
}
public static List<SchematicNode> getSchematicNodeInNode(int owner, int parent) {
ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE ParentNode = ? AND NodeOwner = ?", parent, owner);
try {
List<SchematicNode> nodes = new ArrayList<>();
while (set.next())
nodes.add(new SchematicNode(set));
return nodes;
}catch (SQLException e) {
throw new SecurityException("Failed to load Schemnodes", e);
}
}
public static List<SchematicNode> getSchematicNodeInNode(int owner, SchematicNode parent) {
return getSchematicNodeInNode(owner, parent.getId());
}
public static SchematicNode getSchematicDirectory(int owner, String name, SchematicNode parent) {
return getSchematicNode(owner, name, DIR_TYPE, parent);
}
public static SchematicNode getSchematicDirectory(int owner, String name, int parent) {
return getSchematicNode(owner, name, DIR_TYPE, parent);
}
public static SchematicNode getSchematicNode(int id) {
ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeId", id);
try {
if(!set.next())
return null;
return new SchematicNode(set);
}catch (SQLException e) {
throw new SecurityException("Failed to load Schemnodes", e);
}
}
public static List<SchematicNode> getAllSchematicsOfType(int owner, String schemType) {
ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND SchemType = ?", owner, schemType);
try {
List<SchematicNode> nodes = new ArrayList<>();
while (set.next())
nodes.add(new SchematicNode(set));
return nodes;
}catch (SQLException e) {
throw new SecurityException("Failed to load Schemnodes", e);
}
}
public static List<SchematicNode> getSchematicsOfType(int owner, String schemType, int parent) {
List<SchematicNode> schems = getAllSchematicsOfType(owner, schemType);
Map<Integer, SchematicNode> nodesInParent = new LinkedHashMap<>();
for (SchematicNode schematicNode : schems) {
SchematicNode currentNode = schematicNode;
while (currentNode.getParent() != parent) {
currentNode = currentNode.getParentNode();
}
nodesInParent.putIfAbsent(currentNode.getId(), currentNode);
}
return new ArrayList<>(nodesInParent.values());
}
public static List<SchematicNode> getSchematicsAccessibleByUser(int user, int parent) {
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<SchematicNode> nodes = new ArrayList<>();
while(set.next())
nodes.add(new SchematicNode(set));
return nodes;
}catch(SQLException e){
throw new SecurityException("Failed listing schematics", e);
}
}
private final int id;
private final int owner;
private String name;
private int parent;
private String item;
private String type;
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");
}
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 int getParent() {
return parent;
}
public void setParent(int parent) {
this.parent = parent;
updateDB();
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
updateDB();
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
updateDB();
}
public boolean isDir() {
return type.equals(DIR_TYPE);
}
public SchematicType getSchemtype() {
if(!isDir())
throw new RuntimeException("Is Directory");
return SchematicType.fromDB(type);
}
public SchematicNode getParentNode() {
return SchematicNode.getSchematicNode(parent);
}
public NodeData getNodeData() {
if(isDir())
throw new SecurityException("Could not get NodeData of Directory");
return NodeData.getSchemdata(id);
}
public boolean accessibleByUser(int user) {
return NodeMember.getNodeMember(id, user) != null;
}
public Set<NodeMember> getMembers() {
return NodeMember.getNodeMembers(id);
}
private void updateDB() {
SQL.update("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeItem, NodeType) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUE(NodeName), ParentNode = VALUE(ParentNode), NodeItem = VALUE(NodeItem), NodeType = VALUE(NodeType)",
name, owner, parent, item, type);
}
public void delete() {
SQL.update("DELETE FROM SchematicNode WHERE NodeId = ?", id);
}
}