Correctly handle weird cases whereby an account may be involved in

multiple open reports of the same category
This commit is contained in:
Keir Nellyer 2016-09-14 14:26:12 +01:00
parent 2be0522599
commit 464099dc87
2 changed files with 42 additions and 35 deletions

View File

@ -1,6 +1,7 @@
package mineplex.core.report;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@ -126,12 +127,17 @@ public class ReportManager
public CompletableFuture<Report> createReport(int reporterId, int suspectId, ReportCategory category, String message)
{
// TODO only allow report if suspect actually did something
return _reportRepository.getOngoingReport(suspectId, category)
.thenCompose(reportIdOptional ->
reportIdOptional.isPresent() ? _reportRepository
.getReport(reportIdOptional.get()) : CompletableFuture
.completedFuture(new Report(suspectId, category))
).whenComplete((report, throwable) ->
return _reportRepository.getOngoingReports(suspectId, category).thenCompose(reportIds ->
{
if (reportIds.size() > 0)
{
return _reportRepository.getReport(Collections.max(reportIds));
}
else
{
return CompletableFuture.completedFuture(new Report(suspectId, category));
}
}).whenComplete((report, throwable) ->
{
if (report != null)
{

View File

@ -445,9 +445,9 @@ public class ReportRepository
return future;
}
public CompletableFuture<Optional<Integer>> getOngoingReport(int accountId, ReportCategory category)
public CompletableFuture<List<Integer>> getOngoingReports(int accountId, ReportCategory category)
{
CompletableFuture<Optional<Integer>> future = CompletableFuture.supplyAsync(() ->
CompletableFuture<List<Integer>> future = CompletableFuture.supplyAsync(() ->
{
try (Connection connection = DBPool.getAccount().getConnection())
{
@ -456,23 +456,24 @@ public class ReportRepository
preparedStatement.setInt(2, category.getId());
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next())
List<Integer> reports = new ArrayList<>();
while (resultSet.next())
{
return Optional.of(resultSet.getInt("id"));
reports.add(resultSet.getInt("id"));
}
return reports;
}
catch (SQLException e)
{
throw new RuntimeException(e);
}
return Optional.empty();
});
future.exceptionally(throwable ->
{
_logger.log(Level.SEVERE, "Error fetching ongoing report for account: " + accountId + ", category: " + category + ".", throwable);
return Optional.empty();
return new ArrayList<>();
});
return future;