aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/service/VehicleServiceImpl.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/service/VehicleServiceImpl.java')
-rw-r--r--src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/service/VehicleServiceImpl.java116
1 files changed, 116 insertions, 0 deletions
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/service/VehicleServiceImpl.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/service/VehicleServiceImpl.java
new file mode 100644
index 0000000..a68720d
--- /dev/null
+++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/service/VehicleServiceImpl.java
@@ -0,0 +1,116 @@
+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<Vehicle> list(EnumSet<Status> statuses) throws ServiceException {
+ if (statuses == null) {
+ throw new ServiceException("Statuses may not be null");
+ }
+
+ Set<Vehicle> 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();
+ }
+}