Properly requeue on exception

This commit is contained in:
Tobias Eidelpes 2021-04-27 22:41:55 +02:00
parent 6d319cd378
commit 05d4c2ded6

View File

@ -85,36 +85,37 @@ public class TripService implements ITripService {
public void match(Long tripId, MatchDTO match) throws EntityNotFoundException, DriverNotAvailableException, IllegalStateException {
ITrip trip;
IMatch newMatch;
IDriver driver;
try {
trip = getTrip(tripId);
IDriver driver = getDriver(match.getDriverId());
IMatchDAO matchDAO = daoFactory.createMatchDAO();
for (IMatch m : matchDAO.findAll()) {
if (m.getDriver() != null && m.getDriver().getId().equals(driver.getId()))
driver = getDriver(match.getDriverId());
List<IMatch> matches = daoFactory.createMatchDAO().findAll();
for (IMatch existingMatch : matches) {
if (existingMatch.getDriver().getId().equals(match.getDriverId())) {
throw new DriverNotAvailableException("Driver with id " + driver.getId() + " is already busy");
}
}
IVehicle vehicle = getVehicle(match.getVehicleId());
newMatch = modelFactory.createMatch();
newMatch.setDate(new Date());
newMatch.setTrip(trip);
newMatch.setDriver(driver);
newMatch.setVehicle(vehicle);
IMoney fare = modelFactory.createMoney();
fare.setCurrency(match.getFare().getCurrency());
fare.setValue(match.getFare().getValue());
newMatch.setFare(fare);
trip.setMatch(newMatch);
trip.setState(TripState.MATCHED);
} catch (Exception e) {
// Requeue match
matchingService.queueTripForMatching(tripId);
throw e;
}
IVehicle vehicle = getVehicle(match.getVehicleId());
newMatch = modelFactory.createMatch();
newMatch.setDate(new Date());
newMatch.setTrip(trip);
newMatch.setDriver(driver);
newMatch.setVehicle(vehicle);
IMoney fare = modelFactory.createMoney();
fare.setCurrency(match.getFare().getCurrency());
fare.setValue(match.getFare().getValue());
newMatch.setFare(fare);
trip.setMatch(newMatch);
trip.setState(TripState.MATCHED);
entityManager.merge(trip);
entityManager.persist(newMatch);
}