Skip to content

Commit a3c76a7

Browse files
committed
BC Modularyzacja 9 - Wprowadzenie serwisów aplikacyjnych ChangeDest
1 parent 844c9aa commit a3c76a7

File tree

5 files changed

+149
-67
lines changed

5 files changed

+149
-67
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.legacyfighter.cabs.ride;
2+
3+
import io.legacyfighter.cabs.geolocation.Distance;
4+
import io.legacyfighter.cabs.geolocation.DistanceCalculator;
5+
import io.legacyfighter.cabs.geolocation.GeocodingService;
6+
import io.legacyfighter.cabs.geolocation.address.Address;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Service;
9+
import org.springframework.transaction.annotation.Transactional;
10+
11+
import java.util.UUID;
12+
13+
@Service
14+
class ChangeDestinationService {
15+
16+
@Autowired
17+
private TransitRepository transitRepository;
18+
19+
@Autowired
20+
private DistanceCalculator distanceCalculator;
21+
22+
@Autowired
23+
private GeocodingService geocodingService;
24+
25+
@Transactional
26+
public Distance changeTransitAddressTo(UUID requestUUID, Address newAddress, Address from) {
27+
// FIXME later: add some exceptions handling
28+
double[] geoFrom = geocodingService.geocodeAddress(from);
29+
double[] geoTo = geocodingService.geocodeAddress(newAddress);
30+
Distance newDistance = Distance.ofKm((float) distanceCalculator.calculateByMap(geoFrom[0], geoFrom[1], geoTo[0], geoTo[1]));
31+
Transit transit = transitRepository.findByTransitRequestUUID(requestUUID);
32+
if (transit != null) {
33+
transit.changeDestination(newDistance);
34+
}
35+
return newDistance;
36+
37+
}
38+
39+
40+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package io.legacyfighter.cabs.ride;
2+
3+
import io.legacyfighter.cabs.geolocation.Distance;
4+
import io.legacyfighter.cabs.geolocation.DistanceCalculator;
5+
import io.legacyfighter.cabs.geolocation.GeocodingService;
6+
import io.legacyfighter.cabs.geolocation.address.Address;
7+
import io.legacyfighter.cabs.geolocation.address.AddressDTO;
8+
import io.legacyfighter.cabs.geolocation.address.AddressRepository;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.stereotype.Service;
11+
import org.springframework.transaction.annotation.Transactional;
12+
13+
import java.util.UUID;
14+
15+
@Service
16+
public class ChangePickupService {
17+
18+
@Autowired
19+
private DistanceCalculator distanceCalculator;
20+
21+
@Autowired
22+
private GeocodingService geocodingService;
23+
24+
@Autowired
25+
private AddressRepository addressRepository;
26+
27+
@Autowired
28+
private TransitDemandRepository transitDemandRepository;
29+
30+
@Transactional
31+
public Distance changeTransitAddressFrom(UUID requestUUID, Address newAddress, Address oldAddress) {
32+
newAddress = addressRepository.save(newAddress);
33+
TransitDemand transitDemand = transitDemandRepository.findByTransitRequestUUID(requestUUID);
34+
if (transitDemand == null) {
35+
throw new IllegalArgumentException("Transit does not exist, id = " + requestUUID);
36+
}
37+
38+
// FIXME later: add some exceptions handling
39+
double[] geoFromNew = geocodingService.geocodeAddress(newAddress);
40+
double[] geoFromOld = geocodingService.geocodeAddress(oldAddress);
41+
42+
// https://www.geeksforgeeks.org/program-distance-two-points-earth/
43+
// The math module contains a function
44+
// named toRadians which converts from
45+
// degrees to radians.
46+
double lon1 = Math.toRadians(geoFromNew[1]);
47+
double lon2 = Math.toRadians(geoFromOld[1]);
48+
double lat1 = Math.toRadians(geoFromNew[0]);
49+
double lat2 = Math.toRadians(geoFromOld[0]);
50+
51+
// Haversine formula
52+
double dlon = lon2 - lon1;
53+
double dlat = lat2 - lat1;
54+
double a = Math.pow(Math.sin(dlat / 2), 2)
55+
+ Math.cos(lat1) * Math.cos(lat2)
56+
* Math.pow(Math.sin(dlon / 2), 2);
57+
58+
double c = 2 * Math.asin(Math.sqrt(a));
59+
60+
// Radius of earth in kilometers. Use 3956 for miles
61+
double r = 6371;
62+
63+
// calculate the result
64+
double distanceInKMeters = c * r;
65+
66+
Distance newDistance = Distance.ofKm((float) distanceCalculator.calculateByMap(geoFromNew[0], geoFromNew[1], geoFromOld[0], geoFromOld[1]));
67+
transitDemand.changePickup(distanceInKMeters);
68+
return newDistance;
69+
}
70+
71+
@Transactional
72+
public void changeTransitAddressFrom(UUID requestUUID, AddressDTO newAddress) {
73+
changeTransitAddressFrom(requestUUID, newAddress);
74+
}
75+
76+
}

src/main/java/io/legacyfighter/cabs/ride/RideService.java

Lines changed: 29 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public class RideService {
3636
@Autowired
3737
private RequestTransitService requestTransitService;
3838

39+
@Autowired
40+
private ChangePickupService changePickupService;
41+
42+
@Autowired
43+
private ChangeDestinationService changeDestinationService;
44+
3945
@Autowired
4046
private DriverRepository driverRepository;
4147

@@ -103,6 +109,24 @@ public TransitDTO createTransit(Long clientId, AddressDTO fromDto, AddressDTO to
103109
return loadTransit(requestForTransit.getId());
104110
}
105111

112+
@Transactional
113+
public void changeTransitAddressFrom(UUID requestUUID, Address newAddress) {
114+
if (driverAssignmentFacade.isDriverAssigned(requestUUID)) {
115+
throw new IllegalStateException("Driver already assigned, requestUUID = " + requestUUID);
116+
}
117+
newAddress = addressRepository.save(newAddress);
118+
TransitDetailsDTO transitDetails = transitDetailsFacade.find(requestUUID);
119+
Address oldAddress = transitDetails.from.toAddressEntity();
120+
Distance newDistance = changePickupService.changeTransitAddressFrom(requestUUID, newAddress, oldAddress);
121+
transitDetailsFacade.pickupChangedTo(requestUUID, newAddress, newDistance);
122+
driverAssignmentFacade.notifyProposedDriversAboutChangedDestination(requestUUID);
123+
}
124+
125+
@Transactional
126+
public void changeTransitAddressFrom(UUID requestUUID, AddressDTO newAddress) {
127+
changeTransitAddressFrom(requestUUID, newAddress.toAddressEntity());
128+
}
129+
106130
private Client findClient(Long clientId) {
107131
Client client = clientRepository.getOne(clientId);
108132
if (client == null) {
@@ -116,80 +140,22 @@ private Address addressFromDto(AddressDTO addressDTO) {
116140
return addressRepository.save(address);
117141
}
118142

119-
@Transactional
120-
public void changeTransitAddressFrom(UUID requestUUID, Address newAddress) {
121-
newAddress = addressRepository.save(newAddress);
122-
TransitDemand transitDemand = transitDemandRepository.findByTransitRequestUUID(requestUUID);
123-
if (transitDemand == null) {
124-
throw new IllegalArgumentException("Transit does not exist, id = " + requestUUID);
125-
}
126-
if (driverAssignmentFacade.isDriverAssigned(requestUUID)) {
127-
throw new IllegalStateException("Driver already assigned, requestUUID = " + requestUUID);
128-
}
129-
TransitDetailsDTO transitDetails = transitDetailsFacade.find(requestUUID);
130-
// FIXME later: add some exceptions handling
131-
double[] geoFromNew = geocodingService.geocodeAddress(newAddress);
132-
double[] geoFromOld = geocodingService.geocodeAddress(transitDetails.from.toAddressEntity());
133-
134-
// https://www.geeksforgeeks.org/program-distance-two-points-earth/
135-
// The math module contains a function
136-
// named toRadians which converts from
137-
// degrees to radians.
138-
double lon1 = Math.toRadians(geoFromNew[1]);
139-
double lon2 = Math.toRadians(geoFromOld[1]);
140-
double lat1 = Math.toRadians(geoFromNew[0]);
141-
double lat2 = Math.toRadians(geoFromOld[0]);
142-
143-
// Haversine formula
144-
double dlon = lon2 - lon1;
145-
double dlat = lat2 - lat1;
146-
double a = Math.pow(Math.sin(dlat / 2), 2)
147-
+ Math.cos(lat1) * Math.cos(lat2)
148-
* Math.pow(Math.sin(dlon / 2), 2);
149-
150-
double c = 2 * Math.asin(Math.sqrt(a));
151-
152-
// Radius of earth in kilometers. Use 3956 for miles
153-
double r = 6371;
154-
155-
// calculate the result
156-
double distanceInKMeters = c * r;
157-
158-
Distance newDistance = Distance.ofKm((float) distanceCalculator.calculateByMap(geoFromNew[0], geoFromNew[1], geoFromOld[0], geoFromOld[1]));
159-
transitDemand.changePickup(distanceInKMeters);
160-
transitDetailsFacade.pickupChangedTo(requestUUID, newAddress, newDistance);
161-
driverAssignmentFacade.notifyProposedDriversAboutChangedDestination(requestUUID);
162-
}
163-
164143
@Transactional
165144
public void changeTransitAddressTo(UUID requestUUID, AddressDTO newAddress) {
166145
changeTransitAddressTo(requestUUID, newAddress.toAddressEntity());
167146
}
168147

169-
@Transactional
170-
public void changeTransitAddressFrom(UUID requestUUID, AddressDTO newAddress) {
171-
changeTransitAddressFrom(requestUUID, newAddress.toAddressEntity());
172-
}
173-
174148
@Transactional
175149
public void changeTransitAddressTo(UUID requestUUID, Address newAddress) {
176-
addressRepository.save(newAddress);
177-
RequestForTransit requestForTransit = requestForTransitRepository.findByRequestUUID(requestUUID);
150+
newAddress = addressRepository.save(newAddress);
178151
TransitDetailsDTO transitDetails = transitDetailsFacade.find(requestUUID);
179-
if (requestForTransit == null) {
152+
if (transitDetails == null) {
180153
throw new IllegalArgumentException("Transit does not exist, id = " + requestUUID);
181154
}
182-
183-
// FIXME later: add some exceptions handling
184-
double[] geoFrom = geocodingService.geocodeAddress(transitDetails.from.toAddressEntity());
185-
double[] geoTo = geocodingService.geocodeAddress(newAddress);
186-
Distance newDistance = Distance.ofKm((float) distanceCalculator.calculateByMap(geoFrom[0], geoFrom[1], geoTo[0], geoTo[1]));
187-
Transit transit = transitRepository.findByTransitRequestUUID(requestUUID);
188-
if (transit != null) {
189-
transit.changeDestination(newDistance);
190-
}
155+
Address oldAddress = transitDetails.from.toAddressEntity();
156+
Distance distance = changeDestinationService.changeTransitAddressTo(requestUUID, newAddress, oldAddress);
191157
driverAssignmentFacade.notifyAssignedDriverAboutChangedDestination(requestUUID);
192-
transitDetailsFacade.destinationChanged(requestUUID, newAddress);
158+
transitDetailsFacade.destinationChanged(requestUUID, newAddress, distance);
193159
}
194160

195161
@Transactional

src/main/java/io/legacyfighter/cabs/ride/details/TransitDetails.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,9 @@ void pickupChangedTo(Address newAddress, Distance newDistance) {
152152
this.distance = newDistance;
153153
}
154154

155-
void destinationChangedTo(Address newAddress) {
155+
void destinationChangedTo(Address newAddress, Distance newDistance) {
156156
this.to = newAddress;
157+
this.distance = newDistance;
157158
}
158159

159160
Money getDriversFee() {

src/main/java/io/legacyfighter/cabs/ride/details/TransitDetailsFacade.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import io.legacyfighter.cabs.assignment.InvolvedDriversSummary;
55
import io.legacyfighter.cabs.carfleet.CarClass;
66
import io.legacyfighter.cabs.crm.Client;
7-
import io.legacyfighter.cabs.driverfleet.DriverService;
87
import io.legacyfighter.cabs.geolocation.Distance;
98
import io.legacyfighter.cabs.geolocation.address.Address;
109
import io.legacyfighter.cabs.money.Money;
@@ -46,9 +45,9 @@ public void pickupChangedTo(UUID requestId, Address newAddress, Distance newDist
4645
}
4746

4847
@Transactional
49-
public void destinationChanged(UUID requestId, Address newAddress) {
48+
public void destinationChanged(UUID requestId, Address newAddress, Distance newDistance) {
5049
TransitDetails details = load(requestId);
51-
details.destinationChangedTo(newAddress);
50+
details.destinationChangedTo(newAddress, newDistance);
5251
}
5352

5453
@Transactional

0 commit comments

Comments
 (0)