diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/OperationServiceImpl.java | 90 |
1 files changed, 68 insertions, 22 deletions
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/OperationServiceImpl.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/OperationServiceImpl.java index d07f46f..3c3b91c 100644 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/OperationServiceImpl.java +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/OperationServiceImpl.java @@ -12,7 +12,6 @@ import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidOperationExcepti 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 java.lang.invoke.MethodHandles; import java.time.Instant; import java.util.ArrayList; import java.util.Comparator; @@ -34,7 +33,7 @@ import org.springframework.stereotype.Service; @Service public class OperationServiceImpl implements OperationService { - private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + private static final Logger LOG = LoggerFactory.getLogger(OperationServiceImpl.class); private final OperationDAO operationDAO; private final VehicleDAO vehicleDAO; @@ -49,11 +48,20 @@ public class OperationServiceImpl implements OperationService { @Override public long add(Operation o) throws InvalidOperationException, ServiceException { - if (o.created() != null) throw new InvalidOperationException("Created must not be set"); + if (o.created() != null) { + LOG.info("Invalid Operation: Created must be null"); + throw new InvalidOperationException("Created must not be set"); + } - if (o.severity() != null) throw new InvalidOperationException("Severity must not be set"); + if (o.severity() != null) { + LOG.info("Invalid Operation: Severity must be null"); + throw new InvalidOperationException("Severity must not be set"); + } - if (o.id() != 0) throw new InvalidOperationException("Id must be 0"); + if (o.id() != 0) { + LOG.info("Invalid Operation: OperationId must be 0"); + throw new InvalidOperationException("Id must be 0"); + } if (o.status() != Status.ACTIVE) LOG.warn("Status was set but will be overridden"); // TODO: nullable instead?? @@ -73,11 +81,14 @@ public class OperationServiceImpl implements OperationService { .status(Status.ACTIVE) .build()); } catch (PersistenceException e) { - LOG.error("PersistenceException while adding operation: {}", e); + LOG.error("PersistenceException while adding Operation. Message: {}", e.getMessage()); throw new ServiceException(e); } catch (InvalidVehicleException e) { + // already logged as invalid vehicle throw new InvalidOperationException("Enthaltenes Fahrzeug ist invalid", e); } catch (ElementNotFoundException e) { + LOG.error( + "ElementNotFoundException while adding Operation. Message: {}", e.getMessage()); throw new InvalidOperationException("Enthaltenes Fahrzeug existiert nicht", e); } } @@ -88,32 +99,47 @@ public class OperationServiceImpl implements OperationService { Set<Vehicle> vs = new HashSet<>(); try { - if (operationId <= 0) throw new InvalidOperationException("OperationId ist invalid"); + if (operationId <= 0) { + LOG.info("Invalid Operation: OperationId is invalid (<= 0)"); + throw new InvalidOperationException("OperationId ist invalid"); + } Operation o = operationDAO.get(operationId); validateOperation(o); if (o.opCode().trim().isEmpty() - || extractSeverityFromOpCode(o.opCode()) != o.severity()) + || extractSeverityFromOpCode(o.opCode()) != o.severity()) { + LOG.info("Invalid Operation: OpCode does not match Severity"); throw new InvalidOperationException("Einsatzcode ist invalid"); + } - if (o.status() != Status.ACTIVE) + if (o.status() != Status.ACTIVE) { + LOG.info("Invalid Operation: Operation is not active"); throw new InvalidOperationException("Einsatz ist inaktiv"); + } - if (o.created() == null) + if (o.created() == null) { + LOG.info("Invalid Operation: Created field is null"); throw new InvalidOperationException("Created darf nicht leer sein"); + } for (Long id : vehicleIds) { - if (id <= 0) throw new InvalidVehicleException("VehicleId ist invalid"); + if (id <= 0) { + LOG.info("Invalid Vehicle: VehicleId is not valid"); + throw new InvalidVehicleException("VehicleId ist invalid"); + } try { Vehicle v = vehicleDAO.get(id); VehicleServiceImpl.validateVehicle(v); - if (v.status() == Vehicle.Status.ABGEMELDET) + if (v.status() == Vehicle.Status.ABGEMELDET) { + LOG.info("Invalid Vehicle: Requested vehicle is not active"); throw new InvalidVehicleException( "Kann keine inaktiven Fahrzeuge anfordern"); + } vs.add(v); } catch (ElementNotFoundException e) { + LOG.info("Invalid Vehicle: VehicleId is not valid"); throw new InvalidVehicleException("VehicleId ist invalid"); } } @@ -123,9 +149,13 @@ public class OperationServiceImpl implements OperationService { operationDAO.update(o.toBuilder().vehicles(vs).build()); } catch (ElementNotFoundException e) { + LOG.error( + "ElementNotFoundException while requesting Vehicles. Message: {}", + e.getMessage()); throw new InvalidOperationException("Kein Einsatz mit dieser id existiert"); } catch (PersistenceException e) { - LOG.error("PersistenceException while requesting vehicles: {}", e); + LOG.error( + "PersistenceException while requesting vehicles. Message: {}", e.getMessage()); throw new ServiceException(e); } } @@ -137,9 +167,13 @@ public class OperationServiceImpl implements OperationService { Operation o = operationDAO.get(operationId); operationDAO.update(o.toBuilder().status(status).build()); } catch (ElementNotFoundException e) { + LOG.error( + "ElementNotFoundException while completing operation. Message: {}", + e.getMessage()); throw new InvalidOperationException(e); } catch (PersistenceException e) { - LOG.error("PersistenceException while completing operation: {}", e); + LOG.error( + "PersistenceException while completing operation. Message: {}", e.getMessage()); throw new ServiceException(e); } } @@ -214,40 +248,49 @@ public class OperationServiceImpl implements OperationService { return operations; } catch (PersistenceException e) { - LOG.error("PersistenceException while listing operations", e); + LOG.error("PersistenceException while listing operations. Message: {}", e.getMessage()); throw new ServiceException(e); } catch (InvalidOperationException e) { // database returned invalid values - LOG.error("DB returned invalid operation: {}", e); + LOG.error("DB returned invalid operation. Message: {}", e.getMessage()); throw new ServiceException("DB returned invalid operation", e); } } private static void validateOperation(Operation o) throws InvalidOperationException { - if (o.vehicles().isEmpty()) + if (o.vehicles().isEmpty()) { + LOG.info("Invalid Operation: No vehicle was added"); throw new InvalidOperationException( "Es muss mindestens ein Fahrzeug ausgewählt werden!"); + } for (Vehicle v : o.vehicles()) { try { VehicleServiceImpl.validateVehicle(v); } catch (InvalidVehicleException e) { + LOG.info("Invalid Operation: Vehicle \"{}\" is invalid", v.name()); throw new InvalidOperationException("Fahrzeug " + v.name() + " ist invalid" + e); } - if (v.status() != Vehicle.Status.FREI_FUNK && v.status() != Vehicle.Status.FREI_WACHE) + if (v.status() != Vehicle.Status.FREI_FUNK && v.status() != Vehicle.Status.FREI_WACHE) { + LOG.info("Invalid Operation: Vehicle \"{}\" not available", v.name()); throw new InvalidOperationException( "Fahrzeug nicht verfügbar (" + v.status() + ")"); + } // TODO: validate if NEF/RTW/NAH conditions? } Instant created = o.created(); - if (created != null && created.isAfter(Instant.now())) - throw new InvalidOperationException("Fahrzeug wurde in der Zukunft erstellt"); + if (created != null && created.isAfter(Instant.now())) { + LOG.info("Invalid Operation: Operation was created in the future"); + throw new InvalidOperationException("Einsatz wurde in der Zukunft erstellt"); + } - if (o.destination().trim().isEmpty()) + if (o.destination().trim().isEmpty()) { + LOG.info("Invalid Operation: Adress must not be empty"); throw new InvalidOperationException("Adresse darf nicht leer sein"); + } } private static final Pattern opCodePattern = @@ -257,7 +300,10 @@ public class OperationServiceImpl implements OperationService { throws InvalidOperationException { Matcher m = opCodePattern.matcher(opCode); - if (!m.matches()) throw new InvalidOperationException("Einsatzcode ist invalid"); + if (!m.matches()) { + LOG.info("Invalid Operation: OpCode is invalid"); + throw new InvalidOperationException("Einsatzcode ist invalid"); + } return Severity.valueOf(m.group(1)); } |