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

Last change on this file since 59329aa was 59329aa, checked in by Ema <ema_spirova@…>, 3 years ago

adding photos

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