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 | import java.util.stream.Collectors;
|
---|
11 |
|
---|
12 |
|
---|
13 | @Service
|
---|
14 | public class LocationServiceImpl implements LocationService {
|
---|
15 |
|
---|
16 | private final JpaLocationRepository locationRepository;
|
---|
17 |
|
---|
18 | public LocationServiceImpl(JpaLocationRepository locationRepository) {
|
---|
19 | this.locationRepository = locationRepository;
|
---|
20 | }
|
---|
21 |
|
---|
22 | @Override
|
---|
23 | public List<Location> findLocationsFromCity(String locName, String companion, List<String> categories) {
|
---|
24 | return this.locationRepository.findLocationsFromCity(locName, companion, categories);
|
---|
25 | }
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | public List<Location> findLocationsFromCountry(String locName, String companion, String region, List<String> categories) {
|
---|
29 | return this.locationRepository.findLocationsFromCountry(locName, companion, region, categories);
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public List<Location> findAll() {
|
---|
34 | return this.locationRepository.findAll();
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public Location getById(Long id) {
|
---|
39 | Optional<Location> location = this.locationRepository.findById(id);
|
---|
40 | if (!location.isPresent()) {
|
---|
41 | throw new LocationNotFoundException(id);
|
---|
42 | }
|
---|
43 | return location.get();
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | public Optional<Location> findById(Long id) {
|
---|
48 | return this.locationRepository.findById(id);
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public List<Location> findLocationsFromCityForm(Long cityId, Long companionId, Long lengthOfStay, String categoryIds) {
|
---|
53 | List<Long> categories = null;
|
---|
54 | if(categoryIds != null && !categoryIds.isEmpty()){
|
---|
55 | List<String> ids = Arrays.asList(categoryIds.split(","));
|
---|
56 | categories = ids.stream().map(Long::valueOf).collect(Collectors.toList());
|
---|
57 | }
|
---|
58 | Long maxMinutesPerDay = lengthOfStay *6 * 60;
|
---|
59 | int minutesPerDay = 0;
|
---|
60 | List<Location> citylocations = this.locationRepository.findLocationsFromCityForm(cityId, companionId, categories);
|
---|
61 | List<Location> newList = new ArrayList<>();
|
---|
62 | int listSize = citylocations.size();
|
---|
63 |
|
---|
64 | while(minutesPerDay < maxMinutesPerDay ){
|
---|
65 | for(Location l: citylocations) {
|
---|
66 | if (minutesPerDay < maxMinutesPerDay && l.getDuration() + minutesPerDay <= maxMinutesPerDay && listSize != 0) {
|
---|
67 | newList.add(l);
|
---|
68 | listSize --;
|
---|
69 | }
|
---|
70 | minutesPerDay += l.getDuration();
|
---|
71 | if (minutesPerDay > maxMinutesPerDay) {
|
---|
72 | break;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | List<Location> foundLocations = locationRepository.findLocationsFromCityForm(cityId, companionId, categories);
|
---|
78 | return newList;
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | public List<Location> findLocationsFromRegionForm(Long regionId, Long companionId, Long lengthOfStay, String categoryIds) {
|
---|
83 | List<Long> categories = null;
|
---|
84 | if(categoryIds != null && !categoryIds.isEmpty()){
|
---|
85 | List<String> ids = Arrays.asList(categoryIds.split(","));
|
---|
86 | categories = ids.stream().map(Long::valueOf).collect(Collectors.toList());
|
---|
87 | }
|
---|
88 | Long maxMinutesPerDay = lengthOfStay *6 * 60;
|
---|
89 | int minutesPerDay = 0;
|
---|
90 | List<Location> countryLocations = this.locationRepository.findLocationsFromRegionForm(regionId, companionId, categories);
|
---|
91 | List<Location> newList = new ArrayList<>();
|
---|
92 | int listCountrySize = countryLocations.size();
|
---|
93 |
|
---|
94 | while(minutesPerDay < maxMinutesPerDay){
|
---|
95 | for(Location l: countryLocations) {
|
---|
96 | if (minutesPerDay < maxMinutesPerDay && l.getDuration() + minutesPerDay <= maxMinutesPerDay && listCountrySize != 0) {
|
---|
97 | newList.add(l);
|
---|
98 | listCountrySize --;
|
---|
99 | }
|
---|
100 | minutesPerDay += l.getDuration();
|
---|
101 | if (minutesPerDay > maxMinutesPerDay) {
|
---|
102 | break;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | }
|
---|
107 | return newList;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | public List<Location> scheduleLocations(String locName, String companion, String region, List<String> categories, int numberOfDays) {
|
---|
113 | int maxMinutesPerDay = numberOfDays *6 * 60;
|
---|
114 | int minutesPerDay = 0;
|
---|
115 | List<Location> locations = this.locationRepository.findLocationsFromCity(locName, companion, categories);
|
---|
116 | List<Location> countryLocations = this.locationRepository.findLocationsFromCountry(locName, companion, region, categories);
|
---|
117 | List<Location> newList = new ArrayList<>();
|
---|
118 | int listSize = locations.size();
|
---|
119 | int listCountrySize = countryLocations.size();
|
---|
120 |
|
---|
121 | if(locName.equals("Macedonia")){
|
---|
122 | while(minutesPerDay < maxMinutesPerDay){
|
---|
123 | for(Location l: countryLocations) {
|
---|
124 | if (minutesPerDay < maxMinutesPerDay && l.getDuration() + minutesPerDay <= maxMinutesPerDay && listCountrySize != 0) {
|
---|
125 | newList.add(l);
|
---|
126 | listCountrySize --;
|
---|
127 | }
|
---|
128 | minutesPerDay += l.getDuration();
|
---|
129 | if (minutesPerDay > maxMinutesPerDay) {
|
---|
130 | break;
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }else{
|
---|
135 | while(minutesPerDay < maxMinutesPerDay ){
|
---|
136 | for(Location l: locations) {
|
---|
137 | if (minutesPerDay < maxMinutesPerDay && l.getDuration() + minutesPerDay <= maxMinutesPerDay && listSize != 0) {
|
---|
138 | newList.add(l);
|
---|
139 | listSize --;
|
---|
140 | }
|
---|
141 | minutesPerDay += l.getDuration();
|
---|
142 | if (minutesPerDay > maxMinutesPerDay) {
|
---|
143 | break;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | return newList;
|
---|
150 | }
|
---|
151 |
|
---|
152 | }
|
---|