1 | package parkup.services;
|
---|
2 |
|
---|
3 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
4 | import org.springframework.stereotype.Service;
|
---|
5 | import parkup.data.ParkingZoneLocation;
|
---|
6 | import parkup.entities.ParkingSpace;
|
---|
7 | import parkup.entities.ParkingZone;
|
---|
8 | import parkup.entities.Worker;
|
---|
9 | import parkup.repositories.ParkingSessionRepository;
|
---|
10 | import parkup.repositories.ParkingSpaceRepository;
|
---|
11 | import parkup.repositories.ParkingZoneRepository;
|
---|
12 | import parkup.repositories.WorkerRepository;
|
---|
13 |
|
---|
14 | import javax.transaction.Transactional;
|
---|
15 | import java.util.ArrayList;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Objects;
|
---|
18 | import java.util.Optional;
|
---|
19 |
|
---|
20 | @Service
|
---|
21 | public class ParkingZoneService {
|
---|
22 | private final ParkingZoneRepository parkingZoneRepository;
|
---|
23 | private final ParkingSpaceRepository parkingSpaceRepository;
|
---|
24 | private final WorkerRepository workerRepository;
|
---|
25 | private final ParkingSessionRepository parkingSessionRepository;
|
---|
26 |
|
---|
27 | @Autowired
|
---|
28 | public ParkingZoneService(ParkingZoneRepository parkingZoneRepository, ParkingSpaceRepository parkingSpaceRepository, WorkerRepository workerRepository, ParkingSessionRepository parkingSessionRepository) {
|
---|
29 | this.parkingZoneRepository = parkingZoneRepository;
|
---|
30 | this.parkingSpaceRepository = parkingSpaceRepository;
|
---|
31 | this.parkingSessionRepository = parkingSessionRepository;
|
---|
32 | this.workerRepository = workerRepository;
|
---|
33 | }
|
---|
34 |
|
---|
35 | public List<ParkingZone> getAllParkingZones() {
|
---|
36 | List<ParkingZone> parkingZones= parkingZoneRepository.findAll();
|
---|
37 | for(ParkingZone pz:parkingZones){
|
---|
38 | setTransientVariables(pz.getId());
|
---|
39 | }
|
---|
40 | return parkingZones;
|
---|
41 | }
|
---|
42 |
|
---|
43 | public ParkingZone findById(int parkingZoneId) {
|
---|
44 | Optional<ParkingZone> parkingZone = Optional.ofNullable(this.parkingZoneRepository.findByPzId(parkingZoneId));
|
---|
45 | if (!parkingZone.isPresent()) {
|
---|
46 | throw new IllegalStateException("ParkingZone does not exist");
|
---|
47 | }
|
---|
48 | setTransientVariables(parkingZone.get().getId());
|
---|
49 | return parkingZone.get();
|
---|
50 | }
|
---|
51 |
|
---|
52 | public List<String> getAllParkingZoneNames() {
|
---|
53 | List<ParkingZone> parkingZones = this.parkingZoneRepository.findAll();
|
---|
54 | List<String> parkingZonesNames = new ArrayList<>();
|
---|
55 | for (ParkingZone pz : parkingZones) {
|
---|
56 | parkingZonesNames.add(pz.getPzName());
|
---|
57 | }
|
---|
58 | return parkingZonesNames;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public Optional<ParkingZone> addParkingZone(ParkingZone parkingZone) { //zavisno vo koj grad ili opstina i napravi proverki pred dodavanje, implementiraj u naredna faza
|
---|
62 | Optional<ParkingZone> parkingZoneOpt = Optional.ofNullable(parkingZoneRepository.findByPzName(parkingZone.getPzName()));
|
---|
63 | if (parkingZoneOpt.isPresent()) {
|
---|
64 | throw new IllegalStateException("Name already taken, try adding a ParkingZone with a different name");
|
---|
65 | } else {
|
---|
66 | System.out.println(parkingZone);
|
---|
67 | parkingZoneRepository.save(parkingZone);
|
---|
68 | }
|
---|
69 | return parkingZoneOpt;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public ParkingZone addParkingZoneNameOnly(String name) {
|
---|
73 | Optional<ParkingZone> parkingZoneOpt = Optional.ofNullable(parkingZoneRepository.findByPzName(name));
|
---|
74 | if (parkingZoneOpt.isPresent()) {
|
---|
75 | throw new IllegalStateException("Name already taken, try adding a ParkingZone with a different name");
|
---|
76 | } else {
|
---|
77 | ParkingZone parkingZone = new ParkingZone(name.trim());
|
---|
78 | parkingZoneRepository.save(parkingZone);
|
---|
79 | return parkingZone;
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | //TODO proveri dali ima odgovorni i dokolku ima dali postojat vo bazata
|
---|
84 | @Transactional
|
---|
85 | public ParkingZone updateParkingZone(int parkingZoneId, String pzName, int price,
|
---|
86 | String location, int from, int to, String color,
|
---|
87 | List<ParkingSpace> parkingSpaces, ParkingZoneLocation parkingZoneLocation, List<Integer> odgovorniLica) {
|
---|
88 |
|
---|
89 | //TODO prati niza od objekti ParkingSpaces i prati objekt ParkingZoneLocation
|
---|
90 |
|
---|
91 | ParkingZone parkingZoneNov = parkingZoneRepository.findByPzId(parkingZoneId);
|
---|
92 | if (parkingZoneNov!=null) {
|
---|
93 | if (pzName != null && pzName.length() > 1) { ;
|
---|
94 | if (parkingZoneNov.getPzName().equals(pzName)&&parkingZoneNov.getId()!=parkingZoneId) {
|
---|
95 | throw new IllegalStateException("There is already a ParkingZone with the same name");
|
---|
96 | }
|
---|
97 |
|
---|
98 | parkingZoneNov.setPzName(pzName);
|
---|
99 | }
|
---|
100 |
|
---|
101 | if (price != 0 && !Objects.equals(parkingZoneNov.getPrice(), price)) {
|
---|
102 | parkingZoneNov.setPrice(price);
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (location != null && location.length() > 0 && !Objects.equals(parkingZoneNov.getAddress(), location)) {
|
---|
106 | parkingZoneNov.setAddress(location);
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (from != 0 && !Objects.equals(parkingZoneNov.getFrom(), from)) {
|
---|
110 | parkingZoneNov.setFrom(from);
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (to != 0 && !Objects.equals(parkingZoneNov.getTo(), to)) {
|
---|
114 | parkingZoneNov.setTo(to);
|
---|
115 | }
|
---|
116 |
|
---|
117 | if (color != null && color.length() > 0 && !Objects.equals(parkingZoneNov.getColor(), color)) {
|
---|
118 | parkingZoneNov.setColor(color);
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (parkingZoneLocation != null) {
|
---|
122 | parkingZoneNov.setParkingZoneLocation(parkingZoneLocation);
|
---|
123 | }
|
---|
124 |
|
---|
125 | if (!parkingSpaces.isEmpty()) {
|
---|
126 | ParkingZone parkingZone = parkingZoneRepository.findByPzId(parkingZoneId);
|
---|
127 | // for(ParkingSpace space :parkingZone.getParkingSpaces()){
|
---|
128 | // if(!parkingSpaces.contains(space)){
|
---|
129 | // for(ParkingSession session: parkingSessionRepository.findByParkingZonePzName(pzName)){
|
---|
130 | // if(session.getParkingSpace().getPsId()==space.getPsId())
|
---|
131 | // throw new IllegalStateException("Vlegov vo iffot");
|
---|
132 | // }
|
---|
133 | // }
|
---|
134 | // }
|
---|
135 | //TODO: treba da se opravi
|
---|
136 | parkingZone.setParkingSpaces(null);
|
---|
137 | List<ParkingSpace> spacesToAdd = new ArrayList<>(parkingSpaces);
|
---|
138 | parkingZone.setParkingSpaces(spacesToAdd);
|
---|
139 | }
|
---|
140 | if (!odgovorniLica.isEmpty()) {
|
---|
141 | ParkingZone parkingZone = parkingZoneRepository.findByPzId(parkingZoneId);
|
---|
142 | for (Integer vrabotenId : odgovorniLica) {
|
---|
143 | Worker workerToAdd = workerRepository.findByWorkerId(vrabotenId);
|
---|
144 | if(workerToAdd !=null)
|
---|
145 | workerToAdd.getParkingZones().add(parkingZone);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | return parkingZoneNov;
|
---|
151 | } else {
|
---|
152 | throw new IllegalStateException("There ParkingZone does not exist");
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | @Transactional
|
---|
158 | public Optional<ParkingZone> deleteParkingZone ( int parkingZoneId){
|
---|
159 | Optional<ParkingZone> parkingZoneOpt = Optional.ofNullable(parkingZoneRepository.findByPzId(parkingZoneId));
|
---|
160 | if (parkingZoneOpt.isPresent()) {
|
---|
161 | parkingZoneRepository.deleteByPzId(parkingZoneId);
|
---|
162 | } else {
|
---|
163 | throw new IllegalStateException("ParkingZone doesn't exist, therefore can't be deleted");
|
---|
164 | }
|
---|
165 | return parkingZoneOpt;
|
---|
166 | }
|
---|
167 |
|
---|
168 | public int calculateTakenSpaces(int pzId){
|
---|
169 | ParkingZone parkingZone=parkingZoneRepository.findByPzId(pzId);
|
---|
170 | return (int)parkingZone.getParkingSpaces().stream().filter(ParkingSpace::isTaken).count();
|
---|
171 | }
|
---|
172 | public int calculateCapacity(int pzId){
|
---|
173 | ParkingZone parkingZone=parkingZoneRepository.findByPzId(pzId);
|
---|
174 | return parkingZone.getParkingSpaces().size();
|
---|
175 | }
|
---|
176 | public void setTransientVariables(int pzId){
|
---|
177 | calculateCapacity(pzId);
|
---|
178 | calculateTakenSpaces(pzId);
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|