From 055e4c5895195ed68e4d8d6a1136e8044a8a90dc Mon Sep 17 00:00:00 2001 From: Jonathan Williams Date: Thu, 7 Aug 2014 00:43:54 -0700 Subject: [PATCH] Converted PreferencesManager to RepositoryBase --- .../core/preferences/PreferencesManager.java | 2 +- .../preferences/PreferencesRepository.java | 163 +++++------------- 2 files changed, 41 insertions(+), 124 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesManager.java b/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesManager.java index 96d089e18..391c334c5 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesManager.java @@ -39,7 +39,7 @@ public class PreferencesManager extends MiniClientPlugin setupConfigValues(); - _repository = new PreferencesRepository(plugin.getConfig().getString("preferences.connectionurl")); + _repository = new PreferencesRepository(plugin, plugin.getConfig().getString("preferences.connectionurl")); _shop = new PreferencesShop(this, clientManager, donationManager); AddCommand(new PreferencesCommand(this)); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesRepository.java index a4ac0be2e..3f764b6f2 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesRepository.java @@ -1,66 +1,39 @@ package mineplex.core.preferences; -import java.sql.Connection; -import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Map.Entry; import java.util.UUID; -import mineplex.core.common.util.NautHashMap; +import org.bukkit.plugin.java.JavaPlugin; -public class PreferencesRepository -{ - private static Object _connectionLock = new Object(); - - private String _connectionString; - private String _userName = "root"; - private String _password = "tAbechAk3wR7tuTh"; - +import mineplex.core.common.util.NautHashMap; +import mineplex.core.database.RepositoryBase; +import mineplex.core.database.ResultSetCallable; +import mineplex.core.database.column.ColumnVarChar; + +public class PreferencesRepository extends RepositoryBase +{ private static String CREATE_ACCOUNT_TABLE = "CREATE TABLE IF NOT EXISTS accountPreferences (id INT NOT NULL AUTO_INCREMENT, uuid VARCHAR(256), games BOOL NOT NULL DEFAULT 1, visibility BOOL NOT NULL DEFAULT 1, showChat BOOL NOT NULL DEFAULT 1, friendChat BOOL NOT NULL DEFAULT 1, privateMessaging BOOL NOT NULL DEFAULT 1, partyRequests BOOL NOT NULL DEFAULT 0, invisibility BOOL NOT NULL DEFAULT 0, forcefield BOOL NOT NULL DEFAULT 0, PRIMARY KEY (id), UNIQUE INDEX uuid_index (uuid));"; private static String INSERT_ACCOUNT = "INSERT INTO accountPreferences (uuid) VALUES (?) ON DUPLICATE KEY UPDATE uuid=uuid;"; private static String RETRIEVE_ACCOUNT_PREFERENCES = "SELECT games, visibility, showChat, friendChat, privateMessaging, partyRequests, invisibility, forcefield FROM accountPreferences WHERE uuid = ?;"; private static String UPDATE_ACCOUNT_PREFERENCES = "UPDATE accountPreferences SET games = ?, visibility = ?, showChat = ?, friendChat = ?, privateMessaging = ?, partyRequests = ?, invisibility = ?, forcefield = ? WHERE uuid=?;"; - - private Connection _connection = null; - - public PreferencesRepository(String connectionUrl) + + public PreferencesRepository(JavaPlugin plugin, String connectionString) { - _connectionString = connectionUrl; - - initialize(); + super(plugin, connectionString, "root", "tAbechAk3wR7tuTh"); } - public void initialize() + @Override + protected void initialize() + { + executeUpdate(CREATE_ACCOUNT_TABLE); + } + + @Override + protected void update() { - PreparedStatement preparedStatement = null; - - try - { - _connection = DriverManager.getConnection(_connectionString, _userName, _password); - - preparedStatement = _connection.prepareStatement(CREATE_ACCOUNT_TABLE); - preparedStatement.execute(); - } - catch (Exception exception) - { - exception.printStackTrace(); - } - finally - { - if (preparedStatement != null) - { - try - { - preparedStatement.close(); - } - catch (SQLException e) - { - e.printStackTrace(); - } - } - } } public void saveUserPreferences(NautHashMap preferences) @@ -69,32 +42,24 @@ public class PreferencesRepository try { - synchronized (_connectionLock) + preparedStatement = getConnection().prepareStatement(UPDATE_ACCOUNT_PREFERENCES); + + for (Entry entry : preferences.entrySet()) { - if (_connection.isClosed()) - { - _connection = DriverManager.getConnection(_connectionString, _userName, _password); - } + preparedStatement.setBoolean(1, entry.getValue().HubGames); + preparedStatement.setBoolean(2, entry.getValue().ShowPlayers); + preparedStatement.setBoolean(3, entry.getValue().ShowChat); + preparedStatement.setBoolean(4, entry.getValue().FriendChat); + preparedStatement.setBoolean(5, entry.getValue().PrivateMessaging); + preparedStatement.setBoolean(6, entry.getValue().PartyRequests); + preparedStatement.setBoolean(7, entry.getValue().Invisibility); + preparedStatement.setBoolean(8, entry.getValue().HubForcefield); + preparedStatement.setString(9, entry.getKey()); - preparedStatement = _connection.prepareStatement(UPDATE_ACCOUNT_PREFERENCES); - - for (Entry entry : preferences.entrySet()) - { - preparedStatement.setBoolean(1, entry.getValue().HubGames); - preparedStatement.setBoolean(2, entry.getValue().ShowPlayers); - preparedStatement.setBoolean(3, entry.getValue().ShowChat); - preparedStatement.setBoolean(4, entry.getValue().FriendChat); - preparedStatement.setBoolean(5, entry.getValue().PrivateMessaging); - preparedStatement.setBoolean(6, entry.getValue().PartyRequests); - preparedStatement.setBoolean(7, entry.getValue().Invisibility); - preparedStatement.setBoolean(8, entry.getValue().HubForcefield); - preparedStatement.setString(9, entry.getKey()); - - preparedStatement.addBatch(); - } - - preparedStatement.executeBatch(); + preparedStatement.addBatch(); } + + preparedStatement.executeBatch(); } catch (Exception exception) { @@ -116,35 +81,17 @@ public class PreferencesRepository } } - public UserPreferences loadClientInformation(UUID uuid) + public UserPreferences loadClientInformation(final UUID uuid) { - ResultSet resultSet = null; - PreparedStatement preparedStatement = null; - UserPreferences preferences = new UserPreferences(); + final UserPreferences preferences = new UserPreferences(); - try + executeQuery(RETRIEVE_ACCOUNT_PREFERENCES, new ResultSetCallable() { - synchronized (_connectionLock) + public void processResultSet(ResultSet resultSet) throws SQLException { - if (_connection.isClosed()) - { - _connection = DriverManager.getConnection(_connectionString, _userName, _password); - } - - preparedStatement = _connection.prepareStatement(RETRIEVE_ACCOUNT_PREFERENCES); - preparedStatement.setString(1, uuid.toString()); - - resultSet = preparedStatement.executeQuery(); - if (!resultSet.next()) { - preparedStatement.close(); - preparedStatement = _connection.prepareStatement(INSERT_ACCOUNT); - preparedStatement.setString(1, uuid.toString()); - - preparedStatement.execute(); - - return new UserPreferences(); + executeUpdate(INSERT_ACCOUNT, new ColumnVarChar("uuid", 100, uuid.toString())); } else { @@ -158,37 +105,7 @@ public class PreferencesRepository preferences.HubForcefield = resultSet.getBoolean(8); } } - } - catch (Exception exception) - { - exception.printStackTrace(); - } - finally - { - if (preparedStatement != null) - { - try - { - preparedStatement.close(); - } - catch (SQLException e) - { - e.printStackTrace(); - } - } - - if (resultSet != null) - { - try - { - resultSet.close(); - } - catch (SQLException e) - { - e.printStackTrace(); - } - } - } + }, new ColumnVarChar("uuid", 100, uuid.toString())); return preferences; }