Converted PreferencesManager to RepositoryBase

This commit is contained in:
Jonathan Williams 2014-08-07 00:43:54 -07:00
parent 6d86f403ac
commit 055e4c5895
2 changed files with 41 additions and 124 deletions

View File

@ -39,7 +39,7 @@ public class PreferencesManager extends MiniClientPlugin<UserPreferences>
setupConfigValues(); setupConfigValues();
_repository = new PreferencesRepository(plugin.getConfig().getString("preferences.connectionurl")); _repository = new PreferencesRepository(plugin, plugin.getConfig().getString("preferences.connectionurl"));
_shop = new PreferencesShop(this, clientManager, donationManager); _shop = new PreferencesShop(this, clientManager, donationManager);
AddCommand(new PreferencesCommand(this)); AddCommand(new PreferencesCommand(this));

View File

@ -1,66 +1,39 @@
package mineplex.core.preferences; package mineplex.core.preferences;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.UUID; import java.util.UUID;
import mineplex.core.common.util.NautHashMap; import org.bukkit.plugin.java.JavaPlugin;
public class PreferencesRepository import mineplex.core.common.util.NautHashMap;
{ import mineplex.core.database.RepositoryBase;
private static Object _connectionLock = new Object(); import mineplex.core.database.ResultSetCallable;
import mineplex.core.database.column.ColumnVarChar;
private String _connectionString;
private String _userName = "root"; public class PreferencesRepository extends RepositoryBase
private String _password = "tAbechAk3wR7tuTh"; {
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 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 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 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 static String UPDATE_ACCOUNT_PREFERENCES = "UPDATE accountPreferences SET games = ?, visibility = ?, showChat = ?, friendChat = ?, privateMessaging = ?, partyRequests = ?, invisibility = ?, forcefield = ? WHERE uuid=?;";
private Connection _connection = null; public PreferencesRepository(JavaPlugin plugin, String connectionString)
public PreferencesRepository(String connectionUrl)
{ {
_connectionString = connectionUrl; super(plugin, connectionString, "root", "tAbechAk3wR7tuTh");
initialize();
} }
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<String, UserPreferences> preferences) public void saveUserPreferences(NautHashMap<String, UserPreferences> preferences)
@ -69,32 +42,24 @@ public class PreferencesRepository
try try
{ {
synchronized (_connectionLock) preparedStatement = getConnection().prepareStatement(UPDATE_ACCOUNT_PREFERENCES);
for (Entry<String, UserPreferences> entry : preferences.entrySet())
{ {
if (_connection.isClosed()) preparedStatement.setBoolean(1, entry.getValue().HubGames);
{ preparedStatement.setBoolean(2, entry.getValue().ShowPlayers);
_connection = DriverManager.getConnection(_connectionString, _userName, _password); 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); preparedStatement.addBatch();
for (Entry<String, UserPreferences> 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.executeBatch();
} }
catch (Exception exception) catch (Exception exception)
{ {
@ -116,35 +81,17 @@ public class PreferencesRepository
} }
} }
public UserPreferences loadClientInformation(UUID uuid) public UserPreferences loadClientInformation(final UUID uuid)
{ {
ResultSet resultSet = null; final UserPreferences preferences = new UserPreferences();
PreparedStatement preparedStatement = null;
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()) if (!resultSet.next())
{ {
preparedStatement.close(); executeUpdate(INSERT_ACCOUNT, new ColumnVarChar("uuid", 100, uuid.toString()));
preparedStatement = _connection.prepareStatement(INSERT_ACCOUNT);
preparedStatement.setString(1, uuid.toString());
preparedStatement.execute();
return new UserPreferences();
} }
else else
{ {
@ -158,37 +105,7 @@ public class PreferencesRepository
preferences.HubForcefield = resultSet.getBoolean(8); preferences.HubForcefield = resultSet.getBoolean(8);
} }
} }
} }, new ColumnVarChar("uuid", 100, uuid.toString()));
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();
}
}
}
return preferences; return preferences;
} }