Implement create(), confirm() and match() (2.1.1.1)

This commit is contained in:
Tobias Eidelpes 2021-04-27 10:59:09 +02:00
parent dd1e95d6aa
commit a36f812edf

View File

@ -0,0 +1,189 @@
package dst.ass2.service.trip.impl;
import dst.ass1.jpa.dao.*;
import dst.ass1.jpa.model.*;
import dst.ass2.service.api.match.IMatchingService;
import dst.ass2.service.api.trip.*;
import javax.annotation.Resource;
import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import java.util.Date;
@Singleton
public class TripService implements ITripService {
@PersistenceContext
private EntityManager entityManager;
@Resource
private IModelFactory modelFactory;
@Resource
private IDAOFactory daoFactory;
@Resource
private IMatchingService matchingService;
@Transactional
@Override
public TripDTO create(Long riderId, Long pickupId, Long destinationId) throws EntityNotFoundException {
IRider rider = getRider(riderId);
ILocation pickup = getLocation(pickupId);
ILocation destination = getLocation(destinationId);
ITrip trip = modelFactory.createTrip();
trip.setRider(rider);
trip.setPickup(pickup);
trip.setDestination(destination);
trip.setState(TripState.CREATED);
entityManager.persist(trip);
TripDTO tripDTO = new TripDTO();
tripDTO.setId(trip.getId());
tripDTO.setRiderId(riderId);
tripDTO.setPickupId(pickupId);
tripDTO.setDestinationId(destinationId);
MoneyDTO fare;
try {
fare = matchingService.calculateFare(tripDTO);
} catch (InvalidTripException e) {
// TODO: print out logging message
fare = null;
}
tripDTO.setFare(fare);
return tripDTO;
}
@Transactional
@Override
public void confirm(Long tripId) throws EntityNotFoundException, IllegalStateException, InvalidTripException {
ITripDAO tripDAO = daoFactory.createTripDAO();
ITrip trip = tripDAO.findById(tripId);
if (trip == null)
throw new EntityNotFoundException("Could not find trip with id " + tripId);
if (trip.getState() != TripState.CREATED)
throw new IllegalStateException("Trip is not in CREATED state");
if (trip.getRider() == null)
throw new IllegalStateException("Trip's rider is not set");
TripDTO tripDTO = new TripDTO();
tripDTO.setPickupId(trip.getPickup().getId());
tripDTO.setDestinationId(trip.getDestination().getId());
matchingService.calculateFare(tripDTO);
matchingService.queueTripForMatching(tripId);
trip.setState(TripState.QUEUED);
entityManager.merge(trip);
}
@Transactional(rollbackOn = {Exception.class})
@Override
public void match(Long tripId, MatchDTO match) throws EntityNotFoundException, DriverNotAvailableException, IllegalStateException {
ITrip trip;
IMatch newMatch;
try {
trip = getTrip(tripId);
if (trip.getState() != TripState.CREATED)
throw new IllegalStateException("Trip is not in CREATED state");
IDriver driver = getDriver(match.getDriverId());
IMatchDAO matchDAO = daoFactory.createMatchDAO();
for (IMatch m : matchDAO.findAll()) {
if (m.getDriver().getId().equals(driver.getId()))
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;
}
entityManager.merge(trip);
entityManager.persist(newMatch);
}
@Override
public void complete(Long tripId, TripInfoDTO tripInfoDTO) throws EntityNotFoundException {
}
@Override
public void cancel(Long tripId) throws EntityNotFoundException {
}
@Override
public boolean addStop(TripDTO trip, Long locationId) throws EntityNotFoundException, IllegalStateException {
return false;
}
@Override
public boolean removeStop(TripDTO trip, Long locationId) throws EntityNotFoundException, IllegalStateException {
return false;
}
@Override
public void delete(Long tripId) throws EntityNotFoundException {
}
@Override
public TripDTO find(Long tripId) {
return null;
}
private IRider getRider(Long riderId) throws EntityNotFoundException {
IRiderDAO riderDAO = daoFactory.createRiderDAO();
IRider rider = riderDAO.findById(riderId);
if (rider == null)
throw new EntityNotFoundException("Could not find rider with id " + riderId);
return rider;
}
private IDriver getDriver(Long driverId) throws EntityNotFoundException {
IDriverDAO driverDAO = daoFactory.createDriverDAO();
IDriver driver = driverDAO.findById(driverId);
if (driver == null)
throw new EntityNotFoundException("Could not find driver with id " + driverId);
return driver;
}
private ILocation getLocation(Long locationId) throws EntityNotFoundException {
ILocationDAO locationDAO = daoFactory.createLocationDAO();
ILocation location = locationDAO.findById(locationId);
if (location == null)
throw new EntityNotFoundException("Could not find driver with id " + locationId);
return location;
}
private ITrip getTrip(Long tripId) throws EntityNotFoundException {
ITripDAO tripDAO = daoFactory.createTripDAO();
ITrip trip = tripDAO.findById(tripId);
if (trip == null)
throw new EntityNotFoundException("Could not find trip with id " + tripId);
return trip;
}
private IVehicle getVehicle(Long vehicleId) throws EntityNotFoundException {
IVehicleDAO vehicleDAO = daoFactory.createVehicleDAO();
IVehicle vehicle = vehicleDAO.findById(vehicleId);
if (vehicle == null)
throw new EntityNotFoundException("Could not find vehicle with id " + vehicleId);
return vehicle;
}
}