[ce6ad22] | 1 | package parkup.services;
|
---|
| 2 |
|
---|
| 3 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
[c71fa06] | 4 | import org.springframework.security.core.Authentication;
|
---|
[ce6ad22] | 5 | import org.springframework.stereotype.Service;
|
---|
[c71fa06] | 6 | import parkup.ParkUpApplication;
|
---|
[df4089c] | 7 | import parkup.data.ParkingZoneAdminView;
|
---|
[97fbc67] | 8 | import parkup.data.ParkingZoneLocation;
|
---|
[df4089c] | 9 | import parkup.data.WorkerDemo;
|
---|
[97fbc67] | 10 | import parkup.entities.ParkingSpace;
|
---|
[ce6ad22] | 11 | import parkup.entities.ParkingZone;
|
---|
[9dd526f] | 12 | import parkup.entities.Worker;
|
---|
[9ff45d6] | 13 | import parkup.repositories.ParkingSessionRepository;
|
---|
| 14 | import parkup.repositories.ParkingSpaceRepository;
|
---|
[ce6ad22] | 15 | import parkup.repositories.ParkingZoneRepository;
|
---|
[9dd526f] | 16 | import parkup.repositories.WorkerRepository;
|
---|
[c71fa06] | 17 | import static parkup.ParkUpApplication.getToken;
|
---|
[ce6ad22] | 18 | import javax.transaction.Transactional;
|
---|
[97fbc67] | 19 | import java.util.ArrayList;
|
---|
[ce6ad22] | 20 | import java.util.List;
|
---|
| 21 | import java.util.Objects;
|
---|
| 22 | import java.util.Optional;
|
---|
[df4089c] | 23 | import java.util.stream.Collectors;
|
---|
[ce6ad22] | 24 |
|
---|
| 25 | @Service
|
---|
| 26 | public class ParkingZoneService {
|
---|
| 27 | private final ParkingZoneRepository parkingZoneRepository;
|
---|
[9ff45d6] | 28 | private final ParkingSpaceRepository parkingSpaceRepository;
|
---|
[9dd526f] | 29 | private final WorkerRepository workerRepository;
|
---|
[9ff45d6] | 30 | private final ParkingSessionRepository parkingSessionRepository;
|
---|
[ce6ad22] | 31 |
|
---|
| 32 | @Autowired
|
---|
[9dd526f] | 33 | public ParkingZoneService(ParkingZoneRepository parkingZoneRepository, ParkingSpaceRepository parkingSpaceRepository, WorkerRepository workerRepository, ParkingSessionRepository parkingSessionRepository) {
|
---|
[ce6ad22] | 34 | this.parkingZoneRepository = parkingZoneRepository;
|
---|
[9ff45d6] | 35 | this.parkingSpaceRepository = parkingSpaceRepository;
|
---|
| 36 | this.parkingSessionRepository = parkingSessionRepository;
|
---|
[9dd526f] | 37 | this.workerRepository = workerRepository;
|
---|
[ce6ad22] | 38 | }
|
---|
| 39 |
|
---|
| 40 | public List<ParkingZone> getAllParkingZones() {
|
---|
[c71fa06] | 41 | Authentication user = getToken();
|
---|
| 42 | String role =user.getAuthorities().stream().findFirst().get().getAuthority();
|
---|
| 43 | String email = user.getName();
|
---|
| 44 | List<ParkingZone> parkingZones;
|
---|
| 45 | if(role.equals("ROLE_REG_USER"))
|
---|
| 46 | parkingZones= parkingZoneRepository.findAll();
|
---|
| 47 | else if(role.equals("ROLE_WORKER")){
|
---|
| 48 | Worker loggedInWorker = workerRepository.findWorkerByEmail(email).orElseThrow(null);
|
---|
| 49 | parkingZones= parkingZoneRepository.findAll().stream().filter(pz->loggedInWorker.getParkingZones().contains(pz)).collect(Collectors.toList());
|
---|
| 50 | }else if(role.equals("ROLE_ADMIN"))
|
---|
| 51 | parkingZones= parkingZoneRepository.findAll();
|
---|
| 52 | else
|
---|
| 53 | parkingZones=parkingZoneRepository.findAll();
|
---|
[9dd526f] | 54 | for(ParkingZone pz:parkingZones){
|
---|
[df4089c] | 55 | setTransientVariables(pz);
|
---|
[9dd526f] | 56 | }
|
---|
| 57 | return parkingZones;
|
---|
[ce6ad22] | 58 | }
|
---|
| 59 |
|
---|
[df4089c] | 60 | public ParkingZoneAdminView findById(int parkingZoneId) {
|
---|
[ce6ad22] | 61 | Optional<ParkingZone> parkingZone = Optional.ofNullable(this.parkingZoneRepository.findByPzId(parkingZoneId));
|
---|
[9dd526f] | 62 | if (!parkingZone.isPresent()) {
|
---|
| 63 | throw new IllegalStateException("ParkingZone does not exist");
|
---|
| 64 | }
|
---|
[df4089c] | 65 | setTransientVariables(parkingZone.get());
|
---|
| 66 | return new ParkingZoneAdminView(parkingZone.get(),getResponsibleWorkers(parkingZoneId));
|
---|
[ce6ad22] | 67 | }
|
---|
| 68 |
|
---|
[97fbc67] | 69 | public List<String> getAllParkingZoneNames() {
|
---|
| 70 | List<ParkingZone> parkingZones = this.parkingZoneRepository.findAll();
|
---|
| 71 | List<String> parkingZonesNames = new ArrayList<>();
|
---|
[9ff45d6] | 72 | for (ParkingZone pz : parkingZones) {
|
---|
[97fbc67] | 73 | parkingZonesNames.add(pz.getPzName());
|
---|
| 74 | }
|
---|
| 75 | return parkingZonesNames;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | public Optional<ParkingZone> addParkingZone(ParkingZone parkingZone) { //zavisno vo koj grad ili opstina i napravi proverki pred dodavanje, implementiraj u naredna faza
|
---|
[ce6ad22] | 79 | Optional<ParkingZone> parkingZoneOpt = Optional.ofNullable(parkingZoneRepository.findByPzName(parkingZone.getPzName()));
|
---|
| 80 | if (parkingZoneOpt.isPresent()) {
|
---|
| 81 | throw new IllegalStateException("Name already taken, try adding a ParkingZone with a different name");
|
---|
| 82 | } else {
|
---|
| 83 | System.out.println(parkingZone);
|
---|
| 84 | parkingZoneRepository.save(parkingZone);
|
---|
| 85 | }
|
---|
[97fbc67] | 86 | return parkingZoneOpt;
|
---|
[ce6ad22] | 87 | }
|
---|
| 88 |
|
---|
[97fbc67] | 89 | public ParkingZone addParkingZoneNameOnly(String name) {
|
---|
| 90 | Optional<ParkingZone> parkingZoneOpt = Optional.ofNullable(parkingZoneRepository.findByPzName(name));
|
---|
| 91 | if (parkingZoneOpt.isPresent()) {
|
---|
| 92 | throw new IllegalStateException("Name already taken, try adding a ParkingZone with a different name");
|
---|
| 93 | } else {
|
---|
| 94 | ParkingZone parkingZone = new ParkingZone(name.trim());
|
---|
| 95 | parkingZoneRepository.save(parkingZone);
|
---|
| 96 | return parkingZone;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | //TODO proveri dali ima odgovorni i dokolku ima dali postojat vo bazata
|
---|
[ce6ad22] | 101 | @Transactional
|
---|
[df4089c] | 102 | public ParkingZoneAdminView updateParkingZone(int parkingZoneId, String pzName, int price,
|
---|
[97fbc67] | 103 | String location, int from, int to, String color,
|
---|
[df4089c] | 104 | List<ParkingSpace> parkingSpaces, ParkingZoneLocation parkingZoneLocation, List<WorkerDemo> responsibleWorkers) {
|
---|
[9ff45d6] | 105 |
|
---|
| 106 | //TODO prati niza od objekti ParkingSpaces i prati objekt ParkingZoneLocation
|
---|
[98f448a] | 107 | ParkingZone parkingZone = parkingZoneRepository.findByPzId(parkingZoneId);
|
---|
[ce6ad22] | 108 | ParkingZone parkingZoneNov = parkingZoneRepository.findByPzId(parkingZoneId);
|
---|
[9ff45d6] | 109 | if (parkingZoneNov!=null) {
|
---|
| 110 | if (pzName != null && pzName.length() > 1) { ;
|
---|
| 111 | if (parkingZoneNov.getPzName().equals(pzName)&&parkingZoneNov.getId()!=parkingZoneId) {
|
---|
| 112 | throw new IllegalStateException("There is already a ParkingZone with the same name");
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | parkingZoneNov.setPzName(pzName);
|
---|
[ce6ad22] | 116 | }
|
---|
| 117 |
|
---|
[9ff45d6] | 118 | if (price != 0 && !Objects.equals(parkingZoneNov.getPrice(), price)) {
|
---|
| 119 | parkingZoneNov.setPrice(price);
|
---|
| 120 | }
|
---|
[ce6ad22] | 121 |
|
---|
[9ff45d6] | 122 | if (location != null && location.length() > 0 && !Objects.equals(parkingZoneNov.getAddress(), location)) {
|
---|
| 123 | parkingZoneNov.setAddress(location);
|
---|
| 124 | }
|
---|
[ce6ad22] | 125 |
|
---|
[9ff45d6] | 126 | if (from != 0 && !Objects.equals(parkingZoneNov.getFrom(), from)) {
|
---|
| 127 | parkingZoneNov.setFrom(from);
|
---|
| 128 | }
|
---|
[ce6ad22] | 129 |
|
---|
[9ff45d6] | 130 | if (to != 0 && !Objects.equals(parkingZoneNov.getTo(), to)) {
|
---|
| 131 | parkingZoneNov.setTo(to);
|
---|
| 132 | }
|
---|
[97fbc67] | 133 |
|
---|
[9ff45d6] | 134 | if (color != null && color.length() > 0 && !Objects.equals(parkingZoneNov.getColor(), color)) {
|
---|
| 135 | parkingZoneNov.setColor(color);
|
---|
| 136 | }
|
---|
[97fbc67] | 137 |
|
---|
[9ff45d6] | 138 | if (parkingZoneLocation != null) {
|
---|
| 139 | parkingZoneNov.setParkingZoneLocation(parkingZoneLocation);
|
---|
| 140 | }
|
---|
[97fbc67] | 141 |
|
---|
[9ff45d6] | 142 | if (!parkingSpaces.isEmpty()) {
|
---|
[df4089c] | 143 | List<String> psNamesInZone=parkingZoneNov.getParkingSpaces().stream().map(ParkingSpace::getPsName).collect(Collectors.toList());
|
---|
| 144 | List<ParkingSpace> psToAdd=parkingSpaces.stream().filter(ps->!psNamesInZone.contains(ps.getPsName())).collect(Collectors.toList());
|
---|
| 145 | for(ParkingSpace ps:psToAdd){
|
---|
| 146 | parkingZoneNov.getParkingSpaces().add(ps);
|
---|
| 147 | }
|
---|
[98f448a] | 148 | }else{
|
---|
[80ddcae] | 149 | parkingZoneNov.setParkingSpaces(new ArrayList<>());
|
---|
[9ff45d6] | 150 | }
|
---|
[df4089c] | 151 | if (!responsibleWorkers.isEmpty()) {
|
---|
[98f448a] | 152 |
|
---|
| 153 | for(Integer workerId: getResponsibleWorkers(parkingZoneId).stream().map(WorkerDemo::getWorkerId).collect(Collectors.toList())){
|
---|
| 154 | workerRepository.findByWorkerId(workerId).getParkingZones().remove(parkingZone);
|
---|
| 155 | }
|
---|
[df4089c] | 156 | for (Integer workerId : responsibleWorkers.stream().map(WorkerDemo::getWorkerId).collect(Collectors.toList())) {
|
---|
| 157 | Worker workerToAdd = workerRepository.findByWorkerId(workerId);
|
---|
[9dd526f] | 158 | if(workerToAdd !=null)
|
---|
| 159 | workerToAdd.getParkingZones().add(parkingZone);
|
---|
[9ff45d6] | 160 | }
|
---|
[98f448a] | 161 | }else{
|
---|
| 162 | workerRepository.findAll().stream().filter(w->w.getParkingZones().contains(parkingZone)).forEach(w->w.getParkingZones().remove(parkingZone));
|
---|
[9ff45d6] | 163 | }
|
---|
[97fbc67] | 164 |
|
---|
[df4089c] | 165 | setTransientVariables(parkingZoneNov);
|
---|
| 166 | return new ParkingZoneAdminView(parkingZoneNov,getResponsibleWorkers(parkingZoneId));
|
---|
[9ff45d6] | 167 | } else {
|
---|
| 168 | throw new IllegalStateException("There ParkingZone does not exist");
|
---|
[97fbc67] | 169 | }
|
---|
[9ff45d6] | 170 | }
|
---|
[97fbc67] | 171 |
|
---|
| 172 |
|
---|
[9ff45d6] | 173 | @Transactional
|
---|
| 174 | public Optional<ParkingZone> deleteParkingZone ( int parkingZoneId){
|
---|
| 175 | Optional<ParkingZone> parkingZoneOpt = Optional.ofNullable(parkingZoneRepository.findByPzId(parkingZoneId));
|
---|
| 176 | if (parkingZoneOpt.isPresent()) {
|
---|
[df4089c] | 177 | parkingZoneOpt.get().setParkingSpaces(null);
|
---|
| 178 | parkingZoneOpt.get().setParkingZoneLocation(null);
|
---|
[98f448a] | 179 | workerRepository.findAll().stream().filter(w->w.getParkingZones().contains(parkingZoneOpt.get())).forEach(w->w.getParkingZones().remove(parkingZoneOpt.get()));
|
---|
[9ff45d6] | 180 | parkingZoneRepository.deleteByPzId(parkingZoneId);
|
---|
| 181 | } else {
|
---|
| 182 | throw new IllegalStateException("ParkingZone doesn't exist, therefore can't be deleted");
|
---|
| 183 | }
|
---|
| 184 | return parkingZoneOpt;
|
---|
[ce6ad22] | 185 | }
|
---|
[9dd526f] | 186 |
|
---|
| 187 | public int calculateTakenSpaces(int pzId){
|
---|
| 188 | ParkingZone parkingZone=parkingZoneRepository.findByPzId(pzId);
|
---|
| 189 | return (int)parkingZone.getParkingSpaces().stream().filter(ParkingSpace::isTaken).count();
|
---|
| 190 | }
|
---|
| 191 | public int calculateCapacity(int pzId){
|
---|
| 192 | ParkingZone parkingZone=parkingZoneRepository.findByPzId(pzId);
|
---|
| 193 | return parkingZone.getParkingSpaces().size();
|
---|
| 194 | }
|
---|
[df4089c] | 195 | public void setTransientVariables(ParkingZone pz){
|
---|
| 196 | pz.setCapacity(calculateCapacity(pz.getId()));
|
---|
| 197 | pz.setTakenSpaces(calculateTakenSpaces(pz.getId()));
|
---|
[80ddcae] | 198 | pz.setResponsibleWorkers(getWorkers(pz.getId()));
|
---|
[df4089c] | 199 | }
|
---|
| 200 | public List<WorkerDemo> getResponsibleWorkers(int pzId){
|
---|
| 201 | ParkingZone parkingZone =parkingZoneRepository.findByPzId(pzId);
|
---|
| 202 | return workerRepository.findAll().stream().filter(w->w.getParkingZones().contains(parkingZone)).map(WorkerDemo::new).collect(Collectors.toList());
|
---|
[9dd526f] | 203 | }
|
---|
[80ddcae] | 204 | public List<String> getWorkers(int pzId){
|
---|
| 205 | ParkingZone parkingZone =parkingZoneRepository.findByPzId(pzId);
|
---|
| 206 | return workerRepository.findAll().stream().filter(w->w.getParkingZones().contains(parkingZone)).map(w->w.getFirstName() + " " + w.getLastName()).collect(Collectors.toList());
|
---|
| 207 |
|
---|
| 208 | }
|
---|
[ce6ad22] | 209 | }
|
---|
| 210 |
|
---|