Implement removeStop() and find() (2.1.1.1)

This commit is contained in:
Tobias Eidelpes 2021-04-27 15:03:12 +02:00
parent b19a2d2ca2
commit 852527755c
2 changed files with 50 additions and 8 deletions

View File

@ -3,6 +3,7 @@ package dst.ass1.jpa.model.impl;
import dst.ass1.jpa.model.IPreferences;
import dst.ass1.jpa.model.IRider;
import dst.ass1.jpa.model.ITrip;
import org.hibernate.annotations.Cascade;
import javax.persistence.*;
import java.util.ArrayList;
@ -35,6 +36,8 @@ public class Rider extends PlatformUser implements IRider {
@JoinColumn(name = I_PREFERENCES, unique = true)
private IPreferences preferences;
@Cascade(org.hibernate.annotations.CascadeType.ALL)
@JoinColumn(name = I_TRIP)
@OneToMany(targetEntity = Trip.class)
private Collection<ITrip> trips = new ArrayList<>();

View File

@ -14,6 +14,7 @@ import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@Named("ITripService")
@Singleton
@ -166,6 +167,31 @@ public class TripService implements ITripService {
} catch (InvalidTripException e) {
trip.setFare(null);
}
updateStops(trip, savedTrip);
return true;
}
@Transactional
@Override
public boolean removeStop(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);
if (trip.getStops().remove(locationId)) {
try {
trip.setFare(matchingService.calculateFare(trip));
} catch (InvalidTripException e) {
trip.setFare(null);
}
updateStops(trip, savedTrip);
return true;
}
return false;
}
private void updateStops(TripDTO trip, ITrip savedTrip) {
ILocationDAO locationDAO = daoFactory.createLocationDAO();
List<ILocation> locations = new ArrayList<>();
for (Long location : trip.getStops()) {
@ -173,23 +199,36 @@ public class TripService implements ITripService {
}
savedTrip.setStops(locations);
entityManager.merge(savedTrip);
return true;
}
@Override
public boolean removeStop(TripDTO trip, Long locationId) throws EntityNotFoundException, IllegalStateException {
return false;
}
@Transactional
@Override
public void delete(Long tripId) throws EntityNotFoundException {
ITrip trip = getTrip(tripId);
entityManager.remove(trip);
}
@Override
public TripDTO find(Long tripId) {
TripDTO tripDTO = new TripDTO();
ITrip trip;
try {
trip = getTrip(tripId);
} catch (EntityNotFoundException e) {
return null;
}
tripDTO.setId(tripId);
tripDTO.setPickupId(trip.getPickup().getId());
tripDTO.setDestinationId(trip.getDestination().getId());
tripDTO.setRiderId(trip.getRider().getId());
tripDTO.setStops(trip.getStops().stream().map(ILocation::getId).collect(Collectors.toList()));
try {
tripDTO.setFare(matchingService.calculateFare(tripDTO));
} catch (InvalidTripException e) {
tripDTO.setFare(null);
}
return tripDTO;
}
private IRider getRider(Long riderId) throws EntityNotFoundException {
IRiderDAO riderDAO = daoFactory.createRiderDAO();