Add support for assigning tokens to snapshots (excluding actual

generation part)
This commit is contained in:
Keir Nellyer 2016-10-11 00:22:29 +01:00
parent e4d3621512
commit 5c5c159ee3
4 changed files with 60 additions and 15 deletions

View File

@ -8,14 +8,14 @@ public class SnapshotMetadata
protected String _token = null;
protected Integer _creatorId = null;
public SnapshotMetadata(int id, String token, int creatorId)
public SnapshotMetadata(int id, String token, Integer creatorId)
{
_id = id;
_token = token;
_creatorId = creatorId;
}
public SnapshotMetadata(int id, int creatorId)
public SnapshotMetadata(int id, Integer creatorId)
{
_id = id;
_creatorId = creatorId;

View File

@ -20,17 +20,24 @@ import mineplex.serverdata.database.DBPool;
*/
public class SnapshotRepository
{
private static final String URL_PREFIX = "http://report.mineplex.com/chatsnap/view.php?id=";
public static String getURL(long reportId)
{
return URL_PREFIX + reportId;
}
private static final String URL_PREFIX = "http://report.mineplex.com/chatsnap/view.php?id=";
public static String generateToken()
{
// TODO: implement
return null;
}
private static final String INSERT_SNAPSHOT = "INSERT INTO snapshots (creator) VALUES (?);";
private static final String INSERT_SNAPSHOT = "INSERT INTO snapshots (token, creator) VALUES (?, ?);";
private static final String INSERT_MESSAGE = "INSERT INTO snapshotMessages (senderId, `server`, `time`, message, snapshotType) VALUES (?, ?, ?, ?, ?);";
private static final String INSERT_MESSAGE_RECIPIENT = "INSERT INTO snapshotRecipients (messageId, recipientId) VALUES (?, ?);";
private static final String INSERT_MESSAGE_MAPPING = "INSERT INTO snapshotMessageMap (snapshotId, messageId) VALUES (?, ?);";
private static final String GET_ID_FROM_TOKEN = "SELECT snapshots.id FROM snapshots WHERE snapshots.token = ?;";
private final String _serverName;
private final Logger _logger;
@ -60,15 +67,15 @@ public class SnapshotRepository
});
}
public CompletableFuture<Integer> saveSnapshot(Collection<SnapshotMessage> messages)
public CompletableFuture<SnapshotMetadata> saveSnapshot(Collection<SnapshotMessage> messages)
{
return CompletableFuture.supplyAsync(() ->
{
try (Connection connection = DBPool.getAccount().getConnection())
{
int snapshotId = createSnapshot(connection, null);
insertMessages(snapshotId, messages, connection);
return snapshotId;
SnapshotMetadata snapshotMetadata = createSnapshot(connection, null);
insertMessages(snapshotMetadata.getId(), messages, connection);
return snapshotMetadata;
}
catch (SQLException e)
{
@ -77,7 +84,7 @@ public class SnapshotRepository
});
}
public CompletableFuture<Integer> createSnapshot(Integer creatorAccountId)
public CompletableFuture<SnapshotMetadata> createSnapshot(Integer creatorAccountId)
{
return CompletableFuture.supplyAsync(() ->
{
@ -92,17 +99,19 @@ public class SnapshotRepository
});
}
private int createSnapshot(Connection connection, Integer creatorAccount) throws SQLException
private SnapshotMetadata createSnapshot(Connection connection, Integer creatorAccount) throws SQLException
{
String token = getToken(connection);
PreparedStatement insertSnapshotStatement = connection.prepareStatement(INSERT_SNAPSHOT, new String[]{"id"});
insertSnapshotStatement.setString(1, token); // TODO: correct data type
if (creatorAccount != null)
{
insertSnapshotStatement.setInt(1, creatorAccount);
insertSnapshotStatement.setInt(2, creatorAccount);
}
else
{
insertSnapshotStatement.setNull(1, Types.INTEGER);
insertSnapshotStatement.setNull(2, Types.INTEGER);
}
insertSnapshotStatement.execute();
@ -111,7 +120,7 @@ public class SnapshotRepository
{
if (resultSet.next())
{
return resultSet.getInt(1);
return new SnapshotMetadata(resultSet.getInt(1), token, creatorAccount);
}
else
{
@ -120,6 +129,40 @@ public class SnapshotRepository
}
}
private String getToken(Connection connection) throws SQLException
{
String token;
do
{
token = generateToken();
}
while(getByToken(connection, token).isPresent());
return token;
}
private Optional<Integer> getByToken(Connection connection, String token) throws SQLException
{
try (PreparedStatement statement = connection.prepareStatement(GET_ID_FROM_TOKEN))
{
statement.setString(1, token); // TODO: correct data type
try (ResultSet resultSet = statement.executeQuery())
{
if (resultSet.next())
{
int snapshotId = resultSet.getInt("id");
return Optional.of(snapshotId);
}
else
{
return Optional.empty();
}
}
}
}
private void insertMessages(int snapshotId, Collection<SnapshotMessage> messages, Connection connection) throws SQLException
{
try (PreparedStatement insertSnapshotStatement = connection.prepareStatement(INSERT_MESSAGE, new String[]{"id"}))

View File

@ -20,6 +20,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.account.CoreClient;
import mineplex.core.account.CoreClientManager;
import mineplex.core.chatsnap.SnapshotManager;
import mineplex.core.chatsnap.SnapshotMetadata;
import mineplex.core.chatsnap.command.PushSnapshotsCommand;
import mineplex.core.chatsnap.command.PushSnapshotsHandler;
import mineplex.core.command.CommandCenter;
@ -149,8 +150,8 @@ public class ReportManager
// create snapshot id ahead of time
if (category == ReportCategory.CHAT_ABUSE)
{
int snapshotId = _snapshotManager.getSnapshotRepository().createSnapshot(null).join();
report.setSnapshotId(snapshotId);
SnapshotMetadata snapshotMetadata = _snapshotManager.getSnapshotRepository().createSnapshot(null).join();
report.setSnapshotId(snapshotMetadata.getId());
}
saveReport(report).join();

View File

@ -1,3 +1,4 @@
ALTER TABLE Account.snapshots ADD token CHAR(6);
CREATE UNIQUE INDEX snapshots_token_uindex ON snapshots (token);
ALTER TABLE Account.snapshots
MODIFY COLUMN creator INT(11) AFTER token;