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
+72 -56

No files matched your search

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