Skip to content

Commit

Permalink
use java.time an java.util.UUID everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
svenwiltink committed Sep 6, 2019
1 parent 165c767 commit ae3a0b7
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 92 deletions.
58 changes: 10 additions & 48 deletions src/main/java/modreq/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,36 @@
*/
package modreq;

import java.time.Instant;
import java.util.UUID;

public class Comment {

private int id;
private String commenter;
private String commenterUUID;
private String date;
private UUID commenterUUID;
private Instant date;
private String comment;

/**
* Used for the comment system
*/
public Comment(String commenter, String commenterUUID, String comment, CommentType commenttype) {
public Comment(String commenter, UUID commenterUUID, String comment, CommentType commenttype) {
this.id = 0;
this.commenter = commenter;
this.commenterUUID = commenterUUID;
this.comment = comment + " " + commenttype.getSuffix();
this.date = ModReq.getTimeString();
this.date = Instant.now();
}

public Comment(int id, String commenter, String commenterUUID, String comment, String date) {
public Comment(int id, String commenter, UUID commenterUUID, String comment, Instant date) {
this.id = id;
this.commenter = commenter;
this.commenterUUID = commenterUUID;
this.comment = comment;
this.date = date;
}

public Comment() {
}


public int getId() {
return id;
}
Expand All @@ -56,52 +55,15 @@ public String getCommenter() {
return commenter;
}

private void setCommenter(String commenter) {
this.commenter = commenter;
}

public String getDate() {
public Instant getDate() {
return date;
}

private void setDate(String date) {
this.date = date;
}

public String getComment() {
return comment;
}

private void setComment(String comment) {
this.comment = comment;
}

public boolean equalsComment(Comment c) {
if (c.getCommenter().equals(commenter)) {
if (c.getComment().equals(comment)) {
if (c.getDate().equals(date)) {
return true;
}
}
}
return false;
}

public boolean isValid() {
if (commenter == null) {
return false;
}
if (comment == null) {
return false;
}
if (date == null) {
return false;
}
return true;

}

public String getCommenterUUID() {
public UUID getCommenterUUID() {
return commenterUUID;
}
}
44 changes: 20 additions & 24 deletions src/main/java/modreq/Ticket.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,41 @@
*/
package modreq;

import java.sql.SQLException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import modreq.repository.TicketRepository;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;

import java.sql.SQLException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

public class Ticket {

private int id;
private String submitter;
private String submitterUUID;
private UUID submitterUUID;
private String message;
private Instant date;
private Status status;
private String location;
private String staff;
private String staffUUID;
private UUID staffUUID;

private TicketRepository ticketRepository;
private List<Comment> comments;

public Ticket(int id, String submitter, String submitterUUID, String message, Instant date, Status status, String location, String staff, String staffUUID) {
public Ticket(int id, String submitter, UUID submitterUUID, String message, Instant date, Status status, String location, String staff, UUID staffUUID) {
this.submitter = submitter;
this.submitterUUID = submitterUUID;
this.id = id;
this.staff = staff;
this.staffUUID = staff;
this.staffUUID = staffUUID;
this.date = date;
this.message = message;
this.status = status;
Expand Down Expand Up @@ -151,7 +147,7 @@ public String getLocationString() {
* @return
*/
public void sendSummarytoPlayer(Player p) {
ChatColor namecolor = Bukkit.getPlayer(UUID.fromString(submitterUUID)) == null ? ChatColor.RED : ChatColor.GREEN;
ChatColor namecolor = Bukkit.getPlayer(submitterUUID) == null ? ChatColor.RED : ChatColor.GREEN;

String summessage = message;
if (summessage.length() > 15) {
Expand Down Expand Up @@ -246,11 +242,11 @@ private void sendComments(Player p) {
int number = comments.size();
for (Comment c : comments) {
String commenter = c.getCommenter();
String date = c.getDate();
Instant date = c.getDate();
String comment = c.getComment();
comment = ChatColor.translateAlternateColorCodes('&', comment);
p.sendMessage(ChatColor.GOLD + "#" + Integer.toString(number) + " "
+ ChatColor.AQUA + date + " " + ChatColor.GOLD + commenter
p.sendMessage(ChatColor.GOLD + "#" + number + " "
+ ChatColor.AQUA + ModReq.getDateTimeFormatter().format(date) + " " + ChatColor.GOLD + commenter
+ ": " + ChatColor.GRAY + comment);
number--;
}
Expand Down Expand Up @@ -332,7 +328,7 @@ public void insertComment(Comment c) {
}

public void addDefaultComment(Player p, CommentType c) {
Comment comment = new Comment(p.getName(), p.getUniqueId().toString(), c.getDefaultComment(), c);
Comment comment = new Comment(p.getName(), p.getUniqueId(), c.getDefaultComment(), c);
addComment(comment);
}

Expand All @@ -345,19 +341,19 @@ public void notifyStaff(String notification) {

}

public String getSubmitterUUID() {
public UUID getSubmitterUUID() {
return submitterUUID;
}

public void setSubmitterUUID(String submitterUUID) {
public void setSubmitterUUID(UUID submitterUUID) {
this.submitterUUID = submitterUUID;
}

public String getStaffUUID() {
public UUID getStaffUUID() {
return staffUUID;
}

public void setStaffUUID(String staffUUID) {
public void setStaffUUID(UUID staffUUID) {
this.staffUUID = staffUUID;
}
}
1 change: 1 addition & 0 deletions src/main/java/modreq/commands/ClaimCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
}

t.setStaff(staff);
t.setStaffUUID(p.getUniqueId());
t.setStatus(status);
t.addDefaultComment(p, CommentType.CLAIM);
t.update();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/modreq/commands/CommentCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}
String commenter = p.getName();
String comment = Utils.join(args, " ", 1);
Comment c = new Comment(commenter, p.getUniqueId().toString(), comment, CommentType.COMMENT);
Comment c = new Comment(commenter, p.getUniqueId(), comment, CommentType.COMMENT);

t.addComment(c);
sender.sendMessage(ModReq.format(ModReq.getInstance().Messages.getString("staff.executor.ticket.comment"), "", "", ""));
Expand All @@ -64,7 +64,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
op.sendMessage(ModReq.format(ModReq.getInstance().Messages.getString("player.comment"), sender.getName(), args[0], ""));
} else if (t.getStaff().equals(sender.getName())) {//it is the staff member
op.sendMessage(ModReq.format(ModReq.getInstance().Messages.getString("staff.all.comment"), sender.getName(), args[0], ""));
} else if (t.getCommentsBy(op.getName()).isEmpty() == false) {//it is someone else that commented earlier
} else if (!t.getCommentsBy(op.getName()).isEmpty()) {//it is someone else that commented earlier
op.sendMessage(ModReq.format(ModReq.getInstance().Messages.getString("staff.all.comment"), sender.getName(), args[0], ""));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/modreq/commands/DoneCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
return true;
}

t.addComment(new Comment(sender.getName(), p.getUniqueId().toString(), comment, CommentType.CLOSE));
t.addComment(new Comment(sender.getName(), p.getUniqueId(), comment, CommentType.CLOSE));

t.setStaff(staff);
t.setStatus(Status.CLOSED);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/modreq/commands/ModreqCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private int savereq(String message, Player sender) {// save
+ Math.round(loc.getZ());

try {
Ticket t = new Ticket(0, sender.getName(), sender.getUniqueId().toString(), message, Instant.now(), Status.OPEN, location, "no staff member yet", null);
Ticket t = new Ticket(0, sender.getName(), sender.getUniqueId(), message, Instant.now(), Status.OPEN, location, "no staff member yet", null);
return tickets.addTicket(t);
} catch (SQLException e) {
e.printStackTrace();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/modreq/commands/ReopenCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
Status status = Status.OPEN;
String staff = sender.getName();

t.addComment(new Comment(sender.getName(), p.getUniqueId().toString(), comment, CommentType.REOPEN));
t.addComment(new Comment(sender.getName(), p.getUniqueId(), comment, CommentType.REOPEN));
t.setStaff(staff);
t.setStatus(status);
try {
Expand Down
31 changes: 16 additions & 15 deletions src/main/java/modreq/repository/TicketRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,17 @@
import modreq.ModReq;
import modreq.Status;
import modreq.Ticket;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import java.sql.*;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.logging.Logger;

public class TicketRepository {
Expand Down Expand Up @@ -221,7 +218,7 @@ public int addTicket(Ticket t) throws SQLException {

PreparedStatement prep = conn.prepareStatement("INSERT INTO ticket (submitter, submitterUUID, message, date, status, location, staff, staffUUID) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
prep.setString(1, t.getSubmitter());
prep.setString(2, t.getSubmitterUUID());
prep.setString(2, t.getSubmitterUUID().toString());
prep.setString(3, t.getMessage());
prep.setObject(4, LocalDateTime.ofInstant(t.getDate(), ZoneOffset.UTC));
prep.setString(5, t.getStatus().getStatusString());
Expand Down Expand Up @@ -269,7 +266,7 @@ public void updateTicket(Ticket t) throws SQLException {

prep.setString(1, t.getStatus().getStatusString());
prep.setString(2, t.getStaff());
prep.setString(3, t.getStaffUUID());
prep.setString(3, t.getStaffUUID() == null ? null : t.getSubmitterUUID().toString());
prep.setInt(4, t.getId());
prep.execute();

Expand All @@ -279,16 +276,20 @@ public void updateTicket(Ticket t) throws SQLException {
private Ticket getTicketByResultSet(ResultSet result) throws SQLException {
int id = result.getInt(1);
String submitter = result.getString(2);
String submitterUUID = result.getString(3);
String submitterUUIDString = result.getString(3);
String message = result.getString(4);
LocalDateTime date = result.getObject(5, LocalDateTime.class);
String status = result.getString(6);
String location = result.getString(7);
String staff = result.getString(8);
String staffUUID = result.getString(9);
String staffUUIDString = result.getString(9);

Instant utc = date.toInstant(ZoneOffset.UTC);
return new Ticket(id, submitter, submitterUUID, message, utc, Status.getByString(status), location, staff, staffUUID);
UUID staffUUID = null;
if (staff != null) {
staffUUID = UUID.fromString(staffUUIDString);
}

return new Ticket(id, submitter, UUID.fromString(submitterUUIDString), message, date.toInstant(ZoneOffset.UTC), Status.getByString(status), location, staff, staffUUID);
}

private void addCommentsToTicket(Connection conn, Ticket t) throws SQLException {
Expand All @@ -298,11 +299,11 @@ private void addCommentsToTicket(Connection conn, Ticket t) throws SQLException
while (rs.next()) {
int id = rs.getInt(1);
String commenter = rs.getString(3);
String commenterUUID = rs.getString(4);
String commenterUUIDString = rs.getString(4);
String comment = rs.getString(5);
String date = rs.getString(6);
LocalDateTime date = rs.getObject(6, LocalDateTime.class);

Comment c = new Comment(id, commenter, commenterUUID, comment, date);
Comment c = new Comment(id, commenter, UUID.fromString(commenterUUIDString), comment, date.toInstant(ZoneOffset.UTC));
t.insertComment(c);
}
stat.close();
Expand All @@ -324,9 +325,9 @@ private void updateComments(Connection conn, Ticket t) throws SQLException {

prep.setInt(1, t.getId());
prep.setString(2, c.getCommenter());
prep.setString(3, c.getCommenterUUID());
prep.setString(3, c.getCommenterUUID().toString());
prep.setString(4, c.getComment());
prep.setObject(5, LocalDateTime.ofInstant(t.getDate(), ZoneOffset.UTC));
prep.setObject(5, LocalDateTime.ofInstant(c.getDate(), ZoneOffset.UTC));
prep.addBatch();
prep.executeBatch();
}
Expand Down

0 comments on commit ae3a0b7

Please sign in to comment.