2023-08-02 18:57:10 +02:00
package com.craftaro.ultimatemoderation.database ;
2020-08-05 00:20:25 +02:00
2023-08-02 18:57:10 +02:00
import com.craftaro.core.database.DataManager ;
2023-07-20 17:41:31 +02:00
import com.craftaro.core.database.DatabaseConnector ;
2023-08-02 18:57:10 +02:00
import com.craftaro.ultimatemoderation.punish.AppliedPunishment ;
import com.craftaro.ultimatemoderation.punish.PunishmentNote ;
import com.craftaro.ultimatemoderation.punish.PunishmentType ;
import com.craftaro.ultimatemoderation.punish.template.Template ;
import com.craftaro.ultimatemoderation.tickets.Ticket ;
import com.craftaro.ultimatemoderation.tickets.TicketResponse ;
import com.craftaro.ultimatemoderation.tickets.TicketStatus ;
2020-08-05 00:20:25 +02:00
import org.bukkit.Bukkit ;
import org.bukkit.Location ;
import org.bukkit.plugin.Plugin ;
2020-09-08 22:29:25 +02:00
2022-08-14 11:53:38 +02:00
import java.sql.Connection ;
2020-08-05 00:20:25 +02:00
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;
import java.sql.Statement ;
2020-09-08 22:29:25 +02:00
import java.util.ArrayList ;
import java.util.List ;
import java.util.Map ;
import java.util.TreeMap ;
import java.util.UUID ;
2020-08-05 00:20:25 +02:00
import java.util.function.Consumer ;
2023-08-02 18:57:10 +02:00
public class DataHelper {
private final DatabaseConnector databaseConnector ;
private final DataManager dataManager ;
private final Plugin plugin ;
public DataHelper ( DataManager dataManager , Plugin plugin ) {
this . dataManager = dataManager ;
this . databaseConnector = dataManager . getDatabaseConnector ( ) ;
this . plugin = plugin ;
}
private void runAsync ( Runnable runnable ) {
2023-08-15 23:24:07 +02:00
this . dataManager . getAsyncPool ( ) . execute ( runnable ) ;
2023-08-02 18:57:10 +02:00
}
private void sync ( Runnable runnable ) {
2023-08-15 23:24:07 +02:00
Bukkit . getScheduler ( ) . runTask ( this . plugin , runnable ) ;
2023-08-02 18:57:10 +02:00
}
private String getTablePrefix ( ) {
return this . dataManager . getTablePrefix ( ) ;
2020-08-05 00:20:25 +02:00
}
public void createTemplate ( Template template ) {
2022-08-14 11:53:38 +02:00
this . runAsync ( ( ) - > {
try ( Connection connection = this . databaseConnector . getConnection ( ) ) {
2023-08-15 23:24:07 +02:00
int nextId = this . dataManager . getNextId ( " templates " ) ;
2023-08-02 18:57:10 +02:00
2022-08-14 11:53:38 +02:00
String createTemplate = " INSERT INTO " + this . getTablePrefix ( ) + " templates (punishment_type, duration, reason, name, creator) VALUES (?, ?, ?, ?, ?) " ;
PreparedStatement statement = connection . prepareStatement ( createTemplate ) ;
2020-08-05 00:20:25 +02:00
statement . setString ( 1 , template . getPunishmentType ( ) . name ( ) ) ;
statement . setLong ( 2 , template . getDuration ( ) ) ;
statement . setString ( 3 , template . getReason ( ) ) ;
statement . setString ( 4 , template . getName ( ) ) ;
statement . setString ( 5 , template . getCreator ( ) . toString ( ) ) ;
statement . executeUpdate ( ) ;
2023-08-02 18:57:10 +02:00
template . setId ( nextId ) ;
2022-08-14 11:53:38 +02:00
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
}
} ) ;
2020-08-05 00:20:25 +02:00
}
public void deleteTemplate ( Template template ) {
2022-08-14 11:53:38 +02:00
this . runAsync ( ( ) - > {
try ( Connection connection = this . databaseConnector . getConnection ( ) ) {
String deleteTemplate = " DELETE FROM " + this . getTablePrefix ( ) + " templates WHERE id = ? " ;
PreparedStatement statement = connection . prepareStatement ( deleteTemplate ) ;
2020-08-05 00:20:25 +02:00
statement . setLong ( 1 , template . getId ( ) ) ;
statement . executeUpdate ( ) ;
2022-08-14 11:53:38 +02:00
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
2020-08-05 00:20:25 +02:00
}
2022-08-14 11:53:38 +02:00
} ) ;
2020-08-05 00:20:25 +02:00
}
public void getTemplates ( Consumer < List < Template > > callback ) {
List < Template > templates = new ArrayList < > ( ) ;
2022-08-14 11:53:38 +02:00
this . runAsync ( ( ) - > {
try ( Connection connection = this . databaseConnector . getConnection ( ) ) {
Statement statement = connection . createStatement ( ) ;
2020-08-05 00:20:25 +02:00
String selectTemplates = " SELECT * FROM " + this . getTablePrefix ( ) + " templates " ;
ResultSet result = statement . executeQuery ( selectTemplates ) ;
while ( result . next ( ) ) {
int id = result . getInt ( " id " ) ;
PunishmentType punishmentType = PunishmentType . valueOf ( result . getString ( " punishment_type " ) ) ;
long duration = result . getLong ( " duration " ) ;
String reason = result . getString ( " reason " ) ;
String name = result . getString ( " name " ) ;
UUID creator = UUID . fromString ( result . getString ( " creator " ) ) ;
Template template = new Template ( punishmentType , duration , reason , creator , name ) ;
template . setId ( id ) ;
templates . add ( template ) ;
}
2022-08-14 11:53:38 +02:00
this . sync ( ( ) - > callback . accept ( templates ) ) ;
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
}
} ) ;
2020-08-05 00:20:25 +02:00
}
public void createAppliedPunishment ( AppliedPunishment punishment ) {
2022-08-14 11:53:38 +02:00
this . runAsync ( ( ) - > {
try ( Connection connection = this . databaseConnector . getConnection ( ) ) {
2023-08-15 23:24:07 +02:00
int nextId = this . dataManager . getNextId ( " punishments " ) ;
2023-08-02 18:57:10 +02:00
2022-08-14 11:53:38 +02:00
String createPunishment = " INSERT INTO " + this . getTablePrefix ( ) + " punishments (type, duration, reason, victim, punisher, expiration) VALUES (?, ?, ?, ?, ?, ?) " ;
PreparedStatement statement = connection . prepareStatement ( createPunishment ) ;
2020-08-05 00:20:25 +02:00
statement . setString ( 1 , punishment . getPunishmentType ( ) . name ( ) ) ;
statement . setLong ( 2 , punishment . getDuration ( ) ) ;
statement . setString ( 3 , punishment . getReason ( ) ) ;
statement . setString ( 4 , punishment . getVictim ( ) . toString ( ) ) ;
statement . setString ( 5 , punishment . getPunisher ( ) . toString ( ) ) ;
statement . setLong ( 6 , punishment . getExpiration ( ) ) ;
statement . executeUpdate ( ) ;
2023-08-02 18:57:10 +02:00
punishment . setId ( nextId ) ;
2022-08-14 11:53:38 +02:00
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
}
} ) ;
2020-08-05 00:20:25 +02:00
}
public void deleteAppliedPunishment ( AppliedPunishment punishment ) {
2022-08-14 11:53:38 +02:00
this . runAsync ( ( ) - > {
try ( Connection connection = this . databaseConnector . getConnection ( ) ) {
String deletePunishment = " DELETE FROM " + this . getTablePrefix ( ) + " punishments WHERE id = ? " ;
PreparedStatement statement = connection . prepareStatement ( deletePunishment ) ;
2020-08-05 00:20:25 +02:00
statement . setLong ( 1 , punishment . getId ( ) ) ;
statement . executeUpdate ( ) ;
2022-08-14 11:53:38 +02:00
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
2020-08-05 00:20:25 +02:00
}
2022-08-14 11:53:38 +02:00
} ) ;
2020-08-05 00:20:25 +02:00
}
public void updateAppliedPunishment ( AppliedPunishment punishment ) {
2022-08-14 11:53:38 +02:00
this . runAsync ( ( ) - > {
try ( Connection connection = this . databaseConnector . getConnection ( ) ) {
String updatePunishment = " UPDATE " + this . getTablePrefix ( ) + " punishments set type = ?, duration = ?, reason = ?, victim = ?, punisher = ?, expiration = ? WHERE id = ? " ;
PreparedStatement statement = connection . prepareStatement ( updatePunishment ) ;
2020-08-05 00:20:25 +02:00
statement . setString ( 1 , punishment . getPunishmentType ( ) . name ( ) ) ;
statement . setLong ( 2 , punishment . getDuration ( ) ) ;
statement . setString ( 3 , punishment . getReason ( ) ) ;
statement . setString ( 4 , punishment . getVictim ( ) . toString ( ) ) ;
statement . setString ( 5 , punishment . getPunisher ( ) . toString ( ) ) ;
statement . setLong ( 6 , punishment . getExpiration ( ) ) ;
statement . setLong ( 7 , punishment . getId ( ) ) ;
statement . executeUpdate ( ) ;
2022-08-14 11:53:38 +02:00
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
2020-08-05 00:20:25 +02:00
}
2022-08-14 11:53:38 +02:00
} ) ;
2020-08-05 00:20:25 +02:00
}
public void getAppliedPunishments ( Consumer < List < AppliedPunishment > > callback ) {
List < AppliedPunishment > appliedPunishments = new ArrayList < > ( ) ;
2022-08-14 11:53:38 +02:00
this . runAsync ( ( ) - > {
try ( Connection connection = this . databaseConnector . getConnection ( ) ) {
Statement statement = connection . createStatement ( ) ;
2020-08-05 00:20:25 +02:00
String selectPunishments = " SELECT * FROM " + this . getTablePrefix ( ) + " punishments " ;
ResultSet result = statement . executeQuery ( selectPunishments ) ;
while ( result . next ( ) ) {
int id = result . getInt ( " id " ) ;
PunishmentType punishmentType = PunishmentType . valueOf ( result . getString ( " type " ) ) ;
long duration = result . getLong ( " duration " ) ;
String reason = result . getString ( " reason " ) ;
UUID victim = UUID . fromString ( result . getString ( " victim " ) ) ;
UUID punisher = UUID . fromString ( result . getString ( " punisher " ) ) ;
long expiration = result . getLong ( " expiration " ) ;
appliedPunishments . add ( new AppliedPunishment ( punishmentType , duration , reason , victim , punisher , expiration , id ) ) ;
}
2022-08-14 11:53:38 +02:00
this . sync ( ( ) - > callback . accept ( appliedPunishments ) ) ;
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
}
} ) ;
2020-08-05 00:20:25 +02:00
}
public void createNote ( PunishmentNote note ) {
2022-08-14 11:53:38 +02:00
this . runAsync ( ( ) - > {
try ( Connection connection = this . databaseConnector . getConnection ( ) ) {
2023-08-15 23:24:07 +02:00
int nextId = this . dataManager . getNextId ( " notes " ) ;
2023-08-02 18:57:10 +02:00
2022-08-14 11:53:38 +02:00
String createNote = " INSERT INTO " + this . getTablePrefix ( ) + " notes (note, author, subject, creation) VALUES (?, ?, ?, ?) " ;
PreparedStatement statement = connection . prepareStatement ( createNote ) ;
2020-08-05 00:20:25 +02:00
statement . setString ( 1 , note . getNote ( ) ) ;
statement . setString ( 2 , note . getAuthor ( ) . toString ( ) ) ;
statement . setString ( 3 , note . getSubject ( ) . toString ( ) ) ;
statement . setLong ( 4 , note . getCreationDate ( ) ) ;
statement . executeUpdate ( ) ;
2023-08-02 18:57:10 +02:00
note . setId ( nextId ) ;
2022-08-14 11:53:38 +02:00
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
}
} ) ;
2020-08-05 00:20:25 +02:00
}
public void deleteNote ( PunishmentNote note ) {
2022-08-14 11:53:38 +02:00
this . runAsync ( ( ) - > {
try ( Connection connection = this . databaseConnector . getConnection ( ) ) {
String deleteNote = " DELETE FROM " + this . getTablePrefix ( ) + " notes WHERE id = ? " ;
PreparedStatement statement = connection . prepareStatement ( deleteNote ) ;
2020-08-05 00:20:25 +02:00
statement . setLong ( 1 , note . getId ( ) ) ;
statement . executeUpdate ( ) ;
2022-08-14 11:53:38 +02:00
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
2020-08-05 00:20:25 +02:00
}
2022-08-14 11:53:38 +02:00
} ) ;
2020-08-05 00:20:25 +02:00
}
public void getNotes ( Consumer < List < PunishmentNote > > callback ) {
List < PunishmentNote > notes = new ArrayList < > ( ) ;
2022-08-14 11:53:38 +02:00
this . runAsync ( ( ) - > {
try ( Connection connection = this . databaseConnector . getConnection ( ) ) {
Statement statement = connection . createStatement ( ) ;
2020-08-05 00:20:25 +02:00
String getNotes = " SELECT * FROM " + this . getTablePrefix ( ) + " notes " ;
ResultSet result = statement . executeQuery ( getNotes ) ;
while ( result . next ( ) ) {
int id = result . getInt ( " id " ) ;
String note = result . getString ( " note " ) ;
UUID author = UUID . fromString ( result . getString ( " author " ) ) ;
UUID subject = UUID . fromString ( result . getString ( " subject " ) ) ;
long creation = result . getLong ( " creation " ) ;
notes . add ( new PunishmentNote ( id , note , author , subject , creation ) ) ;
}
2022-08-14 11:53:38 +02:00
this . sync ( ( ) - > callback . accept ( notes ) ) ;
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
}
} ) ;
2020-08-05 00:20:25 +02:00
}
public void createTicket ( Ticket ticket ) {
2022-08-14 11:53:38 +02:00
this . runAsync ( ( ) - > {
try ( Connection connection = this . databaseConnector . getConnection ( ) ) {
2023-08-15 23:24:07 +02:00
int nextId = this . dataManager . getNextId ( " tickets " ) ;
2023-08-02 18:57:10 +02:00
2022-08-14 11:53:38 +02:00
String createTicket = " INSERT INTO " + this . getTablePrefix ( ) + " tickets (victim, subject, type, status, world, x, y, z, pitch, yaw) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) " ;
PreparedStatement statement = connection . prepareStatement ( createTicket ) ;
2020-08-05 00:20:25 +02:00
statement . setString ( 1 , ticket . getVictim ( ) . toString ( ) ) ;
statement . setString ( 2 , ticket . getSubject ( ) ) ;
statement . setString ( 3 , ticket . getType ( ) ) ;
statement . setString ( 4 , ticket . getStatus ( ) . name ( ) ) ;
Location location = ticket . getLocation ( ) ;
statement . setString ( 5 , location . getWorld ( ) . getName ( ) ) ;
statement . setDouble ( 6 , location . getX ( ) ) ;
statement . setDouble ( 7 , location . getY ( ) ) ;
statement . setDouble ( 8 , location . getZ ( ) ) ;
statement . setFloat ( 9 , location . getPitch ( ) ) ;
statement . setFloat ( 10 , location . getYaw ( ) ) ;
statement . executeUpdate ( ) ;
2022-08-14 11:53:38 +02:00
for ( TicketResponse response : ticket . getResponses ( ) ) {
createTicketResponse ( response ) ;
}
2020-08-05 00:20:25 +02:00
2023-08-02 18:57:10 +02:00
ticket . setId ( nextId ) ;
2022-08-14 11:53:38 +02:00
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
}
} ) ;
2020-08-05 00:20:25 +02:00
}
public void deleteTicket ( Ticket ticket ) {
2022-08-14 11:53:38 +02:00
this . runAsync ( ( ) - > {
try ( Connection connection = this . databaseConnector . getConnection ( ) ) {
String deleteTicket = " DELETE FROM " + this . getTablePrefix ( ) + " tickets WHERE id = ? " ;
try ( PreparedStatement statement = connection . prepareStatement ( deleteTicket ) ) {
statement . setLong ( 1 , ticket . getId ( ) ) ;
statement . executeUpdate ( ) ;
}
2020-08-05 00:20:25 +02:00
2022-08-14 11:53:38 +02:00
String deleteTicketResponses = " DELETE FROM " + this . getTablePrefix ( ) + " ticket_responses WHERE ticket_id = ? " ;
try ( PreparedStatement statement = connection . prepareStatement ( deleteTicketResponses ) ) {
statement . setLong ( 1 , ticket . getId ( ) ) ;
statement . executeUpdate ( ) ;
}
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
2020-08-05 00:20:25 +02:00
}
2022-08-14 11:53:38 +02:00
} ) ;
2020-08-05 00:20:25 +02:00
}
public void updateTicket ( Ticket ticket ) {
2022-08-14 11:53:38 +02:00
this . runAsync ( ( ) - > {
try ( Connection connection = this . databaseConnector . getConnection ( ) ) {
String updateTicket = " UPDATE " + this . getTablePrefix ( ) + " tickets SET victim = ?, subject = ?, type = ?, status = ?, world = ?, x = ?, y = ?, z = ?, pitch = ?, yaw = ? WHERE id = ? " ;
PreparedStatement statement = connection . prepareStatement ( updateTicket ) ;
2020-08-05 00:20:25 +02:00
statement . setString ( 1 , ticket . getVictim ( ) . toString ( ) ) ;
statement . setString ( 2 , ticket . getSubject ( ) ) ;
statement . setString ( 3 , ticket . getType ( ) ) ;
statement . setString ( 4 , ticket . getStatus ( ) . name ( ) ) ;
Location location = ticket . getLocation ( ) ;
statement . setString ( 5 , location . getWorld ( ) . getName ( ) ) ;
statement . setDouble ( 6 , location . getX ( ) ) ;
statement . setDouble ( 7 , location . getY ( ) ) ;
statement . setDouble ( 8 , location . getZ ( ) ) ;
statement . setFloat ( 9 , location . getPitch ( ) ) ;
statement . setFloat ( 10 , location . getYaw ( ) ) ;
statement . setInt ( 11 , ticket . getId ( ) ) ;
statement . executeUpdate ( ) ;
2022-08-14 11:53:38 +02:00
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
2020-08-05 00:20:25 +02:00
}
2022-08-14 11:53:38 +02:00
} ) ;
2020-08-05 00:20:25 +02:00
}
public void getTickets ( Consumer < Map < Integer , Ticket > > callback ) {
Map < Integer , Ticket > tickets = new TreeMap < > ( ) ;
2022-08-14 11:53:38 +02:00
this . runAsync ( ( ) - > {
try ( Connection connection = this . databaseConnector . getConnection ( ) ) {
try ( Statement statement = connection . createStatement ( ) ) {
String selectTickets = " SELECT * FROM " + this . getTablePrefix ( ) + " tickets " ;
ResultSet result = statement . executeQuery ( selectTickets ) ;
while ( result . next ( ) ) {
int id = result . getInt ( " id " ) ;
UUID victim = UUID . fromString ( result . getString ( " victim " ) ) ;
String subject = result . getString ( " subject " ) ;
String type = result . getString ( " type " ) ;
TicketStatus status = TicketStatus . valueOf ( result . getString ( " status " ) ) ;
String world = result . getString ( " world " ) ;
double x = result . getDouble ( " x " ) ;
double y = result . getDouble ( " y " ) ;
double z = result . getDouble ( " z " ) ;
float pitch = result . getFloat ( " pitch " ) ;
float yaw = result . getFloat ( " yaw " ) ;
Location location = Bukkit . getWorld ( world ) = = null ? null : new Location ( Bukkit . getWorld ( world ) , x , y , z , yaw , pitch ) ;
Ticket ticket = new Ticket ( id , victim , subject , type , status , location ) ;
ticket . setId ( id )
;
tickets . put ( id , ticket ) ;
}
2020-08-05 00:20:25 +02:00
}
2022-08-14 11:53:38 +02:00
try ( Statement statement = connection . createStatement ( ) ) {
String selectTickets = " SELECT * FROM " + this . getTablePrefix ( ) + " ticket_responses " ;
ResultSet result = statement . executeQuery ( selectTickets ) ;
while ( result . next ( ) ) {
int id = result . getInt ( " ticket_id " ) ;
2020-08-05 00:20:25 +02:00
2022-08-14 11:53:38 +02:00
Ticket ticket = tickets . get ( id ) ;
if ( ticket = = null ) continue ;
2020-08-05 00:20:25 +02:00
2022-08-14 11:53:38 +02:00
UUID author = UUID . fromString ( result . getString ( " author " ) ) ;
String message = result . getString ( " message " ) ;
long postedDate = result . getLong ( " posted_date " ) ;
2020-08-05 00:20:25 +02:00
2022-08-14 11:53:38 +02:00
TicketResponse ticketResponse = new TicketResponse ( author , message , postedDate ) ;
ticketResponse . setTicketId ( id ) ;
2020-08-05 00:20:25 +02:00
2022-08-14 11:53:38 +02:00
ticket . addResponse ( ticketResponse ) ;
}
2020-08-05 00:20:25 +02:00
}
2022-08-14 11:53:38 +02:00
this . sync ( ( ) - > callback . accept ( tickets ) ) ;
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
}
} ) ;
2020-08-05 00:20:25 +02:00
}
public void createTicketResponse ( TicketResponse ticketResponse ) {
2022-08-14 11:53:38 +02:00
this . runAsync ( ( ) - > {
try ( Connection connection = this . databaseConnector . getConnection ( ) ) {
String createTicketResponse = " INSERT INTO " + this . getTablePrefix ( ) + " ticket_responses (ticket_id, author, message, posted_date) VALUES (?, ?, ?, ?) " ;
PreparedStatement statement = connection . prepareStatement ( createTicketResponse ) ;
2020-08-05 00:20:25 +02:00
statement . setInt ( 1 , ticketResponse . getTicketId ( ) ) ;
statement . setString ( 2 , ticketResponse . getAuthor ( ) . toString ( ) ) ;
statement . setString ( 3 , ticketResponse . getMessage ( ) ) ;
statement . setLong ( 4 , ticketResponse . getPostedDate ( ) ) ;
statement . executeUpdate ( ) ;
2022-08-14 11:53:38 +02:00
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
2020-08-05 00:20:25 +02:00
}
2022-08-14 11:53:38 +02:00
} ) ;
2020-08-05 00:20:25 +02:00
}
}