Implement cancel() and addStop() (2.1.1.1)
This commit is contained in:
parent
2b4ee8acbf
commit
b19a2d2ca2
@ -6,12 +6,16 @@ import dst.ass2.service.api.match.IMatchingService;
|
|||||||
import dst.ass2.service.api.trip.*;
|
import dst.ass2.service.api.trip.*;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import javax.inject.Named;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.PersistenceContext;
|
import javax.persistence.PersistenceContext;
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Named("ITripService")
|
||||||
@Singleton
|
@Singleton
|
||||||
public class TripService implements ITripService {
|
public class TripService implements ITripService {
|
||||||
@PersistenceContext
|
@PersistenceContext
|
||||||
@ -82,13 +86,10 @@ public class TripService implements ITripService {
|
|||||||
IMatch newMatch;
|
IMatch newMatch;
|
||||||
try {
|
try {
|
||||||
trip = getTrip(tripId);
|
trip = getTrip(tripId);
|
||||||
if (trip.getState() != TripState.CREATED)
|
|
||||||
throw new IllegalStateException("Trip is not in CREATED state");
|
|
||||||
|
|
||||||
IDriver driver = getDriver(match.getDriverId());
|
IDriver driver = getDriver(match.getDriverId());
|
||||||
IMatchDAO matchDAO = daoFactory.createMatchDAO();
|
IMatchDAO matchDAO = daoFactory.createMatchDAO();
|
||||||
for (IMatch m : matchDAO.findAll()) {
|
for (IMatch m : matchDAO.findAll()) {
|
||||||
if (m.getDriver().getId().equals(driver.getId()))
|
if (m.getDriver() != null && m.getDriver().getId().equals(driver.getId()))
|
||||||
throw new DriverNotAvailableException("Driver with id " + driver.getId() + " is already busy");
|
throw new DriverNotAvailableException("Driver with id " + driver.getId() + " is already busy");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,6 +134,7 @@ public class TripService implements ITripService {
|
|||||||
// Set trip and tripInfo
|
// Set trip and tripInfo
|
||||||
tripInfo.setTrip(trip);
|
tripInfo.setTrip(trip);
|
||||||
trip.setTripInfo(tripInfo);
|
trip.setTripInfo(tripInfo);
|
||||||
|
trip.setState(TripState.COMPLETED);
|
||||||
entityManager.persist(tripInfo);
|
entityManager.persist(tripInfo);
|
||||||
entityManager.merge(trip);
|
entityManager.merge(trip);
|
||||||
}
|
}
|
||||||
@ -140,12 +142,38 @@ public class TripService implements ITripService {
|
|||||||
@Transactional
|
@Transactional
|
||||||
@Override
|
@Override
|
||||||
public void cancel(Long tripId) throws EntityNotFoundException {
|
public void cancel(Long tripId) throws EntityNotFoundException {
|
||||||
|
ITrip trip = getTrip(tripId);
|
||||||
|
trip.setState(TripState.CANCELLED);
|
||||||
|
entityManager.merge(trip);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
@Override
|
@Override
|
||||||
public boolean addStop(TripDTO trip, Long locationId) throws EntityNotFoundException, IllegalStateException {
|
public boolean addStop(TripDTO trip, Long locationId) throws EntityNotFoundException, IllegalStateException {
|
||||||
|
ITrip savedTrip = getTrip(trip.getId());
|
||||||
|
if (savedTrip.getState() != TripState.CREATED)
|
||||||
|
throw new IllegalStateException("Trip with id " + trip.getId() + " is not in CREATED state");
|
||||||
|
getLocation(locationId);
|
||||||
|
|
||||||
|
List<Long> stops = trip.getStops();
|
||||||
|
if (stops.contains(locationId))
|
||||||
return false;
|
return false;
|
||||||
|
// Add the stop
|
||||||
|
stops.add(locationId);
|
||||||
|
// Estimate fare (set to null if failed)
|
||||||
|
try {
|
||||||
|
trip.setFare(matchingService.calculateFare(trip));
|
||||||
|
} catch (InvalidTripException e) {
|
||||||
|
trip.setFare(null);
|
||||||
|
}
|
||||||
|
ILocationDAO locationDAO = daoFactory.createLocationDAO();
|
||||||
|
List<ILocation> locations = new ArrayList<>();
|
||||||
|
for (Long location : trip.getStops()) {
|
||||||
|
locations.add(locationDAO.findById(location));
|
||||||
|
}
|
||||||
|
savedTrip.setStops(locations);
|
||||||
|
entityManager.merge(savedTrip);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user