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();
_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));

View File

@ -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<String, UserPreferences> preferences)
@ -69,32 +42,24 @@ public class PreferencesRepository
try
{
synchronized (_connectionLock)
preparedStatement = getConnection().prepareStatement(UPDATE_ACCOUNT_PREFERENCES);
for (Entry<String, UserPreferences> 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<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.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;
}