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