From 3765c79ce8ff4d673c427ca5af1114cb21e3d661 Mon Sep 17 00:00:00 2001 From: Dominic Rogetzer Date: Wed, 23 May 2018 16:26:35 +0200 Subject: JF: Remove duplicate code by extracting validation-method [#27241] --- .../service/VehicleServiceImpl.java | 58 +++++----------------- 1 file changed, 13 insertions(+), 45 deletions(-) (limited to 'src') diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/VehicleServiceImpl.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/VehicleServiceImpl.java index 47a2520..da995c4 100644 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/VehicleServiceImpl.java +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/VehicleServiceImpl.java @@ -24,43 +24,7 @@ public class VehicleServiceImpl implements VehicleService { public long add(Vehicle vehicle) throws InvalidVehicleException, ServiceException { - 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 ServiceException("not a Valid type"); - } + validateVehicle(vehicle); try { vehiclePersistence.add(vehicle); } catch (PersistenceException e) { @@ -70,6 +34,18 @@ public class VehicleServiceImpl implements VehicleService { } public Vehicle update(Vehicle vehicle) throws InvalidVehicleException, ServiceException { + validateVehicle(vehicle); + try { + vehiclePersistence.update(vehicle); + } catch (ElementNotFoundException e) { + throw new ServiceException("Element not found"); + } catch (PersistenceException e) { + throw new ServiceException(e); + } + return vehicle; + } + + private void validateVehicle(Vehicle vehicle) throws InvalidVehicleException, ServiceException { switch (vehicle.type()) { case RTW: if (vehicle.constructionType() == ConstructionType.NORMAL) { @@ -107,14 +83,6 @@ public class VehicleServiceImpl implements VehicleService { default: throw new ServiceException("not a Valid type"); } - try { - vehiclePersistence.update(vehicle); - } catch (ElementNotFoundException e) { - throw new ServiceException("Element not found"); - } catch (PersistenceException e) { - throw new ServiceException(e); - } - return vehicle; } @Override -- cgit v1.2.3-70-g09d2