diff options
Diffstat (limited to 'src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/OperationServiceImpl.java')
-rw-r--r-- | src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/OperationServiceImpl.java | 264 |
1 files changed, 0 insertions, 264 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 deleted file mode 100644 index d07f46f..0000000 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/OperationServiceImpl.java +++ /dev/null @@ -1,264 +0,0 @@ -package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service; - -import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao.OperationDAO; -import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao.VehicleDAO; -import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Operation; -import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Operation.Severity; -import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Operation.Status; -import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle; -import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle.VehicleType; -import at.ac.tuwien.sepm.assignment.groupphase.exception.ElementNotFoundException; -import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidOperationException; -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; -import java.util.EnumSet; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; -import java.util.function.Predicate; -import java.util.function.Supplier; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.Collectors; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Service; - -@Service -public class OperationServiceImpl implements OperationService { - - private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - - private final OperationDAO operationDAO; - private final VehicleDAO vehicleDAO; - private final VehicleService vehicleService; - - public OperationServiceImpl( - OperationDAO operationDAO, VehicleDAO vehicleDAO, VehicleService vehicleService) { - this.operationDAO = operationDAO; - this.vehicleDAO = vehicleDAO; - this.vehicleService = vehicleService; - } - - @Override - public long add(Operation o) throws InvalidOperationException, ServiceException { - if (o.created() != null) throw new InvalidOperationException("Created must not be set"); - - if (o.severity() != null) throw new InvalidOperationException("Severity must not be set"); - - if (o.id() != 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?? - - try { - for (long id : (Iterable<Long>) o.vehicles().stream().map(Vehicle::id)::iterator) { - Vehicle v = vehicleDAO.get(id); - VehicleServiceImpl.validateVehicle(v); - } - - validateOperation(o); - - return operationDAO.add( - o.toBuilder() - .created(Instant.now()) - .severity(extractSeverityFromOpCode(o.opCode())) - .status(Status.ACTIVE) - .build()); - } catch (PersistenceException e) { - LOG.error("PersistenceException while adding operation: {}", e); - throw new ServiceException(e); - } catch (InvalidVehicleException e) { - throw new InvalidOperationException("Enthaltenes Fahrzeug ist invalid", e); - } catch (ElementNotFoundException e) { - throw new InvalidOperationException("Enthaltenes Fahrzeug existiert nicht", e); - } - } - - @Override - public void requestVehicles(long operationId, Set<Long> vehicleIds) - throws InvalidOperationException, InvalidVehicleException, ServiceException { - Set<Vehicle> vs = new HashSet<>(); - - try { - if (operationId <= 0) throw new InvalidOperationException("OperationId ist invalid"); - Operation o = operationDAO.get(operationId); - validateOperation(o); - - if (o.opCode().trim().isEmpty() - || extractSeverityFromOpCode(o.opCode()) != o.severity()) - throw new InvalidOperationException("Einsatzcode ist invalid"); - - if (o.status() != Status.ACTIVE) - throw new InvalidOperationException("Einsatz ist inaktiv"); - - if (o.created() == null) - throw new InvalidOperationException("Created darf nicht leer sein"); - - for (Long id : vehicleIds) { - if (id <= 0) throw new InvalidVehicleException("VehicleId ist invalid"); - - try { - Vehicle v = vehicleDAO.get(id); - VehicleServiceImpl.validateVehicle(v); - if (v.status() == Vehicle.Status.ABGEMELDET) - throw new InvalidVehicleException( - "Kann keine inaktiven Fahrzeuge anfordern"); - - vs.add(v); - } catch (ElementNotFoundException e) { - throw new InvalidVehicleException("VehicleId ist invalid"); - } - } - - vs.addAll(o.vehicles()); - if (vs.equals(o.vehicles())) return; - - operationDAO.update(o.toBuilder().vehicles(vs).build()); - } catch (ElementNotFoundException e) { - throw new InvalidOperationException("Kein Einsatz mit dieser id existiert"); - } catch (PersistenceException e) { - LOG.error("PersistenceException while requesting vehicles: {}", e); - throw new ServiceException(e); - } - } - - @Override - public void complete(long operationId, Status status) - throws InvalidOperationException, ServiceException { - try { - Operation o = operationDAO.get(operationId); - operationDAO.update(o.toBuilder().status(status).build()); - } catch (ElementNotFoundException e) { - throw new InvalidOperationException(e); - } catch (PersistenceException e) { - LOG.error("PersistenceException while completing operation: {}", e); - throw new ServiceException(e); - } - } - - @Override - public SortedSet<Vehicle> rankVehicles(String opCode) - throws InvalidOperationException, ServiceException { - Set<Vehicle> vehicles = - vehicleService.list(EnumSet.complementOf(EnumSet.of(Vehicle.Status.ABGEMELDET))); - - List<Predicate<Vehicle>> priorities = new ArrayList<>(); - Predicate<Vehicle> ktw = v -> v.type() == VehicleType.KTW; - Predicate<Vehicle> rtwNoNEF = v -> v.type() == VehicleType.RTW && !v.hasNef(); - Predicate<Vehicle> rtwNEF = v -> v.type() == VehicleType.RTW && v.hasNef(); - Predicate<Vehicle> nef = v -> v.type() == VehicleType.NEF; - Predicate<Vehicle> nah = v -> v.type() == VehicleType.NAH; - - switch (extractSeverityFromOpCode(opCode)) { - case A: - // fallthrough - case B: - // fallthrough - case O: - priorities.add(ktw); - priorities.add(rtwNoNEF); - priorities.add(rtwNEF); - break; - case C: - priorities.add(rtwNEF); - priorities.add(rtwNoNEF); - priorities.add(ktw); - break; - case D: - priorities.add(rtwNEF); - priorities.add(nef); - priorities.add(nah); - priorities.add(rtwNoNEF); - priorities.add(ktw); - break; - case E: - priorities.add(nah); - priorities.add(nef); - priorities.add(rtwNEF); - priorities.add(rtwNoNEF); - priorities.add(ktw); - break; - } - - Comparator<Vehicle> vehicleComparator = - (v1, v2) -> { - for (Predicate<Vehicle> priority : priorities) { - if (priority.test(v1)) { - return -1; - } - if (priority.test(v2)) { - return +1; - } - } - return 0; - }; - - Supplier<TreeSet<Vehicle>> supplier = () -> new TreeSet<>(vehicleComparator); - - return vehicles.stream().collect(Collectors.toCollection(supplier)); - } - - @Override - public Set<Operation> list(EnumSet<Status> statuses) throws ServiceException { - try { - Set<Operation> operations = operationDAO.list(statuses); - for (Operation o : operations) validateOperation(o); - - return operations; - } catch (PersistenceException e) { - LOG.error("PersistenceException while listing operations", e); - throw new ServiceException(e); - } catch (InvalidOperationException e) { - // database returned invalid values - LOG.error("DB returned invalid operation: {}", e); - throw new ServiceException("DB returned invalid operation", e); - } - } - - private static void validateOperation(Operation o) throws InvalidOperationException { - if (o.vehicles().isEmpty()) - throw new InvalidOperationException( - "Es muss mindestens ein Fahrzeug ausgewählt werden!"); - - for (Vehicle v : o.vehicles()) { - try { - VehicleServiceImpl.validateVehicle(v); - } catch (InvalidVehicleException e) { - throw new InvalidOperationException("Fahrzeug " + v.name() + " ist invalid" + e); - } - - if (v.status() != Vehicle.Status.FREI_FUNK && v.status() != Vehicle.Status.FREI_WACHE) - 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 (o.destination().trim().isEmpty()) - throw new InvalidOperationException("Adresse darf nicht leer sein"); - } - - private static final Pattern opCodePattern = - Pattern.compile("(?:\\w{1,3}-\\d{0,2})([ABCDEO])(?:.*)"); - - private static Severity extractSeverityFromOpCode(String opCode) - throws InvalidOperationException { - Matcher m = opCodePattern.matcher(opCode); - - if (!m.matches()) throw new InvalidOperationException("Einsatzcode ist invalid"); - - return Severity.valueOf(m.group(1)); - } -} |