source: trip-planner/src/main/java/finki/diplomska/tripplanner/service/impl/LocationServiceImpl.java

Last change on this file was 6fe77af, checked in by Ema <ema_spirova@…>, 2 years ago

add location feature

  • Property mode set to 100644
File size: 9.3 KB
Line 
1package finki.diplomska.tripplanner.service.impl;
2
3import finki.diplomska.tripplanner.models.*;
4import finki.diplomska.tripplanner.models.dto.LocationDto;
5import finki.diplomska.tripplanner.models.dto.PlannerLocationDto;
6import finki.diplomska.tripplanner.models.exceptions.CityNotFoundException;
7import finki.diplomska.tripplanner.models.exceptions.CompanionNotFoundException;
8import finki.diplomska.tripplanner.models.exceptions.LocationNotFoundException;
9import finki.diplomska.tripplanner.models.exceptions.RegionNotFoundException;
10import finki.diplomska.tripplanner.repository.jpa.*;
11import finki.diplomska.tripplanner.service.LocationService;
12import org.springframework.stereotype.Service;
13
14import java.util.*;
15import java.util.stream.Collectors;
16
17
18@Service
19public class LocationServiceImpl implements LocationService {
20
21 private final JpaLocationRepository locationRepository;
22 private final JpaPlannerRepository plannerRepository;
23 private final JpaRegionRepository regionRepository;
24 private final JpaCityRepository cityRepository;
25 private final JpaUserRepository userRepository;
26 private final JpaCategoryRepository categoryRepository;
27 private final JpaCompanionRepository companionRepository;
28
29 public LocationServiceImpl(JpaLocationRepository locationRepository, JpaPlannerRepository plannerRepository, JpaRegionRepository regionRepository, JpaCityRepository cityRepository, JpaUserRepository userRepository, JpaCategoryRepository categoryRepository, JpaCompanionRepository companionRepository) {
30 this.locationRepository = locationRepository;
31 this.plannerRepository = plannerRepository;
32 this.regionRepository = regionRepository;
33 this.cityRepository = cityRepository;
34 this.userRepository = userRepository;
35 this.categoryRepository = categoryRepository;
36 this.companionRepository = companionRepository;
37 }
38
39 @Override
40 public List<Location> findLocationsFromCity(String locName, String companion, List<String> categories) {
41 return this.locationRepository.findLocationsFromCity(locName, companion, categories);
42 }
43
44 @Override
45 public List<Location> findLocationsFromCountry(String locName, String companion, String region, List<String> categories) {
46 return this.locationRepository.findLocationsFromCountry(locName, companion, region, categories);
47 }
48
49 @Override
50 public List<Location> findAll() {
51 return this.locationRepository.findAll();
52 }
53
54 @Override
55 public Location getById(Long id) {
56 Optional<Location> location = this.locationRepository.findById(id);
57 if (!location.isPresent()) {
58 throw new LocationNotFoundException(id);
59 }
60 return location.get();
61 }
62
63 @Override
64 public Optional<Location> findById(Long id) {
65 return this.locationRepository.findById(id);
66 }
67
68 @Override
69 public List<Location> findLocationsFromCityForm(Long cityId, Long companionId, Long lengthOfStay, String categoryIds) {
70 List<Long> categories = null;
71 if(categoryIds != null && !categoryIds.isEmpty()){
72 List<String> ids = Arrays.asList(categoryIds.split(","));
73 categories = ids.stream().map(Long::valueOf).collect(Collectors.toList());
74 }
75 Long maxMinutesPerDay = lengthOfStay *6 * 60;
76 int minutesPerDay = 0;
77 List<Location> citylocations = this.locationRepository.findLocationsFromCityForm(cityId, companionId, categories);
78 List<Location> newList = new ArrayList<>();
79 int listSize = citylocations.size();
80
81 while(minutesPerDay < maxMinutesPerDay ){
82 for(Location l: citylocations) {
83 if (minutesPerDay < maxMinutesPerDay && l.getDuration() + minutesPerDay <= maxMinutesPerDay && listSize != 0) {
84 newList.add(l);
85 listSize --;
86 }
87 minutesPerDay += l.getDuration();
88 if (minutesPerDay > maxMinutesPerDay) {
89 break;
90 }
91 }
92 }
93
94 List<Location> foundLocations = locationRepository.findLocationsFromCityForm(cityId, companionId, categories);
95 return newList;
96 }
97
98 @Override
99 public List<Location> findLocationsFromRegionForm(Long regionId, Long companionId, Long lengthOfStay, String categoryIds) {
100 List<Long> categories = null;
101 if(categoryIds != null && !categoryIds.isEmpty()){
102 List<String> ids = Arrays.asList(categoryIds.split(","));
103 categories = ids.stream().map(Long::valueOf).collect(Collectors.toList());
104 }
105 Long maxMinutesPerDay = lengthOfStay *6 * 60;
106 int minutesPerDay = 0;
107 List<Location> countryLocations = this.locationRepository.findLocationsFromRegionForm(regionId, companionId, categories);
108 List<Location> newList = new ArrayList<>();
109 int listCountrySize = countryLocations.size();
110
111 while(minutesPerDay < maxMinutesPerDay){
112 for(Location l: countryLocations) {
113 if (minutesPerDay < maxMinutesPerDay && l.getDuration() + minutesPerDay <= maxMinutesPerDay && listCountrySize != 0) {
114 newList.add(l);
115 listCountrySize --;
116 }
117 minutesPerDay += l.getDuration();
118 if (minutesPerDay > maxMinutesPerDay) {
119 break;
120 }
121 }
122
123 }
124 return newList;
125 }
126
127
128 @Override
129 public List<Location> scheduleLocations(String locName, String companion, String region, List<String> categories, int numberOfDays) {
130 int maxMinutesPerDay = numberOfDays *6 * 60;
131 int minutesPerDay = 0;
132 List<Location> locations = this.locationRepository.findLocationsFromCity(locName, companion, categories);
133 List<Location> countryLocations = this.locationRepository.findLocationsFromCountry(locName, companion, region, categories);
134 List<Location> newList = new ArrayList<>();
135 int listSize = locations.size();
136 int listCountrySize = countryLocations.size();
137
138 if(locName.equals("Macedonia")){
139 while(minutesPerDay < maxMinutesPerDay){
140 for(Location l: countryLocations) {
141 if (minutesPerDay < maxMinutesPerDay && l.getDuration() + minutesPerDay <= maxMinutesPerDay && listCountrySize != 0) {
142 newList.add(l);
143 listCountrySize --;
144 }
145 minutesPerDay += l.getDuration();
146 if (minutesPerDay > maxMinutesPerDay) {
147 break;
148 }
149 }
150 }
151 }else{
152 while(minutesPerDay < maxMinutesPerDay ){
153 for(Location l: locations) {
154 if (minutesPerDay < maxMinutesPerDay && l.getDuration() + minutesPerDay <= maxMinutesPerDay && listSize != 0) {
155 newList.add(l);
156 listSize --;
157 }
158 minutesPerDay += l.getDuration();
159 if (minutesPerDay > maxMinutesPerDay) {
160 break;
161 }
162 }
163 }
164 }
165 return newList;
166 }
167
168 @Override
169 public Location addLocationToPlanner(PlannerLocationDto plannerLocationDto) {
170 Location location = this.locationRepository.findById(plannerLocationDto.getLocationId())
171 .orElseThrow(() -> new LocationNotFoundException(plannerLocationDto.getLocationId()));
172 Planner planner = this.plannerRepository.getById(plannerLocationDto.getPlannerId());
173 planner.getLocationList().add(location);
174 return this.locationRepository.save(location);
175 }
176
177 @Override
178 public List<Location> getAllLocationsForPlanner(Long plannerId) {
179 return this.locationRepository.getAllLocationsForPlanner(plannerId);
180 }
181
182 @Override
183 public List<Location> getWeekendGetaways() {
184 return this.locationRepository.getWeekendGetaways();
185 }
186
187 @Override
188 public List<Location> getVillages() {
189 return this.locationRepository.getVillages();
190 }
191
192 @Override
193 public List<Long> getAllLocationIdsForPlanner(Long plannerId) {
194 return this.locationRepository.getAllLocationIdsForPlanner(plannerId);
195 }
196
197 @Override
198 public List<Location> getAllLocations(String place) {
199 return this.locationRepository.getAllLocations(place);
200 }
201
202 @Override
203 public Optional<Location> save(LocationDto locationDto, String username) {
204 City city = new City();
205 Region region = this.regionRepository.findById(locationDto.getRegion())
206 .orElseThrow(() -> new RegionNotFoundException(locationDto.getRegion()));
207 if(locationDto.getCity() != null){
208 city = this.cityRepository.findById(locationDto.getCity())
209 .orElseThrow(() -> new CityNotFoundException(locationDto.getCity()));
210 }else{
211 city = null;
212 }
213 User user = this.userRepository.findByUsername(username);
214 locationDto.setUser(user.getUsername());
215 return Optional.of(this.locationRepository.save(new Location(locationDto.getName(), locationDto.getDescription(), locationDto.getAddress(), locationDto.getPriority(),
216 locationDto.getDuration(), locationDto.getTrivia(), locationDto.getPhoto(), region, city, user)));
217 }
218
219}
Note: See TracBrowser for help on using the repository browser.