Finished frame for database concurrence

Fixed some error too
This commit is contained in:
Anthony committed 2018-02-17 20:55:25 -05:00
1 parent d82ac61d4f
commit 072bcb0211
8 files changed
+70 -54

No files matched your search

+2 -1
View File
@@ -70,7 +70,8 @@ public class MusicBot {
} }
public static void disconnect() { public static void disconnect() {
jda.shutdown(); if (jda != null)
jda.shutdown();
} }
@@ -70,7 +70,7 @@ public class BlacklistCommand extends AbstractCommand {
out += bl.getWord() + " -> " + (bl.isEnabled() ? "enabled" : "disabled") + "\n"; out += bl.getWord() + " -> " + (bl.isEnabled() ? "enabled" : "disabled") + "\n";
} }
} catch (RuntimeException e) { } catch (RuntimeException e) {
e.printStackTrace();
} }
t.editMessage("List of blacklisted words for this server [" + event.getGuild().getName() + "] :\n" + (blacklist.isEmpty() ? "No word found" : out)).queue(); t.editMessage("List of blacklisted words for this server [" + event.getGuild().getName() + "] :\n" + (blacklist.isEmpty() ? "No word found" : out)).queue();
@@ -19,13 +19,15 @@ public abstract class AbstractController {
} }
public AbstractController start() { public AbstractController start() {
System.out.println("Starting task.."); System.out.println("Starting task.. ["+ getControllerName() + "}");
DBController.isDoingSomething = true;
isDoingSomething = true; isDoingSomething = true;
return this; return this;
} }
public AbstractController end() { public AbstractController end() {
System.out.println("Ended task!"); System.out.println("Ended task! ["+ getControllerName() + "}");
DBController.isDoingSomething = false;
isDoingSomething = false; isDoingSomething = false;
return this; return this;
} }
@@ -16,12 +16,15 @@ public class DBController {
*/ */
private static HashMap<String, AbstractController> controllers = new HashMap<>(); private static HashMap<String, AbstractController> controllers = new HashMap<>();
private static MysqlDatabase database = new MysqlDatabase("discord"); private static MysqlDatabase database = new MysqlDatabase("discord");
protected static boolean isDoingSomething = false;
/** /**
* @param controller The name of the controller as defined in the {@link AbstractController#getControllerName()}'s method * @param controller The name of the controller as defined in the {@link AbstractController#getControllerName()}'s method
* @return {@link AbstractController} The class with an AbstractController type * @return {@link AbstractController} The class with an AbstractController type
*/ */
public static AbstractController get(String controller) { public static AbstractController get(String controller) {
if (controllers.isEmpty()) if (controllers.isEmpty())
init(); init();
@@ -38,10 +41,12 @@ public class DBController {
MusicBot.LOGGER.debug("Getting controller " + controllers.get(controller).getControllerName()); MusicBot.LOGGER.debug("Getting controller " + controllers.get(controller).getControllerName());
do { do {
System.out.println("task is running");
if (!controllers.get(controller).isDoingSomething()) { if (!isDoingSomething && !controllers.get(controller).isDoingSomething()) {
return controllers.get(controller).start(); return controllers.get(controller).start();
} }
} while (true); } while (true);
} }
@@ -28,68 +28,66 @@ public class DBBlacklist extends AbstractController {
public void add(MBlacklist obj) { public void add(MBlacklist obj) {
try { try {
if (!isAlreadyBlacklisted(obj.getWord(), obj.getServerId())) { if (!isAlreadyBlacklisted(obj.getWord(), obj.getServerId())) {
int result = database.insert("INSERT INTO blacklist VALUES(?,?,?,?)", obj.getWord(), obj.getServerId(), database.insert("INSERT INTO blacklist VALUES(?,?,?,?)", obj.getWord(), obj.getServerId(), obj.isEnabled(), obj.getCreatorId());
obj.isEnabled(), obj.getCreatorId()); } else {
end(); end();
} else
throw new RuntimeException("Word is already blacklisted"); throw new RuntimeException("Word is already blacklisted");
}
} catch (SQLException e) { } catch (SQLException e) {
// TODO Auto-generated catch block end();
e.printStackTrace(); e.printStackTrace();
} }
end(); end();
} }
public int delete(String word, String serverId) { public void delete(String word, String serverId) {
try { try {
if (isAlreadyBlacklisted(word, serverId)) { if (isAlreadyBlacklisted(word, serverId)) {
int result = database.delete("DELETE FROM blacklist WHERE word = ? AND serverId = ?", word, serverId); database.delete("DELETE FROM blacklist WHERE word = ? AND serverId = ?", word, serverId);
} else {
end(); end();
return result;
} else
throw new RuntimeException("Is not blacklisted"); throw new RuntimeException("Is not blacklisted");
}
} catch (SQLException e) { } catch (SQLException e) {
// TODO Auto-generated catch block end();
e.printStackTrace(); e.printStackTrace();
} }
end(); end();
return -1;
} }
public int enable(String word, String serverId) { public void enable(String word, String serverId) {
try { try {
if (isAlreadyBlacklisted(word, serverId)) { if (isAlreadyBlacklisted(word, serverId)) {
int result = database.update("UPDATE blacklist SET isEnabled = 1 WHERE word = ? AND serverId = ?", word, serverId); database.update("UPDATE blacklist SET isEnabled = 1 WHERE word = ? AND serverId = ?", word, serverId);
} else {
end(); end();
return result;
} else
throw new RuntimeException("Is not blacklisted"); throw new RuntimeException("Is not blacklisted");
}
} catch (SQLException e) { } catch (SQLException e) {
// TODO Auto-generated catch block end();
e.printStackTrace(); e.printStackTrace();
} }
end(); end();
return -1;
} }
public int disable(String word, String serverId) { public void disable(String word, String serverId) {
try { try {
if (isAlreadyBlacklisted(word, serverId)) { if (isAlreadyBlacklisted(word, serverId)) {
int result = database.update("UPDATE blacklist SET isEnabled = 0 WHERE word = ? AND serverId = ?", word, serverId); database.update("UPDATE blacklist SET isEnabled = 0 WHERE word = ? AND serverId = ?", word, serverId);
} else {
end(); end();
return result;
} else
throw new RuntimeException("Is not blacklisted"); throw new RuntimeException("Is not blacklisted");
}
} catch (SQLException e) { } catch (SQLException e) {
// TODO Auto-generated catch block end();
e.printStackTrace(); e.printStackTrace();
} }
end(); end();
return -1;
} }
public MBlacklist getBlacklistForWord(String word, String serverId) { public MBlacklist getBlacklistForWord(String word, String serverId) {
@@ -103,9 +101,12 @@ public class DBBlacklist extends AbstractController {
end(); end();
return blacklisted; return blacklisted;
} }
} else } else {
end();
throw new RuntimeException("Is not blacklisted"); throw new RuntimeException("Is not blacklisted");
}
} catch (SQLException e) { } catch (SQLException e) {
end();
e.printStackTrace(); e.printStackTrace();
} }
@@ -121,19 +122,22 @@ public class DBBlacklist extends AbstractController {
TableModel model = database.convertToTableModel(rs); TableModel model = database.convertToTableModel(rs);
if (model.getNbRows() != 0) { if (model.getNbRows() != 0) {
for (int row = 0; row < database.getNbRow(); row++) { for (int row = 0; row < model.getNbRows(); row++) {
output.add(new MBlacklist((String) model.getRow(row).get(0), (Boolean) model.getRow(row).get(2), (String) model.getRow(row).get(3), output.add(new MBlacklist((String) model.getRow(row).get(0), (Boolean) model.getRow(row).get(2), (String) model.getRow(row).get(3),
(String) model.getRow(row).get(1))); (String) model.getRow(row).get(1)));
} }
} else } else {
end();
throw new RuntimeException("Blacklist is empty"); throw new RuntimeException("Blacklist is empty");
}
} catch (SQLException e) { } catch (SQLException e) {
end();
e.printStackTrace(); e.printStackTrace();
} catch (NullPointerException e) { } catch (NullPointerException e) {
end();
} catch (IndexOutOfBoundsException i) { } catch (IndexOutOfBoundsException i) {
throw new RuntimeException("Blacklist is empty"); end();
} }
end(); end();
@@ -28,24 +28,23 @@ public class DBMessage extends AbstractController {
try { try {
database.insert("INSERT INTO log_message VALUES(?,?,?,?,?,?,?)", servername, channelname, username, datesent, messageId, message, null); database.insert("INSERT INTO log_message VALUES(?,?,?,?,?,?,?)", servername, channelname, username, datesent, messageId, message, null);
} catch (SQLException e) { } catch (SQLException e) {
end();
LogHandler.log(e); LogHandler.log(e);
} }
end(); end();
} }
public int logEdit(String messageId, String newMessage) { public void logEdit(String messageId, String newMessage) {
try { try {
int result = database.update("UPDATE log_message SET editedMessage = ? WHERE messageId = ?", newMessage, messageId); database.update("UPDATE log_message SET editedMessage = ? WHERE messageId = ?", newMessage, messageId);
end();
return result;
} catch (SQLException e) { } catch (SQLException e) {
end();
LogHandler.log(e); LogHandler.log(e);
} }
return -1; end();
} }
public MMessage getMessage(String messageId) { public MMessage getMessage(String messageId) {
@@ -60,9 +59,11 @@ public class DBMessage extends AbstractController {
} catch (SQLException e) { } catch (SQLException e) {
end();
LogHandler.log(e); LogHandler.log(e);
} }
end();
return null; return null;
} }
@@ -79,6 +80,7 @@ public class DBMessage extends AbstractController {
} }
} catch (SQLException e) { } catch (SQLException e) {
end();
LogHandler.log(e); LogHandler.log(e);
} }
@@ -96,6 +98,7 @@ public class DBMessage extends AbstractController {
} }
} catch (SQLException e) { } catch (SQLException e) {
end();
LogHandler.log(e); LogHandler.log(e);
} }
@@ -113,6 +116,7 @@ public class DBMessage extends AbstractController {
} }
} catch (SQLException e) { } catch (SQLException e) {
end();
LogHandler.log(e); LogHandler.log(e);
} }
@@ -130,6 +134,7 @@ public class DBMessage extends AbstractController {
} }
} catch (SQLException e) { } catch (SQLException e) {
end();
LogHandler.log(e); LogHandler.log(e);
} }
@@ -147,6 +152,7 @@ public class DBMessage extends AbstractController {
} }
} catch (SQLException e) { } catch (SQLException e) {
end();
e.printStackTrace(); e.printStackTrace();
} }
@@ -14,8 +14,10 @@ public class DBSettings extends AbstractController {
} }
public String getLoggingChannel(String serverId) { public String getLoggingChannel(String serverId) {
if (serverId.isEmpty() || serverId == null) if (serverId.isEmpty() || serverId == null) {
end();
return null; return null;
}
try { try {
ResultSet rs = database.prepareAndExecute("SELECT channelId FROM logging WHERE serverId = ? AND enabled = 1", serverId); ResultSet rs = database.prepareAndExecute("SELECT channelId FROM logging WHERE serverId = ? AND enabled = 1", serverId);
@@ -24,7 +26,7 @@ public class DBSettings extends AbstractController {
return rs.getString(1); return rs.getString(1);
} }
} catch (SQLException e) { } catch (SQLException e) {
// TODO Auto-generated catch block end();
e.printStackTrace(); e.printStackTrace();
} }
@@ -40,7 +42,7 @@ public class DBSettings extends AbstractController {
return rs.getString(1); return rs.getString(1);
} }
} catch (SQLException e) { } catch (SQLException e) {
// TODO Auto-generated catch block end();
e.printStackTrace(); e.printStackTrace();
} }
@@ -56,7 +58,7 @@ public class DBSettings extends AbstractController {
return rs.getString(1); return rs.getString(1);
} }
} catch (SQLException e) { } catch (SQLException e) {
// TODO Auto-generated catch block end();
e.printStackTrace(); e.printStackTrace();
} }
@@ -72,7 +74,7 @@ public class DBSettings extends AbstractController {
return rs.getString(1); return rs.getString(1);
} }
} catch (SQLException e) { } catch (SQLException e) {
// TODO Auto-generated catch block end();
e.printStackTrace(); e.printStackTrace();
} }
@@ -46,14 +46,12 @@ public class NewCommandListener extends ListenerAdapter {
if (event.isFromType(ChannelType.TEXT)) { if (event.isFromType(ChannelType.TEXT)) {
new Thread(() -> { new Thread(() -> {
((DBMessage) DBController.get("message")).log(event.getGuild().getName(), event.getTextChannel().getName(), ((DBMessage) DBController.get("message")).log(event.getGuild().getName(), event.getTextChannel().getName(),
event.getMember().getUser().getName(), event.getMember().getUser().getName(),
event.getMessage().getCreationTime().format(DateTimeFormatter.ofPattern(Settings.DATETIMEFORMAT)), event.getMessage().getId(), event.getMessage().getCreationTime().format(DateTimeFormatter.ofPattern(Settings.DATETIMEFORMAT)), event.getMessage().getId(),
event.getMessage().getContentDisplay().trim()); event.getMessage().getContentDisplay().trim());
}).start(); }, "log_thread").start();
} }
@@ -83,8 +81,6 @@ public class NewCommandListener extends ListenerAdapter {
c.execute(event, msg.replaceFirst("(" + cmd + ")(\\ )?", "")); c.execute(event, msg.replaceFirst("(" + cmd + ")(\\ )?", ""));
} }
} }
} }
} else { } else {