Fix reports duplicating themselves and causing massive issues

This commit is contained in:
Keir Nellyer 2016-08-30 23:18:12 +01:00
parent 96b74444e5
commit a7d224b7e4

View File

@ -45,8 +45,9 @@ import org.apache.commons.lang3.StringUtils;
public class ReportRepository
{
private static final String INSERT_REPORT = "INSERT INTO reports (suspectId, categoryId, snapshotId)\n" +
"VALUES (?, ?, ?)\n" +
"ON DUPLICATE KEY UPDATE snapshotId = ?;";
"VALUES (?, ?, ?);";
private static final String UPDATE_REPORT = "UPDATE reports SET snapshotId = ? WHERE id = ?;";
private static final String SET_REPORT_MESSAGE = "REPLACE INTO reportReasons (reportId, reporterId, reason, `server`, weight, `time`)" +
" VALUES (?, ?, ?, ?, ?, ?);";
@ -488,36 +489,40 @@ public class ReportRepository
{
try (Connection connection = DBPool.getAccount().getConnection())
{
PreparedStatement insertUpdateReportStatement = connection.prepareStatement(INSERT_REPORT, Statement.RETURN_GENERATED_KEYS);
insertUpdateReportStatement.setInt(1, report.getSuspectId());
insertUpdateReportStatement.setInt(2, report.getCategory().getId());
Optional<Integer> snapshotIdOptional = report.getSnapshotId();
if (snapshotIdOptional.isPresent())
{
int snapshotId = snapshotIdOptional.get();
insertUpdateReportStatement.setInt(3, snapshotId);
insertUpdateReportStatement.setInt(4, snapshotId);
}
else
{
insertUpdateReportStatement.setNull(3, Types.INTEGER);
insertUpdateReportStatement.setNull(4, Types.INTEGER);
}
insertUpdateReportStatement.executeUpdate();
Optional<Long> reportIdOptional = report.getId();
Optional<Integer> snapshotIdOptional = report.getSnapshotId();
long reportId;
if (reportIdOptional.isPresent())
{
reportId = reportIdOptional.get();
if (snapshotIdOptional.isPresent())
{
PreparedStatement updateReportStatement = connection.prepareStatement(UPDATE_REPORT);
updateReportStatement.setInt(1, snapshotIdOptional.get());
updateReportStatement.setLong(2, reportId);
updateReportStatement.execute();
}
}
else
{
ResultSet resultSet = insertUpdateReportStatement.getGeneratedKeys();
PreparedStatement insertReportStatement = connection.prepareStatement(INSERT_REPORT, Statement.RETURN_GENERATED_KEYS);
insertReportStatement.setInt(1, report.getSuspectId());
insertReportStatement.setInt(2, report.getCategory().getId());
if (snapshotIdOptional.isPresent())
{
insertReportStatement.setInt(3, snapshotIdOptional.get());
}
else
{
insertReportStatement.setNull(3, Types.INTEGER);
}
insertReportStatement.executeUpdate();
ResultSet resultSet = insertReportStatement.getGeneratedKeys();
if (resultSet.next())
{
reportId = resultSet.getLong(1);