blob: a68720d4a763a519ceea65ddd031274963c06fda (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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();
}
}
|