1 | package finki.diplomska.tripplanner.service.impl;
|
---|
2 |
|
---|
3 | import finki.diplomska.tripplanner.models.Location;
|
---|
4 | import finki.diplomska.tripplanner.models.exceptions.LocationNotFoundException;
|
---|
5 | import finki.diplomska.tripplanner.repository.jpa.JpaLocationRepository;
|
---|
6 | import finki.diplomska.tripplanner.service.LocationService;
|
---|
7 | import org.springframework.stereotype.Service;
|
---|
8 |
|
---|
9 | import java.util.*;
|
---|
10 |
|
---|
11 |
|
---|
12 | @Service
|
---|
13 | public class LocationServiceImpl implements LocationService {
|
---|
14 |
|
---|
15 | private final JpaLocationRepository locationRepository;
|
---|
16 |
|
---|
17 | public LocationServiceImpl(JpaLocationRepository locationRepository) {
|
---|
18 | this.locationRepository = locationRepository;
|
---|
19 | }
|
---|
20 |
|
---|
21 | @Override
|
---|
22 | public List<Location> findLocationsFromCity(String locName, String companion, List<String> categories) {
|
---|
23 | return this.locationRepository.findLocationsFromCity(locName, companion, categories);
|
---|
24 | }
|
---|
25 |
|
---|
26 | @Override
|
---|
27 | public List<Location> findLocationsFromCountry(String locName, String companion, String region, List<String> categories) {
|
---|
28 | return this.locationRepository.findLocationsFromCountry(locName, companion, region, categories);
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Override
|
---|
32 | public List<Location> findAll() {
|
---|
33 | return this.locationRepository.findAll();
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override
|
---|
37 | public Location getById(Long id) {
|
---|
38 | Optional<Location> location = this.locationRepository.findById(id);
|
---|
39 | if (!location.isPresent()) {
|
---|
40 | throw new LocationNotFoundException(id);
|
---|
41 | }
|
---|
42 | return location.get();
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public Optional<Location> findById(Long id) {
|
---|
47 | return this.locationRepository.findById(id);
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public List<Location> scheduleLocations(String locName, String companion, String region, List<String> categories, int numberOfDays) {
|
---|
52 | int maxMinutesPerDay = numberOfDays *6 * 60;
|
---|
53 | int minutesPerDay = 0;
|
---|
54 | List<Location> locations = this.locationRepository.findLocationsFromCity(locName, companion, categories);
|
---|
55 | List<Location> countryLocations = this.locationRepository.findLocationsFromCountry(locName, companion, region, categories);
|
---|
56 | List<Location> newList = new ArrayList<>();
|
---|
57 | int listSize = locations.size();
|
---|
58 | int listCountrySize = countryLocations.size();
|
---|
59 |
|
---|
60 | if(locName.equals("Macedonia")){
|
---|
61 | while(minutesPerDay < maxMinutesPerDay){
|
---|
62 | for(Location l: countryLocations) {
|
---|
63 | if (minutesPerDay < maxMinutesPerDay && l.getDuration() + minutesPerDay <= maxMinutesPerDay && listCountrySize != 0) {
|
---|
64 | newList.add(l);
|
---|
65 | listCountrySize --;
|
---|
66 | }
|
---|
67 | minutesPerDay += l.getDuration();
|
---|
68 | if (minutesPerDay > maxMinutesPerDay) {
|
---|
69 | break;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }else{
|
---|
74 | while(minutesPerDay < maxMinutesPerDay ){
|
---|
75 | for(Location l: locations) {
|
---|
76 | if (minutesPerDay < maxMinutesPerDay && l.getDuration() + minutesPerDay <= maxMinutesPerDay && listSize != 0) {
|
---|
77 | newList.add(l);
|
---|
78 | listSize --;
|
---|
79 | }
|
---|
80 | minutesPerDay += l.getDuration();
|
---|
81 | if (minutesPerDay > maxMinutesPerDay) {
|
---|
82 | break;
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | return newList;
|
---|
89 | }
|
---|
90 |
|
---|
91 | }
|
---|