package at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.service; import at.ac.tuwien.sepm.assignment.groupphase.exception.ElementNotFoundException; import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidVehicleException; import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException; import at.ac.tuwien.sepm.assignment.groupphase.exception.ServiceException; import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dao.VehicleDAO; import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Vehicle; import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Vehicle.ConstructionType; import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Vehicle.Status; import java.util.EnumSet; import java.util.Set; import java.util.stream.Collectors; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; @Service public class VehicleServiceImpl implements VehicleService { private VehicleDAO vehiclePersistence; public VehicleServiceImpl(VehicleDAO vehiclePersistence) { this.vehiclePersistence = vehiclePersistence; } public long add(Vehicle vehicle) throws InvalidVehicleException, ServiceException { if (!CollectionUtils.isEmpty(vehicle.registrations())) { throw new InvalidVehicleException( "Fahrzeug kann nicht mit Anmeldungen erstellt werden"); } validateVehicle(vehicle); try { vehiclePersistence.add(vehicle); } catch (PersistenceException e) { throw new ServiceException(e); } return 0; } public Vehicle update(Vehicle vehicle) throws InvalidVehicleException, ServiceException { validateVehicle(vehicle); try { vehiclePersistence.update(vehicle); } catch (ElementNotFoundException e) { throw new ServiceException("Element not found", e); } catch (PersistenceException e) { throw new ServiceException(e); } return vehicle; } protected static void validateVehicle(Vehicle vehicle) throws InvalidVehicleException { switch (vehicle.type()) { case RTW: if (vehicle.constructionType() == ConstructionType.NORMAL) { throw new InvalidVehicleException("RTW darf kein Normales Dach haben"); } else if (vehicle.constructionType() == ConstructionType.MITTELHOCHDACH) { throw new InvalidVehicleException("RTW darf kein Mittelhochdach haben"); } break; case KTW: if (vehicle.constructionType() == ConstructionType.NORMAL) { throw new InvalidVehicleException("KTW darf kein Normales Dach haben"); } break; case KTW_B: if (vehicle.constructionType() == ConstructionType.NORMAL) { throw new InvalidVehicleException("KTW-B darf kein Normales Dach haben"); } break; case NEF: if (vehicle.constructionType() == ConstructionType.MITTELHOCHDACH) { throw new InvalidVehicleException("NEF darf kein Mittelhochdach haben"); } else if (vehicle.constructionType() == ConstructionType.HOCHDACH) { throw new InvalidVehicleException("NEF darf kein Hochdach haben"); } break; case NAH: if (vehicle.constructionType() == ConstructionType.MITTELHOCHDACH) { throw new InvalidVehicleException("NEF darf kein Mittelhochdach haben"); } else if (vehicle.constructionType() == ConstructionType.HOCHDACH) { throw new InvalidVehicleException("NEF darf kein Hochdach haben"); } break; case BKTW: break; default: throw new IllegalStateException("BUG: invalid vehicle type" + vehicle.type()); } } @Override public Set list(EnumSet statuses) throws ServiceException { if (statuses == null) { throw new ServiceException("Statuses may not be null"); } Set vehicles; try { vehicles = vehiclePersistence.list(); } catch (PersistenceException e) { throw new ServiceException(e); } return vehicles.stream() .filter(vehicle -> statuses.contains(vehicle.status())) .collect(Collectors.toSet()); } @Override public void remove(long id) throws InvalidVehicleException, ServiceException { throw new UnsupportedOperationException(); } }